Reputation: 23
I am a beginner in MATLAB trying to use it to help me understand random signals, I was doing some Normal probability density function calculations until i came across this problem :
Write a MATLAB program to calculate the probability Pr(x1 ≤ X ≤ x2)
if X is a Gaussian random variable for an arbitrary x1
and x2
.
Note that you will have to specify the mean
and variance
of the Gaussian random
variable.
I usually use the built in normpdf
function of course selecting my mean and variance, but for this one since it has a range am not sure what I can do to find the answer
Y = normpdf(X,mu,sigma)
Upvotes: 2
Views: 3253
Reputation: 104503
If you recall from probability theory, you know that the Cumulative Distribution Function sums up probabilities from -infinity
up until a certain point x
. Specifically, the CDF F(x)
for a probability distribution P
with random variable X
evaluated at a certain point x
is defined as:
Note that I am assuming that we are dealing with the discrete case. Also, let's call this a left-tail sum, because it sums all of the probabilities from the left of the distribution up until the point x
. Consequently, this defines the area underneath the curve up until point x
.
Now, what your question is asking is to find the probability between a certain range x1 <= x <= x2
, not just with using a left-tail sum (<= x
). Now, if x1 <= x2
, this means that total area where the end point is x2
, or the probability of all events up to and including x2
, is also part of the area defined by the end point defined at x1
. Because you want the probability between a certain range, you need to accumulate all events that happen between x1
and x2
, and so you want the area under the PDF curve that is in between this range. Also, you want to have the area that is greater than x1
and less than x2
.
Here's a pictorial example:
The top figure is the PDF of a Gaussian distribution function, while the bottom figure denotes the CDF of a Gaussian distribution. You see that if x1 <= x2
, the area defined by the point at x1
is also captured by the point at x2
. Here's a better graph:
Source: Introduction to Statistics
Here the CDF is continuous instead of discrete, but the result is still the same. If you want the area in between two intervals and ultimately the probability in between the two ranges, you need to take the CDF value at x2
and subtract the CDF value at x1
. You want the remaining area, and so you just need to subtract the CDF values and ultimately the left-tail areas, and so:
As such, to calculate the CDF of the Gaussian distribution use normcdf
and specify the mean and standard deviation of your Gaussian distribution. Therefore, you just need to do this:
y = normcdf(x2, mu, sigma) - normcdf(x1, mu, sigma);
x1
and x2
are the values of the interval that you want to calculate the sum of probabilities under.
Upvotes: 3
Reputation: 4336
You can use erf
,
mu = 5;
sigma = 3;
x1 = 3;
x2 = 8;
p = .5*(erf((x2-mu)/sigma/2^.5) - erf((x1-mu)/sigma/2^.5));
error function
is defined like this in MATLAB,
Upvotes: 1