Posted on

Learn MATLAB Episode #26: Continuous Variables

In this lecture we’re going to talk about continuous variables, so we’ve talked about discrete variables up until now. Discrete variables can only take on distinct values, but continuous variables can take on any value. So with continuous variables we don’t have a notion of probabilities for exact values because X can take on an infinite number of values, so the probability of equaling any specific exact value is zero. We can have probabilities for ranges though. So, for example, we can say the probability of X being between 3.13 and 3.15 is greater than zero. We have a useful function called the cumulative distribution function, or the CDF, that helps us measure such probabilities. We usually label this function as big F of X, and so the definition of F(X) is it’s the probability that the random variable big X is greater than negative infinity, but less than little x. Note that the probability of big X being between negative infinity and positive infinity is 1 since X has to take on a value, therefore the value of big F of positive infinity is equal to 1. Now how about going back to our original problem if we want to calculate the probability that X is between 3.13 and 3.15. That would just be big F of 3.15 minus big F of 3.13. So now let’s talk about the other useful function when we’re talking about continuous variables. This one’s called the probability density function, or the PDF. We usually denote it by little f of X, and it is defined as the derivative of big F of X with respect to X, so it’s like the slope of big F of X. Note that this function can be greater than one since it’s not a probability, it is a probability density f of X, little f of X does have to be greater than or equal to 0 though. So here’s one example where little f of X can be bigger than one. So, let’s say little f of X is uniform between zero and 0.1, so that means if you try to sample from this random variable X you’ll always get a value between zero and 0.1, and the probability of any particular value is equal to all the others. Now I’m going to claim that little f of x has to equal 10 if X is between 0 and 0.1, and 0 otherwise. Now why is this, because big F of X. Since little f of X is the derivative of big F of X, big F of X is the integral of little f of X. In the integral we can take the constant out and then calculate the integral from 0 to X. Now we know that from above big F of infinity has to equal 1, so the integral from minus infinity to infinity equals to 1, but since little f of x is 0 after 0.1. we can just take the integral from 0 to 0.1. That gives us 0.1c, and if we solve for c, c equals 10. Therefore, we’ve seen a scenario where little f of X can have a value greater than one because it’s a probability density, and not a probability value. Later on in this course we’ll look at more complex continuous distributions.

Posted on

Learn MATLAB Episode #25: Birthday Paradox

One interesting and popular problem in probability is called the birthday problem, or the birthday paradox, and the problem goes something like this. So, given a classroom of n students, what is the probability that at least one pair of students shares a birthday? Now you might be surprised that at N equals 23 the probability is about fifty percent, which is why this problem is called a paradox. So that means in an average sized classroom there’s probably a pretty good chance that two people in the class share a birthday. This is counterintuitive since there are 365 days in a year. In this lecture I’ll show you the theory behind the solution, and how to visualize it in MATLAB. So the first thing is the problem of at least one pair of people sharing a birthday is difficult, but remember that the probability of all distinct events have to add up to one. So what is the opposite of at least one pair of people sharing a birthday? It’s not two people sharing a birthday, or three people sharing a birthday, or two pairs of two people sharing a birthday, these events all fit into at least one pair sharing a birthday. So the two disjoint events that we want to talk about are the probability that at least one pair shares a birthday, and nobody shares a birthday. So these two events are disjoint and therefore they have to add up to one. So we can calculate then the probability that two people or at least one pair of people shares a birthday as 1 minus the probability that nobody shares a birthday, and so in mathematics we would call this a counting problem. So now let’s think about how do we calculate the probability that nobody shares a birthday. So there are 365 days in a year. Now if you think of each day as a bucket we have one person and they have 365 buckets to choose from. The probability that this one person will collide with another is 0. The probability that one person shares a birthday with somebody else when there’s only that one person is 0, so the probability that nobody shares a birthday in this case is 1, or 365.365. Now what about two people? So with one person already having chosen a birthday or a bucket, the second person has a 1/365 chance of colliding with that person. So the probability of at least one pair having a common birthday is 364/365. So this is the case with two people. Now if we have three people the third person only has 363 buckets to choose from. So we have 364/365 times 363/365, and we can multiply these probabilities because they are independent. So this is the probability that three people, and a group of three people, at least one pair would share a birthday. So we can continue this pattern but it would probably be easier to write a matlab function to do this. So my birthday function is going to return all the probabilities up to the value n. So I’m going to initialize a to be an array of zeros, and I’m going to count up to n, and fill in the values of a. Actually, I’m going to say one is because we’re subtracting from one. Actually, it doesn’t matter what I initialize date to because i’m going to say 1 minus over here. Okay, so, we know that we have to multiply by something over 365 each time and subtract that from one, and so we can use a for loop to iterate over the thing that has to be subtracted and multiply the new value iteratively. Now that we have our function let’s test it, so let’s say a equals birthday, and let’s set n equal to 100, and let’s plot. Okay, so, you can see here when n is about 23 you get the probability around 0.5. When n is equal to 50 you’re right above 90%, so there’s a pretty good chance in a group of 50 people that at least one of those pairs of people shares a birthday. So let’s check A(23), right it’s about 50%, and that is the solution to the birthday paradox.

