ITEC 2600 Introduction to Analytical Programming Instructor: Prof. Z. Yang Office: DB3049
Lecture Eleven Monte Carlo Simulation
Monte Carlo Simulation Monte Carlo simulation is a computerized mathematical technique. The main idea behind this method is that the results are computed based on repeated random sampling and statistical analysis. Monte Carlo simulations sample from a probability distribution for each variable to produce hundreds or thousands of possible outcomes. The results are analyzed to get probabilities of different outcomes occurring. 3
Flip a coin We consider an example: flip a coin. We get either H or T by flipping a coin. We know the chance for either H or T is 0.5 (50%). We can do experiments by simulations. 4
MATLAB Random Number X = rand returns a single uniformly distributed random number in the interval (0,1). X = rand(1, n) returns an 1-by-n vector of random numbers in the interval (0,1). Example: X = rand(1, 10) returns an 1-by-10 vector. X = rand(n) returns an n-by-n matrix of random numbers in the interval (0,1). Example: X = rand(10) returns an 10-by-10 matrix. 5
Sign Function Y = sign(x) returns an array Y the same size as x, where each element of Y is: 1 if the corresponding element of x is greater than 0. 0 if the corresponding element of x equals 0. -1 if the corresponding element of x is less than 0. 6
Sum Function S = sum(a) returns the sum of the elements of A along the first array dimension whose size does not equal 1. If A is a vector, then sum(a) returns the sum of the elements. If A is a matrix, then sum(a) returns a row vector containing the sum of each column. Examples 7
Experiment - Flip a Coin Flip a coin once{1 H ; -1 T } A = sign(0.5 - rand) Flip a coin five times A = sign(0.5 - rand(1,5)) We can count how many times we get H B = sum(sign(0.5 - rand(1,5))>0) How to count how many times we get T 8
Probability Flip Coins Let s flip a coin N times (i.e. 1,000) Count how many times to have H Calculate the probability to have H How to use MATLAB to implement it In the program, the users are prompted to input N 9
Function Ceil Y = ceil(x) rounds each element of X to the nearest integer greater than or equal to that element. Y = ceil(5.37) Y = ceil(-5.37) Y = ceil([0.1 1.2 2.3 3.4 4.5 5.6]) Using the function ceil, we can simulate rolling a dice. 10
Floor Other Similar Functions Y = floor(x) rounds each element of X to the nearest integer less than or equal to that element. Fix Y = fix(x) rounds each element of X to the nearest integer toward zero. For positive X, the behavior of fix is the same as floor. For negative X, the behavior of fix is the same as ceil. Round 11
How to Generate Random Integers How to randomly generate a number among{1, 2, 3, 4, 5, 6} X = (6 * rand) generates a random number and 0<X<6. X = ceil(6 * rand) generates a random integer between 1 and 6, i.e, 1, 2, 3, 4, 5, or 6. How to use floor to do it. 12
Rolling a Dice The outcome of rolling a dice is a number among {1, 2, 3, 4, 5, 6} What is the probability of getting a particular number when rolling a dice? probability = # of favorable outcomes # of total possible outcomes Roll a dice N times (i.e. 1000) 13
CASINO 14
CASINO Casinos make a profit by offering games of chance where the average payouts are lower than the income produced by the overall wagers. The statistical advantage that the casino has on each game, and each bet, is called the house edge. Because the outcome is unknown, and regardless of who holds the edge, either party may win at any one time. 15
Casino Revenue (a Single Game) We construct an expression that computes the net revenue for a single game based on a random number chosen between 0 and 1. If the random number 0.51, the casino wins one betting unit; If the random number > 0.51, the casino loses one betting unit. 16
Casino Revenue (a Single Game) (Cont d) The following Matlab command will return simulated casino win (+1) or loss (- 1): Revenue = sign(0.51 rand) The win or loss is unknown until the game is completed. The result (+1 or -1) is random. 17
Casino Revenue (10 Games) To simulate 10 games, we use Revenue = sign(0.51 rand(1, 10)) Revenue is a vector of 10 elements. Each element represents a win or a loss (+1 or -1). How many wins are unknown until all games are completed. 18
Simulation Results (One Trial) 19
Questions If the game is run10,000 times, can the casino know which game it will win? What is the probability of the casino s total winning? After10,000 games completed, how many games would the casino win? 20
Casino Profit If the casino runs 100 games per day. If the house wins a game, the casino earns $1; otherwise loses $1. Can we calculate the casino s daily profit approximately? What if the casino runs 1000 games per day? 21
More Questions We know how to compute the casino s daily approximate profit, can we compute its weekly profit? How about monthly? If the casino wants to increase its profit, what can it do? 22