MATLAB Addendum for Lecture 1

ECE 650
Some MATLAB Help
(addendum to Lecture 1)
D. Van Alphen
(Thanks to Prof. Katz for the Histogram  PDF Notes!)
MATLAB GUI for Examining PDF’s, CDF’s:
>> disttool
Choose pdf
Select one of 22
types of distributions
or cdf
Moveable
data cursor
Set desired
parameters,
within limits
ECE 650
D. van Alphen
2
Obtaining Probabilities for Gaussian RV’s
- An Example
Let X be N(1, 4). Find Pr(X < 3).
3  m
3  1


Pr( X  3)  Pr  Z 
  Pr  Z 
  (1)

2




Standard Normal PDF
MATLAB Code* (2 approaches):
0.8
0.6
0.4
0.2
0
-8
-6
-4
-2
0
2
4
6
8
>> x = 3; z = 1;
>> F = normcdf(z, 0, 1)
F=
0.8413
>> F1 = normcdf(x, 1, 2)
F1 =
0.8413
MATLAB function normcdf(x, m, s) returns FX(x) for RV X ~ N(m, 2)
ECE 650
D. van Alphen
3
Obtaining Probabilities for Gaussian RV’s
- Another Example
Let Z be N(0, 1). Find Pr(-1 < Z < 1) = P( |Z| < 1).
Pr(-1 < Z < 1) = (1) –  (-1)
0.5
MATLAB Code:
(-1)
>> z = [-1 1];
>> F = normcdf(z, 0, 1); % 2D vector
>> diff = F(2) - F(1)
diff =
0.6827
 (1)
0.4
0.3
0.2
0.1
Probability within 1 standard
deviation of the mean
ECE 650
D. van Alphen
0
-2
0
2
4
Obtaining Probabilities for the Standard
Normal Random Variable, Z: N(0, 1)
Probability within 1
 of the mean
Probability within 3
 of the mean
Probability within 2
 of the mean