Posted on

Learn MATLAB Episode #24: Generating Random Values

So in this class we’re going to talk about generating a random variable from a certain distribution. This could be useful for doing simulations of systems that have uncertainty. Matlab has some built-in functions to help us do this. So the first one we’re going to talk about is called randi, and it takes one argument called imax, and this function gives us a uniformly distributed variable between one and imax. So let’s try it. So 9 is in between 1 and 10. Now there’s another function randi which takes in a maximum value, and another parameter called n. So let’s set n to 3, so that returns an n-by-n matrix of random values between 1 and imax. So suppose I wanted to generate random values between 10 and 20, how would we do that? Because randi can give us values anywhere between 1 and imax, so what we could do is we could just add 10 to all the values that randi returns. So this gives us a 3 by 3 matrix with values only between 10 and 20. Another useful function is just rand by itself. So this function gives us a random number between 0 and 1, so it’s different from the previous one where we don’t get integers we get real numbers. rand returns a number with a uniform distribution, so the probability of getting point .25 is the same as the probability of getting 0.75. So let’s try and imply histograms for different values of n. Alright, so this is a histogram for random numbers between 0 and 1, and N equals 10 array. Alright, so it’s not quite uniformly distributed, let’s try a bigger n value. Alright, so immediately it starts looking more uniformly distributed as n increases, so let’s try a bigger n. Alright, so it looks even more uniformly distributed. Now, 10,000. Alright, so it’s almost flat even. That’s 100,000, and this is a million, it looks almost perfectly flat. So that’s the idea with the frequentist view of probability is that when n approaches infinity, your probabilities approach their true values. So now let’s think about a different problem. Suppose I want a specific discrete distribution, so say I want to simulate an unfair coin. So, to write it out I want p of heads equal to .25, and I want p of tails to equal 0.75. How could I write a function to give me random values that could draw from this distribution instead of a uniform distribution? So we can create a function to do this. We can call it biased coin, it’s going to take in one value little p which represents, let’s call it P heads which is probability of getting heads, and it’s going to return the coin face. So we’re going to generate a random value, if it’s less than P heads we’re going to return heads, else we’re going to return tails. Let’s try our function. Alright, so now we’re going to try our new biased coin function by initializing an array of say size 1,000…you know what we’re going to do this in a separate function. We’re going to initialize a n by 1 array, we’re going to count from 1 to n, and we’re going to use the biased coin function to generate a value for each element of the array. Alright, so let’s try the function we just made. Test coin .25 for n equal to 1,000. Alright, so you see the number of heads which resolves to the integer 104, and then tails resolves to the integer 116. So you see this is about 250 and this is about 750 which is what we would expect in the thousand coin tosses.

Posted on

FREE Python Programming Course on Teachable

If you want to learn how to program, you will LOVE this course! This course was designed for complete beginners with little to no understanding of programming, and will give you the knowledge to get started coding using Python 3.

Enroll now for FREE on Teachable!

https://goo.gl/x6oBPE

We will cover the following topics in this course:

  • Python installation
  • Running Python scripts in terminal
  • PyCharm IDE setup
  • Numbers, strings, Boolean operators, lists, dictionaries, and variables
  • Functions, arguments, return values, loops, and modules
  • Final project using the information covered in the course

We hope you enjoy the course and it our goal to give you the knowledge to begin writing your own programs in Python!

https://goo.gl/x6oBPE

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.

Posted on

Learn MATLAB Episode #22: Introduction to Probability

