0:13

Welcome to class.

Â In this lecture we're going to talk about the basics of probability.

Â Now you've probably already seen some examples of probability in action.

Â For example, what's the chance that I'll lose to Scott in four conflicts?

Â What's the chance that Scott's cheating?

Â What's the chance that I'm going to grade all this week's assignments?

Â 0:46

On a serious note, we're going to teach you the basics of probability here, and

Â kind of equip you then to use those in Python to do interesting things.

Â So let's get to work.

Â [BLANK_AUDIO]

Â All right, to get started, I've built you a class page

Â that contains an overview of the

Â terminology that we'll use for probability.

Â 1:54

Let's say we have a trial involving a single six-sided die.

Â So what are the outcomes of that?

Â Well, it's the numbers one to six.

Â Okay.

Â If the die is fair, again, we have the same chance of rolling any of

Â the outcomes, then the probability for each

Â of those individual outcomes is exactly 1 6th.

Â You may occasionally hear the phrase uniformly distributed.

Â That just means that all the outcomes have the same chance.

Â Okay, one more piece of terminology that you'll often hear is talk about events.

Â So an event is a set of outcomes.

Â For example, what is the event corresponding to an even die roll?

Â 2:34

It's the set 2, 4, and 6.

Â It's whenever the die comes up with an even number.

Â We can talk about the probability associated with an event.

Â In that particular case see we would add up

Â the probabilities attached to each of the individual outcomes.

Â So it would be, 1 6th plus 1 6th plus 1 6th

Â to give a probability of one half for an even die roll.

Â 3:07

To do that we just simply have to understand how to generate random numbers.

Â You've probably already seen that in your previous Python programming experience, so

Â I'm just going to show you a very simple example to get started.

Â So here I've wrote a little Python code.

Â And this does the following, this function roll_die just takes a number of sides for

Â my die and uses random.randrange to actually

Â generate a die roll and return that result.

Â The only thing you might notice here is that

Â I've added one to it because typically our dies

Â are numbered from one to the number of sides,

Â not zero to the number of sides minus one.

Â And then I built a little function here that I give it a number of sides, a number

Â of rolls and it just rolls the die a

Â particular number of times, and just prints out the outcomes.

Â So in this particular case let's roll a

Â six-sided die, and I'm going to do ten trials.

Â 4:10

So now let's go on and I'll show you a

Â little more interesting application of computing to probability and randomness.

Â In some more interesting application, let's go back and

Â think about how Scott is whenever we play games.

Â So you probably seen instances where maybe he

Â fudges a little bit, maybe even just cheats.

Â For example we're rolling a die, he might have

Â a die that's loaded, so it prefers ones over sixes.

Â Or kind of more worrisome, maybe we're playing a game in Code Sculptor

Â and somehow randrange always returns the

Â right random number for his particular action.

Â How can we catch him?

Â 4:58

that.

Â All right, let's do that.

Â So here I have a function of Python.

Â It's called plot fairness.

Â And what it takes is it takes a particular side for die roller looking for.

Â It takes the number of sides of the die.

Â And it takes a maximum number of trials

Â that we're going to actually do our test on.

Â So if you look inside the code, what it

Â does is up to max trials it actually runs a

Â sequence of trials where we roll the die and

Â keep track if it came up with the desired side.

Â And then we compare the ratio of the times it comes up

Â to what we want versus the max, the total number of trials.

Â 5:44

So what we should see here is we increase the maximum number of trials

Â that the difference between the mathematical and

Â the computed value should get smaller and smaller.

Â So let's try that real quick and see what happens.

Â So here's a plot with 0 trials.

Â The difference between the mathematical and

Â the computed value is actually pretty big.

Â But as we increase the number of trials, notice here that

Â the difference is kind of getting closer and closer to 0.

Â This is what we should expect if the number of trials was really, really large.

Â So when we get up to 300 trials here, you can see that the

Â values are maybe kind of bouncing around by about maybe 3 to 5% around 0.

Â 6:26

So we'd like to try maybe even more trials.

Â So here's a trick we can do.

Â We don't have to basically try, like, 200 trials

Â and then 201 trials and 202 trials and so forth.

Â We can jump up by maybe strides of ten

Â trials and just 200, 210, 220 and so forth.

Â So I have a stride that I can vary here.

Â And so if I increase that stride, I can do more trials, so let's do that.

Â So I'm going to go up the max number of trials we're going to try is a thousand.

Â 6:52

So if we look at that, what you can see here is

Â we get out past 400 or 500 or 600, up to 1,000

Â trials, the spread has now gone down, is actually much smaller and

Â we're only maybe, I don't know, like 2 or 3% away from zero.

Â 7:05

So you can actually keep this, keep doing this and increasing

Â the stride to say, 100, and increase the number of trials.

Â You can kind of see if that difference is getting smaller and smaller.

Â That's a sign that your die is actually fair.

Â This is kind of a first little trick that we can use

Â computation to check for things like is a random process fair or not.

Â I, I'll finish up by showing you one more kind of very interesting example.

Â [BLANK_AUDIO]

Â All right, let's finish off with a lighthearted example

Â of a trial that has lots of potential outcomes.

Â So here I've got a program.

Â This was written by a student in our intro Python class.

Â He wrote it after only knowing Python for two weeks.

Â It's actually quite a remarkable program.

Â It's a love song generator.

Â It was by Antonio, and he has a little function here called print verse,

Â print first verse, that actually chooses between

Â four possible lines to start his love song.

Â And he has some more helper functions.

Â And at the end down here, he has kind of two main functions.

Â One that prints a stanza by kind of

Â putting together four lines in a single stanza randomly,

Â and then he makes a love song that consists

Â of three stanzas, and an optional riff by Pitbull.

Â [LAUGH] Let's just run it.

Â So here it is, all right.

Â What did, what did the random numbers generator give us this time?

Â In this dark and cold winter, I walk alone, trying to forget you.

Â Even if you don't share my passion for Lord Fenner.

Â I don't know who Lord Fenner is.

Â One day we'll run away together because I love you babe, because I love you, babe.

Â Oh, and we didn't get the riff by Pitbull.

Â I'm so disappointed.

Â Let's run it again here.

Â Oh come on, I want the riff by Pitbull.

Â Come on random number generator, don't be this way.

Â There we go.

Â We finally got tunes, tunes, tunes, tunes.

Â Excellent.

Â 8:47

So this is a example of a trial that can have lots and lots of outcomes.

Â They don't always have to be, maybe one to six, or deal a card.

Â You can have programs that generate millions of possible outcomes.

Â In fact, let's just do a quick bit of counting here to finish off lecture.

Â Let's count how many possible love songs this generator can create.

Â 9:08

So we need to do a little counting.

Â So we look at how we built up the thing we had, three stanzas

Â and an optional riff by Pitbull, so how many possible stanzas can we have?

Â Well, let's see, a stanza consists of a

Â first line, there were four possibilities for that,

Â four possibilities for the second line, four possibilities

Â for the third, four possibilities for the fourth.

Â So there's four to the fourth possible stanzas we can create.

Â Four to the fourth is 256.

Â So the number of love songs is going to be 256 times 256 times 256.

Â And then we have, I guess, a 50/50 chance whether Pitbull actually provides a riff.

Â So let's go over and compute that real quick.

Â So we're going to do print, let's see two, that's whether or not we have a

Â riff by Pitbull, and it's going to be 256 to the third.

Â