Monte Carlo Methods Some example applications in C++

Slides:



Advertisements
Similar presentations
Functions. COMP104 Functions / Slide 2 Introduction to Functions * A complex problem is often easier to solve by dividing it into several smaller parts,
Advertisements

Random variables 1. Note  there is no chapter in the textbook that corresponds to this topic 2.
Fundamentals of Data Analysis Lecture 12 Methods of parametric estimation.
Random Processes and the Binomial and Poisson distributions.
Mathematics in Today's World
1 Spreadsheet Modeling & Decision Analysis: A Practical Introduction to Management Science, 3e by Cliff Ragsdale.
The Princeton Egg The Global Consciousness Project (video)The Global Consciousness Project (video) Princeton Egg Website Our Egg: PrincetonEgg.cppPrincetonEgg.cpp.
1 Random numbers Random  completely unpredictable. No way to determine in advance what value will be chosen from a set of equally probable elements. Impossible.
Statistics for Managers Using Microsoft Excel, 4e © 2004 Prentice-Hall, Inc. Chap 6-1 Chapter 6 The Normal Distribution and Other Continuous Distributions.
Chapter 9 Introducing Probability - A bridge from Descriptive Statistics to Inferential Statistics.
Function. Introduction Library function New defined function Random number generator Scope Inline function Function overload Function Function.
1 TAC2000/ Protocol Engineering and Application Research Laboratory (PEARL) MATH Functions in C Language.
Approximate computations Jordi Cortadella Department of Computer Science.
OPIM 5103-Lecture #3 Jose M. Cruz Assistant Professor.
Monte Carlo Methods.
CIS 2033 based on Dekking et al. A Modern Introduction to Probability and Statistics Michael Baron. Probability and Statistics for Computer Scientists,
1.3 Simulations and Experimental Probability (Textbook Section 4.1)
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
CS221 Random Numbers. Random numbers are often very important in programming Suppose you are writing a program to play the game of roulette The numbers.
40S Applied Math Mr. Knight – Killarney School Slide 1 Unit: Statistics Lesson: ST-5 The Binomial Distribution The Binomial Distribution Learning Outcome.
Simulation is the process of studying the behavior of a real system by using a model that replicates the system under different scenarios. A simulation.
Computer simulation Sep. 9, QUIZ 2 Determine whether the following experiments have discrete or continuous out comes A fair die is tossed and the.
Probability.
Statistical Estimation Vasileios Hatzivassiloglou University of Texas at Dallas.
Random numbers in C++ Nobody knows what’s next....
Simulation Chapter 16 of Quantitative Methods for Business, by Anderson, Sweeney and Williams Read sections 16.1, 16.2, 16.3, 16.4, and Appendix 16.1.
CSci 162 Lecture 7 Martin van Bommel. Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based.
Chapter 2: Probability. Section 2.1: Basic Ideas Definition: An experiment is a process that results in an outcome that cannot be predicted in advance.
3/7/20161 Now it’s time to look at… Discrete Probability.
1 Generating Random Numbers Textbook ch.6, pg
MR. CRONE Generating Random Numbers. Random Numbers Many programs require the computer to generate random numbers Random numbers are used in many applications.
Chapter 8 Probability Section 1 Sample Spaces, Events, and Probability.
Random Variables Lecture Lecturer : FATEN AL-HUSSAIN.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
Introduction to programming in java Lecture 16 Random.
Introduction to Discrete Probability
Numerical Methods Some example applications in C++
Now it’s time to look at…
Approximate computations
Probability Imagine tossing two coins and observing whether 0, 1, or 2 heads are obtained. It would be natural to guess that each of these events occurs.
MONTE CARLO SIMULATION
Intro CS – Probability and Random Numbers
TMF1414 Introduction to Programming
© 2016 Pearson Education, Ltd. All rights reserved.
Random Numbers Until now, all programs have behaved deterministically - completely predictable and repeatable based on input values Some applications.
Al-Imam Mohammad Ibn Saud University Large-Sample Estimation Theory
Iterative Constructs Review
Conditional Probability
CSC113: Computer Programming (Theory = 03, Lab = 01)
Approximate computations
The Random Class and its Methods
Computational Lab in Physics: Numerical Integration
توابع در C++ قسمت اول اصول كامپيوتر 1.
Monte Carlo Approximations – Introduction
Random Number Generation
Random numbers What does it mean for a number to be random?
Probability “When you deal in large numbers, probabilities are the same as certainties. I wouldn’t bet my life on the toss of a single coin, but I would,
Section 1 Sample Spaces, Events, and Probability
COUNTING AND PROBABILITY
2.3 Estimating PDFs and PDF Parameters
Computational Lab in Physics: Monte Carlo Integration.
Chapter 6: User-Defined Functions I
CS150 Introduction to Computer Science 1
Lecture 2 Binomial and Poisson Probability Distributions
Now it’s time to look at…
Now it’s time to look at…
CS 144 Advanced C++ Programming January 31 Class Meeting
Random Numbers while loop
Monte Carlo simulation
Programming Fundamental
Presentation transcript:

