Posted on

Learn MATLAB Episode #23: Measuring Probability

Welcome to this third lecture on matlab and probability. In this lecture we’re going stray from your typical probability course. So we’re going to have some data matrix, and we’re going to measure the probability of a variable that the data matrix represents. So what I want you to do is I want you to go to in your web browser github.com/lazyprogrammer/matlab-probability-class. Once you go there, you’re going to copy and paste this SSH clone URL, and I want you to go into your terminal and type in and git clone, and then paste that URL. I’ve already done it so I’m not going to do it again. This is going to give you some files that are relevant to this class that I’m going to use for the coming lectures. Okay, so, now that you have those files you want to go into matlab, change your directory to work in that same directory that you guys checked out from git. So we’re going to load this data. Ok, so, r is a 100 by 1 matrix, so let’s just plot r amd let’s see what it looks like. Alright, so, it has a bunch of random values that are between -5 and 5. So now let’s say I wanted to calculate the probability that r is equal to -5. How would I do that? So, one way is I could sum all the values where r is equal to -5 and divide it by the total number of values in r. So it gives me .07. I can do the same thing for every other value in the matrix. So we get about .06 to .11. So this is what we call the frequentist view of statistics. It means that if we flip a coin 1000 times, and that’s a fair coin with heads or tails, the probability is 50% and we should get heads about 500 times, and we should get tails about 500 times. The idea is that as the number of coin flips approaches infinity, our measurement of the probability of heads should approach 0.5. So we can also plot the histogram of r, and this should give us an idea of the shape of the distribution. Alright, so, let’s do a little more complex example. Let’s say I want to calculate the probability r is even. How can we do that? So in the same way as we did before, we might want to say r equals -4 or r equals -2, but that wouldn’t be the best way to do this. We would use the modulo function. So if mod(R,2) is equal to 0, that means r is even, divided by the length of r. Alright, so the probability that r is even is .43, and we can do the same thing to determine the probability that r is odd. And so notice that these two events are disjoint. You can either have r equals even or r equals odd, and those two events are the only possible events. So their probabilities should add up to 1, and we can verify that .43 plus .57 is equal to 1. Now so let’s say I want to test if r takes on a specific value, so let’s say I want to calculate the probability that r is equal to -5 or positive 5. How would I do that? So we would use the or operator. So r equals 5 or r equals -5, divided by the length of r. So the probability that r is equal to 5 or negative 5 is .21. Notice that we can use the same method for our first problem which was to determine even or odd. Which is the way I suggested not doing it, but we want to check our answers, and so that gives us the same answer. The probability that r is equal to -4, -2, 0, 2, or 4 is the same as the probability that r is even.