|
What is a Monte Carlo simulation?
A technique which randomly generates values for uncertain variables over
and over to simulate a model. The name Monte Carlo derives from the games of
chance for which the Principality is famous, such as roulette and baccarat.
On first hearing the notion of a simulation based on chance, you may
think of never winning at the roulette. However the technique should be
viewed from the point of the Casino. They also have no idea which number the
ball is going to land on next. Even the next 100 rolls will be difficult to
predict.
What the casino does know though is that if the wheel is spun 30 million
times, there will be one million each of 1,2,3 etc. As each spin is an
independent act of chance, it is impossible to predict in which order the
numbers will come. This predictability allows us to generate transactions that mirror the real world, I.e. we can simulate user activity for the Software Under Test (SUT).
The randomness displayed on the roulette table can be displayed in many
situations including stock prices, weather and behaviour of website
users.
When is it used?
Monte Carlo simulation is used extensively in statistical analysis and extensively in modelling large financial environments. For example if a fund manager wants to predict how well his fund is going to perform, he might simulate a range of growth rates over the next twenty years, each month is different, conform to the boundaries laid down by the fund manager.
The most common use in Software testing would be I suggest with performance testing.
How is the data generated?
I use two types of data generation. Firstly a spreadsheet is created with a row for each transaction that is to be used as an input to the test. The advantage of this method is the amount of control over the data.
An example is to use the normal distribution. In the real world we know that we never get exactly the same probabilities for such as on roulette. Thus we have to use some other method of generating variability.
Interest rates are a good test. Of course there are periods where they are at the extremes of 1-2% or 12% plus. However these are rare. Over a 25 year mortgage they are likely to be in the 5-8% range. (These are not accurate real world figures.) Thus if we are to calculate transactions on hundreds of mortgages with different rates, we need most of them to be in 5-8% interest, but some will be at the extreme.
The table below shows a spreadsheet calculation. The first row is the Normal Distribution for a given amount. (This calculation is not discussed here.) The second is the percentage of cases that will use the interest rate. The cumulative number of cases is 4th and the interest rate used is last.
As we can see the majority of test inputs will be in the 4.5% - 8.5% range. This mirrors real life.
| Normal Distribution | % Of Cases | Number Of Cases | Cumulative Total | Interest Rate |
| 0.135 | 0.01% | 6 | 6 | 0.5% |
| 0.621 | 0.05% | 26 | 32 | 1% |
| 2.275 | 0.19% | 95 | 127 | 1.5% |
| 6.681 | 0.56% | 278 | 405 | 2% |
| 15.866 | 1.32% | 661 | 1066 | 2.5% |
| 30.854 | 2.57% | 1285 | 2351 | 3% |
| 50 | 4.17% | 2083 | 4434 | 3.5% |
| 69.146 | 5.76% | 2881 | 7315 | 4% |
| 84.134 | 7.01% | 3505 | 10820 | 4.5% |
| 93.319 | 7.78% | 3888 | 14708 | 5% |
| 97.725 | 8.14% | 4071 | 18779 | 5.5% |
| 99.379 | 8.28% | 4140 | 22919 | 6% |
| 99.865 | 8.32% | 4161 | 27080 | 6.5% |
| 99.379 | 8.28% | 4140 | 31220 | 7% |
| 97.725 | 8.14% | 4071 | 35291 | 7.5% |
| 93.319 | 7.78% | 3888 | 39179 | 8% |
| 84.134 | 7.01% | 3505 | 42684 | 8.5% |
| 69.146 | 5.76% | 2881 | 45565 | 9% |
| 50 | 4.17% | 2083 | 47648 | 9.5% |
| 30.854 | 2.57% | 1285 | 48933 | 10% |
| 15.866 | 1.32% | 661 | 49594 | 10.5% |
| 6.681 | 0.56% | 278 | 49872 | 11% |
| 2.275 | 0.19% | 95 | 49967 | 11.5% |
| 0.621 | 0.05% | 26 | 49993 | 12% |
| 0.135 | 0.01% | 6 | 49999 | 12.5% |
| 1200.135 | 100% | 50000 | | |
Thus 4141 of transactions will involve an interest rate of 4.5%. 0.5% will the rate in only 6 cases out of 50,000.
The alternative is to generate a random number and according to a defined set of rules set the case input accordingly.
|