Upload
lamngoc
View
231
Download
0
Embed Size (px)
Citation preview
2WB05 SimulationLecture 1: Introduction andMonte Carlo simulation
Marko Boonhttp://www.win.tue.nl/courses/2WB05
November 12, 2012
2/39
• studeerwijzer available (with notes, slides, programs, assignments)
• examination consists of 4 or 5 take home assignments. Assignments are given on Mondays. Some assign-ments take two weeks to complete, while others have to be handed in one week later.
• assignments done in groups of 2
• register your group (on the homepage of 2WB05) and register yourself in OASE.
Organisation
3/39
Week Dates Monday Thursday1 12-18 Nov Course 12 19-25 Nov Course 2 Course 33 26 Nov - 2 Dec Course 44 3-9 Dec Course 55 10-16 Dec Course 6 Extra course ???6 17-23 Dec Course 77 7-13 Jan Course 8
Warning: this is a preliminary schedule. Check the course website for the definite schedule.
Schedule
4/39
• Monte Carlo simulation
• Modelling of discrete-event systems
• Programming
• Random number generation
• Output analysis
Topics
5/39
State changes at (random) discrete points in time
Examples:
• Manufacturing systemsCompletion times of jobs at machines, machine break-downs
• Inventory systemsArrival times of customer demand, replenishments
• Communication systemsArrival times of messages at communication links
Discrete-event systems
6/39
Programming tools:
• General purpose languages (C, C++, Java, Mathematica, . . .);
• Simulation language χ developed by the Systems Engineering group
• Simulation system Arena
Discrete-event systems
7/39
The law of large numbersZ1, Z2, . . . Zn are i.i.d. random variables with mean z := E[Z1] and finite variance.The probability of the sample mean being close to z is large. In fact, for every ε > 0,
limn→∞
P(∣∣∣∣ Z1 + Z2 + . . .+ Zn
n− z
∣∣∣∣ < ε
)= 1.
Estimator for z:
Z ≡ Zn :=Z1 + Z2 + · · · + Zn
nEstimator for P(Zi > t):
E[1{Zi>t}]
Monte Carlo simulation
8/39
Simulation is a perfect tool to develop and sharpen your intuitionfor probabilistic models (see also Tijms’ book Spelen met kansen)
In no time probabilistic properties can be illustrated by a simulationexperiment and the results can be shown in graphs or tables!
Problems
• coin tossing
• (nearly) birth day problem
• lottery
Simple probabilistic problems
9/39
Coin tossing
Two players A and B throw a fair coin N times.If Head, then A gets 1 point; otherwise B.
• What happens to the absolute difference in points as N increases?
• What is the probability that one of the players is leading between 50% and 55% of the time? Or more than95% of the time?
• In case of 20 trials, say, what is the probability of 5 Heads in a row?
Simple probabilistic problems
10/39
Birthday problem
Consider a group of N randomly chosen persons.What is the probability that at least 2 persons have the same birthday?
Nearly birthday problem
What is the probability that at least 2 persons have their birthday within r days of each other?
Simple probabilistic problems
11/39
Lottery
Each week a very popular lottery in Andorra prints 104 tickets. Each tickets has two 4-digit numbers on it, onevisible and the other covered. The numbers are randomly distributed over the lots. If someone, after uncoveringthe hidden number, finds two identical numbers, he wins a large amount of money.
• What is the average number of winners per week?
• What is the probability of at least one winner?
The same lottery prints 107 tickets in Spain. What about the answers to the questions above?
Simple probabilistic problems
12/39
Monte Carlo simulation of Coin tossing
int n = 0;int pointsA = 0;int pointsB = 0;
while (n < N) {if (rand.nextDouble() < p)pointsA++;
elsepointsB++;
n++;System.out.println(pointsA - pointsB);
}
Note: rand is instance of java.util.Random
Coin Tossing
13/39
Complete Java-code of simulating coin tossing
import java.util.Random;
public class CoinToss {
/* total number of trials */protected int N;/* probability on head */protected double p;/* random number generator */protected Random rand;
/*** Constructs a CoinToss object.* The probability on head is taken 0.5.* @param N the number of realisations*/
public CoinToss(int N) {this(N, 0.5);
}
/*** Constructs a CoinToss object.* The probability on head is 0.5.* @param N the number of realisations* @param seed the random seed for the random number generator*/
public CoinToss(int N, long seed) {this(N, 0.5, seed);
}
Coin Tossing
14/39
/*** Constructs a CoinToss object.* The probability on head is 0.5.* @param N the number of realisations* @param p the probability on head.*/
public CoinToss(int N, double p) {this.N = N;this.p = p;this.rand = new Random();
}
/*** Constructs a CoinToss object.* @param N the number of realisations* @param p the probability on head.* @param seed the random seed for the random number generator*/
public CoinToss(int N, double p, long seed) {this.N = N;this.p = p;this.rand = new Random(seed);
}
Coin Tossing
15/39
/*** Tosses a coin <i>N</i> times, and prints* <i>number of heads - number of tails</i>*/
public void printRealisation() {int n = 0; /* initialization */int points_A = 0;int points_B = 0;
while (n < N) {if (rand.nextDouble() < p) /* coin tossing */
points_A = points_A + 1; /* it is Head */else
points_B = points_B + 1;n = n + 1;System.out.println("" + (points_A - points_B));
}
}
public static void main(String[] arg) {CoinToss c = new CoinToss(1000, 0.5);c.printRealisation();
}
}
Coin Tossing
16/39
Mathematica
realisation = RandomChoice[{-1, 1}, 10000]Count[realisation, 1]
cumulative = Accumulate[realisation];ListLinePlot[cumulative]
Coin Tossing
17/39
2000 4000 6000 8000 10 000
50
100
Oscillations become bigger and bigger (grow as√
N )
Coin Tossing
18/39
Fraction of time one of the players is leading
Let P(α, β) be the probability that one of the players is leading between 100α% and 100β% of the time
To determine P(α, β) do the experiment “Throw N times with a coin” many times; an experiment is successfulif one of the players is leading between 100α% and 100β% of the time
Then
P(α, β) ≈number of successful experiments
total number of experiments
Coin Tossing
19/39
Simulation of M experiments
int success = 0
for (int run = 0; run < M; run++) {coinTossing();if ((alpha < 1.0 * time_A / N &&
1.0 * time_A / N < beta) ||(alpha < 1.0 * time_B / N &&1.0 * time_B / N < beta))
success++;}System.out.println(1.0 * success / M);
Coin Tossing
20/39
Simulation of coin tossing
int n = 0;int points_A = 0;int points_B = 0;int time_A = 0
while (n < N) {if (rand.nextDouble() < 0.5) points_A++;else points_B++;if (points_A - points_B >= 0)time_A++;
n++;}
time_B = N - time_A;
Coin Tossing
21/39
Results for M = 104, N = 104:
(α, β) P(α, β)(0.50, 0.55) 0.0648(0.50, 0.60) 0.1294(0.90, 1.00) 0.3972(0.95, 1.00) 0.2743(0.98, 1.00) 0.1692
Coin Tossing
22/39
Histogram of time_A/N
0.2 0.4 0.6 0.8 1
100
200
300
400
500
600
Coin Tossing
23/39
Successive Heads
Let P(k) be the probability of at least k successive Heads in case of 20 trials
To determine P(k) do the experiment “Throw 20 times with a coin” many times; an experiment is successful isa row of at least k Heads appears
Then
P(k) ≈number of successful experiments
total number of experiments
Coin Tossing
24/39
Simulation of M experiments
int success = 0;
for (int run = 0; run < M; run++) {coinTossing();if (k_row) success++;
}
System.out.println(1.0 * success / M);
Coin Tossing
25/39
Simulation of coin tossing
int n = 0;int nr_Heads = 0;boolean k_row = false;
while (n < 20 && !k_row) {if (rand.nextDouble() < 0.5)nr_Heads++;
else nr_Heads = 0;if (nr_Heads >= k) k_row = true;n++;
}
Coin Tossing
26/39
Results for M = 104:
k P(k)1 1.000002 0.983173 0.786434 0.476675 0.250786 0.120817 0.058178 0.027169 0.0125510 0.00582 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
0.0
0.2
0.4
0.6
0.8
1.0
Coin Tossing
27/39
Histogram of the maximum number of successive heads
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 170
5000
10 000
15 000
20 000
25 000
30 000
Coin Tossing
28/39
Birthday problem
Let P(N ) be the probability that at least two persons have the same birthday in a group of size N
To determine P(N ) do the experiment “Take a group of N randomly chosen persons and get their birthdays”many times; an experiment is successful is at least two persons have the same birthday
Then
P(N ) ≈number of successful experiments
total number of experiments
Birthday problem
29/39
Simulation of birthday problem
int success = 0;
for (int run = 0; run < M; run++) {"take group of size N"if (same_birthday)success++;
}
System.out.println(1.0 * success / M);
Birthday problem
30/39
Simulation of taking random group
int n = 0;boolean[] birthday = new boolean[365];boolean same_birthday = false;
while (n < N && !same_birthday) {int newBirthday = rand.nextInt(birthday.length);if (birthday[newBirthday]) same_birthday = true;else birthday[newBirthday] = true;n++;
}
Birthday problem
31/39
Results for M = 103 and seed = 1
N P(N )10 0.12615 0.26920 0.42225 0.57230 0.69340 0.89350 0.974
Birthday problem
32/39
Simulation of the nearly birthday problem
int n = 0;boolean[] birthday = new boolean[365];boolean same_birthday = false;
while (n < N && !same_birthday) {int newBirthday = rand.nextInt(365);for (int i = -r; i < r; i++) {if (birthday[(newBirthday + i) % 365])
same_birthday = true;birthday[newBirthday] = true;
}n++;
}
Birthday problem
33/39
Results for M = 103 and seed = 1
N r P(N )10 0 0.126
1 0.3242 0.4767 0.878
20 0 0.4221 0.811
30 0 0.6931 0.971
Birthday problem
34/39
Lottery problem
Tickets are numbered 1, . . . , N
To print covered numbers, first generate a random permutation of 1, . . . , N and then print first number on firstticket, second one on second ticket, and so on; hence:
Number of winners is equal to number of numbers that stay on their position after this permutation
Average number of winners in a lottery
≈total number of winners in all experiments
total number of experiments
Lottery problem
35/39
Generating a random permutation of 1, . . . , N
1. Initialise t = N and A[i] = i for i = 1, . . . , N ;
2. Generate a random number u between 0 and 1;
3. Set k = 1+ btuc; swap values of A[k] and A[t];
4. Set t = t − 1;If t > 1, then return to step 2;otherwise stop and A[1], . . . , A[N ] yields a permutation.
Complexity is O(N )
Lottery problem
36/39
Results for M = 103, N = 104 and seed = 1
Average numbers of winners is 1.005
k P(k winners)0 0.3511 0.3852 0.1853 0.0664 0.013
Lottery problem
37/39
Results for M = 103, N = 105 and seed = 1
Average numbers of winners is 1.047
k P(k winners)0 0.3501 0.3802 0.1723 0.0744 0.020
Lottery problem
38/39
Observe that number of winners is equal to number of loops of length 1
Further, consider 1, . . . , s;then length of loop generated by 1 is uniform on 1, . . . , s
Loops of a random permutation of 1, . . . , N
1. Initialise t = N and W = 0;
2. Generate a random number u between 0 and 1;
3. Set l = 1+ btuc; if l = 1, then W = W + 1;
4. Set t = t − l; if t ≥ 1, return to step 2;otherwise stop and W yields number of loops of length 1.
Complexity is O(log N )
Lottery problem
39/39
Results for M = 105, N = 107 and seed = 1
Average numbers of winners is 1.001
k P(k winners)0 0.3671 0.3702 0.1833 0.0614 0.016
Lottery problem