Presentation is loading. Please wait.

Presentation is loading. Please wait.

Homework #5 — Monte Carlo Simulation

Similar presentations


Presentation on theme: "Homework #5 — Monte Carlo Simulation"— Presentation transcript:

1 Homework #5 — Monte Carlo Simulation
Professor Hugh C. Lauer CS-1004 — Introduction to Programming for Non-Majors (Slides include materials from Python Programming: An Introduction to Computer Science, 2nd edition, by John Zelle and copyright notes by Prof. George Heineman of Worcester Polytechnic Institute) CS-1004, A-Term 2014 Monte Carlo Simulation

2 Monte Carlo Simulation
A program that mimics the behavior of a real-world system Or a system under design, … … or a fantasy world! Values of program variables model states of the system Actions of program model behavior of the system E.g., graphic ball of HW#3 models behavior of a bouncing ball in a 2-dimensional, frictionless world with no gravity CS-1004, A-Term 2014 Monte Carlo Simulation

3 Monte Carlo Simulation
A simulation of a system whose behavior is characterized by probabilities Examples Stock market Weather Traffic patterns Migratory bird patterns Diseases and epidemics CS-1004, A-Term 2014 Monte Carlo Simulation

4 Monte Carlo Simulation
Homework #5 Model the game of Craps Player rolls two (normal) dice 2, 3, 12  player loses 7, 11  player wins Otherwise roll again Initial roll value  player wins 7  player loses Otherwise, repeat! Questions to answer for this assignment:– What is the probability that player wins eventually wins? What percentage of games are decided on 1st roll? 2nd roll? 3rd roll? etc.? What is average number of rolls per game? CS-1004, A-Term 2014 Monte Carlo Simulation

5 How to answer questions like these?
Roll the dice … … many times … (i.e., 1000s, 10,000s, or more games!) … make experimental observations, and … infer probabilities from them … i.e., what percentage win, lose, etc. CS-1004, A-Term 2014 Monte Carlo Simulation

6 How do we roll dice in a computer?
Answer:– Random number generator Object of class random Returns a different value each time its method is called Seemingly random, unpredictable … … but values exhibit a desired probability distribution E.g., Uniform Normal Logarithmic Exponential CS-1004, A-Term 2014 Monte Carlo Simulation

7 Random number generators
Extensive mathematical theory Tests exist to measure the “quality” of the randomness Detect bias Etc. CS-1004, A-Term 2014 Monte Carlo Simulation

8 Monte Carlo Simulation
Good news and bad news Good news:– Seemingly unpredictable sequence of numbers that do not repeat Great for modeling probabilistic systems Can run many, many experiments in a short amount of time Without impacting physical environment! Bad news:– Terrible for debugging programs! Non-reproducible results CS-1004, A-Term 2014 Monte Carlo Simulation

9 Monte Carlo Simulation
Solution — the “seed” Random number generators … … generate sequences of numbers that only look random Can be restarted to generate same sequence again … … and again … …etc. … without compromising the appearance of randomness Definition:– “seed” Value for initializing internal state of random number generator … so that it can be replayed exactly as before! CS-1004, A-Term 2014 Monte Carlo Simulation

10 Python random number class
import random random.random() generates a new “random” number to a specified distribution random.seed(str) Initializes random number generator Examples If no seed, random class initializes from internal variable in OS Good quality random variable Very, very difficult to reproduce Very, very difficult to predict CS-1004, A-Term 2014 Monte Carlo Simulation

11 Monte Carlo Simulation
Summary Play many 1000’s or 10,000s of games of Craps Use random number generator to provide data values that satisfy a probability distribution Gather statistics Use information to answer the questions Justify your answer with words, graphs, and data! CS-1004, A-Term 2014 Monte Carlo Simulation

12 Monte Carlo Simulation
Questions? CS-1004, A-Term 2014 Monte Carlo Simulation


Download ppt "Homework #5 — Monte Carlo Simulation"

Similar presentations


Ads by Google