Pr( |Z| < 1)  68%
Pr( |Z| < 2)  95.4%
0.5
0.5
0.5
0.4
0.4
0.4
0.3
0.3
0.3
0.2
0.2
0.2
0.1
0
ECE 650
0.1
68%
-2
0
2
0
0.1
95.4%
-2
0
D. van Alphen
Pr( |Z| < 3)  99.7%
2
0
99.7%
-2
0
2
5
(By Claims 1 & 2, Lecture 1, p. 36):
For Any RV X ~ N(m, 2)
0.25
0.2
pdf
-1 
+1 
68%
0.15
-2 
0.1
0.05
0
-8
-3 
-6
-4
-2
95.4%
99.7%
0
m
+2 
+3 
2
4
6
8
Pr( |X – m| < 1 ) = P( |Z| < 1)  68%
Pr( |X – m| < 2 ) = P( |Z| < 2)  95.4%
Pr( |X – m| < 3 ) = P( |Z| < 3)  99.7%
ECE 650
D. van Alphen
6
MATLAB Functions for Gaussian RV’s
normcdf(x, m, s) computes the cdf, F(x) = Pr(X  x) for RV X ~ N(m, s2)
norminv(p, m, s) computes the inverse cdf for RV X ~ N(m, s2); i.e., it
returns the value x such that FX(x) = Pr(X  x) = p
normpdf(x, m, s) computes the pdf, f(x), of the normal distribution with mean
m and standard deviation s
normrnd(m, s) returns a pseudorandom number (called a variate) from a
normal distribution with mean m and standard deviation s. To generate a
matrix of size (#_rows, #_cols) of such variates, use: normrnd(m, s,
#_rows, #_cols)
See randn for a similar function available without the statistics toolbox.
ECE 650
D. van Alphen
7
Histograms
• A histogram is a bar graph in which the bar heights show the
frequency of (random) data occurring over intervals of equal width
– The individual bars are referred to as bins
• Example: a 10-bin
histogram of measured
voltages for 1000
nominal 10-KW
resistors
• MATLAB Command:
>> hist(y)
250
200
150
100
50
0
6
data vector
ECE 650
8
10
12
14
Resistance, KW
D. van Alphen
8
Histograms, continued
• MATLAB allows you to specify the number of bins:
120
– >> hist(y, 20)
100
80
60
(a 20-bin histogram of
data vector y)
40
20
0
6
8
10
12
14
Resistance, KW
• MATLAB allows you to specify the bin centers:
400
– >> centers = [7 : 15];
– >> hist(y, centers)
300
200
100
0
7
8
9
10
11
12
13
14
15
Resistance, KW
ECE 650
D. van Alphen
9
Frequency Histogram
 Relative Frequency Histogram  Density Histogram
• Relative Frequency Histograms show the fraction of values in
each bin (freq. hist. heights  # data points)
• Density Histograms are scaled so that the total area (in the bins) is
equal to one. (rel. freq. hist. heights  bin width)
• MATLAB Example (Graphs on next page)
>> centers = [7 : .5 : 13];
>> freq = hist(y, centers);
>> bar(centers, freq, 1)
>> rel_freq = freq/sum(freq);
>> bar(centers, rel_freq, 1)
>> dens = rel_freq/.5;
>> bar(centers, dens, 1)
% histogram
% rel. freq. histogram
% bin width = .5
% density histogram
Note: The “1” in the 3rd argument of “bar” makes the bars adjacent,
without spaces between them.)
ECE 650
D. van Alphen
10
Frequency Histogram
 Rel. Freq. Histogram  Density Histogram
Histogram
250
0.25
200
0.2
150
0.15
100
0.1
 1000
50
0
6
8
10
12
Rel. Freq. Histogram
0.05
0
6
14
Resistance, KW
8
10
12
Resistance, KW
14
Density
0.5
0.4
Total area = 1,
like pdf
0.3
0.2
 .5
0.1
0
6
8
10
12
14
Resistance, KW
ECE 650
D. van Alphen
11
Density Histograms and PDF’s
• Recall that the density histogram gives the distribution of “relative
frequencies” divided by the bin width for the data
# data pts  
Probability Density
Function
Density Histogram
bin width  0
Generating 10,000 variates from the distribution: N(0, 1)
>> x = normrnd(0, 1, 10000, 1);
To generate the theoretical pdf:
>> x1 = -4 : .01 : 4;
>> y1 = normpdf(x1, 0, 1);
>> plot(x1, y1)
ECE 650
D. van Alphen
12
Density Histogram  PDF
10,000 Observations of a “Standard Normal” RV
0.4
0.5

10 bins
0.4
0.3

30 bins
0.3
0.2
0.2
0.1
0.1
0
-4
-2
0
2
0
-4
4
0.5
0.5
0.4
100 bins
0.4
0.3
0.3
0.2
0.2
0.1
0.1
0
-4
ECE 650

-2
0
2
0
-4
4
D. van Alphen
-2
0

2
4
pdf
-2
0
2
4
13
MATLAB Function Summary
(Requiring the Statistics Toolbox)
For Arbitrary RV’s, with dist representing the particular type of RV:
• distcdf(x, ... ) computes the cdf, F(x) = Pr(X  x)
• distinv(p, ... ) computes the inverse cdf; i.e., it returns the value x
such that FX(x) = Pr(X  x) = p
• distpdf(x, ... ) computes the pdf, f(x), of the specified distribution
defined at the point(s) x
• distrnd( ... ) returns a pseudorandom number (called a variate) from
the specified distribution
ECE 650
D. van Alphen
14
MATLAB Function Summary
(Requiring the Statistics Toolbox)
• When using the functions from the previous page, for arbitrary RV’s,
recall that dist represents the particular type of RV.
– Some of the (~22) RV types available:
• Gaussian (dist = norm)
• Binomial (dist = bino)
• Uniform (dist = unif)
• Chi-Square (dist = chi2)
• Student’s t (dist = t)
• Hypergeometric (dist = hyge)
• Rayleigh (dist = rayl)
• Exponential (dist = exp)
• Discrete Uniform (dist = unid)
ECE 650
D. van Alphen
15
MATLAB Functions
(Requiring the Statistics Toolbox)
• disttool brings up a GUI to display pdf’s and cdf’s for a many types of
random variables, with settable parameters
• randtool brings up a GUI to generate random variates from many
(~22) types of random variables, with settable parameters
MATLAB Functions
(No Toolbox Required)
• randn(…) generates random variates from a normal distribution
• rand(…) generates random variates from a uniform distribution
ECE 650
D. van Alphen
16