You are on page 1of 4

Probability and Statistics Using Matlab

0.1

Statistical Properties

Matlab has many functions that are used to statistically quantify a set of
numbers. The size of a matrix of numbers can be evaluated through the
statement \[N,col]=size(data)", which gives the number of rows (N) and
columns (col) of the matrix called \data". The Matlab command \mean(X)"
provides the mean of the vector X. If \data" has more than one column,
then each column can be vectorized using the statement \X(i)=data(:,i)",
which assigns the vector X(i) the elements in the i-th column of the matrix \data". Subsequently, one can nd the minimum value of X(i) by
the command \min(X(i))", the maximum by \max(X(i))", the standard
deviation by \std(X(i))", the variance by \[std(X(i))]^2", and the rootmean-square by \norm(X(i))/sqrt(N)". The command \norm(X(i),p)" equals
\sum(abs(X(i)).^p)^(1/p)". The default value of p is 2 if a value for p is
not entered into the argument of \norm", i.e., \norm(X(i))=norm(X(i),2)".
Other statistical properties, such as the mode, skewness and kurtosis can be
obtained by writing your own m- le using other Matlab commands.
0.2

Probability Density and Distribution Functions

The probability density and distribution functions for many common statistical distributions are available via Matlab's statistics toolbox. These include,
among others, the binomial (\bino"), Poisson (\poiss"), normal (\norm"),
Chi-square (\chi2"), lognormal (\logn"), Weibull (\weib"), uniform (\unif")
and exponential (\exp") distributions. Three Matlab functions used quite
often are \pdf", \cdf" and \inv", denoting the probability density, cumulative distribution and inverse functions, respectively. Matlab provides each of
these functions for speci c distributions, where di erent input arguments are
required for each distribution. These functions carry a pre x signifying the
abbreviated name of the distribution, as indicated above, and the sux of
either \pdf", \cdf" or \inv". For example, \normpdf" is the normal distribution probability density function. Matlab also provides generic \pdf" and
\cdf" functions for which one of the function's arguments is the abbreviated
name of the distribution.
1

0.3

The Normal Distribution

The normal distribution-speci c \pdf" function contains arguments that has


three arguments: a particular x-value at which you seek the \pdf" value, the
mean value of the distribution and the standard deviation of the distribution.
For example, the Matlab command \normpdf(x,xmean,sigma)" returns the
value of the probability density function, p(x), evaluated at the value of x of
a normal probability distribution having a mean of xmean and a standard deviation of sigma. Speci cally, typing the Matlab command \normpdf(2,0,1)"
yields p(2) = 0:0540. A similar result can be obtained using the generic
Matlab command \pdf(norm,2,0,1)".
The normal distribution \cdf" function also has similar arguments: a
particular x-value at which you seek the \cdf" value, the mean value of the
distribution and the standard deviation of the distribution. This is none
other than the integral of the \pdf" function from 1 to x ; P (x). So,
typing the Matlab command \normcdf(1,0,1)" gives P (1) = 0:8413. This
tells us that 84.13% of all normally-distributed values lie between 1 and
one standard deviation above the mean. Typing \cdf(norm,1,0,1)" yields the
same value.
The normal distribution "inv" function gives the inverse of the cdf function, i.e., it provides the value of x for the cdf value of P . Thus, typing
the Matlab command \norminv(0.9772,0,1)" results in the value of 2. This
says that 97.72% of all normally-distributed values lie below two standard
deviations above the mean. There is not an equivalent generic Matlab \inv"
command.
0.4

The Normal Error Function

To obtain a value of the normal error function, p(z1 ), using Matlab, we make
use of Matlab's \cdf" function. This is speci ed by Equation 1:
p (z1 ) = normcdf (z1 ; 0; 1)

0:5

(1)

Recall that normcdf is the integral of the normal probability density function
from 1 to x , or to z1 in this case. Because the normal error function
is de ned between the limits of a normal mean equal to zero and z1 , we
must subtract a value of 0.5 from normcdf to obtain the correct normal
error function value. (Why 0.5?) For example, typing the Matlab command
2

normcdf (1:37; 0; 1)

0.4147.
0.5

0:5 returns the correct normal error function value of

Probability of Occurrence within a Range of Values

~
When we need to determine the probability, P (z ), that a normally-distributed
value will lie between z , we can use the Matlab \cdf" function. This probability is determined by Equation 2:
~

P (z ) =

p(x)dx

p(x)dx = normcdf (z; 0; 1)

normcdf ( z; 0; 1)

(2)
Using this formulation we can nd that P (2) = 0:9545. That is, 94.45% of
all normally-distributed values lie between z = 2. Further, recalling that
Matlab's P (z ) denotes the cdf value from 1 to z , a particular value of
a z can be obtained by typing the command \normcdf( 1 2 ; 0; 1)" and of
a +z by \normcdf( 1+2 ; 0; 1)". For example, typing the Matlab command
\normcdf( 1 029545 ; 0; 1)" gives the z-value of -2.00. A similar approach can
~
be taken to nd P for other distributions.
~

0.6

Student's t Distribution

The Matlab functions \tpdf", \tcdf" and \tinv" are used for Student's t
distribution. Matlab's command \tpdf(t,nu)" returns the value of the probability density function, p(t;  ), evaluated at the value of t of Student's t
distribution having a nu degrees of freedom (recall that there are an in nite
number of Student's t distribution, one for each degree of freedom value).
Speci cally, typing the Matlab command \tpdf(2,1000)" yields at value of
0.0541. Compare this to "normpdf(2,0,1)", which gives the value of 0.0540.
This shows that the probability density function value for Student's t distribution for a very number (here 1000) of degrees of freedom is e ectively
the same as that of the normal distribution. However, for a low value of
the number of degrees of freedom, say equal to 2, the probability density
function value of Student's t distribution di ers signi cantly from its normal
distribution counterpart ( \tpdf(2,2)" yields a value of 0.0680).
3

Exercises similar to those performed for the normal distribution can be


done for Student's t distribution. Typing \tcdf(1,1000)" gives the value of
0.8412 (compare this to the normal distribution value). Typing \tinv(0.9772,1000)"
results in the value of 2.0016 (again, compare this to the normal distribution
value). Finally, typing \tinv( 1+029545 ; 1000) yields a value of 2.0025. This
tells us that 95.45% of all Student's t-distributed values lie between 2:0025
standard deviations from the mean of the distribution.
:

0.7

Chi-Square Distribution

The Matlab functions \chi2pdf", \chi2cdf" and \chi2inv" are used for Chisquare distribution. Matlab's command \chi2pdf(chisq,nu)" returns the value
of the probability density function, p(2 ;  ), evaluated at the value of 2 of
the Chi-square distribution having a nu degrees of freedom. Note that, like
for Student's t distribution, there are an in nite number of Chi-square distributions. Typing the Matlab command \chi2pdf(10,10)" yields at value
of 0.0877. Typing \chi2cdf(10,10)" gives a value of 0.5595, signifying that
55.95% of all Chi-square-distributed values for 10 degrees of freedom lie between 0 and 10 (recall that there are no negative Chi-square values). Or,
in other words, there is a 44.15% chance of nding a value greater than 10
for a 10-degree-of-freedom Chi-square distribution. Similar to other speci cdistribution \inv" functions, \chi2inv(P,nu)" returns the 2 value at which
between it and 0 lie P% of all the nu-degree-of-freedom Chi-square distribution values lie. For example, typing \chi2inv(95.96,4)" yields a value of
10.

You might also like