Monte Carlo Methods Some example applications in C++

Monte Carlo methods - applications in C++ Introduction http://en.wikipedia.org/wiki/Monte_Carlo_method “Monte Carlo methods (or Monte Carlo experiments) are a class of computational algorithms that rely on repeated random sampling to compute their results. Monte Carlo methods are often used in simulating physical and mathematical systems.” To illustrate the implementation of this kind of algorithm in C++ we will look at just a few basic applications where the MC method can help us solve problems in: Maxima, Minima and Optimization Probability and Counting Experiments Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ The rand() function Key to the Monte Carlo method is the generation of sequences of random numbers. C++ has a built-in function for this: rand() returns a number randomly selected in the range 0 to RAND_MAX Note that the sequence of number is not actually random, an algorithm is used to generate the numbers in a chaotic manner with a large period(the number of values returned before the sequence repeats). Related function: srand(n) sets the seed of the random number generator to n (to allow us to obtain different sequences of random numbers). Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Generating and processing rand() values Monte Carlo methods use large sets of random numbers, so we generally place the rand() function inside a large loop. For example: Note that the cstdlib header is required. #include <iostream> #include <cstdlib> using namespace std; int main() { int n = 100000; cout << RAND_MAX << endl; for (int i=0; i<n; i++) { int k = rand(); cout << k << endl; } 2147483647 1804289383 846930886 1681692777 1714636915 1957747793 424238335 719885386 1649760492 . Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Obtaining a discrete uniform distribution For example if we want to simulate the throw of a die having six discrete random outcomes, all with equal probability: int k = 1 + rand() % 6; Modulo 6 returns 0 ≤ k ≤ 5 +1 gives 1 ≤ k ≤ 6 k Store in integer k Histogram Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code Roll ten dice: Output Seed = 12345679 #include <iostream> #include <cstdlib> using namespace std; int main() { srand(12345678); for (int i=0; i<10; i++) { int d = 1 + rand() % 6; cout << d << endl; } 2 6 4 3 1 1 5 6 4 6 3 2 1 5 Seed = 12345680 Seed = 12345678 Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Obtaining a continuous uniform distribution Often we require floating-point random values: double r = rand()/(1.0+RAND_MAX); Cast to a double value slightly larger than RAND_MAX Returns 0.0 ≤ r < 1.0 Example sequence: 0.24308 0.62229 0.77675 0.46182 0.24792 0.95476 0.74463 0.52671 Histogram of r r = 1.0 would be an overflow in the histogram Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Example Populate a square with n randomly-place points [the blue and red points] Count the number of points m that lie inside the circle [the blue points] The ratio 4m/n is then an approximation to the area of the circle (the area of the square being 4 units2) and therefore an approximation to . -1.0 ≤ x < +1.0 -1.0 ≤ y < +1.0 Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code Output for n = 105 #include <iostream> #include <cstdlib> using namespace std; int main() { int n = 100000; int m = 0; for (int i=0; i<n; i++) { double x = 2.0*rand()/(RAND_MAX+1.0) -1.0; double y = 2.0*rand()/(RAND_MAX+1.0) -1.0; if ( x*x+y*y < 1.0 ) m++; } cout << 4.0*m/n << endl; -1.0 ≤ x < +1.0 -1.0 ≤ y < +1.0 3.14376 Convergence n 102 104 106 108  2.84 3.1288 3.14307 3.14162 3.14159 A factor of 100 increase in n yields a factor of 10 improvement in accuracy This method works! but requires very large statistics to obtain good accuracy. Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Maxima/Minima Aim We wish find the maximum or minimum of a function f(x). For example, find the maximum of the following function in the range 0 ≤ x <  How can we do this with random numbers? Solution Generate a large number of random values x in the range 0 ≤ x < , evaluate f(x) at each point, record the position of the largest value. f(x) x  Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code #include <iostream> #include <cmath> #include <cstdlib> using namespace std; int main() { int n = 100000; double xmax = 0., fmax = 0.; for (int i=0; i<n; i++) { double x = M_PI * rand()/(RAND_MAX+1.0); double f = x*pow((0.5+exp(-x)*sin(x*x*x)),2); if ( f > fmax ) { fmax = f; xmax = x; } cout << "f=" << fmax << endl; cout << "x=" << xmax << endl; Output for n = 105 0 ≤ x <  f=0.90536 x=2.99013 time = 12 ms Convergence n 101 102 103 104 105 f=0.837919 f=0.905246 f=0.904914 f=0.905358 f=0.905360 http://www.wolframalpha.com/ Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Optimization Aim Similar to the idea of obtaining the maximum or minimum of a function, we sometimes wish to optimize a system; i.e. maximise or minimize a target quantity. Example We have 50 meters of fencing and wish to construct a fenced rectangular area, sides a and b with the target of maximizing the area A = a b enclosed by the fence. b A = a b a a b 2a + 2b = 50  b = 25 – a with 0 < a < 25 Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code Output for n = 105 #include <iostream> #include <cstdlib> using namespace std; int main() { int n = 1e5; double max_area = 0., max_a = 0.; for (int i=0; i<n; i++) { double a = 25.0 * rand()/(RAND_MAX+1.0); double b = 25.0 - a; double area = a*b; if ( area > max_area ) { max_area = area; max_a = a; } cout << max_a << endl; b = 25 – a with 0 < a < 25 12.4998 time = 1 ms Convergence n 101 102 103 104 105 106 13.8492 12.3396 12.4631 12.5005 12.4998 12.5000 12.5 12.5 Monte Carlo methods - applications in C++

