Processing math: 100%
Skip to main content
Library homepage
 

Text Color

Text Size

 

Margin Size

 

Font Type

Enable Dyslexic Font
Mathematics LibreTexts

2.1: Introduction to Stochastic Simulation

  • Franz S. Hover & Michael S. Triantafyllou
  • Massachusetts Institute of Technology via MIT OpenCourseWare

( \newcommand{\kernel}{\mathrm{null}\,}\)

A stochastic simulation is a simulation of a system that has variables that can change stochastically (randomly) with individual probabilities. These random variables are generated, inserted into a model of the system, their outputs are recorded, and then the process is repeated using a new set of random variables.

Suppose you have a coin and don't know the probability of it landing on heads versus tails. One way to approximate this probability is by flipping the coin over and over, recording the outcome each time, and then analyzing the results. Suppose your record the following.

Heads Tails Total
498 502 1000

Then you would approximate that your coin will land on heads 498/1000 = 49.8% of the time and heads 502/1000 = 50.2% of the time.

We, of course, will be using Python to do the work for us. Below, we will calculate the probability of landing on heads. We use the random package to find a random integer between 0 and 1, inclusive. We decide that 0 will represent heads and keep track of the number of "successes", that is the number of flips that land heads.

1import random
2 
3tries = 10000
4success = 0
5for a in range(1,tries+1):
6  flip = random.randrange(0,2)
7  if flip == 0:
8    success += 1
9print(success/tries)

output:     0.4958

Of course, if you run the above program, you will likely get a slightly different probability. Our successive runs of the above program returned 0.5061, 0.4869, and 0.4994. Try increasing or decreasing the number of "tries" above to see how they affect the predictability of the probability returned.


This page titled 2.1: Introduction to Stochastic Simulation is shared under a CC BY-NC-SA 4.0 license and was authored, remixed, and/or curated by Franz S. Hover & Michael S. Triantafyllou (MIT OpenCourseWare) via source content that was edited to the style and standards of the LibreTexts platform.

Support Center

How can we help?