The Monte Carlo Method |
Written by Mike James | ||||||||
Friday, 01 November 2024 | ||||||||
Page 4 of 4
Working out the matrix productTo work out the matrix product we have to form sums involving the matrix elements a(i,j) and the vector elements b(k) to be precise
or in other words
This isn’t the place to be teaching matrix algebra, but it comes down to multiplying the elements row j in the matrix by the corresponding elements of vector and adding them up - usually said as "row times column".
How to multiply a vector by a matrix
Computing this very non-random value using random numbers seems a very unlikely prospect but it is fairly easy. For the moment let's suppose that all the values in the matrix are positive and less than one and that each row adds up to one. If this isn't the case then we can modify the matrix to make it true and the solution of the new matrix is related to the solution to the original. This condition allows us to treat the rows as probabilities of something happening. Focus for a moment on a single row - its entries give you the probability of some event occurring. To simulate these events all you do is set up a number of random number generators R(i) one for each row of the matrix. Each random number generator is set up so that it returns b(j) i.e a value in the B vector, with probability a(i,j) . For example, if the matrix A was
and the vector b was
Then for the first row of the matrix we would create a random number generator R(1) that implemented the distribution of the first row. That is R(1) produces 4.3 with probability 0.5, 6.2 with probability 0.3 and 3.2 with probability 0.2. For the second row we would create a random number generator R(2) that produced 4.3 with probability 0.2, 6.2 with probability 0.1 and 3.2 with probability 0.7. That is each random number generator returns the elements of the vector with probabilities given by the rows of the matrix – this can be achieved using the simple methods described earlier. Now we can forget about matrix multiplication and simply run the random number generators - one for each row. We run the simulation and work out the average value produced by each of the random number generators. The resulting vector of averages one from each random number generator estimates the vector we want i.e. y. In terms of our earlier example once we have R(1) and R(2) we would run them and work out the average value they produced. This average is an estimate of the result of multiplying the vector b by the matrix A. That is:
I don’t know if you’ll be impressed by this demonstration of how randomness can be used to get an estimate of an answer that seems to have nothing to do with chance - but you should be. Let's look at a simple JavaScript program to implement this algorithm to see how easy it really is. First we need two random number generators R1 and R1:
These use the "size of interval" method described in the introduction to generate random numbers with the specified probabilities. Now all we have to do is to write a loop that generates the appropriate random numbers using R1 and R2 and work out the mean:
If you try this out using Firefox say you should get results something like:
which should be compared to the exact result R1=4.65 R2=3.72 The complete program as a simple HTML page is:
This is the beginning of a wide range of computing applications that use random numbers to get at answers that would otherwise be too difficult to compute exactly. Who needs quantum computers when you have Monte Carlo to play with… Related ArticlesInside Random Numbers
Comments
or email your comment to: comments@i-programmer.info To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.
<ASIN:352740760X> <ASIN:0812975219> <ASIN:0387942823> |
||||||||
Last Updated ( Friday, 01 November 2024 ) |