Probability and Counting Experiments Aim Many physical systems are governed by random processes. Common examples are the tossing of a coin and the throw of dice. Monte Carlo methods allow us to simulate such systems and calculate outcomes in terms of probabilities. “6” For example, two coins and one die are thrown. What is the probability of obtaining a “Tail”, “Head” and a “6” in any order: P(“T”,“H”,“6”) ? Head “H” Solution; throw n times and count the number of times m that we get the outcome. Then m / n  P(T,H,6) as n   Tail “T” Experimental definition of probability Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code Output for n = 105 #include <iostream> #include <cstdlib> using namespace std; int main() { int n = 1e5; int m = 0; for (int i=0; i<n; i++) { int die = rand() % 6 + 1; int coin1 = rand() % 2; int coin2 = rand() % 2; if ( die==6 && (coin1 != coin2) ) m++; } cout << "1/" << 1/(double(m)/n) << endl; 1/11.925 Convergence n 102 103 104 105 106 107 108 109 1/7.6923 1/10.870 1/11.933 1/11.925 1/11.997 1/12.017 1/11.996 1/12.001 “T” “H” or “H” “T” This method works! but requires very large statistics to obtain good accuracy. Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ Example Bacteria are grown in culture dishes in a laboratory. Experience tells us that on average in this lab 20% of the dishes become contaminated by unwanted bacteria (thus spoiling the culture). Question: If the lab is growing bacteria in ten dishes, what is the probability that more then half of the dishes will become contaminated? Solution: We have ten dishes, P(“contamination of each dish”) = 0.2 Use a Monte Carlo experiment to test each dish against the probability of 0.2. Repeat this n times and count the number of times m where more than 5 dishes become contaminated. Then n / m  P(“more than 5 dishes are contaminated”) as n   Experimental definition of probability Monte Carlo methods - applications in C++

Monte Carlo methods - applications in C++ C++ code Output for n = 108 #include <iostream> #include <cstdlib> using namespace std; int main() { int n = 1e8; int m = 0; for (int i=0; i<n; i++) { int k=0; for (int j=0; j<10; j++) { // 10 dishes double r = rand()/(RAND_MAX+1.0); if ( r < 0.2 ) k++; // contaminated } if (k>5) m++; // > 5 dished spoiled cout << m/double(n) << endl; Ten dishes P(“contamination”) = 0.2 P(“> 5 dishes contaminated”)? 0.006365 time = 12 s Convergence n 103 104 105 106 107 108 109 0.01 0.0057 0.00672 0.006291 0.006374 0.006365 0.006368 true = 0.637% Binomial distribution. Monte Carlo methods - applications in C++