Welcome to this course on matlab and probability. This first lecture will focus on the introduction and course outline. Because this course uses matlab it won’t follow a traditional probability and statistics course outline, rather i’ll show you as we go along how the concepts of probability can be applied, or viewed, through the lens of matlab. More generally, I want to show you how a programmer might approach problems in probability. So now let’s talk about some of the topics we’re going to go through in the second ecture. We’re going to talk about what is probability, and we’re going to give some definitions and examples. In the lecture after that we’re going to talk about how we can measure probability given some data. So, how to open a file, and measure the probability of some of the features of your data. In the next lecture we’ll talk about how do you generate random data, so how can you do a probabilistic simulation. In the next lecture we’ll talk about a famous problem in probability called the birthday problem, or the birthday paradox. In the next lecture we’ll extend the idea of probability from discrete variables to continuous variables. In a lecture after that we’ll talk about a special continuous variable distribution called the Gaussian distribution, or the normal distribution. In the next lecture we’ll talk about if you have some data that is continuous, how do you test if it is Gaussian distributed? In the lecture after that, we’ll talk about if you have two different Gaussian distributed groups of data, how can you compare the two? And then in the last lecture, we will extend the idea of the Gaussian to a multi dimensional Gaussian. So, what will you be able to do by the end of this course? You’ll understand mathematical problems that contain uncertainty. You’ll be able to quantify uncertainty both in theory and in practice. You’ll be able to use matlab to measure uncertainty in your data. You’ll be able to use matlab to simulate systems that contain uncertainty. You’ll be able to understand and calculate probabilities in the famous birthday paradox. You’ll understand the mathematics behind the very famous bell curve, or a normal distribution, or Gaussian distribution. You’ll know whether or not the data you’re working with is Gaussian distributed, and you’ll know how to handle Gaussian distributed data. I look forward to teaching you.

Posted on

Learn MATLAB Episode #21: Gaussian Filter Blur and Edge Detection

So now let’s take our Gaussian and convolve it with the image. So now remember that A is 512 x 512 x 3, which is a three-dimensional matrix, and H is a two-dimensional matrix. So if I try to do this I’m going to get an error I’m sorry we are using the CONV2 function, and you still get an error because A is not supposed to be a three-dimensional matrix, right. So if we look at the definition of convolution we’re working with two dimensional matrices, if we have two dimensional convolution. So what we want to do is let’s just take the red channel and it doesn’t matter which one, because remember when we were looking at the red, green, and blue channels we pretty much saw the same image for every channel. Ok, so, we still get another warning and that’s that A is still in values of UINT8, so let’s change that to double. Ok, so, now let’s imshow(C) and see what we get. So it’s all white, and so what does white mean? That means all of the values are too high, right, so there’s too much intensity in this image. So, you have to play around with the values a little bit, so let’s try making a smaller filter. Ok, so, let’s say H is equal to my_gaussian 25 and then sigma is 5. So now the filter’s smaller and the sigma is smaller, so the blur is going to be less, but the intensity is still too high. So that could mean just the values are too high and we need to multiply by lower values. So let’s divide the filter values H x 1000, and imshow(C) again. So now we’re starting to see some black, right, so that’s good. Ok, so, I’m decreasing values and I’m starting to see the image. Okay, so, here’s the original image but blurred using a Gaussian of sigma equals 5. Now one thing I didn’t show you guys because I wanted you to go through the exercise of creating a two dimensional Gaussian filter yourself, is that we already have a function called fspecial in matlab that create filters for us, and so fspecial can create many different types of filters. So, in addition to the Gaussian it can create laplacian filters, an averaging filter which is another thing we’ve used, the Sobell filter which is useful for finding edges, so all different types of filters. So let’s try using fspecial instead. fspecial(‘gaussian’, 25, 5); Now let’s do our convolution. So I’m going to divide by a thousand, because I know I’m going to have to divide. So let’s just see what we see. Ok, and so it’s a little dark so I didn’t have to divide by a thousand, maybe I could had divided a lower number, but you can see the idea is that we’ve blurred the original image using the Gaussian filter given to us by fspecial instead of our own Gaussian filter that we built. Ok, so, like we did before I want to plot the filter that we created. So I’m going to go imshow little h, which is the filter we created with fspecial, and notice how it’s just all black. Remember that 0 is black and 1 is white. So what we could do if we want to look at what’s in h, let’s check the maximum value of h. So max, max because it’s two-dimensional. The maximum value is .0065, so of course when we plot that it’s going to be pretty close to black. So what we could do if we want to scale it by one, if we want the maximum value to be one, we could do imshow(h) divided by the maximum value. Ok, so, now we see what we expect to see which is white in the middle. So one question you might have is why is the value of h so small when we use fspecial? That’s because it does a thing called normalization. So if you have ever studied probability you know that a probability distribution has to sum to 1, so here it is a similar thing. So if we sum across both dimensions of h we get 1, and so that’s why the values of h are so small. And so now just for completion sake, I want to do sort of what’s the opposite of blurring, I want to do edge detection of the image. So I want to find all the edges and set those values to 1. So matlab has a method called edge that will do this, and it’s very simple to use. You pass in again the gray-scale image, right, so only a two-dimensional matrix that won’t work if you pass in the entire image matrix. Ok, so, edge just does all of that automatically for you. So if imshow(E) I can more or less see where all the sharpest edges are, and of course there are parameters you can pass into edge to make it more or less sensitive, but this is by default what it does.

