0:15

In Monte Carlo methods, you perform a bunch of random trials in order

Â to figure out the expectation that some

Â event, or multiple events, will actually occur.

Â Now, why would you do that?

Â Well, usually you do this in situations

Â where it is difficult or impossible to directly

Â compute the answer, or perhaps directly computing

Â the answer would take far too long Okay.

Â Now why is this called the Monte Carlo methods?

Â [LAUGH] Well, it's, because of the similarity to going to

Â a casino and basically playing and recording the result of a

Â bunch of, of gambling games, okay, and in that situation you're

Â basically randomly sampling,the probabilities of

Â winning and losing those games, okay?

Â And perhaps such casinos appear in Monte Carlo, alright, now, in

Â the real world what kind of of situations do we use this in?

Â Generally Monte Carlo methods are used for you

Â know, sort of physical or mathematical problems and the

Â the major classes of problems are optimisation problems,

Â figuring out probability, distributions and numerical integration, all right?

Â So let's take a look at how this works.

Â 1:23

This program uses Monte Carlo simulations, to determine the expected value

Â of getting exactly three of a kind when you roll five dice.

Â Now, I could compute the exact probability here, but

Â it's actually a little bit difficult to do so.

Â So we can use Monte Carlo methods to make our lives a little easier.

Â But because can compute it directly, we can also check the answer.

Â Now, the answer isn't completely obvious, because I

Â did search the web a little bit to see

Â if other people are able to do, and there

Â are actually a lot of wrong answers out there.

Â Okay, so we'll be able to confirm that the right answer

Â actually is the right answer alright, so how do we do this?

Â Well, we need to have a notion of what is a trial and what is an event, okay?

Â Here a trial is one roll of these five dice.

Â So you can see I have a function called compute trial, that rolls five dice.

Â Basically, it picks five random numbers between one

Â and six and returns them in a list, okay?

Â Now what is an event?

Â An event is that I got exactly three of a kind.

Â So I have a function here called check event that checks this.

Â It looks through the five dice and sees if

Â there are three of a kind of any number alright.

Â 2:25

Now, I use these two functions in one

Â simulation step, so what is one simulation step?

Â I compute a trial, so I roll the dice,

Â and I check if the event has occurred, alright?

Â And my simulation step function here returns true or false.

Â True if the even occurred during this step, false if it did not alright?

Â And then we have now our Monte Carlo function alright.

Â All right we're just doing the basic Monte Carlo simulation where it

Â takes as input the number of trials that I'd like to run.

Â And then there's just a loop here that iterates

Â for that many trials, each time I run one

Â simulation step, and if the event happens I increment

Â my counter if it does not I don't okay.

Â And so now the expected probability that the

Â event will occur, is simply the number of times

Â the event did occur divided by the number of

Â trials, and that's what this Monte Carlo function returns.

Â 3:16

Okay, so, I have my Monte Carlo function that

Â performs the simulation, how do we actually run it?

Â Well, you can see I have a run function here which is going to

Â run the simulation for different numbers of trials, so we can see what happens.

Â You can see in the comment that I also have put the actual probability of

Â getting three of a kind when you roll five dice that number is .1929, alright?

Â 3:37

So basically one out of every five times you

Â can probably expect to get three of a kind, okay.

Â Now you'll notice I pick a number of trials between 10 and 100,000.

Â Now before we run anything I want to you think about this for a minute, okay.

Â Do you expect to get more accurate results running 10 trials or 100,000 trials?

Â Why?

Â 4:02

Okay, I cut the video so you didn't have to wait for it to actually complete here.

Â But you can see what happened, right, with

Â ten trials I get a wildly incorrect result, 0.4.

Â Alright, then it gets a little bit better, 0.18, and then oscillates around and

Â finally converges to about 0.19373, which is

Â actually very close to the expected probability.

Â Okay, so you can see as I run more trials, things get a little bit more accurate.

Â Now there's no way that, no reason why it needs to look exactly like

Â this In fact it's going to look different every time, so let's run it again.

Â 4:33

Okay, you can see that this time I got different results.

Â With only ten trials it says 0.1, but then with 100,000 trials

Â I have 0.19375, which again is very close to the actual probability.

Â Okay, so there's no reason why you should believe

Â that this, that the results should look any particular way.

Â The one thing though is that as you run larger and larger

Â numbers of trials, I would expect the simulation to yield more accurate results.

Â And you can see about 100,000 trials here,

Â this simulation is pretty consistently accurate about giving

Â me a number that is very close to the real probability of rolling three of a kind.

Â 5:11

So the example I've used here to

Â motivate Monte Carlo methods is actually pretty simplistic,

Â and in fact, I can directly compute the

Â answer, I gave you the answer, even, right?

Â But that allowed us to see that

Â this method actually does produce the expected results.

Â But things really get interesting when you have a problems for which you can't

Â directly compute the answer, and that's where

Â Monte Carlo methods really become invaluable, right?

Â And they get used in lots of places.

Â They get used in the physical sciences.

Â They get used in engineering.

Â They get used in computational biology.

Â You can even use them in finance, you can

Â imagine using Monte Carlo methods to evaluate the expected

Â value of an investment portfolio alright, now, when you

Â use Monte Carlo methods, you have to be careful.

Â You have to start thinking about what

Â you're doing, alright, and making sure that

Â you use enough trials so that you're

Â goinig to get an accurate, accurate result, okay?

Â You need to make sure that these trials are correct, that you

Â are randomly picking from all of the possibilities in the right way, okay?

Â And you also have to make sure that the way that you're

Â doing this is accurately simulating, the

Â phenomenon that you're trying to model okay?

Â 6:15

But if you do all these things, we now have this method that allows

Â us to just randomly try a couple times well, a bunch of times, alright?

Â And if you try this enough, you can actually figure out the expected

Â value of some event occurring, rather than having to directly compute this, alright?

Â And this is an extremely powerful technique.

Â