next up previous
Next: Using Simulations to Answer Up: Approximating the Solution of Previous: Adding to the Simulation

Getting an Approximate Probability from the Simulator

Now that we have a Module that works, we can use the code to generate a sequence of games played that lead to either the computer simulated gambler losing all the initial money or the gambler increasing the initial amount of money to the desired amount. In order to use the module we could (1) input the Module into a cell, (2) evaluate that cell to load in the definition of the Module, and (3) call the Module repeatedly, keeping track of the results. For example, if we wanted to simulate the process of ten gamblers playing a game starting with $10, trying to reach $15, with a probability of winning an individual play of the game being $p=18/38$, we could create a cell with the input line

  GamblersRuin[10, 15, 18/38]
and evaluate this cell ten times. Each time the cell is evaluated we would have to write down the information returned by the Module. We could then tabulate the information and analyze all the output.

Recall that our goal is to compute the probability of a gambler achieving the goal. Using the module one time only tells us what would happen to a single person gambling. To compute an approximation of the probability of winning we will need to repeat the process many times and use the output of all the simulations to compute an approximate probability. In particular, suppose that we consider simulating a total of 150 gamblers. At the end of evaluating the simulator 150 times we might find that 11 of the simulated gamblers won and 139 were ruined. An approximate probability for achieving the goal is given by

\begin{displaymath}
P = {{11}\over{150}} \approx 0.073
\end{displaymath}

So according to the data, the gambler has about a 7.3 percent chance of achieving the goal. The accuracy of the approximation above depends on the number of gamblers that we choose to simulate. The more gamblers we simulate and use in the computation, the better the approximation will be. In some cases, we may be required to simulate the process many hundreds of times to get a sufficiently accurate approximation.

Our first reaction to this prospect should be that even though the Module does most of the work, evaluating the Module many times and writing down the individual gamblers results will be tedious. Since computers were put on this world to perform lots of tedious tasks we should try to get the software to handle this. The first thing we should do is build a piece of code that will run GamblersRuin as many times as we want and then compute the approximate probability for us. Let's consider the following new Module.

  
  SimulateGR[a_, c_, p_, ngamblers_] :=
            Module[ {var1,var2,var3,nwins,nlosses,output},
                  var2 = c;
                  var3 = p;
                  nwins = 0;
                  nlosses = 0;
                  Do[
                    Clear[var1,output];
                    var1 = a;
                    output = GamblersRuin[var1,var2,var3];
                    If[output[[2]], nwins = nwins + 1,
                               nlosses = nlosses + 1]
                    {i,1,ngamblers}
                    ];
                  Return[nwins/(nwins+nlosses)]
                  ]
The module is a little longer and has added a few variables and lines of code. The important features in the Module are:
  1. We pass in the same parameters as before and along with the number of times we want to run the simulation. This is in effect is telling the computer the number of gamblers that we want to simulate going through the process. The variable use in the Module is ngamblers.
  2. We use the GamblersRuin module that already written to avoid recoding the simulation of a single gambler again. Note that the code for the GamblersRuin module must be loaded into our notebook before we can use the code above. It might be a good idea to include both modules in the same cell along with a Clear command for both names.
  3. The Do statement sums up the wins and losses and then the Return computes and passes back the probability of the gambler achieving the goal. In Do command, there are two arguments. The first argument contains all the commands to execute each time through the loop and the second argument is the range of values to use in incrementing the loop. In our case the Do command sums from one to ngamblers.
  4. The loop keeps track of the number of wins and losses by the computer gamblers in the variables, nwins and nlosses. Note that these are local variables. In the end the approximate probability is computed and returned in the last command in the module.

The output from this module is an approximate probability for a gambler achieving the goal. We can now compare the results of calculations using the new simulator to the exact value from earlier in the chapter. That is, we can use the simulator to compute an approximation of the exact value computed in the exact solution method. For example, suppose that after loading the two modules we need, GamblersRuin and SimulateGR, we enter the line

  SimulateeGR[10,15,0.44,500]
An example output would be the number $127/500=0.254$. This means the simulator predicts the chances of achieving the goal of ending the evening with $15 when we start from $10 is 25.4%.

The exact value in this case comes from evaluating the function

\begin{displaymath}
s_{15}(10) = {{(0.44)^{15}}\over{(0.44)^{15}-(0.56)^{15}}}
(1-({(0.56)\over (0.44)})^{(0.44)} )
\end{displaymath}

Computing this number gives the value $\approx 0.280119$ or about 28.01%. The value is reasonably close. It is a worthwhile exercise to examine the accuracy of the approximation as the number of gamblers is changed; see Problem 7.


next up previous
Next: Using Simulations to Answer Up: Approximating the Solution of Previous: Adding to the Simulation
Joe Koebbe 2003-10-01