Statistics With MATLAB/Octave: Andreas Stahel Bern University of Applied Sciences Version of 30th June 2017
Statistics With MATLAB/Octave: Andreas Stahel Bern University of Applied Sciences Version of 30th June 2017
Statistics With MATLAB/Octave: Andreas Stahel Bern University of Applied Sciences Version of 30th June 2017
Andreas Stahel
Bern University of Applied Sciences
There is no such thing as “the perfect document” and improvements are always possible. I welcome
feedback and constructive criticism. Please let me know if you use/like/dislike these notes. Please
send your observations and remarks to [email protected] .
Andreas
c Stahel, 2016
Statistics with MATLAB/Octave by Andreas Stahel is licensed under a Creative Commons Attribution-
ShareAlike 3.0 Unported License. To view a copy of this license, visit http://creativecommons.org/licenses/by-
sa/3.0/ or send a letter to Creative Commons, 444 Castro Street, Suite 900, Mountain View, California,
94041, USA.
You are free: to copy, distribute, transmit the work, to adapt the work and to make commercial use of the
work. Under the following conditions: You must attribute the work to the original author (but not in any
way that suggests that the author endorses you or your use of the work). Attribute this work as follows:
Andreas Stahel: Statistics with MATLAB/Octave, BFH-TI, Biel.
If you alter, transform, or build upon this work, you may distribute the resulting work only under the same
or similar license to this one.
CONTENTS 1
Contents
1 Introduction 3
Index 44
SHA 30-6-17
LIST OF TABLES 2
List of Figures
1 Histograms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2 A histogram with matching normal distribution . . . . . . . . . . . . . . . . . . . . . 5
3 Bar diagram . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
4 Pie charts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
5 Stem plots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
6 Rose plot and stairs plot . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
7 Boxplots . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
8 Multiple boxplots in one figure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
9 Results of two linear regressions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
10 Result of a 3D linear regression . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
11 Random numbers generated by a binomial distribution . . . . . . . . . . . . . . . . . 16
12 Graphs of some discrete distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
13 A Poisson distribution with λ = 2.5 . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
14 Graphs of some continous distributions . . . . . . . . . . . . . . . . . . . . . . . . . . 23
15 Student-t distributions and a normal distribution . . . . . . . . . . . . . . . . . . . . 24
16 χ2 distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
17 Exponential distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
18 Confidence intervals at levels of significance α = 0.05 and α = 0.01 . . . . . . . . . . 27
19 Two- and one–sided confidence intervals . . . . . . . . . . . . . . . . . . . . . . . . . 29
List of Tables
1 Commands to load data from a file . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2 Commands to generate statistical graphs . . . . . . . . . . . . . . . . . . . . . . . . . 4
3 Commands for data reduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
4 Commands for generating random numbers . . . . . . . . . . . . . . . . . . . . . . . 16
5 Functions for distributions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
6 Discrete distributions, mean value µ and standard deviation σ . . . . . . . . . . . . . 19
7 Continuous distributions, mean value µ, standard deviation σ and median . . . . . . 21
8 Student-t distribution for some small values of ν . . . . . . . . . . . . . . . . . . . . 24
9 Commands for testing a hypothesis . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
10 Errors when testing a hypothesis with level of significance α . . . . . . . . . . . . . . 34
SHA 30-6-17
2 COMMANDS TO LOAD DATA FROM FILES 3
1 Introduction
In this document a few MATLAB/Octave commands for statistics are listed and elementary sample
codes are given. This should help you to get started using Octave/MATLAB for statistical problems.
The notes can obviously not replace a regular formation in statistics and probability. Find the
current version of this document at web.ti.bfh.ch/˜sha1/StatisticsWithMatlabOctave.pdf.
• The short notes you are looking at right now should serve as a starting point and will not
replace reading and understanding the built-in documentation of Octave and/or MATLAB.
• There are many good basic introductions to Octave and MATLAB. Use you favourite docu-
ment and the built-in help. The author of these notes maintains a set of lecture notes at
web.ti.bfh.ch/˜sha1/Labs/PWF/Documentation/OctaveAtBFH.pdf
• For users of Octave is is assumed that the statistics package is available and loaded.
– Use pkg list to display the list of available packages. Packages already loaded are
marked by *.
– If the package statistics is installed, but not loaded, then type pkg load statistics .
– If the package statistics is not installed on a Unix system, then type pkg install
-forge statistics. It is possible that you have to install the package io first by pkg
install -forge io .
– On a typical Win* system all packages are usually installed, since it is very difficult to
install. Consult the documentation provided with your installation of Octave.
SHA 30-6-17
3 COMMANDS TO GENERATE GRAPHICS 4
3.1 Histograms
With the command hist() you can generate histograms, as seen in Figure 1.
250 80 120
100
200
60
80
150
40 60
100
40
20
50
20
0 0 0
-4 -2 0 2 4 6 8 10 -4 -2 0 2 4 6 8 10 -4 -2 0 2 4 6 8 10
(a) default values (b) with 30 classes (c) with selected centers
Figure 1: Histograms
It is possible to specify the centers of the classes and then compute the number of elements in
the class by giving the command hist() two return arguments. Then use bar() to generate the
histogram. The code below chooses classes of width 0.5 between −2.5 and +9.5 . Thus the first
center is at −2.25, the last center at 9.25 and the centers are 0.5 apart.
SHA 30-6-17
3 COMMANDS TO GENERATE GRAPHICS 5
The number of elements on the above classes can also be computed with the command heights =
histc(data,[-2.5:0.5:9.5]). Here we specify the limits of the classes.
With a combination of the commands unique() and hist() one can also count the number of
entries in a vector.
With the command histfit() generate a histogram and the best matching normal distribution
as a graph. Find the result of the code below in Figure 2.
140
120
100
number of events
80
60
40
20
0
-20 0 20 40 60 80 100 120
values
ages = 2 0 : 2 7 ; students = [ 2 1 4 3 2 2 0 1 ] ;
SHA 30-6-17
3 COMMANDS TO GENERATE GRAPHICS 6
5
27
4 26
number of students
25
age of students
3
24
23
2
22
1 21
20
0
20 21 22 23 24 25 26 27 0 1 2 3 4 5
age of students number of students
(a) vertical (b) horizontal
Using the commands pie() and pie3() one can generate pie charts. With the correct options
set labels and some of the slices can be drawn slightly removed from the pie. The result of the code
below is shown in Figure 4.
strength = [55 52 36 28 13 1 6 ] ;
Labels = { ’SVP’ , ’ SP’ , ’FDP’ , ’CVP’ , ’GR’ , ’ Div ’ }
f i g u r e ( 1 ) ; pie ( strength )
f i g u r e ( 2 ) ; pie ( strength , [ 0 1 0 0 0 0 ] , Labels )
f i g u r e ( 3 ) ; pie3 ( strength , Labels )
8% Div
7% GR GR
28% SVP Div
CVP
FDP
18% FDP SP
26% SP
SHA 30-6-17
3 COMMANDS TO GENERATE GRAPHICS 7
12
6
10 5
number of events
8 4
height
3
6 2
4 1
0
2 1
0.5 1
0 0 0.5
-0.5 0
y -0.5
0 2 4 6 8 10 -1 x
-1
value
(a) 2D (b) 3D
theta = 0 : 0 . 2 : 6 ;
stem3 ( cos ( theta ) , s i n ( theta ) , theta ) ;
x l a b e l ( ’ x ’ ) ; y l a b e l ( ’ y ’ ) ; z l a b e l ( ’ height ’ )
20
65
180 0
60
210 330
55
240 300
270 50
5 6 7 8 9 10
value
SHA 30-6-17
4 DATA REDUCTION COMMANDS 8
dataRaw = randi ( [ 5 1 0 ] , 1 , 4 0 0 ) ;
f i g u r e ( 1 ) ; rose (dataRaw , 8 ) % generate rose plot
t i t l e ( ’ angular histogram with 8 sectors ’ )
[ data , cent ] = h i s t (dataRaw , unique (dataRaw ) ) % count the events
• For a sorted vector x ∈ RN the command median(x) determines the median value by
(
x(N +1)/2 if N is odd
median(x) = 1
2 (xN/2 + xN/2+1 ) if N is even
• For a vector x ∈ RN the command std(x) determines the standard deviation by the formula
1/2
n
p 1 X
std(x) = var(x) = (xj − x̄)2
N −1
j=1
By default cov() will normalize by (N − 1), but using an option you may divide by N , e.g.
std(x, 1).
SHA 30-6-17
4 DATA REDUCTION COMMANDS 9
• For a vector x ∈ RN the command var(x) determines the variance by the formula
n
1
2
X
var(x) = (std(x)) = (xj − x̄)2
N −1
j=1
By default var() will normalize by (N − 1), but using an option you may divide by N , e.g.
var(x,1).
• With the command quantile() you can compute arbitrary quantiles. Observe that there are
different methods to determine the quantiles, leading to different results! Consult the built-in
documentation in Octave by calling help quantile .
• Often quartile (division by four) or percentiles (division by 100) have to be determined. The
command quantile() with a proper set of parameters does the job. You may also use
prctile() to determine the values for the quartile (default) or other values for the divisions.
• With the command boxplot() you can generate a plot showing the median, the first and
third quartile as a box and the extreme values. Observe that there are different ways to
compute the positions of the quartiles and some implementations of boxplot() detect and
mark outliers. By using an optional argument you can select which points are considered as
outliers. Consult the documentation in Octave. Boxplots can also be displayed horizontally
or vertically, as shown in Figure 7.
20
15
10
0
0 5 10 15 20
Figure 7: Boxplots
f i g u r e (1)
Quartile1 = boxplot ( data1 ) ’ % in Matlab s l i g h t l y d i f f e r e n t
s e t ( gca , ’ XTickLabel ’ , { ’ ’}) % remove l a b e l s on x a x i s
c a x i s = a x i s ( ) ; a x i s ( [ 0 . 5 1.5 c a x i s ( 3 : 4 ) ] )
SHA 30-6-17
4 DATA REDUCTION COMMANDS 10
f i g u r e (2)
%boxplot ( data1 , ’ orientation ’ , ’ horizontal ’ ) % Matlab
boxplot ( data1 ,0 , ’+ ’ ,0 ) % Octave
s e t ( gca , ’ YTickLabel ’ , { ’ ’}) % remove l a b e l s on y a x i s
c axis = axis ( ) ; axis ( [ c axis (1:2) ,0.5 1 . 5 ] )
data2 = randi ( 1 0 , [ 1 0 0 , 1 ] ) ;
ModalValue = mode( data2 ) % determine the value occuring most often
It is possible to put multiple boxplots in one graph, and label the axis according to the data.
In Figure 8 the weekdays are used to label the horizontal axis.
boxplot ( data ) ;
s e t ( gca ( ) , ’ xtick ’ , [ 1 : 7 ] , ’ x t i c k l a b e l ’ , { ’Mo’ , ’Tu’ , ’We’ , ’Th’ , ’ Fr ’ , ’ Sa ’ , ’ Su ’ } ) ;
10
0
Mo Tu We Th Fr Sa Su
SHA 30-6-17
4 DATA REDUCTION COMMANDS 11
n
1 X
= (xj yj − mean(x) mean(y))
n−1
j=1
By default cov() will normalize by (n − 1), but using an option you may divide by n, e.g.
cov(x,y,1). If x = y we obtain
n
1 X
cov(x, x) = (xj − mean(x))2 = var(x)
n−1
j=1
cov(x, y)
corr(x, y) =
std(x) · std(y)
h(~x − mean(~x)) , (~y − mean(~y ))i
=
k~x − mean(~x)k k~y − mean(~y )k
Pn
j=1 (x − mean(x))j · (y − mean(y))j
=
( j=1 (xj − mean(x))2 )1/2 ( nj=1 (yj − mean(y))2 )1/2
Pn P
Observe that if the average value of the components of both vectors are zero, then there is a
geometric interpretation of the correlation coefficient as the angle between the two vectors.
h~x , ~y i
corr(~x, ~y ) = = cos(α)
k~xk k~y k
This correlation coefficient can also be computed with by corrcoef(), which is currently not
available in Octave.
4.3 For matrices: mean(), std(), var(), median(), cov(), corr(), corrcoef()
Most of the above commands can be applied to matrices. Use each column as one data vector.
Assume that M ∈ RN ×m is a matrix of m column vectors with N values in each column.
• mean(M) compute the average of each column. The result is a row vector with m components.
• std(M) compute the standard deviation of each column. The result is a row vector with m
components.
• var(M) compute the variance of each column. The result is a row vector with m components.
SHA 30-6-17
5 PERFORMING LINEAR REGRESSION 12
• median(M) compute the median value of each column. The result is a row vector with m
components.
To describe the effect of cov() and corr() the first step is to assure that the average of each
column equals zero.
Observe that this operation does not change the variance of the column vectors.
• The m × m correlation matrix contains all correlation coefficients of the m column vectors
in the matrix M. To compute this, first make sure that the norm of each column vector
equals 1, i.e. the variance of the column vectors is normalized to 1 .
Observe that the diagonal entries are 1, since the each column vector correlates perfectly with
itself.
• For Octave the command is contained in the optimization package optim. You may download
the code at LinearRegression.m.
• The command can be used with MATLAB too, but you need a Matlab version.
With this command you can apply the method of least square to fit a curve to a given set of
data points. The curve does not have to be a linear function, but a linear combination of (almost
arbitrary) functions. In the code below a straight line is adapted to some points on a curve
y = sin(x). Thus we try to find the optimal values for a and m such that
X
χ2 = (a + m xj − yj )2 is minimal
j
SHA 30-6-17
5 PERFORMING LINEAR REGRESSION 13
The above result implies that the best fitting straight line is given by
y = a + m x = 0.202243 + 0.477863 x
Assuming that the data is normally distributed one can show that the values of a and m normally
distributed. For this example the estimated standard deviation of a is given by 0.09 and the
standard deviation of m is 0.08. The standard deviation of the residuals rj = a + m xj − yj is
estimated by 0.15 . This is visually confirmed by Figure 9(a), generated by the following code.
y reg = F∗p ;
plot (x , y , ’+ ’ , x , y reg )
1.4 1
1.2 0.8
1
0.6
0.8
0.4
0.6
0.2
0.4
0.2 0
0 -0.2
0 0.5 1 1.5 2 0 0.5 1 1.5 2
With linear regression one may fit different curves to the given data. The code below generates
the best matching parabola and the resulting Figure 9(b).
SHA 30-6-17
5 PERFORMING LINEAR REGRESSION 14
y reg = F∗p ;
plot (x , y , ’+ ’ , x , y reg )
−−>
Parameters and StandardDeviation = −0.026717 0.015619
1.250604 0.036370
−0.386371 0.017506
estimated std = 0.019865
Since the parabola is a better match for the points on the curve y = sin(x) we find smaller estimates
for the standard deviations of the parameters and residuals.
It is possible perform linear regression with functions of multiple variables. The function
describes a plane in 3D space. A surface of this type is fit to a set of given points (xj , yj , zj ) by
the code below, resulting in Figure 10. The columns of the matrix F have to contain the values of
the basis functions 1, x and y at the given data points.
F = [ ones ( s i z e (x ) ) , x , y ] ;
p = LinearRegression (F, z )
figure (1);
plot3 (x , y , z , ’ ∗ ’ )
hold on
mesh( x grid , y grid , z g r i d )
xlabel ( ’x ’ ) ; ylabel ( ’y ’ ) ; zlabel ( ’ z ’ ) ;
hold o f f
−−>
p = 1.7689 2.0606 −1.4396
Since only very few (N=100) points were used the exact parameter values p~ = (+2, +2, −1.5) are
note very accurately reproduced. Increasing N will lead to more accurate results for this simulation,
or decrease the size of the random noise in +0.5*randn(N,1).
F = [ ones ( s i z e (x ) ) , x ] ;
[ p , p int , r , r i n t , s t a t s ] = r e g r e s s (y ,F) ;
parameters = p
parameter intervals = p i n t
estimated std = std ( r )
−−>
parameters = 0.20224
0.47786
parameter intervals = −0.0093515 0.4138380
SHA 30-6-17
5 PERFORMING LINEAR REGRESSION 15
z 0
-2
-4 2
1.5
3 2.5 1
2 1.5 0.5
1 0.5 x
y 0 0
0.2995040 0.6562220
estimated std = 0.14719
The values of the optimal parameters (obviously) have to coincide with the result generated by
LinearRegression(). Instead of the standard deviations for the parameters regress() returns the
confidence intervals for the parameters. The above numbers imply for the straight line y = a + m x
The value of the confidence level can be adjusted by calling regress() with a third argument.
[ p , s ] = p o l y f i t (x , y , 2 ) ;
p
−−>
p = −0.386371 1.250604 −0.026717
Observe that the coefficient of the polynomial are returned in deceasing order. Since regress()
and LinearRegression() are more flexible and provide more information your author’s advice is
to use those, even if polyfit() would work.
With Octave one may also use the command ols(), short for Ordinary Least Square. But as
above, there is no advantage over using LinearRegression().
p = o l s (y ,F)
−−>
p = −0.026717 1.250604 −0.386371
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 16
0.3
experimental probability
0.25
0.2
0.15
0.1
0.05
0
2 3 4 5 6 7 8 9
value
Figure 11: Histogram of random numbers, generated by a binomial distribution with n = 9, p = 0.8
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 17
i.e. solve CDF(x) = y for x . As examples examine the following short code segments, using the
normal distribution.
• To determine the values of the PDF for a normal distribution with mean 3 and standard
deviation 2 for x values between −1 and 7 use
x = l i n s p a c e ( −1 ,7); p = normpdf (x , 3 , 2 ) ;
plot (x , p)
• To determine for what values of x the value of the CDF equals 0.025 and 0.975 use
norminv ( [ 0 . 0 2 5 , 0 . 9 7 5 ] , 3 , 2 )
−−>
−0.91993 6.91993
The result implies that 95% of all values are between −0.91 ≈ −1 and +6.91 ≈ 7. This is
consistent with the approximative rule of thumb µ ± 2 σ.
For most distributions MATLAB and Octave provide multiple commands to work with the distri-
bution, see Table 7. The first part of the name of the command consists of an abbreviated name of
the distribution and the second part spells out the operation to be applied. As an example consider
the normal distribution and then use the command normpdf(), normcdf(), norminv(), normrnd()
and normstat() to work with the normal distribution.
In addition one may use the commands cdf() and pdf() to compute values of the probability
density function. As example consider the result of cdf(’normal’,0,0,1), leading to 0.500 .
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 18
0.4
0.4
0.2 0.2
0 0
0.6 0.6
0.4 0.4
0.2 0.2
0 0
-1 0 1 2 3 4 5 -2 0 2 4 6 8 10
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 19
x = −1:2;
p = d i s c r e t e p d f (x , [ 0 1 ] , [ 2 / 3 1 / 3 ] ) ;
cp = d i s c r e t e c d f (x , [ 0 1 ] , [ 2 / 3 1 / 3 ] ) ;
f i g u r e ( 2 ) ; stem (x , p , ’ b ’ ) ; hold on
s t a i r s (x , cp , ’ k ’ )
t i t l e ( ’ Bernoulli d i s t r i b u t i o n with p=1/3’)
a x i s ([−1 2 −0.1 1 . 1 ] )
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ )
hold o f f
The Bernoulli distribution can also be considered a special case of the binomial distribution, with
n=1.
Throwing a regular dice also leads to a discrete distribution, each of the possible results 1, 2,
3, 4, 5 and 6 will show with probability 1/6 . Find the result in Figure 12(b).
x = 0:7;
p = d i s c r e t e p d f (x , 1 : 6 , ones ( 6 , 1 ) / 6 ) ;
cp = d i s c r e t e c d f (x , 1 : 6 , ones ( 6 , 1 ) / 6 ) ;
f i g u r e ( 1 ) ; stem (x , p , ’ b ’ ) ; hold on
s t a i r s (x , cp , ’ k ’ )
t i t l e ( ’ d i s c r e t e d i s t r i b u t i o n , throwing dice ’ )
a x i s ( [ 0 7 −0.1 1 ] )
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ ) ; hold o f f
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 20
The code below generates the PDF and CDF for a binomial distribution with 4 events and the
individual probability p = 0.5. Find the results in Figure 12(c). The resulting distribution is
symmetric about the mean value 2 .
x = −1:5;
p = binopdf (x , 4 , 0 . 5 ) ;
cp = binocdf (x , 4 , 0 . 5 ) ;
f i g u r e ( 1 ) ; stem (x , p , ’ b ’ ) ; hold on
s t a i r s (x , cp , ’ k ’ )
t i t l e ( ’ binomial d i s t r i b u t i o n , n=4, p=0.5 ’)
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ ) ; hold o f f
Similarly you may examine the distribution function of 9 draws with an individual probability
of p = 0.8. In the result in Figure 12(d) it is clearly visible that the result is skewed towards higher
values, since they occur with a higher probability..
x = −1:10;
p = binopdf (x , 9 , 0 . 8 ) ;
cp = binocdf (x , 9 , 0 . 8 ) ;
f i g u r e ( 1 ) ; stem (x , p , ’ b ’ ) ; hold on
s t a i r s (x , cp , ’ k ’ )
t i t l e ( ’ binomial d i s t r i b u t i o n , n=9, p=0.8 ’)
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ ) ; hold o f f
The command binostat determines mean and standard deviation of a binomial distribution.
λi −λ
P (X = i) = p(i) = e
i!
Find the graph of the Poisson distribution with λ = 2.5 in Figure 13, generated by
x = −1:10; lambda = 2 . 5 ;
p = poisspdf (x , lambda ) ;
cp = p o i s s c d f (x , lambda ) ;
f i g u r e ( 1 ) ; stem (x , p , ’ b ’ ) ; hold on
s t a i r s (x , cp , ’ k ’ )
t i t l e ( ’ Poisson d i s t r i b u t i o n , \lambda=2.5 ’)
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ ) ; hold o f f
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 21
0.6
0.4
0.2
0
-2 0 2 4 6 8 10
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 22
Find the result for a uniform distribution on the interval [0 , 1] in Figure 14(a), generated by
x = l i n s p a c e ( −0.5 ,1.5);
p = unifpdf (x , 0 , 1 ) ;
cp = u n i f c d f (x , 0 , 1 ) ;
f i g u r e (1)
plot (x , p , ’ b ’ , x , cp , ’ k ’ )
t i t l e ( ’ uniform d i s t r i b u t i o n ’ )
a x i s ([ −0.5 1.5 −0.1 1 . 2 ] )
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ )
x = l i n s p a c e ( −0.5 ,3.0);
p = normpdf (x , 1 , 0 . 5 ) ;
cp = normcdf (x , 1 , 0 . 5 ) ;
f i g u r e (1)
plot (x , p , ’ b ’ , x , cp , ’ k ’ )
t i t l e ( ’ normal d i s t r i b u t i o n , \mu= 1 , \sigma =0.5 ’)
a x i s ([ −0.5 3.0 −0.1 1 . ] )
legend ( ’ pdf ’ , ’ cdf ’ , ’ location ’ , ’ northwest ’ )
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 23
0.8
0.6
0.6
0.4
0.4
0.2 0.2
0 0
-0.5 0 0.5 1 1.5 -0.5 0 0.5 1 1.5 2 2.5 3
0.8
0.6
0.6
0.4
0.4
0.2
0.2
0 0
-3 -2 -1 0 1 2 3 0 1 2 3 4
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 24
where ν ∈ N is the number of degrees of freedom. The corresponding cumulative density function
is given by the general formula Z x
cdf(x) = pdf(s) ds
−∞
and there is no elementary expression for it, for most values of ν.
• The probability density function resembles a normal distribution with mean 0 and standard
deviation 1, but it is wider and lower.
• For some small values of ν there are explicit formulas, shown in Table 8.
x = l i n s p a c e ( −3 ,3);
p1 = tpdf (x , 1 ) ; p2 = tpdf (x , 2 ) ; p10 = tpdf (x , 1 0 ) ; pn = normpdf (x , 0 , 1 ) ;
f i g u r e (1)
plot (x , p1 , x , p2 , x , p10 , x , pn)
t i t l e ( ’ Student−t d i s t r i b u t i o n ’ ) ; a x i s ([−3 3 −0.1 0 . 5 ] )
legend ( ’\nu=1 ’ , ’\nu=2 ’ , ’\nu=10 ’ , ’ normal ’ , ’ location ’ , ’ northwest ’ )
Student-t distribution
0.5
ν=1
ν=2
0.4
ν=10
normal
0.3
0.2
0.1
-0.1
-3 -2 -1 0 1 2 3
ν pdf(x) cdf(x)
1 1 1
1 π (1+x2 ) 2 + π arctan(x)
1 1 √x
2 (2+x√2 )3/2 2 + 2 2+x2
6 3
3 π (3+x2 )2
2
∞ √1 exp(− x2 ) 1
(1 + erf( √x2 ))
2π 2
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 25
7.2.4 χ2 distribution
The χ2 –distribution with parameter n (degrees of freedom) is defined for x > 0 and given by
1 n x
pdf(x) = x 2 −1 exp(− )
2n/2 Γ( n2 ) 2
Z x
1 n x
cdf(x) = pdf(s) ds = n γ( , )
0 Γ( 2 ) 2 2
The mode (maximal value) is attained at max{n−2 , 0}. Find the result for a a few χ2 distributions
in Figure 16, generated by
x = linspace (0 ,4);
pdf1 = chi2pdf (x , 1 ) ; pdf2 = chi2pdf (x , 2 ) ; pdf3 = chi2pdf (x , 3 ) ; pdf5 = chi2pdf (x , 5 ) ;
cdf1 = chi2cdf (x , 1 ) ; cdf2 = chi2cdf (x , 2 ) ; cdf3 = chi2cdf (x , 3 ) ; cdf5 = chi2cdf (x , 5 ) ;
f i g u r e (1)
plot (x , pdf1 , x , pdf2 , x , pdf3 , x , pdf5 )
y l a b e l ( ’ pdf (x ) ’ ) ; t i t l e ( ’\ chi ˆ2 pdf ’ ) ;
legend ( ’ n=1 ’ , ’n=2 ’ , ’n=3 ’ , ’n=5 ’); a x i s ( [ 0 , 4 , 0 , 1 ] )
f i g u r e (2)
plot (x , cdf1 , x , cdf2 , x , cdf3 , x , cdf5 )
y l a b e l ( ’ cdf (x ) ’ ) ; t i t l e ( ’\ chi ˆ2 cdf ’ )
legend ( ’ n=1 ’ , ’n=2 ’ , ’n=3 ’ , ’n=5 ’ , ’ location ’ , ’ northwest ’ ) ; a x i s ( [ 0 , 4 , 0 , 1 . 1 ] )
2 2
χ pdf χ cdf
1
n=1 n=1
1
n=2 n=2
0.8 n=3 n=3
n=5 0.8 n=5
0.6
pdf(x)
cdf(x)
0.6
0.4
0.4
0.2 0.2
0 0
0 1 2 3 4 0 1 2 3 4
SHA 30-6-17
7 COMMANDS TO WORK WITH PROBABILITY DISTRIBUTIONS 26
and are computed by exppdf(x,lambda) and expcdf(x,lambda). Find the graphs for λ = 1, 0.5
and 0.2 in Figure 17.
cdf(x)
0.6
1
0.4
0.5
0.2
0 0
0 1 2 3 4 0 1 2 3 4
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 27
For the following examples we repeatedly use a data set to illustrate the commands. The
numbers may represent the number of defects detected on a sample of 17 silicon wafers selected at
random from a large production.
WaferDefects.txt
7 16 19 12 15 9 6 16 14 7 2 15 23 15 12 18 9
8.1.1 Estimating the mean value µ, with (supposedly) known standard deviation σ
Assume to have data with an unknown mean value µ, but a known standard deviation σ. This is
a rather unusual situation, in most cases the standard deviation is not known and one has to use
the similar computations in the next section 8.1.2. For sake of simplicity start with a known value
of σ.
A sampling of n data points leads to xi and you want to determine the mean value µ. According
to the central limit theorem the random variable
n
1 X
X̄ = Xi
n
i=1
√
is approximated by a normal distribution with mean µ and standard deviation σ/ n. Now we seek
a value u such that the green area in Figure 19(a) equals (1 − α), where α is a small positive value.
This implies that the true (unknown) value of µ is with a high probability in the green section in
Figure 19(a). This is a two–sided confidence interval. If we want to know whether the true value
of µ is below (or above) a certain threshold we use a similar argument to determine the one–sided
confidence interval in Figure 19(b) .
• To determine a two–sided confidence interval for the significance level α examine Figure 19(a)
and proceed as follows:
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 28
uσ uσ
P (x̄ − √ < x < x̄ + √ ) = 1 − α
n n
You may also use the command ztest() from Section 8.2.2 to compute the confidence interval.
The above code can be reused with a smaller significance level alpha = 0.01, leading to a
wider confidence interval of [9.2760 , 16.0182] .
• To determine the one–sided confidence interval for the significance level α examine Fig-
ure 19(b) and proceed as follows:
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 29
0.5 0.5
0.4 0.4
0.3 0.3
pdf
pdf
0.2 P 0.2 P
0.1 0.1
α
α/2 α/2
0 0
-u +u +u
-0.1 -0.1
-4 -2 0 2 4 -4 -2 0 2 4
x x
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 30
Observe that this confidence interval is slightly wider that the one with (supposedly) known
standard deviation σ. This is reasonable, since we have less information at our disposition.
• To determine the one–sided confidence interval for the significance level α examine Fig-
ure 19(b) and proceed as follows:
1. Determine u such that
Z u
(1 − α) = P (U < u) = pdf(x) dx = cdf(u)
−∞
With MATLAB/Octave this value may be determined by u = tinv(1-alpha,n-1).
2. With the estimators
n n
1 X 1 X
x̄ = xi and σ 2 = (xi − x̄)2
n n−1
i=1 i=1
uσ
the one–sided confidence interval is given by [−∞ , x̄ +√ n
], i.e.
uσ
P (x < x̄ + √ ) = 1 − α
n
• For the above example we may use the following code.
Observe that for large samples (N 1) the Student-t distribution is close to the standard
normal distribution and the results for the confidence intervals for known or unknown standard
deviation σ differ very little.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 31
This is a random variable whose distribution is related to the χ2 distribution. The modified variable
n
(n − 1) S 2 1 X
Y = = (Xi − X̄)2
σ2 σ2
i=1
(n − 1) S 2
2 2
(1 − α) = P χα/2,n−1 < < χ1−α/2,n−1
σ2
χ2α/2,n−1 χ21−α/2,n−1
!
1
= P < 2 <
(n − 1) S 2 σ (n − 1) S 2
!
(n − 1) S 2 (n − 1) S 2
= P < σ2 < 2
χ21−α/2,n−1 χα/2,n−1
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 32
Observe that the confidence interval for the standard deviation is not symmetric about the esti-
mated value of 5.3961 .
as an unbiased estimator for the parameter p. To construct a confidence interval for p we seek a
lower limit pl and an upper limit pu such that
Thus we have to solve the equations 1 − cdf(k̄, pl ) = α/2 and cdf(k̄, pu ) = α/2 for the unknowns
pl and pu . This can be done using the command fzero(). Use help fzero to obtain information
about this command, used to solve a single equation. For the command fzero() we have to provide
the interval in which p is to be found, e.g. 0 ≤ p ≤ 1 .
Assuming that out of 1000 samples only 320 objects satisfy the desired property. The estimator
320
for p is p̄ = 1000 = 0.32. Then use the code below to determine the two–sided confidence interval
[pl , pu ] at significance level α = 0.05.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 33
In the above example a two–sided interval is constructed. There are applications when a one–
sided interval is required. Examine a test of 100 samples, with only 2 samples failing. Now
determine an upper limit for the fail rate, using a confidence level of α = 0.01. The parameter p
(the fail rate) is too large if the probability to detect 2 or less fails is smaller than α. Thus the
upper limit pu satisfies the equation
2
!
X 100
P (Sn ≤ 2) = piu (1 − pu )100−i = cdf(2, pu ) = α
i=0 i
The code below shows that the fail rate is smaller than ≈ 8%, with a probability of 1 − α = 99% .
Rerunning the above code with α = 5% leads to a fail rate smaller than ≈ 6%, with a probability of
95% . The maximal fail rate is smaller now, since we accept a lower probability. An approximation
by a normal distribution is not justified in this example, since p is rather close to 0 . The (wrong)
result for the fail rate would be ≈ 5.3% for α = 1% .
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 34
By flipping a coin 20 times you want to decide whether the coin is fair. Choose a level of sig-
nificance α and determine the resulting domain of acceptance A. In this example the domain
of acceptance is an interval containing 10 . If the actual number of heads is in A you accept the
hypothesis p = 21 , otherwise you reject it. The probability of rejecting H0 , even if is is true, should
be α, i.e. α is the probability of committing a type 1 error. You might also commit a type 2 error,
i.e. accept H0 even if it is not true. The probability of committing a type 2 error is β and 1 − β is
called the power of the test. The relations are shown in Table 10.
H0 is true H1 is true
H0 accepted 1−α β
correct type 2 error
H0 rejected α 1−β
type 1 error correct
Obviously the choice of the level of significance has an influence on the result.
– The domain of acceptance A will be large, and we are more likely to accept the hypoth-
esis, even if it is wrong. Thus we might make a type 2 error.
– The probability to reject a true hypothesis is small, given by α. Thus we are not very
likely to make a type 1 error.
– The domain of acceptance A will be small, and we are more likely to reject the hypothesis,
even if it is true. Thus we might make a type 1 error.
– The probability to accept a false hypothesis is small. Thus we are not very likely to
make a type 2 error.
• The smaller the value α of the level of significance, the more likely the hypothesis H0 is
accepted. Thus there is a smallest value of α, leading to rejection of H0 .
The smallest value of α for which the null hypothesis H0 is rejected, based on the given
sampling, is called the P value.
8.2.2 Testing for the mean value µ, with (supposedly) known standard deviation σ
Assume to have normally distributed data with an unknown mean value µ, but known standard
deviation σ, just as in Section 8.1.1. The null hypothesis to be tested is that the mean value equals
a given value µ0 . For a given level of significance α the domain of acceptance A is characterized
by
1 − α = P (X̄ ∈ A) = P (µ0 − a ≤ X̄ ≤ µ0 + a)
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 35
Pn
where X̄ = n1 i=1 Xi is an unbiased estimator of the true mean value µ. Using the central limit
X̄−µ
theorem we know that the random variable U = σ/ √ follows a standard normal distribution.
n
Using this we compute a by a = -norminv(alpha/2), i.e.
Z −a
α
= pdf(x) dx
2 −∞
As example we want to test the hypothesis that the average number of defects in the wafers for
the data set introduced on page 27 is given by 14.
a = −norminv ( alpha / 2 ) ;
i f abs (mu−mu0)<a∗sigma/ s qr t (n)
disp ( ’ H 0 not rejected , might be true ’ )
else
disp ( ’ H 0 rejected , probably f a l s e ’ )
end%i f
P value = 2∗normcdf(−abs (mu−mu0)∗ s qrt (n)/ sigma )
−−>
mu = 12.647
H 0 not rejected , might be true
P value = 0.30124
Observe that the average µ of the sample is well below the tested value of µ0 = 14. Since the size
of the sample is rather small, we do not have enough evidence to reject the hypothesis. This does
not imply that the hypothesis is true. The computed P value is larger than the chosen level of
significance α = 0.05, which also indicated that the hypothesis can not be rejected.
With the command ztest() you can test this hypothesis. It will in addition determine the
confidence interval using the results from Section 8.1.1.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 36
• Since the P value of 0.3 is larger than α = 0.05 the null hypothesis not rejected.
• The confidence interval [10.082 , 15.212] is determined by the method in Section 8.1.1.
The default value for the level of significance is α = 0.05 = 5%. By providing more arguments you
can used different values, e.g. [H, PVAL, CI] = ztest(data,mu0,sigma,’alpha’,0.01) .
8.2.3 Testing for the mean value µ, with unknown standard deviation σ
Assume to have normally distributed data with an unknown mean value µ and standard deviation σ,
just as in Section 8.1.2. The null hypothesis to be tested is that the mean value equals a given
value µ0 . For a given level of significance α the domain of acceptance A is characterized by
1 − α = P (X̄ ∈ A) = P (µ0 − a ≤ X̄ ≤ µ0 + a)
1 Pn 1 Pn
where X̄ = n i=1 Xi is an unbiased estimator of the true mean value µ an S 2 = n−1 i=1 (Xi −
X̄−µ
X̄)2 is an estimator of the variance σ 2 . Now use that the random variable Z = √
S/ n
fol-
lows a Student-t distribution with n − 1 degrees of freedom. Using this we compute a by a =
-tinv(alpha/2,n-1), i.e. Z −a
α
= pdf(x) dx
2 −∞
and then the domain of acceptance is given by
σ σ
A = [µ0 − a √ , µ0 + a √ ]
n n
As example we want to test the hypothesis that the average number of defects in the wafers for
the data set introduced on page 27 is given by 14.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 37
Observe that the average µ of the sample is well below the tested value of µ0 = 14. Since the size
of the sample is rather small, we do not have enough evidence to reject the hypothesis. This does
not imply that the hypothesis is true. The computed P value is larger then the chosen level of
significance α = 0.05, which also indicated that the hypothesis can not be rejected.
With the command ttest() you can test this hypothesis. It will also compute the confidence
interval4 by the methods from Section 8.1.2.
• Since the P value of 0.32 is larger than α = 0.05 the null hypothesis is not rejected.
• The two–sided confidence interval [9.8727 , 15.4215] is determined by the method in Sec-
tion 8.1.2.
The default value for the level of significance is α = 0.05 = 5%. By providing more arguments you
can used different values, e.g. [H, PVAL, CI] = ttest(data,mu0,’alpha’,0.01) .
8.2.4 One–sided testing for the mean value µ, with unknown standard deviation σ
One can also apply one sided tests. Assume that we claim the the actual mean value µ is below a
given value µ0 . For a given level of significance α the domain of acceptance A is characterized by
1 − α = P (X̄ ∈ A) = P (X̄ ≤ µ0 + a)
4
The version of ttest() from the package statistics 1.2.4 in Octave does not produce the correct confidence
interval. The bug is reported and fixed in version 1.3.0 .
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 38
As example we want to test the hypothesis that the average number of defects in the wafers for
the data set introduced on page 27 is smaller than 14.
a = tinv(1−alpha , n−1);
i f mu < mu0+a∗sigma/ s qr t (n) disp ( ’ H 0 not rejected , might be true ’ )
else disp ( ’ H 0 rejected , probably f a l s e ’ )
end%i f
P value = 1 − t c d f ( (mu−mu0)∗ sq rt (n)/ sigma , n−1)
−−>
mu = 12.647
H 0 not rejected , might be true
P value = 0.84169
Observe that the average µ of the sample is well below the tested value of µ0 = 14. Thus the
hypothesis is very likely to be correct, which is confirmed by the above result.
With the command ttest() you can test this hypothesis. It will also compute the one–sided
confidence interval.
• Since the P value of 0.84 is larger than α = 0.05 the null hypothesis is not rejected.
• The two–sided confidence interval [10.362 , +∞] is determined by the method in Section 8.1.2.
The default value for the level of significance is α = 0.05 = 5%. By providing more arguments you
can used different values, e.g. [H, PVAL, CI] = ttest(data,mu0,’tail’,’right’,’alpha’,0.01) .
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 39
Assume to have data given by a binomial distribution with parameter p, i.e. we have N data
points and each point has value 1 with probability p and 0 otherwise. The null hypothesis to be
tested is that the parameter p equals a given value p0 . Let k = N k 1 PN
P
i=1 x i and x̄ = N = N i=1 xi be
the result of a sample, then x̄ is an estimator of p. For a given level of significance α the domain
Null hypothesis H0 p = p0
Alternative hypothesis H1 p 6= p0
of acceptance A is characterized by
where cdf() is the cumulative density function for the binomial distribution with parameter p0 .
This condition translates to
α α
cdf(Alow ) ≤ and 1 − cdf(Ahigh ) ≤
2 2
Since the binomial distribution is a discrete distribution we can not insist on the limiting equality,
but have to work with inequalities, leading to
α α
binocdf(N · Alow , N, p0 ) ≤ and 1 − binocdf(N · Ahigh , N, p0 ) ≤
2 2
Using MATLAB/Octave commands this can be solved by
α α
Alow = binoinv( , N, p0 )/N and Ahigh = binoinv(1 − , N, p0 )/N
2 2
The null hypothesis H0 : p = p0 is accepted if
k
Alow ≤ ≤ Ahigh
N
Since the P value is also given by the total probability of all events less likely than the observed k,
we have an algorithm to determine P .
1. Compute the probability that the observed number k of “heads” shows by pk = binopdf(k, N, p0 ).
2. Of all pj = binopdf(j, N, p0 ) add those that are smaller or equal to pk .
3. This can be packed into a few lines of code.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 40
p k = binopdf (k , n , p0 ) ;
p a l l = binopdf ( [ 0 : n ] , n , p0 ) ;
p = sum( p a l l ( f i n d ( p a l l <= p k ) ) ) ;
As an example consider flipping a coin 1000 times and observe 475 “heads”. To test whether
this coin is fair make the hypothesis p = 0.5 and test with a significance level of α = 0.05.
The above can be compared with the confidence interval determined in Section 8.1.4, see page 32.
Since the value of p = 12 is inside the interval of confidence we conclude that the coin might be fair.
Observe that the confidence interval is built around the estimated expected value p = 0.475, while
the domain of acceptance is built around the tested value p0 = 12 .
The above result can be generated by the Octave command binotest()5 .
With MATLAB the command binofit() determines the estimator for the parameter p and the
confidence interval. Observe that the hypothesis is not tested and the P value not computed. Thus
the command binofit() uses results from Section 8.1.4 on page 32.
5
The code in binotest.m is available in the statistics package with version 1.3.0 or newer. There is now also a
version of the code for MATLAB. Contact this author.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 41
Null hypothesis H0 p ≤ p0
Alternative hypothesis H1 p > p0
The null hypothesis H0 : p ≤ p0 is accepted if Nk ≤ Ahigh . Since the P value is defined as the
smallest value of the level of significance α for which the null hypothesis is rejected we use
P = αmin = 1 − binocdf(k − 1, N, p0 )
For the above coin flipping example we claim that the coin is less likely to show “heads” than
“tail”.
The result states that the coin might be more likely to show “heads”. The observed value of
p ≈ 0.475 is well within the domain of acceptance A = [0 , 0.526].
The above result can be generated by the Octave command binotest.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 42
Null hypothesis H0 p ≥ p0
Alternative hypothesis H1 p < p0
Obviously we can also test whether the coin is less likely to show “heads”. Since the arguments
are very similar to the above we just show the resulting code.
The result states that the coin might be less likely to show “heads”. But the observed value of
p ≈ 0.475 is barely within the domain of acceptance A = [0.474 , 1]. The P value of P ≈ 0.06 is
just above α = 5%. If we increase α slightly, i.e. be more tolerant towards errors of the first type,
the hypothesis would be rejected.
The above result can be generated by the Octave command binotest.
Observe that in the previous examples for 475 “heads” on 1000 coin flips none of the three null
hypothesis p = 21 , p ≤ 12 or p ≥ 21 is rejected. This is clearly illustrating that we do not prove that
one of the hypothesis is correct. All we know is that they are not very likely to be false.
8.2.8 Testing for the parameter p for a binomial distribution for large N
If N andPN p0 (1 − p0 ) are large (e.g. N > 30 and N p0 (1 − p0 ) > 10) the binomial distribution of
N
Y = N1 i=1 Xi with parameter p0 can be approximated by a normal distribution with mean p0
q
and standard deviation σ = p0 (1−p N
0)
. Thus we can replace the binomial distribution in the above
section by this normal distribution and recompute the domains of acceptance and the P values.
The formulas to be used are identical to the ones in Section 8.2.2. For the confidence intervals use
the tools from Section 8.1.1.
SHA 30-6-17
8 COMMANDS FOR CONFIDENCE INTERVALS AND HYPOTHESIS TESTING 43
u = −norminv ( alpha / 2 ) ;
DomainOfAcceptance = [ p0−u∗sigma , p0+u∗sigma ]
i f ( abs (Heads/N−p0)<u∗sigma ) disp ( ’ H 0 not rejected , might be true ’ )
else disp ( ’ H 0 rejected , probably f a l s e ’ )
end%i f
P value = 2∗normcdf(−abs (Heads/N−p0)/ sigma )
−−>
DomainOfAcceptance = [ 0 . 4 6 9 0 . 5 3 1 ]
H 0 not rejected , might be true
P value = 0.11385
u = −norminv ( alpha ) ;
DomainOfAcceptance = [ 0 p0+u∗sigma ]
i f (Heads/N<p0+u∗sigma ) disp ( ’ H 0 not rejected , might be true ’ )
else disp ( ’ H 0 rejected , probably f a l s e ’ )
end%i f
P value = 1−normcdf ( ( Heads/N−p0)/ sigma )
−−>
DomainOfAcceptance = [ 0 0.52601]
H 0 not rejected , might be true
P value = 0.94308
u = −norminv ( alpha ) ;
DomainOfAcceptance = [ p0−u∗sigma 1 ]
i f (Heads/N>p0−u∗sigma ) disp ( ’ H 0 not rejected , might be true ’ )
else disp ( ’ H 0 rejected , probably f a l s e ’ )
end%i f
P value = normcdf ( ( Heads/N−p0)/ sigma )
−−>
DomainOfAcceptance = [0.47399 1 ]
H 0 not rejected , might be true
P value = 0.056923
• All of the above results are very close to the numbers obtained by the binomial distribution
in Sections 8.2.6 and 8.2.7. This is no surprise, since N = 1000 is large enough and N p0 (1 −
p0 ) = 250 10 and thus the normal distribution is a good approximation of the binomial
distribution.
SHA 30-6-17
Index
alternative hypothesis, 33 exppdf, 21, 26
exprnd, 16
bar, 4, 5
bar diagram, 5 fclose, 3
barh, 4, 5 fgetl, 3
binocdf, 19, 32, 33, 39–41 fopen, 3
binofit, 40 fread, 3
binoinv, 39, 41 fzero, 32, 33, 40
binopdf, 19
binornd, 16 generating random numbers, 16
binostat, 20 gls, 8
binotest, 33, 40–42
hist, 4
boxplot, 4, 9, 10
histc, 4, 5
CDF, 16 histfit, 4, 5
cdf, 17 histogram, 4
chi2cdf, 25 hygepdf, 19
chi2inv, 31 hypothesis testing, 27, 33
chi2pdf, 25
least square, 12
coin flipping, 39
level of confidence, 27, 33
confidence interval, 15, 27–33, 35, 37, 38, 40
level of significance, 27–30, 32, 34–39, 41
confidence interval, one–sided, 28
linear regression, 12, 14
confidence interval, two–sided, 27
LinearRegression, 8, 12
corr, 8, 11, 12
load, 3
corrcoef, 8, 11
loading data, 3
correlation coefficient, 11
correlation matrox, 12 mean, 8, 11
cov, 8, 10–12 mean value, 11
covariance, 10, 12 median, 8, 11, 12
cumulative density function, 16 mode, 8
discrete distribution, 17 normcdf, 22, 35
discrete cdf, 19 norminv, 17, 28, 32, 35
discrete pdf, 19 normpdf, 21, 22
discrete rnd, 16 normrnd, 16
distribution, χ2 , 25 null hypothesis, 33
distribution, Bernoulli, 19
distribution, binomial, 19 ols, 8, 15
distribution, continuous, 21 one sided test, 37
distribution, discrete, 17 outlier, 9
distribution, exponential, 25
distribution, normal, 22 P value, 33–39, 41, 42
distribution, Poisson, 20 package, statistics, 3
distribution, Student-t, 22 PDF, 16
distribution, uniform, 22 pdf, 17
dlmread, 3 percentile, 9
dlmwrite, 3 pie, 4, 6
domain of acceptance, 34, 36, 37, 39–42 pie chart, 5
pie3, 4, 6
expcdf, 26 poisscdf, 20
44
INDEX 45
poisspdf, 19, 20
polyfit, 15
power of test, 34
prctile, 9
probability density function, 16
quantile, 8, 9
quartile, 9
rand, 16
rande, 16
randg, 16
randn, 16
random numbers, 16
randp, 16
regress, 8, 14
regression, 12, 14
rose, 4, 7
sprintf, 3
sscanf, 3
stairs, 4, 7
stairstep plot, 7
standard deviation, 8, 11
std, 8, 11
stem, 4, 6
stem plot, 6
stem3, 4, 7
strread, 3
unifcdf, 22
unifpdf, 21, 22
unique, 5, 6
var, 8, 9, 11
variance, 9, 11
SHA 30-6-17