Читать книгу Probability - Robert P. Dobrow - Страница 38
MONTE CARLO SIMULATION
ОглавлениеImplementing a Monte Carlo simulation of requires three steps:
1 Simulate a trial: Model, or translate, the random experiment using random numbers on the computer. One iteration of the experiment is called a “trial.”
2 Determine success: Based on the outcome of the trial, determine whether or not the event occurs. If yes, call that a “success.”
3 Replication: Repeat the aforementioned two steps many times. The proportion of successful trials is the simulated estimate of
Monte Carlo simulation is intuitive and matches up with our sense of how probabilities “should” behave. We give a theoretical justification for the method in Chapter 10, where we study limit theorems and the law of large numbers.
Here is a most simple, even trivial, starting example.
Example 1.32 Consider simulating the probability that an ideal fair coin comes up heads. One could do a physical simulation by just flipping a coin many times and taking the proportion of heads to estimate .Using a computer, choose the number of trials (the larger the better) and type the R command> sample(0:1, n, replace = T)The command samples with replacement from times such that outcomes are equally likely. Let 0 represent tails and 1 represent heads. The output is a sequence of ones and zeros corresponding to heads and tails. The average, or mean, of the list is precisely the proportion of ones. To simulate , type> mean(sample(0:1, n, replace = T))Repeat the command several times (use the up arrow key). These give repeated Monte Carlo estimates of the desired probability. Observe the accuracy in the estimate with one million trials:> mean(sample(0:1, 1000000, replace = T)) [1] 0.500376 > mean(sample(0:1, 1000000, replace = T)) [1] 0.499869 > mean(sample(0:1, 1000000, replace = T)) [1] 0.498946 > mean(sample(0:1, 1000000, replace = T)) [1] 0.500115
The R script CoinFlip.R simulates a familiar probability—the probability of getting three heads in three coin tosses.