Skip to main content
Mathematics LibreTexts

2.1: Introduction to Stochastic Simulation

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

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    \( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

    \( \newcommand{\id}{\mathrm{id}}\)

    \( \newcommand{\Span}{\mathrm{span}}\)

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

    \( \newcommand{\range}{\mathrm{range}\,}\)

    \( \newcommand{\RealPart}{\mathrm{Re}}\)

    \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)

    \( \newcommand{\Argument}{\mathrm{Arg}}\)

    \( \newcommand{\norm}[1]{\| #1 \|}\)

    \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)

    \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    \( \newcommand{\vectorA}[1]{\vec{#1}}      % arrow\)

    \( \newcommand{\vectorAt}[1]{\vec{\text{#1}}}      % arrow\)

    \( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vectorC}[1]{\textbf{#1}} \)

    \( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)

    \( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)

    \( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)

    \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)

    \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)

    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.

    import random
    
    tries = 10000
    success = 0
    for a in range(1,tries+1):
      flip = random.randrange(0,2)
      if flip == 0:
        success += 1
    print(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; a detailed edit history is available upon request.