Posted on 6 Comments

FREE MATLAB Course on Teachable

MATLAB is much easier to learn when you can try everything for yourself in this course for beginners! With more than a million users, MATLAB is a must know programming language for science, engineering, and economics professionals.

Learn about solving equations in MATLAB, data structures, probability, and how to plot data in MATLAB from a software engineer with proven experience using MATLAB. If you want a screencapture course that shows you exactly how to use MATLAB, you’re ready to take this course!

https://goo.gl/qNqhlD

 

Posted on

Learn MATLAB Episode #20: Gaussian Image Noise Reduction

Now we’re going to move on to the next step in order to implement our blurring tool, or our blurring filter. So the first thing we need to do is we need to extend this idea of convolution to two dimensions. So, the first modification we did so we looked at convolution at it’s most basic definition where it’s of a continuous variable filtered by another continuous variable, so we discretize it by turning it into a sum, and so we already have discrete signals because we’re working in matlab. Now, when we do two-dimensional convolution notice we now have two dummy variables. So before tau or k, now we have two dummy variables tau 1 and tau 2, or n1 and n2,. And so it’s basically what you would expect when you have one deconvolution you’re going from minus infinity to infinity along the one dimension that’s the independent variable, so you can think of that as time, and in two dimensions we go from minus infinity to infinity for both independent variables so that the x and y, or in other words the two spatial dimensions of the image. So now that we’ve extended our idea of convolution to two dimensions let’s think about how we could implement a blur using a Gaussian. So a Gaussian is basically a spread, right, there’s a middle point and then it spreads out over a radius in a circular fashion. So, my question to you is how can we build a two-dimensional Gaussian image that we can use as a filter on the original image, so that we can do a convolution between those two? So, I’m going to give you a minute to think about that, please pause this video and come back when you have figured it out. Ok, so, I have in fact actually been showing you the solution to this for multiple videos now. So I’ve created a function called my Gaussian, and it takes in two parameters n and Sigma. So n is going to be the size of the square, so the output is going to be an n x n matrix, and Sigma is going to represent the standard deviation of the Gaussian as is convention when we’re talking about Gaussians. So we have the output called H, now I only need the value of two Sigma squared ever in this equation, so I’m just going to calculate 2 sigma squared at the beginning so I don’t have to do that on every iteration of the loop. I’m going to initialize h to be an n x n matrix of all zeros, and then I’m going to use a for loop to assign every value of age. So, I going from 1 to n, and j going from one to n. Now I assign x to equal i minus n/2, and y to equal j minus n/2. Why is that? Because I want the center point of H, so that would be H of n over 2 over 2, to to be the highest point of the Gaussian, and so that’s when x is equal to 0 and y is equal to 0, and the exponent of 0 is 1. So that would be the maximum value of the Gaussian and then every point from there would be smaller, right, so when I is n/2 x=0, when j is n/2 y=0. And the formula for a Gaussian is x squared plus y squared over 2 sigma squared, and then you take the exponent of the negative of that. Ok, so, what does this actually give us? So let’s use the my Gaussian function, say n is a hundred and Sigma is 10. Now let’s imshow the H that I got. Ok, so you can see so remember that white is the maximum value 1, and black is the minimum value zero, and so this is a Gaussian what a Gaussian looks like when you plot it on an image. So what would I do if I wanted to see more white? I could increase Sigma, right? So, let’s say Sigma is 25, imshow H, and so you see the radius of the white part has increased.

Posted on

(FREE) Create Your First Udemy Course!

If you want practical tips for publishing your first course on Udemy, you are going to love this FREE course! Watch as I go step-by-step through the course creation process and show you the quickest and most efficient way to get your first course published.

http://bit.ly/2mqxviS

Learn from an instructor with over 2 years of experience teaching on Udemy, working with top instructors to create bestselling courses, and marketing online courses across multiple platforms!

This course requires absolutely no experience working with Udemy, teaching online, or recording in front of a camera. Anyone can start teaching on Udemy today, and that’s what makes it such a fantastic learning platform!

Thank you for taking the time to read this, and I hope to see you in the course!\

http://bit.ly/2mqxviS