00:01
For this exercise, we are asked to simulate 1 ,000 world serieses using our software.
00:09
The series is a best of seven games, and we are told that team a has a 55 % chance of winning each game.
00:17
And so we simulate the probability that team a wins the world series.
00:23
So the first thing i'll tell you is i'm not a great programmer.
00:26
So in terms of format or efficiency, this may not be the most impressive way to do it.
00:32
But i'll show you what i've done.
00:35
So i've made n the number of simulations that we're going to do.
00:38
That's 1000 according to the question.
00:42
And also i've just made an object called number of games series, which is 7.
00:47
It's the number of games in a series.
00:52
And then to start with, i just created a vector of zeros that is the size of 1 ,000.
00:59
So that's n.
01:01
And i only did that so that we would have something to use the s -apply function on.
01:08
So, so far at this line we just have a vector of 1 ,000 zeros.
01:16
Now on line 7, series wins, i replace each of those zeros in the vector, each of the zeros and the replicates vector, with the simulated number of wins in the single series.
01:33
And i do that by applying to each of the zeros in the replicates vector, this function.
01:44
This function simulates the number of games 1 in a series.
01:49
And it uses the r uniform function.
01:56
It draws seven values from the uniform function on 0 to 1.
02:04
And if the value is less than 0 .55, which is a probability of 0 .55, then the outcome of this function is true.
02:16
Otherwise, it's false.
02:17
So in this function, a true represents winning a game, probability 0 .55.
02:24
A false represents losing a game.
02:28
The function takes seven of these games and then sums them.
02:33
So the sum is the number of wins in the world series.
02:39
So this function simply returns the number of wins in a simulated world series...