0% found this document useful (0 votes)
57 views137 pages

Scilab Text Probability - and - Stat For Engineers Ross

Uploaded by

Thomas Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views137 pages

Scilab Text Probability - and - Stat For Engineers Ross

Uploaded by

Thomas Wong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 137

Scilab Textbook Companion for

Probability And Statistics For Engineers And


Scientists
by S. M. Ross1

Created by
Deeksha Sinha
Dual Degree
Electrical Engineering
IIT Bombay
College Teacher
None
Cross-Checked by
Mukul R. Kulkarni

August 10, 2013

1 Funded by a grant from the National Mission on Education through ICT,


http://spoken-tutorial.org/NMEICT-Intro. This Textbook Companion and Scilab
codes written in it can be downloaded from the ”Textbook Companion Project”
section at the website http://scilab.in
Book Description

Title: Probability And Statistics For Engineers And Scientists

Author: S. M. Ross

Publisher: Elsevier, New Delhi

Edition: 3

Year: 2005

ISBN: 81-8147-730-8

1
Scilab numbering policy used in this document and the relation to the
above book.

Exa Example (Solved example)

Eqn Equation (Particular equation of the above book)

AP Appendix to Example(Scilab Code that is an Appednix to a particular


Example of the above book)

For example, Exa 3.51 means solved example 3.51 of this book. Sec 2.3 means
a scilab code whose theory is explained in Section 2.3 of the book.

2
Contents

List of Scilab Codes 4

2 Descriptive Statistics 11

3 Elements Of Probability 19

4 Random Variables And Expectation 26

5 Special Random Variables 37

6 Distribution of Sampling Statistics 47

7 Parameter Estimation 51

8 Hypothesis Testing 64

9 Regression 75

10 Analysis of Variance 99

11 Goodness of Fit Tests and Categorical Data Analysis 105

12 Non parametric Hypothesis Tests 115

13 Quality Control 122

14 Life Testing 133

3
List of Scilab Codes

Exa 2.2a Relative Frequency . . . . . . . . . . . . . . . . . . . . 11


Exa 2.2b pie chart . . . . . . . . . . . . . . . . . . . . . . . . . 11
Exa 2.3a Sample mean . . . . . . . . . . . . . . . . . . . . . . . 13
Exa 2.3b Sample mean of age . . . . . . . . . . . . . . . . . . . 13
Exa 2.3c Sample Median . . . . . . . . . . . . . . . . . . . . . . 13
Exa 2.3d Mean and Median . . . . . . . . . . . . . . . . . . . . 14
Exa 2.3e Mean Median and Mode . . . . . . . . . . . . . . . . . 14
Exa 2.3f sample variance . . . . . . . . . . . . . . . . . . . . . 15
Exa 2.3g sample variance of accidents . . . . . . . . . . . . . . . 15
Exa 2.3h Percentile . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exa 2.3i Quartiles . . . . . . . . . . . . . . . . . . . . . . . . . 16
Exa 2.4a Chebyshev Inequality . . . . . . . . . . . . . . . . . . 16
Exa 2.5a Empirical Rule . . . . . . . . . . . . . . . . . . . . . . 17
Exa 2.6a Sample Correlation Coefficient . . . . . . . . . . . . . 17
Exa 2.6b Sample Correlation Coefficient . . . . . . . . . . . . . 18
Exa 3.4a Union . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
Exa 3.5a Basic Principle of Counting . . . . . . . . . . . . . . . 19
Exa 3.5b Basic Principle of Counting . . . . . . . . . . . . . . . 20
Exa 3.5c Basic Principle of Counting . . . . . . . . . . . . . . . 20
Exa 3.5d Committee Probability . . . . . . . . . . . . . . . . . 20
Exa 3.5f Pairing Probability . . . . . . . . . . . . . . . . . . . . 21
Exa 3.6a Acceptable Transistor . . . . . . . . . . . . . . . . . . 22
Exa 3.6b Both Boys . . . . . . . . . . . . . . . . . . . . . . . . 22
Exa 3.6c Branch Manager . . . . . . . . . . . . . . . . . . . . . 23
Exa 3.7a Accident Probability . . . . . . . . . . . . . . . . . . . 23
Exa 3.7b Accident within a year . . . . . . . . . . . . . . . . . . 23
Exa 3.7c Multiple Choice Test . . . . . . . . . . . . . . . . . . . 23
Exa 3.7d blood test . . . . . . . . . . . . . . . . . . . . . . . . . 24

4
Exa 3.7e Criminal Investigation . . . . . . . . . . . . . . . . . . 24
Exa 3.7f Missing Plane . . . . . . . . . . . . . . . . . . . . . . . 24
Exa 3.8a Independent Events . . . . . . . . . . . . . . . . . . . 25
Exa 4.1a sum of two fair dice . . . . . . . . . . . . . . . . . . . 26
Exa 4.1b Defective or Acceptable . . . . . . . . . . . . . . . . . 27
Exa 4.1c X exceeds 1 . . . . . . . . . . . . . . . . . . . . . . . . 28
Exa 4.2a sum of pmf . . . . . . . . . . . . . . . . . . . . . . . . 28
Exa 4.2b pdf . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Exa 4.3a Joint distribution of batteries . . . . . . . . . . . . . . 29
Exa 4.3b Joint distribution of boys and girls . . . . . . . . . . . 30
Exa 4.3c Joint Density Function . . . . . . . . . . . . . . . . . . 31
Exa 4.3e Density of Independent Random Variables . . . . . . . 31
Exa 4.3f Conditional Probability Mass Function . . . . . . . . . 31
Exa 4.3g Conditional Probability Mass Function . . . . . . . . . 32
Exa 4.4a Expectation of a fair die . . . . . . . . . . . . . . . . . 32
Exa 4.4d Expectation of the message time . . . . . . . . . . . . 32
Exa 4.5a Expectation . . . . . . . . . . . . . . . . . . . . . . . . 33
Exa 4.5b Expected cost of breakdown . . . . . . . . . . . . . . . 33
Exa 4.5c Expectation . . . . . . . . . . . . . . . . . . . . . . . . 33
Exa 4.5d Expectation . . . . . . . . . . . . . . . . . . . . . . . . 33
Exa 4.5e Expected profit . . . . . . . . . . . . . . . . . . . . . . 34
Exa 4.5f Letters in Correct Envelopes . . . . . . . . . . . . . . 34
Exa 4.5g Different types of coupons . . . . . . . . . . . . . . . . 34
Exa 4.6a Variance of a fair die . . . . . . . . . . . . . . . . . . . 35
Exa 4.7a Variance of 10 rolls of a fair die . . . . . . . . . . . . . 35
Exa 4.7b Variance of 10 tosses of a coin . . . . . . . . . . . . . . 36
Exa 4.9a Inequalities . . . . . . . . . . . . . . . . . . . . . . . . 36
Exa 5.1a Returning of disks . . . . . . . . . . . . . . . . . . . . 37
Exa 5.1b Colour of Eyes . . . . . . . . . . . . . . . . . . . . . . 37
Exa 5.1e Binomial Random Variable . . . . . . . . . . . . . . . 38
Exa 5.2a Probability of accident . . . . . . . . . . . . . . . . . . 38
Exa 5.2b Defective Items . . . . . . . . . . . . . . . . . . . . . . 39
Exa 5.2c Number of Alpha particles . . . . . . . . . . . . . . . 39
Exa 5.2d Claims handled by an insurance company . . . . . . . 39
Exa 5.2f Defective stereos . . . . . . . . . . . . . . . . . . . . . 40
Exa 5.3a Functional system . . . . . . . . . . . . . . . . . . . . 40
Exa 5.3b Determining Population Size . . . . . . . . . . . . . . 41
Exa 5.3c Conditional Probability . . . . . . . . . . . . . . . . . 41

5
Exa 5.4b Bus Timings . . . . . . . . . . . . . . . . . . . . . . . 42
Exa 5.4c Current in a diode . . . . . . . . . . . . . . . . . . . . 42
Exa 5.5a Normal Random Variable . . . . . . . . . . . . . . . . 42
Exa 5.5b Noise in Binary Message . . . . . . . . . . . . . . . . . 43
Exa 5.5c Power dissipation . . . . . . . . . . . . . . . . . . . . . 43
Exa 5.5d Yearly precipitation . . . . . . . . . . . . . . . . . . . 43
Exa 5.6a Wearing of Battery . . . . . . . . . . . . . . . . . . . . 44
Exa 5.6b Working Machines . . . . . . . . . . . . . . . . . . . . 44
Exa 5.6c Series System . . . . . . . . . . . . . . . . . . . . . . . 45
Exa 5.8a Chi square random variable . . . . . . . . . . . . . . . 45
Exa 5.8b Chi square random variable . . . . . . . . . . . . . . . 45
Exa 5.8c Locating a Target . . . . . . . . . . . . . . . . . . . . 45
Exa 5.8d Locating a Target in 2D space . . . . . . . . . . . . . 45
Exa 5.8e T distribution . . . . . . . . . . . . . . . . . . . . . . 46
Exa 5.8f F Distribution . . . . . . . . . . . . . . . . . . . . . . 46
Exa 6.3a Claims handled by an insurance company . . . . . . . 47
Exa 6.3c Class strength . . . . . . . . . . . . . . . . . . . . . . 47
Exa 6.3d Weights of workers . . . . . . . . . . . . . . . . . . . . 48
Exa 6.3e Distance of a start . . . . . . . . . . . . . . . . . . . . 48
Exa 6.5a Processing time . . . . . . . . . . . . . . . . . . . . . . 49
Exa 6.6a Candidate winning an election . . . . . . . . . . . . . 49
Exa 6.6b Pork consumption . . . . . . . . . . . . . . . . . . . . 50
Exa 7.2a Maximum likelihood estimator of a bernoulli parameter 51
Exa 7.2b Errors in a manuscript . . . . . . . . . . . . . . . . . . 51
Exa 7.2c Maximum likelihood estimator of a poisson parameter 51
Exa 7.2d Number of traffic accidents . . . . . . . . . . . . . . . 52
Exa 7.2e Maximum likelihood estimator in a normal population 52
Exa 7.2f Kolmogorovs law of fragmentation . . . . . . . . . . . 52
Exa 7.2g Estimating Mean of a Uniform Distribution . . . . . . 53
Exa 7.3a Error in a signal . . . . . . . . . . . . . . . . . . . . . 53
Exa 7.3b Confidence interval . . . . . . . . . . . . . . . . . . . . 53
Exa 7.3c Confidence interval . . . . . . . . . . . . . . . . . . . . 54
Exa 7.3d Weight of a salmon . . . . . . . . . . . . . . . . . . . . 55
Exa 7.3e Error in a signal . . . . . . . . . . . . . . . . . . . . . 55
Exa 7.3f Average resting pulse . . . . . . . . . . . . . . . . . . 55
Exa 7.3g Evaluating integrals . . . . . . . . . . . . . . . . . . . 56
Exa 7.3h Thickness of washers . . . . . . . . . . . . . . . . . . . 56
Exa 7.4a Cable insulation . . . . . . . . . . . . . . . . . . . . . 57

6
Exa 7.4b Battery production . . . . . . . . . . . . . . . . . . . . 58
Exa 7.5a Transistors . . . . . . . . . . . . . . . . . . . . . . . . 59
Exa 7.5b Survey . . . . . . . . . . . . . . . . . . . . . . . . . . 59
Exa 7.5c Acceptable chips . . . . . . . . . . . . . . . . . . . . . 59
Exa 7.6a Life of a product . . . . . . . . . . . . . . . . . . . . . 60
Exa 7.7a Point estimator . . . . . . . . . . . . . . . . . . . . . . 60
Exa 7.7b Point estimator . . . . . . . . . . . . . . . . . . . . . . 61
Exa 7.7c Point estimator of a uniform distribution . . . . . . . 61
Exa 7.8a Bayes estimator . . . . . . . . . . . . . . . . . . . . . 62
Exa 7.8b Bayes estimator of a normal population . . . . . . . . 62
Exa 7.8d estimator of the signal value . . . . . . . . . . . . . . . 62
Exa 8.3a Noise in a Signal . . . . . . . . . . . . . . . . . . . . . 64
Exa 8.3b Error in a signal . . . . . . . . . . . . . . . . . . . . . 64
Exa 8.3c Error in a signal . . . . . . . . . . . . . . . . . . . . . 65
Exa 8.3d Number of signals to be sent . . . . . . . . . . . . . . 65
Exa 8.3e Number of signals to be sent . . . . . . . . . . . . . . 66
Exa 8.3f Nicotine content in a cigarette . . . . . . . . . . . . . 66
Exa 8.3g Blood cholestrol level . . . . . . . . . . . . . . . . . . 67
Exa 8.3h Water usage . . . . . . . . . . . . . . . . . . . . . . . 67
Exa 8.3i Life of a tire . . . . . . . . . . . . . . . . . . . . . . . 68
Exa 8.3j Service Time . . . . . . . . . . . . . . . . . . . . . . . 68
Exa 8.4a Tire lives . . . . . . . . . . . . . . . . . . . . . . . . . 69
Exa 8.4b Medicine for cold . . . . . . . . . . . . . . . . . . . . . 69
Exa 8.4c Unknown population variance . . . . . . . . . . . . . . 70
Exa 8.4d effectiveness of safety program . . . . . . . . . . . . . 71
Exa 8.5a effectiveness of machine . . . . . . . . . . . . . . . . . 71
Exa 8.5b Catalyst . . . . . . . . . . . . . . . . . . . . . . . . . . 71
Exa 8.6a Computer chip manufacturing . . . . . . . . . . . . . . 72
Exa 8.6b Finding p value . . . . . . . . . . . . . . . . . . . . . . 72
Exa 8.6c Change in manufacturing pattern . . . . . . . . . . . . 73
Exa 8.7a Mean number of defective chips . . . . . . . . . . . . . 73
Exa 8.7b Safety Conditions in a plant . . . . . . . . . . . . . . . 73
Exa 8.7c Better proof reader . . . . . . . . . . . . . . . . . . . . 74
Exa 9.1a Scatter Diagram . . . . . . . . . . . . . . . . . . . . . 75
Exa 9.2a Relative humidity and moisture content . . . . . . . . 75
Exa 9.3a Moisture against Density . . . . . . . . . . . . . . . . 78
Exa 9.4a Effect of speed on mileage . . . . . . . . . . . . . . . . 79
Exa 9.4b Confidence interval estimate . . . . . . . . . . . . . . . 80

7
Exa 9.4c Regression to the mean . . . . . . . . . . . . . . . . . 81
Exa 9.4d Motor vehicle deaths . . . . . . . . . . . . . . . . . . . 84
Exa 9.4e Confidence interval for height . . . . . . . . . . . . . . 84
Exa 9.4f Confidence interval for height . . . . . . . . . . . . . . 85
Exa 9.5a Height of son and father . . . . . . . . . . . . . . . . . 87
Exa 9.7a Percentage of chemical used . . . . . . . . . . . . . . . 89
Exa 9.8b Distance vs Travel Time . . . . . . . . . . . . . . . . . 90
Exa 9.9a Polynomial Fitting . . . . . . . . . . . . . . . . . . . . 90
Exa 9.10a Multiple Linear Regression . . . . . . . . . . . . . . . 92
Exa 9.10b Estimate of variance . . . . . . . . . . . . . . . . . . . 94
Exa 9.10c Diameter of a tree . . . . . . . . . . . . . . . . . . . . 95
Exa 9.10d Estimating hardness . . . . . . . . . . . . . . . . . . . 96
Exa 9.11a Animal fsickalling . . . . . . . . . . . . . . . . . . . . 98
Exa 10.3a Dependence of mileage on gas used . . . . . . . . . . . 99
Exa 10.3b Dependence of mileage on gas used . . . . . . . . . . . 100
Exa 10.3c Difference in GPA . . . . . . . . . . . . . . . . . . . . 101
Exa 10.4b Estimating Parameters . . . . . . . . . . . . . . . . . 102
Exa 10.5a Species collected . . . . . . . . . . . . . . . . . . . . . 103
Exa 11.2a Relation between death date and birth date . . . . . . 105
Exa 11.2b Quality of bulbs . . . . . . . . . . . . . . . . . . . . . 106
Exa 11.2d Six outcomes . . . . . . . . . . . . . . . . . . . . . . . 106
Exa 11.3a Weekly accidents . . . . . . . . . . . . . . . . . . . . . 107
Exa 11.4a Ploitical affiliation and Gender . . . . . . . . . . . . . 109
Exa 11.4b Machine Breakdown and shift . . . . . . . . . . . . . . 110
Exa 11.5a Lung cancer and smoking . . . . . . . . . . . . . . . . 111
Exa 11.5b Females reporting abuse . . . . . . . . . . . . . . . . . 112
Exa 11.6a Testing distribution of a population . . . . . . . . . . 113
Exa 12.2a testing the median . . . . . . . . . . . . . . . . . . . . 115
Exa 12.2b testing the median . . . . . . . . . . . . . . . . . . . . 115
Exa 12.3b Signed Rank Test . . . . . . . . . . . . . . . . . . . . 116
Exa 12.3c Determining Population Distribution . . . . . . . . . . 117
Exa 12.4a Treatments against corrosion . . . . . . . . . . . . . . 117
Exa 12.4b Determining P . . . . . . . . . . . . . . . . . . . . . . 118
Exa 12.4c Finding p value . . . . . . . . . . . . . . . . . . . . . . 119
Exa 12.4d Comparing production methods . . . . . . . . . . . . . 119
Exa 12.4e Determining p value . . . . . . . . . . . . . . . . . . . 119
Exa 12.5a Testing randomness . . . . . . . . . . . . . . . . . . . 120
Exa 12.5c Determining p value . . . . . . . . . . . . . . . . . . . 121

8
Exa 13.2a Steel shaft diameter . . . . . . . . . . . . . . . . . . . 122
Exa 13.2b unknown mean and variance . . . . . . . . . . . . . . 123
Exa 13.3a determining control limits . . . . . . . . . . . . . . . . 123
Exa 13.4a Defectives Screws . . . . . . . . . . . . . . . . . . . . 126
Exa 13.5a Control during production of cars . . . . . . . . . . . . 127
Exa 13.6b Service Time . . . . . . . . . . . . . . . . . . . . . . . 128
Exa 13.6c Exponentially weighted moving average control . . . . 129
Exa 13.6d Finding control limit . . . . . . . . . . . . . . . . . . . 131
Exa 14.3a Lifetime of a transistor . . . . . . . . . . . . . . . . . 133
Exa 14.3b Lifetime of Battery . . . . . . . . . . . . . . . . . . . . 134
Exa 14.3c One at a time sequential test . . . . . . . . . . . . . . 134
Exa 14.3d Lifetime of semiconductors . . . . . . . . . . . . . . . 135
Exa 14.3e Bayes estimator . . . . . . . . . . . . . . . . . . . . . 135
Exa 14.4a Lifetime of items produced by two plants . . . . . . . 135

9
List of Figures

2.1 pie chart . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 12

9.1 Scatter Diagram . . . . . . . . . . . . . . . . . . . . . . . . . 76


9.2 Relative humidity and moisture content . . . . . . . . . . . . 77
9.3 Moisture against Density . . . . . . . . . . . . . . . . . . . . 78
9.4 Regression to the mean . . . . . . . . . . . . . . . . . . . . . 82
9.5 Percentage of chemical used . . . . . . . . . . . . . . . . . . 88
9.6 Percentage of chemical used . . . . . . . . . . . . . . . . . . 88
9.7 Polynomial Fitting . . . . . . . . . . . . . . . . . . . . . . . 91

13.1 determining control limits . . . . . . . . . . . . . . . . . . . 124


13.2 determining control limits . . . . . . . . . . . . . . . . . . . 125
13.3 Exponentially weighted moving average control . . . . . . . 130

10
Chapter 2

Descriptive Statistics

Scilab code Exa 2.2a Relative Frequency

1 starting_salary = [47 48 49 50 51 52 53 54 56 57
60];
2 frequency = [4 1 3 5 8 10 0 5 2 3 1];
3 total = sum ( frequency ) ;
4 relative_frequency = frequency / total ;
5 disp ( ” The r e l a t i v e f r e q u e n c i e s a r e ” )
6 disp ( relative_frequency )

Scilab code Exa 2.2b pie chart

1 values = [42 50 32 55 9 12];


2 percentages = values *100 / sum ( values ) ;
3 new_text = string ( percentages ) ;
4 text = [ ” Lung ” , ” B r e a s t ” , ” Colon ” , ” P r o s t a t e ” , ”
Melanoma ” , ” B l a d d e r ” ];
5 percentage_sign = [ ”%” , ”%” , ”%” , ”%” , ”%” , ”%” ];
6 final_text = text + new_text + percentage_sign ;

11
Figure 2.1: pie chart

12
7 // p i e ( [ 4 2 50 32 55 9 1 2 ] , [ ” Lung ” , ” B r e a s t ” , ” Colon
” , ” P r o s t a t e ” , ” Melanoma ” , ” B l a d d e r ” ] ) ;
8 pie ( values , final_text ) ;

Scilab code Exa 2.3a Sample mean

1 scores =[284 , 280 , 277 , 282 , 279 , 285 , 281 , 283 , 278 ,
277];
2 new_scores = scores - 280;
3 final_mean = mean ( new_scores ) + 280;
4 disp ( final_mean )

Scilab code Exa 2.3b Sample mean of age

1 age = [15 16 17 18 19 20];


2 frequencies = [2 5 11 9 14 13];
3 product = age .* frequencies ;
4 total_people = sum ( frequencies ) ;
5 mean_age = sum ( product ) / total_people ;
6 disp ( ” The s a m p l e mean o f t h e a g e s i s ” )
7 disp ( mean_age )

Scilab code Exa 2.3c Sample Median

1 age = [15 16 17 18 19 20];


2 frequencies = [2 5 11 9 14 13];
3 i =1;
4 for j =1:6
5 for k = 1: frequencies ( j )
6 final_age ( i ) = age ( j ) ;

13
7 i = i +1 ;
8 end ;
9 end
10
11
12 final_median = median ( final_age ) ;
13 disp ( final_median ) ;

Scilab code Exa 2.3d Mean and Median

1 germ_free_mice = [158 192 193 194 195 202 212 215


229 230 237 240 244 247 259 301 301 321 337 415
434 444 485 496 529 537 624 707 800];
2 conventional_mice = [159 189 191 198 235 245 250 256
261 265 266 280 343 356 383 403 414 428 432];
3 disp ( mean ( germ_free_mice ) , ” Sample mean f o r germ−
f r e e mice i s ”);
4 disp ( median ( germ_free_mice ) , ” Sample median f o r
germ− f r e e m i c e i s ” ) ;
5 disp ( mean ( conventional_mice ) , ” Sample mean f o r
c o n v e n t i o n a l mice i s ”);
6 disp ( median ( conventional_mice ) , ” Sample mean f o r
c o n v e n t i o n a l mice i s ”);

Scilab code Exa 2.3e Mean Median and Mode

1 value = [1 2 3 4 5 6];
2 frequencies = [9 8 5 5 6 7];
3 i =1;
4 for j =1:6
5 for k = 1: frequencies ( j )
6 final_value ( i ) = value ( j ) ;
7 i = i +1 ;

14
8 end ;
9 end
10 product = value .* frequencies ;
11 disp ( product , sum ( product ) )
12
13 total_value = sum ( frequencies ) ;
14 mean_value = sum ( product ) / total_value ; // t h e a n s w e r
in the textbook i s i n c o r r e c t
15 [ m1 m2 ]= max ( frequencies ) ;
16 n = m2 ;
17
18 disp ( ” The s a m p l e mean i s ” )
19 disp ( mean_value )
20 disp ( median ( final_value ) , ” The median i s ” )
21 disp ( value ( n ) , ” The mode i s ” )

Scilab code Exa 2.3f sample variance

1 A = [ 3 4 6 7 10];
2 B = [ -20 5 15 24];
3 disp ( variance ( A ) , ” The s a m p l e v a r i a n c e o f A i s ”)
4 disp ( variance ( B ) , ” The s a m p l e v a r i a n c e of B i s ”)

Scilab code Exa 2.3g sample variance of accidents

1 accidents = [22 22 26 28 27 25 30 29 24];


2 new_accidents = accidents - 22;
3 disp ( variance ( new_accidents ) , ” The v a r i a n c e o f t h e
number o f a c c i d e n t s i s ” )

15
Scilab code Exa 2.3h Percentile

1 population = [7333253 3448613 2731743 1702086


1524249 1151977 1048949 1022830 998905 992038
816884 752279 734676 702979 665070 635913 617044
614289 579307 567094 547727 520947 514013 504505
493559];
2 disp ( perctl ( population , 10) , ” The s a m p l e 10
p e r c e n t i l e i s ”)
3 disp ( perctl ( population , 80) , ” The s a m p l e 80
p e r c e n t i l e i s ”)
4 disp ( perctl ( population , 50) , ” The s a m p l e 80
p e r c e n t i l e i s ”)
5 disp ( median ( population ) , ” The median i s ” )

Scilab code Exa 2.3i Quartiles

1 noise = [ 82 89 94 110 74 122 112 95 100 78 65 60


90 83 87 75 114 85 69 94 124 115 107 88 97 74 72
68 83 91 90 102 77 125 108 65];
2 disp ( quart ( noise ) , ” The q u a r t i l e s a r e ” )

Scilab code Exa 2.4a Chebyshev Inequality

1 cars = [448162 404192 368327 318308 272122 260486


249128 234936 218540 207977];
2 interval1 = mean ( cars ) - (1.5* st_deviation ( cars ) ) ;
3 interval2 = mean ( cars ) + (1.5* st_deviation ( cars ) ) ;
4 data = 100*5/9;
5 disp ( interval2 , , ” t o ” , interval1 , ” A t l e a s t 5 5 . 5 5%
o f the data l i e s i n the i n t e r v a l ” );

16
Scilab code Exa 2.5a Empirical Rule

1 data = [90 91 94 83 85 85 87 88 72 74 74 75 77 77 78
60 62 63 64 66 66 52 55 55 56 58 43 46];
2 disp ( ” A c c o r d i n g t o t h e e m p i r i c a l r u l e ” )
3 disp ( ” 68% o f t h e d a t a l i e s b e t w e e n ” )
4 disp ( mean ( data ) + st_deviation ( data ) , ” and ” , mean ( data
) - st_deviation ( data ) )
5 disp ( ” 95% o f t h e d a t a l i e s b e t w e e n ” )
6 disp ( mean ( data ) +(2* st_deviation ( data ) ) , ” and ” , mean (
data ) -(2* st_deviation ( data ) ) )
7 disp ( ” 9 9 . 7% o f t h e d a t a l i e s b e t w e e n ” )
8 disp ( mean ( data ) +(3* st_deviation ( data ) ) , ” and ” , mean (
data ) -(3* st_deviation ( data ) ) )

Scilab code Exa 2.6a Sample Correlation Coefficient

1 temp = [24.2 22.7 30.5 28.6 25.5 32.0 28.6 26.5 25.3
26.0 24.4 24.8 20.6 25.1 21.4 23.7 23.9 25.2
27.4 28.3 28.8 26.6];
2 defects = [25 31 36 33 19 24 27 25 16 14 22 23 20 25
25 23 27 30 33 32 35 24];
3 temp_new = temp - mean ( temp ) ;
4 defects_new = defects - mean ( defects ) ;
5 num =0
6 s1 =0;
7 s2 =0;
8 for i =1:22
9 num = num + ( temp_new ( i ) * defects_new ( i ) ) ;
10 s1 = s1 + ( temp_new ( i ) * temp_new ( i ) ) ;
11 s2 = s2 + ( defects_new ( i ) * defects_new ( i ) ) ;
12 end

17
13 coefficient = num / sqrt ( s1 * s2 ) ;
14 disp ( coefficient )

Scilab code Exa 2.6b Sample Correlation Coefficient

1 year = [12 16 13 18 19 12 18 19 12 14];


2 pulserate = [73 67 74 63 73 84 60 62 76 71];
3 year_new = year - mean ( year ) ;
4 pulserate_new = pulserate - mean ( pulserate ) ;
5 num =0
6 s1 =0;
7 s2 =0;
8 for i =1:10
9 num = num + ( year_new ( i ) * pulserate_new ( i ) ) ;
10 s1 = s1 + ( year_new ( i ) * year_new ( i ) ) ;
11 s2 = s2 + ( pulserate_new ( i ) * pulserate_new ( i ) ) ;
12 end
13 coefficient = num / sqrt ( s1 * s2 ) ;
14 disp ( coefficient )

18
Chapter 3

Elements Of Probability

Scilab code Exa 3.4a Union

1 cigarette = 0.28;
2 cigar = 0.07;
3 cigar_and_cigarette = 0.05 ;
4 cigar_or_cigarette = cigarette + cigar -
cigar_and_cigarette ;
5 disp ( ”% o f t h e m a l e s smoke n e i t h e r c i g a r n o r
c i g a r e t t e ” , (1 - cigar_or_cigarette ) *100 )

Scilab code Exa 3.5a Basic Principle of Counting

1 white_balls = 6;
2 black_balls = 5;
3 total = white_balls + black_balls ;
4 probability_whiteandblack = white_balls * black_balls
/( total *( total -1) ) ;
5 probability_blackandwhite = white_balls * black_balls
/( total *( total -1) ) ;
6 reqd_probability = probability_whiteandblack +
probability_blackandwhite ;

19
7 disp ( reqd_probability , ” Thus , t h e r e q u i r e d
p r o b a b i l i t y i s ”)

Scilab code Exa 3.5b Basic Principle of Counting

1 maths = 4;
2 chemistry = 3;
3 history = 2;
4 language = 1;
5 total_arrangements = factorial (4) * factorial ( maths ) *
factorial ( chemistry ) * factorial ( history ) * factorial
( language ) ;
6 disp ( total_arrangements , ” The t o t a l number o f
p o s s i b l e arrangements i s ”)

Scilab code Exa 3.5c Basic Principle of Counting

1 men = 6;
2 women = 4;
3 disp ( factorial ( men + women ) , ”No o f d i f f e r e n t rankings
p o s s i b l e i s ”)
4 women_top4 = factorial ( women ) * factorial ( men ) ;
5 prob = women_top4 / factorial ( men + women ) ;
6 disp ( prob , ” P r o b a b i l i t y t h a t women r e c e i v e t h e t o p 4
s c o r e s i s ”)

Scilab code Exa 3.5d Committee Probability

1 men = 6;
2 women = 9;

20
3 reqd_size =5;
4 total = factorial ( men + women ) /( factorial ( reqd_size ) *
factorial ( men + women - reqd_size ) ) ;
5 given_committee = factorial ( men ) * factorial ( women ) /(
factorial (3) * factorial (2) * factorial ( men -3) *
factorial ( women -2) ) ;
6 prob = given_committee / total ;
7 disp ( prob , ” P r o b a b i l i t y t h a t t h e c o m m i t t e e c o n s i s t s
o f 3 men and 2 women i s ” )

Scilab code Exa 3.5f Pairing Probability

1 black_p = 6;
2 white_p = 6;
3 pair = 2;
4 total_p = black_p + white_p ;
5
6
7
8
9 total_pairs = 1;
10 while ( total_p >0)
11 total_pairs = total_pairs * factorial ( total_p ) /(
factorial ( pair ) * factorial ( total_p - pair ) )
;
12 total_p = total_p -2;
13 // d i s p ( t o t a l p a i r s )
14 end
15 // d i s p ( t o t a l p a i r s )
16 total_pairs = total_pairs / factorial (6) ;
17 black_pairs = 1;
18 while ( black_p >0)
19 black_pairs = black_pairs * factorial ( black_p ) /((
factorial ( pair ) * factorial ( black_p - pair ) )
);

21
20 black_p = black_p -2;
21 // d i s p ( b l a c k p a i r s )
22 end
23 black_pairs = black_pairs / factorial (3) ;
24 // d i s p ( b l a c k p a i r s )
25
26
27 white_pairs = black_pairs ;
28 allowed_pairs = black_pairs * white_pairs ;
29 probb = allowed_pairs / total_pairs ;
30 disp ( probb , ” P r o b a b i l i t y t h a t a random p a i r i n g will
n o t r e s u l t i n any o f t h e w h i t e and b l a c k p l a y e r s
rooming t o g e t h e r i s ”)

Scilab code Exa 3.6a Acceptable Transistor

1 defective =5;
2 partially_defective = 10;
3 acceptable = 25;
4 disp ( acceptable /( acceptable + partially_defective ) , ”
The r e q u i r e d p r o b a b i l i t y i s ” )

Scilab code Exa 3.6b Both Boys

1 prob_bb = 0.25;
2 prob_bg = 0.25;
3 prob_gb = 0.25;
4 prob_gg = 0.25;
5 disp ( prob_bb /( prob_bg + prob_gb + prob_bb ) , ” P r o b a b i l i t y
t h a t both a r e boys i s ”)

22
Scilab code Exa 3.6c Branch Manager

1 prob_phoenix = 0.3;
2 prob_manager = 0.6;
3 disp ( prob_phoenix * prob_manager , ” P r o b a b i l i t y t h a t
P e r e z w i l l be a P h o e n i x b r a n c h o f f i c e manager i s ”
)

Scilab code Exa 3.7a Accident Probability

1 accident_prone = 0.4;
2 nonaccident_prone = 0.2;
3 pop_accident = 0.3;
4 prob = pop_accident * accident_prone + (1 - pop_accident
) * nonaccident_prone ;
5 disp ( prob , ” The r e q u i r e d p r o b a b i l i t y i s ” ) ;

Scilab code Exa 3.7b Accident within a year

1 accident_prone = 0.4;
2 nonaccident_prone = 0.2;
3 pop_accident = 0.3;
4 prob_of_accident = pop_accident * accident_prone + (1 -
pop_accident ) * nonaccident_prone ;
5 prob = pop_accident * accident_prone /
prob_of_accident ;
6 disp ( prob , ” The r e q u i r e d p r o b a b i l i t y i s ” )

Scilab code Exa 3.7c Multiple Choice Test

23
1 m = 5;
2 p =1/2;
3 disp ( ( m * p ) /(1+(( m -1) * p ) ) , ” The r e q u i r e d probability
i s ”)

Scilab code Exa 3.7d blood test

1 detect_present = 0.99;
2 detect_notpresent = 0.01;
3 pop_disease = 0.005;
4 prob = detect_present * pop_disease /(( detect_present *
pop_disease ) +( detect_notpresent *(1 - pop_disease ) )
) ;
5 disp ( prob , ” The r e q u i r e d p r o b a b i l i t y i s ” )

Scilab code Exa 3.7e Criminal Investigation

1 criminal_char = 0.9
2 convinced = 0.6;
3 pop_char = 0.2;
4 prob = ( convinced * criminal_char ) /(( convinced *
criminal_char ) + ( pop_char *(1 - convinced ) ) ) ;
5 disp ( prob , ” The r e q u i r e d p r o b a b i l i t y i s ” )

Scilab code Exa 3.7f Missing Plane

1 alpha1 = 0.4;
2 plane_in_region1 = 1/3;
3 plane_in_region2 = 1/3;
4 plane_in_region3 = 1/3;

24
5 prob1 = ( alpha1 * plane_in_region1 ) /(( alpha1 *
plane_in_region1 ) + 1* plane_in_region2 + 1*
plane_in_region3 ) ;
6 prob2 = (1* plane_in_region2 ) /(( alpha1 *
plane_in_region1 ) + 1* plane_in_region2 + 1*
plane_in_region3 ) ;
7 disp ( prob1 , ” The p r o b a b i l i t y t h a t t h e p l a n e s i s in
region 1 given that the search of region 1 did
not uncover i t ”);
8 disp ( prob2 , ” The p r o b a b i l i t y t h a t t h e p l a n e s i s in
r e g i o n 2/3 g i v e n t h a t t h e s e a r c h o f r e g i o n 1 did
not uncover i t ”);

Scilab code Exa 3.8a Independent Events

1 prob_A = 4/52;
2 prob_H = 13/52;
3 disp ( prob_A * prob_H , ”P(AH) i s ” )

25
Chapter 4

Random Variables And


Expectation

Scilab code Exa 4.1a sum of two fair dice

1 p11 = 1/36;
2 p12 = 1/36;
3 p13 = 1/36;
4 p14 = 1/36;
5 p15 = 1/36;
6 p16 = 1/36;
7 p21 = 1/36;
8 p22 = 1/36;
9 p23 = 1/36;
10 p24 = 1/36;
11 p25 = 1/36;
12 p26 = 1/36;
13 p31 = 1/36;
14 p32 = 1/36;
15 p33 = 1/36;
16 p34 = 1/36;
17 p35 = 1/36;
18 p36 = 1/36;
19 p41 = 1/36;

26
20 p42 = 1/36;
21 p43 = 1/36;
22 p44 = 1/36;
23 p45 = 1/36;
24 p46 = 1/36;
25 p51 = 1/36;
26 p52 = 1/36;
27 p53 = 1/36;
28 p54 = 1/36;
29 p55 = 1/36;
30 p56 = 1/36;
31 p61 = 1/36;
32 p62 = 1/36;
33 p63 = 1/36;
34 p64 = 1/36;
35 p65 = 1/36;
36 p66 = 1/36;
37 disp ( p11 , ” P r o b a b i l i t y t h a t t h e sum i s 2 ” )
38 disp ( p12 + p21 , ” P r o b a b i l i t y t h a t t h e sum i s 3 ” )
39 disp ( p13 + p31 + p22 , ” P r o b a b i l i t y t h a t t h e sum i s 4 ” )
40 disp ( p14 + p41 + p32 + p23 , ” P r o b a b i l i t y t h a t t h e sum i s 5
”)
41 disp ( p15 + p51 + p24 + p42 + p33 , ” P r o b a b i l i t y t h a t t h e sum
i s 6”)
42 disp ( p16 + p61 + p25 + p52 + p34 + p43 , ” P r o b a b i l i t y t h a t t h e
sum i s 7 ” )
43 disp ( p26 + p62 + p35 + p53 + p44 , ” P r o b a b i l i t y t h a t t h e sum
i s 8”)
44 disp ( p36 + p63 + p45 + p54 , ” P r o b a b i l i t y t h a t t h e sum i s 9
”)
45 disp ( p46 + p64 + p55 , ” P r o b a b i l i t y t h a t t h e sum i s 10 ” )
46 disp ( p65 + p56 , ” P r o b a b i l i t y t h a t t h e sum i s 11 ” )
47 disp ( p66 , ” P r o b a b i l i t y t h a t t h e sum i s 12 ” )

Scilab code Exa 4.1b Defective or Acceptable

27
1 pdd = 0.09;
2 pda = 0.21;
3 pad = 0.21;
4 paa = 0.49;
5
6 disp ( pdd , ” P r o b a b i l i t y t h a t t h e number o f a c c e p t a b l e
c o m p o n e n ts i s 0 i s ” )
7 disp ( pda + pad , ” P r o b a b i l i t y t h a t t h e number o f
a c c e p t a b l e components i s 1 i s ”)
8 disp ( paa , ” P r o b a b i l i t y t h a t t h e number o f a c c e p t a b l e
c o m p o n e n ts i s 2 i s ” )
9 disp ( pdd , ” P r o b a b i l i t y t h a t I i s 0 i s ”)
10
11 disp ( paa + pad + pda , ” P r o b a b i l i t y t h a t I i s 1 i s ” )

Scilab code Exa 4.1c X exceeds 1

1 prob = 1 -(1 -(1/ %e ) ) ;


2 disp ( prob , ” P r o b a b i l i t y t h a t X e x c e e d s 1 i s ” )

Scilab code Exa 4.2a sum of pmf

1 p1 = 1/2;
2 p2 = 1/3;
3 disp (1 -( p1 + p2 ) , ” P r o b a b i l i t y that X i s 3 i s ”)

Scilab code Exa 4.2b pdf

1
2 integral = integrate ( ’ ( 4 ∗ x ) −(2∗ x ∗ x ) ’ , ’ x ’ , 0 , 2) ;

28
3 C = 1/ integral ;
4 disp (C , ” The v a l u e o f C i s ” )
5 integral_new = integrate ( ’C∗ ( ( 4 ∗ x ) −(2∗ x ∗ x ) ) ’ , ’ x ’ ,
0 , 1) ;
6 disp (1 - integral_new , ” P r o b a b i l i t y t h a t X i s g r e a t e r
than 1 i s ”)

Scilab code Exa 4.3a Joint distribution of batteries

1 new = 3;
2 working = 4;
3 defective =5;
4 total = factorial (12) /( factorial (3) * factorial (9) ) ;
5 disp ( factorial (5) /( factorial (3) * factorial (2) * total ) ,
” P r o b a b i l i t y t h a t X=0 and Y=0” ) ;
6 disp ( factorial (5) * factorial (4) /( factorial (3) *
factorial (2) * factorial (3) * total ) , ” P r o b a b i l i t y
t h a t X=0 and Y=1” ) ;
7 disp ( factorial (5) * factorial (4) /( factorial (2) *
factorial (2) * factorial (4) * total ) , ” P r o b a b i l i t y
t h a t X=0 and Y=2” ) ;
8 disp ( factorial (4) /( factorial (3) * factorial (1) * total ) ,
” P r o b a b i l i t y t h a t X=0 and Y=3” ) ;
9 disp ( factorial (3) * factorial (5) /( factorial (2) *
factorial (2) * factorial (3) * total ) , ” P r o b a b i l i t y
t h a t X=1 and Y=0” ) ;
10 disp ( factorial (5) * factorial (4) * factorial (3) /(
factorial (2) * factorial (3) * factorial (4) * total ) , ”
P r o b a b i l i t y t h a t X=1 and Y=1” ) ;
11 disp ( factorial (3) * factorial (4) /( factorial (2) *
factorial (2) * factorial (2) * total ) , ” P r o b a b i l i t y
t h a t X=1 and Y=2” ) ;
12 disp ( factorial (3) * factorial (5) /( factorial (2) *
factorial (4) * factorial (1) * total ) , ” P r o b a b i l i t y
t h a t X=2 and Y=0” ) ;

29
13 disp ( factorial (3) * factorial (4) /( factorial (2) *
factorial (1) * factorial (3) * total ) , ” P r o b a b i l i t y
t h a t X=2 and Y=1” ) ;
14 disp ( factorial (3) /( factorial (3) * total ) , ” P r o b a b i l i t y
t h a t X=3 and Y=3” ) ;

Scilab code Exa 4.3b Joint distribution of boys and girls

1 child0 = 0.15;
2 child1 = 0.2;
3 child2 = 0.35;
4 child3 = 0.30;
5 pboy = 0.5;
6 pgirl = 0.5;
7
8 disp ( child0 , ” P r o b a b i l i t y t h a t B=0 and G=0” )
9 disp ( child1 * pgirl , ” P r o b a b i l i t y t h a t B=0 and G=1” )
10 disp ( child2 * pgirl * pgirl , ” P r o b a b i l i t y t h a t B=0 and
G=2” )
11 disp ( child3 * pgirl * pgirl * pgirl , ” P r o b a b i l i t y t h a t B
=0 and G=3” )
12 disp ( child1 * pboy , ” P r o b a b i l i t y t h a t B=1 and G=0” )
13 disp ( child2 * pgirl * pboy , ” P r o b a b i l i t y t h a t B=1 and G
=1” )
14 disp ( child3 * pgirl * pgirl * pboy , ” P r o b a b i l i t y t h a t B=1
and G=2” )
15 disp ( child2 * pboy * pboy , ” P r o b a b i l i t y t h a t B=2 and G
=0” )
16 disp ( child3 * pgirl * pboy * pboy , ” P r o b a b i l i t y t h a t B=2
and G=1” )
17 disp ( child3 * pboy * pboy * pboy , ” P r o b a b i l i t y t h a t B=3
and G=0” )

30
Scilab code Exa 4.3c Joint Density Function

1 intx = integrate ( ’ %eˆ(−x ) ’ , ’ x ’ ,0 , 1 ) ;


2 inty = integrate ( ’ 2∗%eˆ( −2∗ y ) ’ , ’ y ’ , 0 , 1) ;
3 answer = (1 - intx ) * inty ;
4 disp ( answer , ” P r o b a b i l i t y t h a t X>1 and Y<1 i s ” )
5
6 // For o t h e r two p a r t s , s y m b o l i c m a n i p u l a t i o n s a r e
required

Scilab code Exa 4.3e Density of Independent Random Variables

1 pdec3 = 0.05;
2 pdec2 = 0.1;
3 pdec1 = 0.2;
4 p0 = 0.3
5 pinc1 = 0.2;
6 pinc2 = 0.1;
7 pinc3 = 0.05;
8 disp ( pinc1 * pinc2 * p0 , ” P r o b a b i l i t y that the stock
p r i c e w i l l i n c r e a s e s u c c e s s i v e l y by 1 , 2 and 0
p o i n t s i n the next 3 days i s ”)

Scilab code Exa 4.3f Conditional Probability Mass Function

1 disp (0.1/0.3875 , ” p r o b a b i l i t y t h a t B =0 g i v e n G=1 ” )


;
2 disp (0.175/0.3875 , ” p r o b a b i l i t y t h a t B =1 g i v e n G=1
”);
3 disp (0.1125/0.3875 , ” p r o b a b i l i t y t h a t B =2 g i v e n G=1
”);
4 disp (0/0.3875 , ” p r o b a b i l i t y t h a t B =3 g i v e n G=1 ” ) ;
5 // The v a l u e s a r e t a k e n from T a b l e 4 . 2

31
Scilab code Exa 4.3g Conditional Probability Mass Function

1 p00 =0.4;
2 p01 = 0.2;
3 p10 = 0.1;
4 p11 = 0.3;
5
6 pY1 = p01 + p11 ;
7 disp ( p01 / pY1 , ” P r o b a b i l i t y t h a t X=0 and Y=1” )
8 disp ( p11 / pY1 , ” P r o b a b i l i t y t h a t X=1 and Y=1” )

Scilab code Exa 4.4a Expectation of a fair die

1 p1 =1/6;
2 p2 =1/6;
3 p3 =1/6;
4 p4 =1/6;
5 p5 =1/6;
6 p6 =1/6;
7 expec = p1 + (2* p2 ) +(3* p3 ) +(4* p4 ) +(5* p5 ) + (6* p6 ) ;
8 disp ( expec )

Scilab code Exa 4.4d Expectation of the message time

1 expec = integrate ( ’ ( x ) / 1 . 5 ’ , ’ x ’ , 0 ,1.5) ;


2 disp ( ” h o u r s ” , expec , ”On an a v e r a g e , you have t o
wait f o r ”)

32
Scilab code Exa 4.5a Expectation

1 p0 = 0.2;
2 p1 = 0.5;
3 p2 =0.3;
4 expec = 0*0* p0 + 1*1* p1 + 2*2* p2 ;
5 disp ( expec , ” E x p e c t a t i o n o f Xˆ2 i s ” )

Scilab code Exa 4.5b Expected cost of breakdown

1 expec = integrate ( ’ x ˆ3 ’ , ’ x ’ , 0 , 1) ;
2 disp ( expec , ” The e x p e c t a t i o n i s ” )

Scilab code Exa 4.5c Expectation

1 p0 = 0.2;
2 p1 = 0.5;
3 p2 =0.3;
4 expec = 0*0* p0 + 1*1* p1 + 2*2* p2 ;
5 disp ( expec , ” E x p e c t a t i o n o f Xˆ2 i s ” )

Scilab code Exa 4.5d Expectation

1 expec = integrate ( ’ x ˆ3 ’ , ’ x ’ , 0 , 1) ;
2 disp ( expec , ” The e x p e c t a t i o n i s ” )

33
Scilab code Exa 4.5e Expected profit

1 profit1 = 10;
2 profit2 = 20;
3 profit3 = 40;
4 prob1 = 0.2;
5 prob2 = 0.8;
6 prob3 = 0.3;
7 expec = profit1 * prob1 + profit2 * prob2 + profit3 *
prob3 ;
8 disp ( ” t h o u s a n d d o l l a r s ” , expec , ” The e x p e c t d p r o f i t
i s ”)

Scilab code Exa 4.5f Letters in Correct Envelopes

1 // As s c i l a b d o e s n o t s y m b o l i c c o m p u t a t i o n s , t h i s
e x a m p l e i s s o l v e d t a k i n g N=5
2 prob = 1/5 // p r o b a b i l i t y t h a t a l e t t e r i s put i n t o
the r i g h t envelope
3 EX1 = 1* prob +0*(1 - prob ) ;
4 EX2 = 1* prob +0*(1 - prob ) ;
5 EX3 = 1* prob +0*(1 - prob ) ;
6 EX4 = 1* prob +0*(1 - prob ) ;
7 EX5 = 1* prob +0*(1 - prob ) ;
8 EX = EX1 + EX2 + EX3 + EX4 + EX5 ;
9 disp ( EX , ” Thus , t h e e x p e c t a t i o n i s ” )

Scilab code Exa 4.5g Different types of coupons

34
1 ProbXiequals1 = 1 - ((19/20) ^10) ;
2 EXi = ProbXiequals1 ;
3 EX = 20* EXi ;
4 disp ( EX , ” The e x p e c t a t i o n i s ” )

Scilab code Exa 4.6a Variance of a fair die

1 probXequalsi = 1/6;
2 expecXsquared = 0;
3 for n =1:6
4 expecXsquared = expecXsquared + ( n * n *
probXequalsi )
5 end
6
7 expecX = 3.5 // from e g 4 . 4 a
8 var = expecXsquared - ( expecX ^2) ;
9 disp ( var , ” The v a r i a n c e i s ” )

Scilab code Exa 4.7a Variance of 10 rolls of a fair die

1 probXequalsi = 1/6;
2 expecXsquared = 0;
3 for n =1:6
4 expecXsquared = expecXsquared + ( n * n *
probXequalsi )
5 end
6
7 expecX = 3.5 // from e g 4 . 4 a
8 var = expecXsquared - ( expecX ^2) ;
9 var10 = var *10;
10 disp ( var10 , ” The v a r i a n c e i s ” )

35
Scilab code Exa 4.7b Variance of 10 tosses of a coin

1 probIj = 0.5;
2 varIj = probIj *(1 - probIj ) ;
3 var = 10* varIj ;
4 disp ( var , ” Thus , t h e r e q u i r e d v a r i a n c e i s ”)

Scilab code Exa 4.9a Inequalities

1 avg = 50;
2 probX75 = avg /75;
3 disp ( probX75 , ” P r o b a b i l i t y t h a t X>75 i s ” )
4 var = 25;
5 upperlimit = var /100;
6 disp (1 - upperlimit , ” P r o b a b i l i t y t h a t X l i e s between
40 and 60 i s ” )

36
Chapter 5

Special Random Variables

Scilab code Exa 5.1a Returning of disks

1 defects = 0.01;
2 disks = 10;
3 package = 3;
4 probdefect0 = ((1 - defects ) ^10) ;
5 probdefect1 = factorial ( disks ) * defects *((1 - defects )
^9) / factorial ( disks -1) ;
6 prob = 1 - probdefect0 - probdefect1 ;
7 disp ( prob , ” P r o b a b i l i t y t h a t a p a c k a g e w i l l be
returned i s ”)
8 newprob = factorial ( package ) * prob *((1 - prob ) ^2) /
factorial ( package -1) ;
9 disp ( newprob , ” P r o b a b i l i t y t h a t e x a c t l y one o f t h e
p a c k a g e s w i l l be r e t u r n e d among 3 i s ” )
10
11 // t h e s o l u t i o n i n t h e t e x t b o o k i s a p p r o x i m a t e

Scilab code Exa 5.1b Colour of Eyes

37
1 function result = binomial (n , k , p )
2 result = factorial ( n ) *( p ^ k ) *((1 - p ) ^( n - k ) ) /(
factorial ( k ) * factorial (n - k ) )
3 endfunction
4
5children = 4;
6reqdblueyes = 2;
7probblueeyes = 0.5*0.5;
8prob = binomial ( children , reqdblueyes , probblueeyes )
;
9 disp ( prob , ” The r e q d p r o b a b i l i t y i s ” )

Scilab code Exa 5.1e Binomial Random Variable

1 function result = bin (n ,k , p )


2 if ( k ==0)
3 result = (1 - p ) ^ n ;
4 else
5 result = p *( n - k +1) * bin (n , k -1 , p ) /((1 - p ) * k
);
6 end
7 endfunction
8
9 disp ( bin (6 , 0, 0.4) , ” Probability that X=0 i s ”)
10 disp ( bin (6 , 1, 0.4) , ” Probability that X=1 i s ”)
11 disp ( bin (6 , 2, 0.4) , ” Probability that X=2 i s ”)
12 disp ( bin (6 , 3, 0.4) , ” Probability that X=3 i s ”)
13 disp ( bin (6 , 4, 0.4) , ” Probability that X=4 i s ”)
14 disp ( bin (6 , 5, 0.4) , ” Probability that X=5 i s ”)
15 disp ( bin (6 , 6, 0.4) , ” Probability that X=6 i s ”)

Scilab code Exa 5.2a Probability of accident

38
1 [ probX0 , Q ]= cdfpoi ( ”PQ” , 0 , 3) ;
2 probX1 = 1 - probX0 ;
3 disp ( probX1 , ” P r o b a b i l i t y t h a t t h e r e i s a t l e a s t one
a c c i d e n t t h i s week i s ” )

Scilab code Exa 5.2b Defective Items

1 function result = bino (n , k , p )


2 result = factorial ( n ) *( p ^ k ) *((1 - p ) ^( n - k ) ) /(
factorial ( k ) * factorial (n - k ) )
3 endfunction
4
5 prob = bino (10 ,0 , 0.1) + bino (10 , 1 ,0.1 ) ;
6 disp ( prob , ” The e x a c t p r o b a b i l i t y i s ” ) ;
7
8 probp = cdfpoi ( ”PQ” , 1 , 1)
9 disp ( probp , ” The p o i s s o n a p p r o x i m a t i o n i s ” )

Scilab code Exa 5.2c Number of Alpha particles

1 Xlam = 3.2;
2 i =2;
3 prob = cdfpoi ( ”PQ” , i , Xlam ) ;
4 disp ( prob )

Scilab code Exa 5.2d Claims handled by an insurance company

1 function result = bino (n , k , p )


2 result = factorial ( n ) *( p ^ k ) *((1 - p ) ^( n - k ) ) /(
factorial ( k ) * factorial (n - k ) )

39
3 endfunction
4 avg = 5;
5 i =3;
6 prob = cdfpoi ( ”PQ” , 2 , avg ) ;
7 disp ( prob , ” P r o p o r t i o n o f d a y s t h a t have l e s s than 3
claims i s ”)
8 probX4 = cdfpoi ( ”PQ” ,i +1 , avg ) - cdfpoi ( ”PQ” , i , avg
);
9
10 reqdprob = bino (5 ,3 , probX4 ) ;
11 disp ( reqdprob , ” P r o b a b i l i t y t h a t 3 o f t h e n e x t 5
d a y s w i l l have e x a c t l y 4 c l a i m s i s ” )

Scilab code Exa 5.2f Defective stereos

1 avg = 4;
2 prob = cdfpoi ( ”PQ” , 3 , 2* avg )
3 disp ( prob )

Scilab code Exa 5.3a Functional system

1 function result = hyper (N , M , n , i )


2 result = factorial ( N ) * factorial ( M ) * factorial ( n ) *
factorial ( N +M - n ) /( factorial ( i ) * factorial (N - i )
* factorial (n - i ) * factorial (M - n + i ) * factorial ( N +
M));
3 endfunction
4
5 prob = hyper (15 , 5 ,6 , 4) + hyper (15 , 5 ,6 ,5) + hyper
(15 ,5 ,6 ,6) ;
6 disp ( prob , ” P r o b a b i l i t y t h a t t h e s y s t e m w i l l be
f u n c t i o n a l i s ”)

40
Scilab code Exa 5.3b Determining Population Size

1 function result = hyper (N , M , n , i )


2 result = factorial ( N ) * factorial ( M ) * factorial ( n ) *
factorial ( N +M - n ) /( factorial ( i ) * factorial (N - i )
* factorial (n - i ) * factorial (M - n + i ) * factorial ( N +
M));
3 endfunction
4
5 r = 50;
6 n =100;
7 X =25;
8 disp ( r * n / X , ” E s t i m a t e o f t h e number o f a n i m a l s i n
the r e g i o n i s ”)

Scilab code Exa 5.3c Conditional Probability

1 function result = bino (n , k , p )


2 result = factorial ( n ) *( p ^ k ) *((1 - p ) ^( n - k ) ) /(
factorial ( k ) * factorial (n - k ) )
3 endfunction
4
5 function answer = condprob (n ,k ,p , i )
6 answer = bino (n ,i , p ) * bino (m ,k -i , p ) / bino ( n +m ,k , p
);
7 endfunction
8
9 // The f u n c t i o n c o n d p r o b w i l l g i v e P{X= i | X+Y=k }

41
Scilab code Exa 5.4b Bus Timings

1 pass_f = 1/30;
2 prob1 = (15 -10) * pass_f + (30 -25) * pass_f ;
3 prob2 = (3 -0) * pass_f + (18 -15) * pass_f ;
4 disp ( prob1 , ” P r o b a b i l i t y t h a t he w a i t s l e s s t h a n 5
m i n u t e s f o r a bus ” )
5 disp ( prob2 , ” P r o b a b i l i t y t h a t he w a i t s a t l e a s t 12
m i n u t e s f o r a bus ” )

Scilab code Exa 5.4c Current in a diode

1 a =5;
2 I0 =10^ -6;
3 v_f = 1/(3 -1) ;
4 vupperlim = 3;
5 vlowerlim = 1;
6 expecV = ( vupperlim + vlowerlim ) /2;
7 expec = integrate ( ’ ( %e ˆ ( a ∗ x ) ) /2 ’ , ’ x ’ , 1 ,3) ;
8 expecI = I0 *( expec -1) ;
9 disp ( expecI )

Scilab code Exa 5.5a Normal Random Variable

1 u = 3;
2 var = 16;
3
4 prob1 = cdfnor ( ”PQ” , 11 , u , sqrt ( var ) ) ;
5 disp ( prob1 , ” P{X<11} ” ) ;
6 prob2 = 1 - cdfnor ( ”PQ” , -1 , u , sqrt ( var ) ) ;
7 disp ( prob2 , ”P{X>−1}” ) ;
8 prob3 = cdfnor ( ”PQ” , 7 , u , sqrt ( var ) ) - cdfnor ( ”PQ” ,
2 , u , sqrt ( var ) ) ;

42
9 disp ( prob3 , ”P{2<X<7}” ) ;

Scilab code Exa 5.5b Noise in Binary Message

1 disp ( cdfnor ( ”PQ” , -1.5 , 0 , 1) , ”P{ e r r o r | m e s s a g e i s


1} ” )
2 disp (1 - cdfnor ( ”PQ” , 2.5 , 0 , 1) , ”P{ e r r o r | m e s s a g e i s
0 }”)

Scilab code Exa 5.5c Power dissipation

1 r =3;
2 avg = 6;
3 std = 1;
4 var = std ^2;
5 expecV2 = var + ( avg ^2) ;
6 expecW = 3* expecV2 ;
7 disp ( expecW , ” E x p e c t a t i o n o f W i s ” )
8 limw =120;
9 limV = sqrt ( limw / r ) ;
10 disp (1 - cdfnor ( ”PQ” , limV , avg , std ) , ”P{W>120} i s ” )

Scilab code Exa 5.5d Yearly precipitation

1 meanX1 = 12.08;
2 meanX2 = 12.08;
3 stX1 = 3.1;
4 meanX = meanX1 + meanX2 ;
5 varX = 2*(3.1^2) ;
6 lim = 25;

43
7 disp (1 - cdfnor ( ”PQ” , lim , meanX , sqrt ( varX ) ) , ”
P r o b a b i l i t y that the t o t a l p r e c i p i t a t i o n during
t h e n e x t 2 y e a r s w i l l e x c e e d 25 i n c h e s ” )
8
9 meanXnew = meanX1 - meanX2 ;
10 new_lim = 3;
11 disp (1 - cdfnor ( ”PQ” , new_lim , meanXnew , sqrt ( varX ) ) ,
” P r o b a b i l i t y that p r e c i p i t a t i o n in the next year
w i l l e x c e e d t h a t i n t h e f o l l o w i n g y e a r by more
than 3 i n c h e s ”)

Scilab code Exa 5.6a Wearing of Battery

1 lamda = 1/10000;
2 x = 5000;
3 prob = %e ^( -1* lamda * x ) ;
4 disp ( prob , ” P r o b a b i l i t y
t h a t s h e w i l l be a b l e t o
complete her t r i p without having to r e p l a c e her
c a r b a t t e r y i s ”);

Scilab code Exa 5.6b Working Machines

1 //When C i s put t o use , one o t h e r machine ( e i t h e r A


o r B ) w i l l s t i l l be w o r k i n g . The p r o b a b i l i t y o f
t h i s machine o r C f a i l i n g i s e q u a l due t o t h e
m e m o r y l e s s p r o p o e r t y o f e x p o n e n t i a l random
variables .
2
3 disp (1/2 , ” The p r o b a b i l i t y t h a t machine which i s
s t i l l o p e r a b l e i s machine C i s ” )

44
Scilab code Exa 5.6c Series System

1 function result = new ( lamda ,n , t )


2 newsum = 0;
3 for i =1: n
4 newsum = newsum + lamda ( i )
5 result = %e ^( -1* newsum * t )
6 end
7 endfunction

Scilab code Exa 5.8a Chi square random variable

1 disp ( cdfchn ( ”PQ” , 30 , 26 , 0) ) ;

Scilab code Exa 5.8b Chi square random variable

1 disp ( cdfchn ( ”X” , 15 , 0 ,0.95 , 0.05 ) )

Scilab code Exa 5.8c Locating a Target

1 disp (1 - cdfchn ( ”PQ” , 9/4 , 3 , 0) )

Scilab code Exa 5.8d Locating a Target in 2D space

1 disp (1 - cdfchn ( ”PQ” , 2.25 ,2 ,0) )

45
Scilab code Exa 5.8e T distribution

1 disp ( cdft ( ”PQ” , 1.4 , 12) , ”P{ T12 <=1.4} ” ) ;


2 disp ( cdft ( ”T” , 9 , 0.975 , 0.025) , ” t 0 . 0 2 5 , 9 ” )

Scilab code Exa 5.8f F Distribution

1 disp ( cdff ( ”PQ” , 1.5 , 6 , 14) )

46
Chapter 6

Distribution of Sampling
Statistics

Scilab code Exa 6.3a Claims handled by an insurance company

1 number = 25000;
2 meaneach = 320;
3 sdeach = 540;
4 claim = 8300000;
5 meantotal = meaneach * number ;
6 sdtotal = sdeach * sqrt ( number ) ;
7 disp (1 - cdfnor ( ”PQ” , claim , meantotal , sdtotal ) )

Scilab code Exa 6.3c Class strength

1 ideal_num = 150;
2 actual_num = 450;
3 attend = 0.3;
4 tolerance = 0.5
5 disp (1 - cdfnor ( ”PQ” , ideal_num + tolerance , actual_num *
attend , sqrt ( actual_num * attend *(1 - attend ) ) ) )

47
Scilab code Exa 6.3d Weights of workers

1 meaneach = 167;
2 sdeach = 27;
3 num = 36;
4 sdtotal = sdeach / sqrt ( num ) ;
5 // s d t o t a l = s d t o t a l ∗ s d t o t a l ;
6 // d i s p ( s d t o t a l )
7 disp ( cdfnor ( ”PQ” , 170 , meaneach , sdtotal ) - cdfnor ( ”PQ
” , 163 , meaneach , sdtotal ) , ” P r o b a b i l i t y t h a t t h e
s a m p l e mean o f t h e i r w e i g h t s l i e s b e t w e e n 163
and 1 7 0 ( when s a m p l e s i z e i s 3 6 ) ” )
8
9 num =144;
10 sdtotal = sdeach / sqrt ( num ) ;
11 // d i s p ( s d t o t a l )
12 disp ( cdfnor ( ”PQ” , 170 , meaneach , sdtotal ) - cdfnor ( ”PQ
” , 163 , meaneach , sdtotal ) , ” P r o b a b i l i t y t h a t t h e
s a m p l e mean o f t h e i r w e i g h t s l i e s b e t w e e n 163
and 1 7 0 ( when s a m p l e s i z e i s 1 4 4 ) ” )
13
14 // The a n s w e r g i v e n i n t h e t e x t b o o k i s i n c o r r e c t a s
(170 −167) / 4 . 5 i s n o t e q u a l t o 0 . 6 2 5 9 .

Scilab code Exa 6.3e Distance of a start

1 prob = 0.95;
2 lim = 0.5;
3 X = cdfnor ( ”X” , 0 ,1 , 0.975 , 0.025 )
4 disp ( ceil ((4* X ) ^2) , ” O b s e r v a t i o n s a r e n e c e s s a r y (
a t l e a s t ) ”)

48
Scilab code Exa 6.5a Processing time

1 n = 15;
2 sigmasquare = 9;
3 lim =12;
4 actual_lim = (n -1) * lim / sigmasquare ;
5 prob = 1 - cdfchi ( ”PQ” , actual_lim , (n -1) )
6 disp ( prob )

Scilab code Exa 6.6a Candidate winning an election

1 favour = 0.45;
2 samplesize = 200;
3 expec = favour * samplesize ;
4 sd = sqrt ( samplesize * favour *(1 - favour ) ) ;
5 disp ( expec , ” The e x p e c t e d v a l u e i s ” )
6 disp ( sd , ” The s t a n d a r d d e v i a t i o n i s ” )
7
8 function result = bino (n , k , p )
9 result = factorial ( n ) *( p ^ k ) *((1 - p ) ^( n - k ) ) /(
factorial ( k ) * factorial (n - k ) )
10 endfunction
11
12 // newsum = 0 ;
13 // f o r i =1:10
14 // newsum = newsum + b i n o ( 2 0 0 , i , favour )
15 // end
16 // p r o b = 1−newsum ; ∗ /
17
18 lim = 101;
19 tolerance = 0.5;
20 lim = lim - tolerance ;

49
21 prob = 1 - cdfnor ( ”PQ” , lim , expec , sd )
22
23 disp ( prob , ” P r o b a b i l i t y t h a t more t h a n h a l f t h e
members o f t h e s a m p l e f a v o u r t h e c a n d i d a t e ” )

Scilab code Exa 6.6b Pork consumption

1 meaneach = 147;
2 sdeach = 62;
3 samplesize = 25;
4 lim =150;
5 samplemean = meaneach ;
6 samplesd = sdeach / sqrt ( samplesize )
7 prob = 1 - cdfnor ( ”PQ” , lim , samplemean , samplesd )
8 disp ( prob )

50
Chapter 7

Parameter Estimation

Scilab code Exa 7.2a Maximum likelihood estimator of a bernoulli pa-


rameter

1 samplesize = 1000;
2 acceptable =921;
3 disp ( acceptable / samplesize , ” The maximum l i k e l i h o o d
es tim a t e o f p i s ”)

Scilab code Exa 7.2b Errors in a manuscript

1 function result = totalerror ( n1 , n2 , n12 )


2 result = n1 * n2 / n12 ;
3 endfunction

Scilab code Exa 7.2c Maximum likelihood estimator of a poisson param-


eter

1 total_people = 857;

51
2 days = 20;
3 disp ( total_people / days , ” The maximum l i k e l i h o o d
e s t i m a t e o f lambda ” )

Scilab code Exa 7.2d Number of traffic accidents

1 accidents = [4 0 6 5 2 1 2 0 4 3 ];
2 lambda = mean ( accidents )
3 disp ( cdfpoi ( ”PQ” , 2 , lambda ) )

Scilab code Exa 7.2e Maximum likelihood estimator in a normal popula-


tion

1 function [u , sigmasquared ]= normal (X , Xmean , n )


2 u = Xmean ;
3 newsum = 0;
4 for i = 1: n
5 newsum = newsum + ( X ( i ) - Xmean ) ^2
6 end
7 sigmasquared = sqrt (( newsum / n ) ) ;
8 endfunction

Scilab code Exa 7.2f Kolmogorovs law of fragmentation

1 X = [2.2 3.4 1.6 0.8 2.7 3.3 1.6 2.8 2.5 1.9]
2 upperlimX = 3
3 lowerlimX = 2;
4 upperlimlogX = log ( upperlimX ) ;
5 lowerlimlogX = log ( lowerlimX ) ;
6

52
7 logX = log ( X )
8 samplemean = mean ( logX )
9 samplesd = sqrt ( variance ( logX ) )
10 // d i s p ( samplemean )
11 // d i s p ( s a m p l e s d )
12 prob = cdfnor ( ”PQ” , upperlimlogX , samplemean ,
samplesd ) - cdfnor ( ”PQ” , lowerlimlogX , samplemean
, samplesd )
13 disp ( prob )

Scilab code Exa 7.2g Estimating Mean of a Uniform Distribution

1 function result = unif (X , n )


2 result = max ( X ) /2;
3 endfunction

Scilab code Exa 7.3a Error in a signal

1 avg = 0;
2 var = 4;
3 num = 9;
4 X =[5 8.5 12 15 7 9 7.5 6.5 10.5];
5 samplemean = mean ( X ) ;
6 lowerlim = samplemean - (1.96* sqrt ( var / num ) )
7 upperlim = samplemean + (1.96* sqrt ( var / num ) )
8
9 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
i n t e r v a l i s ”, )

Scilab code Exa 7.3b Confidence interval

53
1 avg = 0;
2 var = 4;
3 num = 9;
4 X =[5 8.5 12 15 7 9 7.5 6.5 10.5];
5 samplemean = mean ( X ) ;
6 lowerlim = samplemean - (1.645* sqrt ( var / num ) )
7 upperlim = samplemean + (1.645* sqrt ( var / num ) )
8
9 disp ( ” t o i n f i n i t y ” , lowerlim , ” The 95% u p p e r
confidence interval is ” )
10 disp ( upperlim , ” The 95% u p p e r c o n f i d e n c e i n t e r v a l i s
minus i n f i n i t y t o ” )

Scilab code Exa 7.3c Confidence interval

1 var = 4;
2 num = 9;
3 X =[5 8.5 12 15 7 9 7.5 6.5 10.5];
4 samplemean = mean ( X ) ;
5 alpha = 0.005;
6 zalpha = cdfnor ( ”X” , 0 , 1 ,1 - alpha , alpha ) ;
7 // d i s p ( z a l p h a )
8 lowerlim = samplemean - ( zalpha * sqrt ( var / num ) )
9 upperlim = samplemean + ( zalpha * sqrt ( var / num ) )
10 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
i n t e r v a l i s ”, )
11
12 alpha = 0.01;
13 zalpha = cdfnor ( ”X” , 0 , 1 ,1 - alpha , alpha ) ;
14 lowerlim = samplemean - ( zalpha * sqrt ( var / num ) )
15 upperlim = samplemean + ( zalpha * sqrt ( var / num ) )
16 disp ( ” t o i n f i n i t y ” , lowerlim , ” The 95% u p p e r
confidence interval is ” )
17 disp ( upperlim , ” The 95% u p p e r c o n f i d e n c e i n t e r v a l i s
minus i n f i n i t y t o ” )

54
Scilab code Exa 7.3d Weight of a salmon

1 sd = 0.3;
2 lim = 0.1;
3 num = (1.96* sd / lim ) ^2;
4 disp ( num , ” Sample s i z e s h o u l d be g r e a t e r t h a n ” ) ;

Scilab code Exa 7.3e Error in a signal

1 X = [5 8.5 12 15 7 9 7.5 6.5 10.5];


2 num = 9;
3 meanX = mean ( X ) ;
4 X2 = X ^2;
5 s2 = ( sum ( X2 ) - ( num *( meanX ^2) ) ) /( num -1) ;
6 s = sqrt ( s2 ) ;
7 tval = cdft ( ”T” , num -1 , 0.975 , 0.025) ;
8 // d i s p ( t v a l )
9 upperlim = meanX + ( tval * s ) / sqrt ( num ) ;
10 lowerlim = meanX - ( tval * s ) / sqrt ( num ) ;
11 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
i n t e r v a l i s ”, )

Scilab code Exa 7.3f Average resting pulse

1 X = [54 63 58 72 49 92 70 73 69 104 48 66 80 64 77];


2 num = 15;
3 meanX = mean ( X ) ;
4 X2 = X ^2;
5 s2 = ( sum ( X2 ) - ( num *( meanX ^2) ) ) /( num -1) ;

55
6 s = sqrt ( s2 ) ;
7 tval = cdft ( ”T” , num -1 , 0.975 , 0.025) ;
8 // d i s p ( t v a l )
9 upperlim = meanX + ( tval * s ) / sqrt ( num ) ;
10 lowerlim = meanX - ( tval * s ) / sqrt ( num ) ;
11 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
i n t e r v a l i s ”, )
12 alpha = 0.05;
13 tval = cdft ( ”T” , num -1 , 1 - alpha , alpha ) ;
14 lim = meanX + ( tval * s ) / sqrt ( num ) ;
15 disp ( lim , ” The 95% l o w e r c o n f i d e n c e i n t e r v a l i s from
minus i n f i n i t y t o ” )

Scilab code Exa 7.3g Evaluating integrals

1 meanX = 0.786;
2 s = 0.03;
3 num = 100;
4 alpha = 0.05;
5 tval = cdft ( ”T” , num -1 , 1 - alpha , alpha ) ;
6 upperlim = meanX + ( tval * s ) / sqrt ( num ) ;
7 lowerlim = meanX - ( tval * s ) / sqrt ( num ) ;
8 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
i n t e r v a l i s ”, )

Scilab code Exa 7.3h Thickness of washers

1 num =10;
2 X = [0.123 0.133 0.124 0.126 0.120 0.130 0.125 0.128
0.124 0.126];
3 // d i s p ( v a r i a n c e (X) )
4 s2 = variance ( X ) ;
5 chi1 = cdfchi ( ”X” ,num -1 ,0.95 , 0.05 );

56
6 chi2 = cdfchi ( ”X” ,num -1 ,0.05 , 0.95 ) ;
7 // d i s p ( c h i 1 , c h i 2 )
8 lowerlim = ( num -1) * s2 / chi2 ;
9 upperlim = ( num -1) * s2 / chi1 ;
10 disp ( sqrt ( upperlim ) , ” t o ” , sqrt ( lowerlim ) ,” The 90%
confidence interval is ” )

Scilab code Exa 7.4a Cable insulation

1 A =[36 44 41 53 38 36 34 54 52 37 51 44 35 44];
2 B =[52 64 38 68 66 52 60 44 48 46 70 62];
3 sigmaA = 40;
4 sigmaB = 100;
5 alpha = 1 -0.95;
6 beta = alpha /2;
7 meanA = mean ( A ) ;
8 meanB = mean ( B ) ;
9 zbeta = cdfnor ( ”X” ,0 , 1 , 1 - beta , beta ) ;
10
11 lowerlim = mean ( A ) - mean ( B ) - ( zbeta * sqrt (( sigmaA /
length ( A ) ) + ( sigmaB / length ( B ) ) ) ) ;
12 upperlim = mean ( A ) - mean ( B ) + ( zbeta * sqrt (( sigmaA /
length ( A ) ) + ( sigmaB / length ( B ) ) ) ) ;
13 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
interval is ” )
14
15 beta = alpha ;
16 zbeta = cdfnor ( ”X” ,0 , 1 , 1 - beta , beta ) ;
17
18 upperlim = mean ( A ) - mean ( B ) + ( zbeta * sqrt (( sigmaA /
length ( A ) ) + ( sigmaB / length ( B ) ) ) ) ;
19 disp ( upperlim , ”A v a l u e t h a t e x c e e d t h e d i f f e r e n c e
o f t h e means w i t h 95% c o n f i d e n c e i s ” )

57
Scilab code Exa 7.4b Battery production

1 tech1 = [140 136 138 150 152 144 132 142 150 154 136
142];
2 tech2 = [144 132 136 140 128 150 130 134 130 146 128
131 137 135];
3 num1 = 12;
4 num2 = 14;
5 mean1 = mean ( tech1 ) ;
6 mean2 = mean ( tech2 ) ;
7 // d i s p ( mean1 )
8 // d i s p ( Sp )
9 alpha = 0.9;
10 S1 = variance ( tech1 ) // ∗num1 / ( num1−1) ;
11 S2 = variance ( tech2 ) // ∗num2 / ( num2−1) ;
12 Sp = ((( num1 -1) * S1 ) + (( num2 -1) * S2 ) ) /( num1 + num2 -2)
;
13 Sp = sqrt ( Sp ) ;
14 num = (1/ num1 ) +(1/ num2 ) ;
15 betaa = (1 - alpha ) /2;
16 tval = cdft ( ”T” , num1 + num2 -2 , 1 - betaa , betaa ) ;
17 upperlim = mean1 - mean2 + ( tval * Sp ) * sqrt ( num ) ;
18 lowerlim = mean1 - mean2 - ( tval * Sp ) * sqrt ( num ) ;
19 disp ( upperlim , ” t o ” , lowerlim , ” The 90% c o n f i d e n c e
interval is ” )
20 alpha = 0.95
21 betaaa = 1 - alpha ;
22 tval = cdft ( ”T” , num1 + num2 -2 , 1 - betaaa , betaaa ) ;
23 lowerlim = mean1 - mean2 - ( tval * Sp ) * sqrt ( num ) ;
24 disp ( ” t h e u p p e r c o n f i d e n c e i n t e r v a l i s ” )
25 disp ( ” t o i n f i n i t y ” , lowerlim )

58
Scilab code Exa 7.5a Transistors

1 phat = 0.8;
2 zalpha = 1.96;
3
4 samplesize = 100;
5 lowerlim = phat - ( zalpha * sqrt ( phat *(1 - phat ) /
samplesize ) ) ;
6 upperlim = phat + ( zalpha * sqrt ( phat *(1 - phat ) /
samplesize ) ) ;
7 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
interval is ” )

Scilab code Exa 7.5b Survey

1 phat = 0.52;
2 error = 0.04;
3 zalpha = 1.96;
4 // l o w e r l i m = p h a t − ( z a l p h a ∗ s q r t ( p h a t ∗(1 − p h a t ) /
samplesize ) ) ;
5 // u p p e r l i m = p h a t + ( z a l p h a ∗ s q r t ( p h a t ∗(1 − p h a t ) /
samplesize ) ) ;
6 samplesize = ( error / zalpha ) ^2/( phat *(1 - phat ) ) ;
7 disp (1/ samplesize )

Scilab code Exa 7.5c Acceptable chips

1 initialsample = 30;
2 acceptable = 26;
3 phat = acceptable / initialsample ;
4 error = 0.05/2;
5 zalpha = 2.58;
6

59
7 samplesize = ( error / zalpha ) ^2/( phat *(1 - phat ) ) ;
8 finalsize = ceil (1/ samplesize ) ;
9 acceptablenew = 1040 + acceptable ;
10 phat = acceptablenew / finalsize ;
11 lowerlim = phat - ( zalpha * sqrt ( phat *(1 - phat ) /
finalsize ) ) ;
12 upperlim = phat + ( zalpha * sqrt ( phat *(1 - phat ) /
finalsize ) ) ;
13 disp ( upperlim , ” t o ” , lowerlim , ” The 99% c o n f i d e n c e
interval is ” )

Scilab code Exa 7.6a Life of a product

1 sum_lives = 1740;
2 num = 10;
3 alpha = (1 -0.95) /2;
4 chi1 = cdfchi ( ”X” , (2* num ) , alpha , 1 - alpha ) ;
5 chi2 = cdfchi ( ”X” , (2* num ) , 1 - alpha , alpha ) ;
6 // d i s p ( c h i 2 )
7 lowerlim = 2* sum_lives / chi2 ;
8 upperlim = 2* sum_lives / chi1 ;
9 disp ( upperlim , ” t o ” , lowerlim , ” The 95% c o n f i d e n c e
interval is ” )
10
11 // The c o n f i d e n c e i n t e r v a l i s from 1 0 1 . 8 4 7 t o 3 6 0 . 2 1 1
w h e r e a s my s o l u t i o n i n S c i l a b i s 1 0 1 . 8 4 4 8 9 t o
12 362.8485 because of the difference in the value of
chi - square (0.975 , 20) . The textbook says the
value is
13 9.661 whereas scilab calculates its value as 9.59

Scilab code Exa 7.7a Point estimator

60
1 function result1 = estimator1 ( X )
2 result1 = X (1) ;
3 // r e s u l t 2 = mean (X) ;
4
5 endfunction
6 function result2 = estimator2 ( X )
7 // r e s u l t 1 = X( 1 ) ;
8 result2 = mean ( X ) ;
9
10 endfunction

Scilab code Exa 7.7b Point estimator

1 function result1 = estimate (d , sigma )


2 sigmainv = 1/ sigma ;
3 new = d ./ sigma ;
4 result1 = sum ( new ) / sum ( sigmainv ) ;
5 endfunction
6
7 function result2 = mserror ( sigma )
8 sigmainv = 1/ sigma ;
9
10 result1 = 1/ sum ( sigmainv ) ;
11 endfunction

Scilab code Exa 7.7c Point estimator of a uniform distribution

1 function result = unbiasedestimator (X , n )


2 c =( n +2) /( n +1) ;
3 result = c * max ( X ) ;
4 endfunction

61
Scilab code Exa 7.8a Bayes estimator

1 function result = estimator (X , n )


2 result = ( sum ( X ) +1) /( n +2) ;
3 endfunction

Scilab code Exa 7.8b Bayes estimator of a normal population

1 function result = meanestimator ( sigma0 , u , sigma , n ,


X)
2 meanX = mean ( X ) ;
3 result = ( n * meanX / sigma0 ) /(( n / sigma0 ) +(1/ sigma ) )
+ ( u / sigma ) /(( n / sigma0 ) +(1/ sigma ) ) ;
4 endfunction
5
6 function result = varestimator ( sigma0 , sigma , n )
7 result = ( sigma0 * sigma ) /(( n * sigma ) + sigma0 ) ;
8 endfunction

Scilab code Exa 7.8d estimator of the signal value

1 function result = meanestimator ( sigma0 , u , sigma , n ,


X)
2 meanX = mean ( X ) ;
3 result = ( n * meanX / sigma0 ) /(( n / sigma0 ) +(1/ sigma ) )
+ ( u / sigma ) /(( n / sigma0 ) +(1/ sigma ) ) ;
4 endfunction
5
6 function result = varestimator ( sigma0 , sigma , n )

62
7 result = ( sigma0 * sigma ) /(( n * sigma ) + sigma0 ) ;
8 endfunction
9
10 u = 50;
11 sigma = 100;
12 sigma0 = 60;
13 n =1;
14 X =40;
15 expec = meanestimator ( sigma0 , u , sigma , n , X ) ;
16 var = varestimator ( sigma0 , sigma , n ) ;
17 // d i s p ( e x p e c ) ;
18 // d i s p ( v a r ) ;
19
20 zalpha = 1.645
21 lowerlim = -1* sqrt ( var ) * zalpha + expec ;
22 upperlim = sqrt ( var ) * zalpha + expec ;
23 disp ( upperlim , ” t o ” , lowerlim , ” With p r o b a b i l i t y 0 . 9 ,
the s e n t s i g n a l l i e s between ”, )

63
Chapter 8

Hypothesis Testing

Scilab code Exa 8.3a Noise in a Signal

1 noise_var = 4;
2 noise_mean = 0;
3 num = 5;
4 Xbar = 9.5;
5 u = 8;
6 statistic = sqrt ( num / noise_var ) *( Xbar - u ) ;
7 compare = cdfnor ( ”X” , 0 , 1 , 0.975 , 0.025) ;
8 if ( statistic < compare )
9 disp ( ” H y p o t h e s i s i s a c c e p t e d ” ) ;
10 else
11 disp ( ” H y p o t h e s i s i s n o t a c c e p t e d ” )
12 end

Scilab code Exa 8.3b Error in a signal

1 noise_var = 4;
2 noise_mean = 0;
3 num = 5;

64
4 Xbar = 8.5;
5 u = 8;
6 statistic = sqrt ( num / noise_var ) *( Xbar - u ) ;
7
8 prob = 2* cdfnor ( ”PQ” , -1* statistic , 0 ,1 ) ;
9 disp ( prob , ”P−v a l u e i s ” )

Scilab code Exa 8.3c Error in a signal

1 noise_var = 4;
2 num = 5;
3 Xbar = 10;
4 u = 8;
5 statistic = sqrt ( num / noise_var ) *( Xbar - u ) ;
6 compare = cdfnor ( ”X” , 0 , 1 , 0.975 , 0.025) ;
7 lim1 = statistic + compare ;
8 lim2 = statistic - compare ;
9 prob = cdfnor ( ”PQ” , lim1 , 0 ,1 ) - cdfnor ( ”PQ” , lim2
, 0 ,1 ) ;
10 disp ( prob )

Scilab code Exa 8.3d Number of signals to be sent

1 alpha = 0.025;
2 betaa = 0.25;
3
4 u1 = 9.2;
5 uo = 8;
6 var =4;
7 zalpha = cdfnor ( ”X” , 0 , 1 , 1 - alpha , alpha ) ;
8 zbeta = cdfnor ( ”X” , 0 , 1 , 1 - betaa , betaa ) ;
9 // d i s p ( z a l p h a ) ;
10 n = (( zalpha + zbeta ) /( u1 - uo ) ) ^2 * var ;

65
11 disp ( ceil ( n ) , ” R e q u i r e d number o f s a m p l e s i s ” )
12 statistic = sqrt ( ceil ( n ) / var ) *( u1 - uo ) ;
13 // d i s p ( s t a t i s t i c ) ;
14 lim1 = -1* statistic + zalpha ;
15 lim2 = -1* statistic - zalpha ;
16 // d i s p ( l i m 1 )
17 // d i s p ( l i m 2 )
18 prob = cdfnor ( ”PQ” , lim1 , 0 ,1 ) - cdfnor ( ”PQ” , lim2
, 0 ,1 ) ;
19 disp (1 - prob , ” Thus , i f t h e m e s s a g e i s s e n t t h e r e q d
number o f t i m e s i s , t h e n t h e p r o b a b i l i t y t h a t
t h e n u l l h y p o t h e s i s w i l l be r e j e c t e d i s ” )

Scilab code Exa 8.3e Number of signals to be sent

1 n =5;
2 Xbar = 9.5;
3 uo = 8;
4 var = 4;
5 statistic = sqrt ( n / var ) *( Xbar - u ) ;
6 p = 1 - cdfnor ( ”PQ” , statistic , 0 , 1) ;
7 disp ( ” The t e s t would c a l l f o r r e j e c t i o n a t a l l
s i g n i f i c a n c e l e v e l s g r e a t e r than or e q u a l to ”)
8 disp ( p ) ;

Scilab code Exa 8.3f Nicotine content in a cigarette

1 n =20;
2 Xbar = 1.54;
3 uo = 1.6;
4 sd = 0.8;
5 statistic = sqrt ( n ) *( Xbar - uo ) / sd ;
6 disp ( statistic , ” T e s t s t a t i s t i c i s ” )

66
7 p = cdfnor ( ”PQ” , statistic , 0 , 1) ;
8 disp (p , ”P−v a l u e i s ” )

Scilab code Exa 8.3g Blood cholestrol level

1 n = 50;
2 Xbar = 14.8;
3 S = 6.4;
4 T = sqrt ( n ) * Xbar / S ;
5 disp (T , ” The T v a l u e i s ” )

Scilab code Exa 8.3h Water usage

1 X = [340 356 332 362 318 344 386 402 322 360 362 354
340 372 338 375 364 355 324 370];
2 uo = 350;
3 Xbar = mean ( X ) ;
4 var = variance ( X ) ;
5 S = sqrt ( var )
6 // d i s p ( Xbar , s q r t ( v a r ) ) ;
7 n = length ( X )
8 T = sqrt ( n ) *( Xbar - uo ) / S ;
9 Tvalue = cdft ( ”T” , n -1 , 0.95 , 0.05 ) ;
10 // d i s p ( T v a l u e )
11 disp (T , ” The T v a l u e i s ” )
12 if (T < Tvalue )
13 disp ( ” N u l l h y p o t h e s i s i s a c c e p t e d a t 10% l e v e l
of s i g n i f i c a n c e ”)
14 else
15 disp ( ” N u l l h y p o t h e s i s i s n o t a c c e p t e d a t 10%
l e v e l of s i g n i f i c a n c e ”)
16 end

67
Scilab code Exa 8.3i Life of a tire

1 X = [36.1 40.2 33.8 38.5 42 35.8 37 41 36.8 37.2 33


36];
2 n = length ( X ) ;
3 uo = 40;
4 Xbar = mean ( X ) ;
5 sd = sqrt ( variance ( X ) ) ;
6 T = sqrt ( n ) *( Xbar - uo ) / sd ;
7 Tvalue = cdft ( ”T” , n -1 , 0.05 , 0.95 ) ;
8 // d i s p ( T v a l u e )
9 disp (T , ” The T v a l u e i s ” )
10 if (T < Tvalue )
11 disp ( ” N u l l h y p o t h e s i s i s r e j e c t e d a t 5% l e v e l o f
s i g n i f i c a n c e ”)
12 else
13 disp ( ” N u l l h y p o t h e s i s i s a c c e p t e d a t 5% l e v e l
of s i g n i f i c a n c e ”)
14 end

Scilab code Exa 8.3j Service Time

1 X = [8.6 9.4 5.0 4.4 3.7 11.4 10.0 7.6 14.4 12.2
11.0 14.4 9.3 10.5 10.3 7.7 8.3 6.4 9.2 5.7 7.9
9.4 9.0 13.3 11.6 10.0 9.5 6.6];
2 n = length ( X ) ;
3 uo = 8;
4 Xbar = mean ( X ) ;
5 sd = sqrt ( variance ( X ) ) ;
6 T = sqrt ( n ) *( Xbar - uo ) / sd ;
7 disp (T , ” The t e s t s t a t i s t i c i s ” )
8 p = 1 - cdft ( ”PQ” , T , n -1) ;

68
9 disp (p , ”P−v a l u e i s ” )
10 disp ( ”A s m a l l p v a l u e i n d i c a t e s t h a t t h e mean
s e r v i c e time exceeds 8 minutes ”)

Scilab code Exa 8.4a Tire lives

1 A = [61.1 58.2 62.3 64 59.7 66.2 57.8 61.4 62.2


63.6];
2 B = [62.2 56.6 66.4 56.2 57.4 58.4 57.6 65.4];
3 uA = mean ( A ) ;
4 uB = mean ( B ) ;
5 varA = 40^2;
6 varB =60^2;
7 n = length ( A ) ;
8 m = length ( B ) ;
9 den = sqrt (( varA / n ) + ( varB / m ) ) ;
10 statistic = ( uA - uB ) / den ;
11 disp ( statistic , ” The t e s t s t a t i s t i c i s ” ) ;
12 disp ( ”A s m a l l v a l u e o f t h e t e s t s t a t i s t i c i n d i c a t e s
that the n u l l h y p o t h e s i s i s accepted ”)

Scilab code Exa 8.4b Medicine for cold

1 X = [5.5 6.0 7.0 6.0 7.5 6.0 7.5 5.5 7.0 6.5];
2 Y = [6.5 6.0 8.5 7.0 6.5 8.0 7.5 6.5 7.5 6.0 8.5 7.0
];
3 n = length ( X ) ;
4 m = length ( Y ) ;
5 Xbar = mean ( X ) ;
6 Ybar = mean ( Y ) ;
7 Sx = variance ( X ) ;
8 Sy = variance ( Y ) ;
9 Sp = (( n -1) * Sx /( n +m -2) ) + (( m -1) * Sy /( n +m -2) ) ;

69
10 den = sqrt ( Sp *((1/ n ) +(1/ m ) ) ) ;
11 TS = ( Xbar - Ybar ) / den ;
12 disp ( TS , ” The t e s t s t a t i s t i c i s ” ) ;
13 tvalue = cdft ( ”T” , m +n -2 , 0.95 , 0.05)
14 // d i s p ( t v a l u e )
15 if ( TS < tvalue )
16 disp ( ” N u l l h y p o t h e s i s i s r e j e c t e d a t 5% l e v e l of
s i g n i f i c a n c e ”)
17 else
18 disp ( ” N u l l h y p o t h e s i s i s a c c e p t e d a t 5% l e v e l
of s i g n i f i c a n c e ”)
19 end

Scilab code Exa 8.4c Unknown population variance

1 A = [61.1 58.2 62.3 64 59.7 66.2 57.8 61.4 62.2


63.6];
2 B = [62.2 56.6 66.4 56.2 57.4 58.4 57.6 65.4];
3 uA = mean ( A ) ;
4 uB = mean ( B ) ;
5 n = length ( A ) ;
6 m = length ( B ) ;
7 Sx = variance ( A ) ;
8 Sy = variance ( B ) ;
9 Sp = (( n -1) * Sx /( n +m -2) ) + (( m -1) * Sy /( n +m -2) ) ;
10 den = sqrt ( Sp *((1/ n ) +(1/ m ) ) ) ;
11 TS = ( uA - uB ) / den ;
12 disp ( TS , ” The t e s t s t a t i s t i c i s ” ) ;
13 pvalue = 2*(1 - cdft ( ”PQ” , TS , m +n -2) ) ;
14 // d i s p ( t v a l u e )
15 disp ( pvalue , ” N u l l h y p o t h e s i s i s a c c e p t e d a t any
s i g n i f i c a n c e l e v e l l e s s than ”)

70
Scilab code Exa 8.4d effectiveness of safety program

1 A = [30.5 18.5 24.5 32 16 15 23.5 25.5 28 18];


2 B = [23 21 22 28.5 14.5 15.5 24.5 21 23.5 16.5];
3 n = length ( A ) ;
4 W = B-A;
5 Wbar = mean ( W ) ;
6 S = sqrt ( variance ( W ) ) ;
7 T = sqrt ( n ) * Wbar / S ;
8
9 disp (T , ” The t e s t s t a t i s t i c i s ” ) ;
10 pvalue = cdft ( ”PQ” , T , n -1) ;
11 // d i s p ( t v a l u e )
12 disp ( pvalue , ” The p v a l u e i s ” )

Scilab code Exa 8.5a effectiveness of machine

1 n =20;
2 S2 = 0.025;
3 chk = 0.15;
4 compare = (n -1) * S2 /( chk ^2) ;
5 pvalue = 1 - cdfchi ( ”PQ” , compare , n -1) ;
6 disp ( pvalue , ” The p−v a l u e i s ” )
7 disp ( ” Thus , t h e n u l l h y p o t h e s i s i s a c c e p t e d ” )

Scilab code Exa 8.5b Catalyst

1 S1 = 0.14;
2 S2 = 0.28;
3 n = 10;
4 m = 12;
5 ratio = S1 / S2 ;
6 prob1 = cdff ( ”PQ” , ratio , n -1 , m -1) ;

71
7 prob2 = 1 - prob1 ;
8 prob = min ([ prob1 prob2 ]) ;
9 pvalue = 2* prob ;
10 disp ( pvalue , ” The p v a l u e i s ” )
11 disp ( ” So t h e h y p o t h e s i s o f e q u a l v a r i a n c e c a n n o t be
r e j e c t e d ”)

Scilab code Exa 8.6a Computer chip manufacturing

1 samplesize = 300;
2 p =0.02;
3 defective =9;
4 val = 1 - cdfbin ( ”PQ” , defective , samplesize , p , 1 - p )
;
5 disp ( val , ”P0 . 0 2 { X>10} = ” ) ;
6 disp ( ” M a n u f a c t u r e r s c l a i m c a n n o t be r e j e c t e d a t t h e
5% l e v e l o f s i g n i f i c a n c e ” )

Scilab code Exa 8.6b Finding p value

1 samplesize = 300;
2 p =0.02;
3 defective =9;
4 compare = 10;
5 npo = samplesize * p ;
6 sd = sqrt ( npo *(1 - p ) ) ;
7 tol = 0.5;
8 pvalue = 1 - cdfnor ( ”PQ” , compare - tol , npo , sd ) ;
9 disp ( pvalue , ” The p v a l u e i s ” )

72
Scilab code Exa 8.6c Change in manufacturing pattern

1 samplesize = 500;
2 p =0.04;
3 defective =16;
4 prob1 = cdfbin ( ”PQ” , defective , samplesize , p , 1 - p )
5 prob2 = 1 - cdfbin ( ”PQ” , defective -1 , samplesize , p ,
1-p);
6 pvalue = 2* min ([ prob1 prob2 ]) ;
7 disp ( pvalue , ” The p v a l u e i s ” )

Scilab code Exa 8.7a Mean number of defective chips

1 x = [28 34 32 38 22];
2 claim = 25;
3 total = sum ( x ) ;
4 pval = 1 - cdfpoi ( ”PQ” , total -1 , ( claim * length ( x ) ) ) ;
5 disp ( pval , ” The p v a l u e i s ” )

Scilab code Exa 8.7b Safety Conditions in a plant

1 plant1 = [16 18 9 22 17 19 24 8];


2 plant2 = [22 18 26 30 25 28];
3 X1 = sum ( plant1 ) ;
4 X2 = sum ( plant2 ) ;
5 n = length ( X1 ) ;
6 m = length ( X2 ) ;
7 // d i s p ( X1 , X2 , X1+X2 )
8 prob1 = 1 - cdfbin ( ”PQ” , X1 -1 , X1 + X2 , (4/7) , (3/7) ) ;
9 prob2 = cdfbin ( ”PQ” , X1 , X1 + X2 , 4/7 , 3/7 ) ;
10 disp ( prob1 , prob2 )
11 pvalue = 2* min ([ prob1 prob2 ]) ;
12 disp ( pvalue , ” The p v a l u e i s ” )

73
Scilab code Exa 8.7c Better proof reader

1 Aerror =28;
2 Berror = 18;
3 common =10;
4 N2 = Aerror - common ;
5 N3 = Berror - common ;
6 pval = 1 - cdfbin ( ”PQ” , N2 -1 , N2 + N3 , 0.5 , 0.5) ;
7 disp ( pval , ”P−v a l u e i s ” )

74
Chapter 9

Regression

Scilab code Exa 9.1a Scatter Diagram

1 X = [100 110 120 130 140 150 160 170 180 190];
2 Y = [45 52 54 63 62 68 75 76 92 88];
3 plot2d (X , Y , -1) ;
4 disp ( ”A l i n e a r r e g r e s s i o n model s e e m s a p p r o p r i a t e ” )

Scilab code Exa 9.2a Relative humidity and moisture content

1 A = [46 53 29 61 36 39 47 49 52 38 55 32 57 54 44];
2 B = [12 15 7 17 10 11 11 12 14 9 16 8 18 14 12];
3 plot2d (A , B , -1) ;
4 [X , Y ] = reglin (A , B ) ;
5 // d i s p (X) ;
6 // d i s p (Y) ;
7 p = 0 : 0.1: 65;
8 q = p .* X + Y

75
Figure 9.1: Scatter Diagram

76
Figure 9.2: Relative humidity and moisture content

77
Figure 9.3: Moisture against Density

9 plot2d (p , q , 2) ;

Scilab code Exa 9.3a Moisture against Density

1 x = [5 6 7 10 12 15 18 20];
2 y = [7.4 9.3 10.6 15.4 18.1 22.2 24.1 24.8];
3 plot2d (x ,y , -1) ;
4
5 xbar = mean ( x ) ;
6 ybar = mean ( y ) ;
7 n = 8;

78
8 SxY = 0;
9 for i = 1: n
10 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
11 end
12
13 Sxx = 0;
14 for i =1: n
15 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
16 end
17 SYY = 0;
18 for i =1: n
19 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
20 end
21 B = SxY / Sxx ;
22 A = ybar - ( B * xbar ) ;
23 disp (A , ”A i s ” ) ;
24 disp (B , ”B i s ” ) ;
25 p = 0:0.1: 20;
26 q= A + B*p;
27 plot2d (p ,q ,2) ;
28
29 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
30 disp ( SSR , ” The SSR i s ” )

Scilab code Exa 9.4a Effect of speed on mileage

1 x = [45 50 55 60 65 70 75];
2 y = [24.2 25.0 23.3 22.0 21.5 20.6 19.8];
3 xbar = mean ( x ) ;
4 ybar = mean ( y ) ;
5 n = 7;
6 SxY = 0;
7 for i = 1: n
8 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
9 end

79
10
11 Sxx = 0;
12 for i =1: n
13 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
14 end
15 SYY = 0;
16 for i =1: n
17 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
18 end
19 B = SxY / Sxx ;
20 A = ybar - ( B * xbar ) ;
21 // d i s p (A, ”A i s ” ) ;
22 // d i s p (B , ”B i s ” ) ;
23
24 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
25 // d i s p ( SSR , ” The SSR i s ” )
26 ts = sqrt ((( n -2) * Sxx ) / SSR ) * abs ( B ) ;
27 disp ( ts , ” t h e t e s t s t a t i s t i c i s ” ) ;
28 tvalue = cdft ( ”T” ,5 , 0.995 , 0.005 ) ;
29 // d i s p ( t v a l u e , ” t v a l u e i s ” ) ;
30 if ( tvalue < ts )
31 disp ( ” H y p o t h e s i s b e t a= 0 i s r e j e c t e d a t 1% l e v e l
of s i g n i f i c a n c e ”)
32 else
33 disp ( ” H y p o t h e s i s b e t a= 0 i s a c c e p t e d a t 1% l e v e l
of s i g n i f i c a n c e ”)
34 end

Scilab code Exa 9.4b Confidence interval estimate

1 x = [45 50 55 60 65 70 75];
2 y = [24.2 25.0 23.3 22.0 21.5 20.6 19.8];
3 xbar = mean ( x ) ;
4 ybar = mean ( y ) ;
5 n = 7;

80
6 SxY = 0;
7 for i = 1: n
8 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
9 end
10
11 Sxx = 0;
12 for i =1: n
13 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
14 end
15 SYY = 0;
16 for i =1: n
17 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
18 end
19 B = SxY / Sxx ;
20 A = ybar - ( B * xbar ) ;
21 // d i s p (A, ”A i s ” ) ;
22 // d i s p (B , ”B i s ” ) ;
23
24 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
25 // d i s p ( SSR , ” The SSR i s ” )
26
27 tvalue = cdft ( ”T” ,5 , 0.975 , 0.025 ) ;
28 k = sqrt ( SSR /(( n -2) * Sxx ) ) * tvalue ;
29 int1 = B + k ;
30 int2 = B - k ;
31 disp ( int2 , ” t o ” , int1 ,” The 95% c o n f i d e n c e i n t e r v a l
i s ” );

Scilab code Exa 9.4c Regression to the mean

1 x = [60 62 64 65 66 67 68 70 72 74];
2 y = [63.6 65.2 66 65.5 66.9 67.1 67.4 68.3 70.1 70];
3 plot2d (x ,y , -1) ;

81
Figure 9.4: Regression to the mean

82
4 xbar = mean ( x ) ;
5 ybar = mean ( y ) ;
6 n = 10;
7 SxY = 0;
8 for i = 1: n
9 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
10 end
11
12 Sxx = 0;
13 for i =1: n
14 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
15 end
16 SYY = 0;
17 for i =1: n
18 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
19 end
20 B = SxY / Sxx ;
21 A = ybar - ( B * xbar ) ;
22 // d i s p (A, ”A i s ” ) ;
23 // d i s p (B , ”B i s ” ) ;
24 p = 60:0.1: 72;
25 q= A + B*p;
26 plot2d (p ,q ,2) ;
27 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
28 ts = sqrt ((( n -2) * Sxx ) / SSR ) *( B -1)
29 // d i s p ( t s ) ;
30 tvalue = cdft ( ”T” ,n -2 , 0.99 , 0.01 ) ;
31 // d i s p ( t v a l u e ) ;
32 if ( ts <( -1* tvalue ) )
33 disp ( ” N u l l h y p o t h e i s i s r e j e c t e d a t 1% l e v e l o f
s i g n i f i c a n c e ”)
34 else
35 disp ( ” N u l l h y p o t h e i s i s a c c e p t e d a t 1% l e v e l o f
s i g n i f i c a n c e ”)
36 end

83
Scilab code Exa 9.4d Motor vehicle deaths

1 x = [121 96 85 113 102 118 90 84 107 112 95 101];


2 y = [104 91 101 110 117 108 96 102 114 96 88 106];
3
4 plot2d (x ,y , -1) ;
5 xlabel ( ” D e a t h s i n 1 9 8 8 ” ) ;
6 ylabel ( ” D e a t h s i n 1 9 8 9 ” ) ;
7
8 xbar = mean ( x ) ;
9 ybar = mean ( y ) ;
10 n = 12;
11 SxY = 0;
12 for i = 1: n
13 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
14 end
15
16 Sxx = 0;
17 for i =1: n
18 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
19 end
20 SYY = 0;
21 for i =1: n
22 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
23 end
24 B = SxY / Sxx ;
25 A = ybar - ( B * xbar ) ;
26 disp (A , ”A i s ” ) ;
27 disp (B , ”B i s ” ) ;

Scilab code Exa 9.4e Confidence interval for height

84
1 x = [60 62 64 65 66 67 68 70 72 74];
2 y = [63.6 65.2 66 65.5 66.9 67.1 67.4 68.3 70.1 70];
3 x0 = 68;
4 xbar = mean ( x ) ;
5 ybar = mean ( y ) ;
6 n = 10;
7 SxY = 0;
8 for i = 1: n
9 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
10 end
11 // d i s p ( SxY , ”SxY i s ” ) ;
12 Sxx = 0;
13 for i =1: n
14 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
15 end
16 // d i s p ( Sxx , ” Sxx i s ” ) ;
17 SYY = 0;
18 for i =1: n
19 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
20 end
21 // d i s p (SYY , ”SYY i s ” ) ;
22 B = SxY / Sxx ;
23 A = ybar - ( B * xbar ) ;
24 tvalue = cdft ( ”T” ,n -2 , 0.975 , 0.025 ) ;
25 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
26 // d i s p ( t v a l u e , ” t v a l u e i s ” ) ;
27 intvl = A + ( B * x0 ) ;
28 // d i s p ( i n t v l ) ;
29 change = sqrt ((1/ n ) +((( x0 - xbar ) ^2) / Sxx ) ) * sqrt ( SSR /(
n -2) ) * tvalue ;
30 intvl1 = intvl - change ;
31 intvl2 = intvl + change ;
32 disp ( intvl2 , ” t o ” , intvl1 ,” The 95% c o n f i d e n c e
i n t e r v a l i s ” );

85
Scilab code Exa 9.4f Confidence interval for height

1 x = [60 62 64 65 66 67 68 70 72 74];
2 y = [63.6 65.2 66 65.5 66.9 67.1 67.4 68.3 70.1 70];
3 x0 = 68;
4 xbar = mean ( x ) ;
5 ybar = mean ( y ) ;
6 n = 10;
7 SxY = 0;
8 for i = 1: n
9 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
10 end
11 // d i s p ( SxY , ”SxY i s ” ) ;
12 Sxx = 0;
13 for i =1: n
14 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
15 end
16 // d i s p ( Sxx , ” Sxx i s ” ) ;
17 SYY = 0;
18 for i =1: n
19 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
20 end
21 // d i s p (SYY , ”SYY i s ” ) ;
22 B = SxY / Sxx ;
23 A = ybar - ( B * xbar ) ;
24 tvalue = cdft ( ”T” ,n -2 , 0.975 , 0.025 ) ;
25 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
26 // d i s p ( t v a l u e , ” t v a l u e i s ” ) ;
27 intvl = A + ( B * x0 ) ;
28 // d i s p ( i n t v l ) ;
29 change = sqrt ((( n +1) / n ) +((( x0 - xbar ) ^2) / Sxx ) ) * sqrt (
SSR /( n -2) ) * tvalue ;
30 intvl1 = intvl - change ;
31 intvl2 = intvl + change ;
32 disp ( intvl2 , ” t o ” , intvl1 ,” The 95% c o n f i d e n c e
i n t e r v a l i s ” );

86
Scilab code Exa 9.5a Height of son and father

1 x = [60 62 64 65 66 67 68 70 72 74];
2 y = [63.6 65.2 66 65.5 66.9 67.1 67.4 68.3 70.1 70];
3
4 xbar = mean ( x ) ;
5 ybar = mean ( y ) ;
6 n = 10;
7 SxY = 0;
8 for i = 1: n
9 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
10 end
11
12 Sxx = 0;
13 for i =1: n
14 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
15 end
16 SYY = 0;
17 for i =1: n
18 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
19 end
20 B = SxY / Sxx ;
21 A = ybar - ( B * xbar ) ;
22
23 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
24 R2 = 1 - ( SSR / SYY ) ;
25 disp ( R2 , ” The c o e f f i c i e n t o f d e t e r m i n a t i o n i s ” )

87
Figure 9.5: Percentage of chemical used

Figure 9.6: Percentage of chemical used

88
Scilab code Exa 9.7a Percentage of chemical used

1 x = [5 10 20 30 40 50 60 80];
2 yold = [0.061 0.113 0.192 0.259 0.339 0.401 0.461
0.551];
3 plot2d (x , yold , -1) ;
4 y = -1* log (1 - yold ) ;
5 scf (2) ;
6 plot2d (x , y , -1) ;
7
8
9 xbar = mean ( x ) ;
10 ybar = mean ( y ) ;
11 n = 8;
12 SxY = 0;
13 for i = 1: n
14 SxY = SxY + ( x ( i ) * y ( i ) ) - ( xbar * ybar ) ;
15 end
16
17 Sxx = 0;
18 for i =1: n
19 Sxx = Sxx + ( x ( i ) * x ( i ) ) - ( xbar * xbar ) ;
20 end
21 SYY = 0;
22 for i =1: n
23 SYY = SYY + ( y ( i ) * y ( i ) ) - ( ybar * ybar ) ;
24 end
25 B = SxY / Sxx ;
26 A = ybar - ( B * xbar ) ;
27 // d i s p (A, ”A i s ” ) ;
28 // d i s p (B , ”B i s ” ) ;
29 SSR = (( Sxx * SYY ) - ( SxY * SxY ) ) / Sxx ;
30 chat = exp ( -1* A ) ;
31 dhat = 1 - exp ( -1* B ) ;
32 disp ( chat , ” c h a t i s ” ) ;
33 disp ( dhat , ” d h a t i s ” ) ;

89
Scilab code Exa 9.8b Distance vs Travel Time

1 x = [0.5 1 1.5 2 3 4 5 6 8 10];


2 y = [15 15.1 16.5 19.9 27.7 29.7 26.7 35.9 42 49.4];
3 for i =1:10
4 w ( i ) = 1/ x ( i ) ;
5 end
6 // d i s p (w)
7 n = 10;
8 p = zeros (2 ,2) ;
9 q = zeros (2 , 1) ;
10 p (1 , 1) = sum ( w ) ;
11 p (1 ,2) = n ;
12 p (2 ,1) = n ;
13 p (2 ,2) = sum ( x ) ;
14 for i =1:10
15 new ( i ) = w ( i ) * y ( i )
16 end
17
18 q (1 ,1) = -1* sum ( new ) ;
19 q (2 ,1) = -1* sum ( y ) ;
20 // d i s p ( p ) ;
21 // d i s p ( q ) ;
22 sol = linsolve (p , q ) ;
23 A = sol (1 ,1 ) ;
24 B = sol (2 ,1) ;
25 disp (A , ”A i s ” ) ;
26 disp (B , ”B i s ” ) ;

Scilab code Exa 9.9a Polynomial Fitting

90
Figure 9.7: Polynomial Fitting

91
1 x = 1:1:10;
2 y = [20.6 30.8 55 71.4 97.3 131.8 156.3 197.3 238.7
291.7];
3 plot2d (x , y , -1) ;
4 xlabel ( ’X ’ ) ;
5 ylabel ( ’Y ’ ) ;
6 n = length ( x )
7 xsquared = x .^2;
8 xcube = x .^3;
9 xfour = x .^4;
10 xy = x .* y ;
11 x2y = xy .* x ;
12 p = zeros (3 ,3) ;
13 q = zeros (3 ,1) ;
14 p (1 ,1) = n ;
15 p (1 ,2) = sum ( x ) ;
16 p (1 ,3) = sum ( xsquared ) ;
17 p (2 ,1) = sum ( x ) ;
18 p (2 ,2) = sum ( xsquared ) ;
19 p (2 ,3) = sum ( xcube ) ;
20 p (3 ,1) = sum ( xsquared ) ;
21 p (3 ,2) = sum ( xcube ) ;
22 p (3 ,3) = sum ( xfour ) ;
23 q (1 ,1) = -1* sum ( y ) ;
24 q (2 ,1) = -1* sum ( xy ) ;
25 q (3 ,1) = -1* sum ( x2y ) ;
26 B = linsolve (p , q ) ;
27 disp ( B (1 ,1) , ”B0 i s ” ) ;
28 disp ( B (2 ,1) , ”B1 i s ” ) ;
29 disp ( B (3 ,1) , ”B2 i s ” ) ;

Scilab code Exa 9.10a Multiple Linear Regression

1 x1 = [679 1420 1349 296 6975 323 4200 633];


2 x2 = [30.4 34.1 17.2 26.8 29.1 18.7 32.6 32.5];

92
3 y = ones (8 ,1) ;
4 y = [11.6 ;16.1; 9.3; 9.1; 8.4; 7.7; 11.3; 8.4];
5 x = ones (8 ,3) ;
6 for i =1:8
7 x (i ,2) = x1 ( i ) ;
8 x (i ,3) = x2 ( i ) ;
9 end
10
11 pro1 = x ’;
12 // d i s p ( p r o 1 ) ;
13
14 pro2 = pro1 * x ;
15 // d i s p ( p r o 2 ) ;
16 pro3 = inv ( pro2 ) ;
17 // d i s p ( p r o 3 ) ;
18 pro4 = pro3 * pro1 ;
19 pro5 = pro4 * y ;
20 // d i s p ( p r o 4 ) ;
21 // d i s p ( y ) ;
22 B = ones (3 ,1) ;
23 for i =1:3
24 B (i ,1) = 0;
25 for k =1:8
26 B (i ,1) = B (i ,1) +( pro4 (i , k ) * y (k , 1) ) ;
27 end
28 end
29 disp ( B ) ;
30 //SSR = y ’ ∗ y − B ’ ∗ x ’ y ;
31 SSR = y ’;
32 SSR = SSR * y ;
33 sub = B ’;
34 sub = sub *x ’;
35 sub = sub * y ;
36 SSR = SSR - sub ;
37 disp ( SSR , ” SSr i s ” ) ;

93
Scilab code Exa 9.10b Estimate of variance

1 x1 = [679 1420 1349 296 6975 323 4200 633];


2 x2 = [30.4 34.1 17.2 26.8 29.1 18.7 32.6 32.5];
3 y = ones (8 ,1) ;
4 n = 8;
5 k =2;
6 y = [11.6 ;16.1; 9.3; 9.1; 8.4; 7.7; 11.3; 8.4];
7 x = ones (8 ,3) ;
8 for i =1:8
9 x (i ,2) = x1 ( i ) ;
10 x (i ,3) = x2 ( i ) ;
11 end
12
13 pro1 = x ’;
14 // d i s p ( p r o 1 ) ;
15
16 pro2 = pro1 * x ;
17 // d i s p ( p r o 2 ) ;
18 pro3 = inv ( pro2 ) ;
19 // d i s p ( p r o 3 ) ;
20 pro4 = pro3 * pro1 ;
21 pro5 = pro4 * y ;
22 // d i s p ( p r o 4 ) ;
23 // d i s p ( y ) ;
24 B = ones (3 ,1) ;
25 for i =1:3
26 B (i ,1) = 0;
27 for k =1:8
28 B (i ,1) = B (i ,1) +( pro4 (i , k ) * y (k , 1) ) ;
29 end
30 end
31 // d i s p (B) ;
32 //SSR = y ’ ∗ y − B ’ ∗ x ’ y ;

94
33 SSR = y ’;
34 SSR = SSR * y ;
35 sub = B ’;
36 sub = sub *x ’;
37 sub = sub * y ;
38 SSR = SSR - sub ;
39 // d i s p ( SSR , ” SSr i s ” ) ;
40 k =2;
41 den = n -k -1;
42 disp ( den )
43 sigma = SSR / den ;
44 disp ( sigma , ” The v a r i a n c e i s ” )

Scilab code Exa 9.10c Diameter of a tree

1 x1 = [44 33 33 32 34 31 33 30 34 34 33 36 33 34 37];
2 x2 = [1.3 2.2 2.2 2.6 2.0 1.8 2.2 3.6 1.6 1.5 2.2 1.7
2.2 1.3 2.6];
3 x3 = [250 115 75 85 100 75 85 75 225 250 255 175 75
85 90];
4 x4 = [0.63 0.59 0.56 0.55 0.54 0.59 0.56 0.46 0.63
0.60 0.63 0.58 0.55 0.57 0.62 ];
5 y = [18.1; 19.6; 16.6; 16.4; 16.9 ;17.0; 20.0; 16.6;
16.2; 18.5 ; 18.7; 19.4; 17.6; 18.3; 18.8];
6 n = length ( x1 ) ;
7 x = ones (15 , 5) ;
8 for i =1:15
9 x (i ,2) = x1 ( i ) ;
10 x (i ,3) = x2 ( i ) ;
11 x (i ,4) = x3 ( i ) ;
12 x (i ,5) = x4 ( i ) ;
13 end
14 pro1 = x ’;
15 // d i s p ( p r o 1 ) ;
16 pro2 = pro1 * x ;

95
17 // d i s p ( p r o 2 ) ;
18 pro3 = inv ( pro2 ) ;
19 // d i s p ( p r o 3 ) ;
20 pro4 = pro3 * pro1 ;
21 pro5 = pro4 * y ;
22
23 for i =1:5
24 B (i ,1) = 0;
25 for k =1:15
26 B (i ,1) = B (i ,1) +( pro4 (i , k ) * y (k , 1) ) ;
27 end
28 end
29 SSR = y ’;
30 SSR = SSR * y ;
31 sub = B ’;
32 sub = sub *x ’;
33 sub = sub * y ;
34 SSR = SSR - sub ;
35 // d i s p ( SSR ) ;
36 // d i s p (B ( 2 ) )
37 xxinv = 0.379;
38 k = 4;
39 ts = sqrt (( n -k -2) / SSR ) * B (2) /0.616;
40 pvalue = 2*(1 - cdft ( ”PQ” ,ts , n -k -2 ) ) ;
41 disp ( pvalue , ” The p−v a l u e i s ” )
42
43 The SSR calculated by scilab is 19.34 whereas the
textbook gives the value as 19.26 , thus the
44
45 difference in the final answer .

Scilab code Exa 9.10d Estimating hardness

1 y =[79.2 ;64.0; 55.7; 56.3; 58.6; 84.3; 70.4; 61.3;


51.3; 49.8];

96
2 x1 = [0.02 0.03 0.03 0.04 0.10 0.15 0.15 0.09 0.13
0.09];
3 x2 = [1.05 1.20 1.25 1.30 1.30 1.00 1.10 1.20 1.40
1.40];
4 tvalue = 2.365;
5 x = ones (10 ,3) ;
6 for i =1:10
7 x (i ,2) = x1 ( i ) ;
8 x (i ,3) = x2 ( i ) ;
9 end
10
11 pro1 = x ’;
12 // d i s p ( p r o 1 ) ;
13
14 pro2 = pro1 * x ;
15 // d i s p ( p r o 2 ) ;
16 pro3 = inv ( pro2 ) ;
17 // d i s p ( p r o 3 ) ;
18 pro4 = pro3 * pro1 ;
19 pro5 = pro4 * y ;
20 // d i s p ( p r o 4 ) ;
21 // d i s p ( y ) ;
22 B = ones (3 ,1) ;
23 for i =1:3
24 B (i ,1) = 0;
25 for k =1:10
26 B (i ,1) = B (i ,1) +( pro4 (i , k ) * y (k , 1) ) ;
27 end
28 end
29 // d i s p (B) ;
30 //SSR = y ’ ∗ y − B ’ ∗ x ’ y ;
31 SSR = y ’;
32 SSR = SSR * y ;
33 sub = B ’;
34 sub = sub *x ’;
35 sub = sub * y ;
36 SSR = SSR - sub ;
37 disp ( SSR , ” SSr i s ” ) ;

97
38 smallx = [1 , 0.15 , 1.15];
39 product = smallx * B ;
40 // d i s p ( p r o d u c t ) ;
41 n = 10;
42 k =2;
43 val = sqrt ( SSR /( n -k -1) ) ;
44 // d i s p ( v a l ) ;
45
46 pro5 = smallx * pro3 ;
47 pro6 = pro5 * smallx ’;
48 pro7 = val * sqrt ( pro6 ) * tvalue ;
49 // d i s p ( p r o 7 )
50 up = product + pro7 ;
51 low = product - pro7 ;
52 disp ( ” 95% c o n f i d e n c e i n t e r v a l i s from ” ) ;
53 disp ( up , ” t o ” , low ) ;

Scilab code Exa 9.11a Animal fsickalling

1 cancer = 84;
2 total = 111;
3 level = 250;
4 alpha = -1* log (( total - cancer ) / total ) / level ;
5 disp ( alpha , ” Alpha i s ” )

98
Chapter 10

Analysis of Variance

Scilab code Exa 10.3a Dependence of mileage on gas used

1 Xij = [220 251 226 246 260; 244 235 232 242 225; 252
272 250 238 256];
2 Xi = zeros (3 ,1) ;
3 n = 5;
4 m =3;
5 for i =1:3
6 for j =1:5
7 Xi ( i ) = Xi ( i ) + Xij (i , j ) ;
8 end
9 end
10 Xi = Xi / n ;
11 SSW = 0;
12 for i =1:3
13 for j = 1:5
14 SSW = SSW + (( Xij (i , j ) - Xi ( i ) ) ^2)
15 end
16 end
17 sigma1 = SSW /(( n * m ) -m ) ;
18 Xdotdot = sum ( Xi ) / m ;
19 new = ( Xi - Xdotdot ) ^2;
20 SSb = n * sum ( new ) ;

99
21 sigma2 = SSb /( m -1) ;
22 TS = sigma2 / sigma1 ;
23 // d i s p ( s i g m a 1 ) ;
24 // d i s p ( s i g m a 2 ) ;
25 disp ( TS , ” V a l u e o f t h e t e s t s t a t i s t i c i s ” ) ;
26 pvalue = 1 - cdff ( ”PQ” , TS ,m -1 , (( n * m ) -m ) ) ;
27 disp ( pvalue , ” The p−v a l u e i s ” )
28 if ( pvalue >0.05)
29 disp ( ” S i n c e t h e p−v a l u e i s g r e a t e r t h a n . 0 5 , t h e
n u l l h y p o t h e s i s t h a t t h e mean m i l e a g e i s t h e same
f o r a l l 3 b r a n d s o f g a s o l i n e c a n n o t be r e j e c t e d .
”)
30 end

Scilab code Exa 10.3b Dependence of mileage on gas used

1 Xijold = [220 251 226 246 260; 244 235 232 242 225;
252 272 250 238 256];
2 Xij = Xijold - 220;
3 m =3;
4 n =5;
5 Xidot = zeros (3 ,1) ;
6 for i =1: m
7 for j =1: n
8 Xidot ( i ) = Xidot ( i ) + Xij (i , j ) ;
9 end
10 end
11 Xidot = Xidot / n ;
12 Xdotdot = sum ( Xidot ) / m ;
13 SSb =0;
14 for i =1: m
15 SSb = SSb + ( Xidot ( i ) - Xdotdot ) ^2;
16 end
17 SSb = SSb * n ;
18 Xijsquared = Xij .^2;

100
19 SSW = sum ( Xijsquared ) - ( m * n *( Xdotdot ^2) ) - SSb ;
20 sigma1 = SSW /(( n * m ) -m ) ;
21 sigma2 = SSb /( m -1) ;
22 TS = sigma2 / sigma1 ;
23 disp ( TS , ” V a l u e o f t h e t e s t s t a t i s t i c i s ” ) ;

Scilab code Exa 10.3c Difference in GPA

1 Xij = [3.2 3.4 3.3 3.5; 3.4 3.0 3.7 3.3; 2.8 2.6 3.0
2.7];
2 Xi = zeros (3 ,1) ;
3 n = 4;
4 m =3;
5 for i =1:3
6 for j =1:4
7 Xi ( i ) = Xi ( i ) + Xij (i , j ) ;
8 end
9 end
10 Xi = Xi / n ;
11 SSW = 0;
12 for i =1:3
13 for j = 1:4
14 SSW = SSW + (( Xij (i , j ) - Xi ( i ) ) ^2)
15 end
16 end
17 sigma1 = SSW /(( n * m ) -m ) ;
18 Xdotdot = sum ( Xi ) / m ;
19 new = ( Xi - Xdotdot ) ^2;
20 SSb = n * sum ( new ) ;
21 sigma2 = SSb /( m -1) ;
22 TS = sigma2 / sigma1 ;
23 // d i s p ( s i g m a 1 ) ;
24 // d i s p ( s i g m a 2 ) ;
25 disp ( TS , ” V a l u e o f t h e t e s t s t a t i s t i c i s ” ) ;
26 pvalue = 1 - cdff ( ”PQ” , TS ,m -1 , (( n * m ) -m ) ) ;

101
27 disp ( pvalue , ” The p−v a l u e i s ” )
28 C = 3.95; // from t a b l e A5
29 W = C * sqrt ( SSW /(9*4) ) ;
30 disp ( W ) ;
31 disp ( Xi (1) - Xi (2) + W ,” and ” , Xi (1) - Xi (2) -W , ” Mean1 −
Mean2 l i e s b e t w e e n ” );
32 disp ( Xi (1) - Xi (3) + W ,” and ” , Xi (1) - Xi (3) -W , ” Mean1 −
Mean3 l i e s b e t w e e n ” );
33 disp ( Xi (2) - Xi (3) + W ,” and ” , Xi (2) - Xi (3) -W , ” Mean2 −
Mean3 l i e s b e t w e e n ” );

Scilab code Exa 10.4b Estimating Parameters

1 X =[75 73 60 70 86; 78 71 64 72 90; 80 69 62 70 85;


73 67 63 80 92 ];
2 Xidot = zeros (4 ,1) ;
3 for i =1:4
4 for j =1:5
5 Xidot ( i ) = Xidot ( i ) + X (i , j ) ;
6 end
7 end
8 Xidot = Xidot /5;
9 Xjdot = zeros (5 ,1) ;
10 for j =1:5
11 for i =1:4
12 Xjdot ( j ) = Xjdot ( j ) + X (i , j ) ;
13 end
14 end
15 Xjdot = Xjdot /4;
16 Xdotdot = sum ( Xidot ) /4;
17 // d i s p ( Xdotdot )
18 meanhat = Xdotdot ;
19 alphahat = Xidot - meanhat ;
20 betahat = Xjdot - meanhat ;
21 disp ( meanhat , ” The e s t i m a t o r o f t h e mean i s ” ) ;

102
22 disp ( ” The a l p h a s a r e −” )
23 disp ( alphahat )
24 disp ( ” The b e t a s a r e −” )
25 disp ( betahat )

Scilab code Exa 10.5a Species collected

1 X = [53 35 31 37 40 43; 36 34 17 21 30 18; 47 37 17


31 45 26; 55 31 17 23 43 37; 40 32 19 26 45 37;
52 42 20 27 26 32; 39 28 21 21 36 28; 40 32 21 21
36 35];
2 m = 8;
3 n = 6;
4 Xidot = zeros (8 ,1) ;
5 for i =1:8
6 for j =1:6
7 Xidot ( i ) = Xidot ( i ) + X (i , j ) ;
8 end
9 end
10 Xidot = Xidot /6;
11 Xjdot = zeros (6 ,1) ;
12 for j =1:6
13 for i =1:8
14 Xjdot ( j ) = Xjdot ( j ) + X (i , j ) ;
15 end
16 end
17 Xjdot = Xjdot /8;
18 Xdotdot = sum ( Xidot ) /8;
19 new = ( Xidot - Xdotdot ) ^2;
20 SSr = n * sum ( new ) ;
21 new1 = ( Xjdot - Xdotdot ) ^2;
22 SSc = m * sum ( new1 ) ;
23 SSe = 0;
24 for i =1: m
25 for j =1: n

103
26 SSe = SSe + ( X (i , j ) - Xidot ( i ) - Xjdot ( j ) +
Xdotdot ) ^2;
27 end
28 end
29 N =( m -1) *( n -1) ;
30 TS1 = SSr * N /(( m -1) * SSe ) ;
31 TS2 = SSc * N /(( n -1) * SSe ) ;
32 pvaluec = 1 - cdff ( ”PQ” , TS1 , m -1 , N ) ;
33 pvaluer = 1 - cdff ( ”PQ” , TS2 , n -1 , N ) ;
34 // d i s p ( p v a l u e r , p v a l u e c ) ;
35 // d i s p ( TS1 , TS2 ) ;
36 disp ( TS1 , ” The v a l u e o f t h e F− s t a t i s t i c f o r t e s t i n g
t h a t t h e r e i s no row e f f e c t i s ” ) ;
37 disp ( pvaluec , ” The p−v a l u e f o r t e s t i n g t h a t t h e r e i s
no row e f f e c t i s ” ) ;
38
39 disp ( TS2 , ” The v a l u e o f t h e F− s t a t i s t i c f o r t e s t i n g
t h a t t h e r e i s no column e f f e c t i s ” ) ;
40 disp ( pvaluer , ” The p−v a l u e f o r t e s t i n g t h a t t h e r e i s
no column e f f e c t i s ” ) ;

104
Chapter 11

Goodness of Fit Tests and


Categorical Data Analysis

Scilab code Exa 11.2a Relation between death date and birth date

1 X = [90 100 87 96 101 86 119 118 121 114 113 106];


2 pi = ones (12 ,1) ;
3 pi = pi /12;
4 new = X .^2;
5 npi = sum ( X ) * pi ;
6 T = sum ( new ) ;
7 T = T / npi ;
8 T = T - sum ( X ) ;
9 disp ( ”When t h e r e a r e 12 r e g i o n s ” )
10 disp ( T (1) , ” The t e s t s t a t i s t i c i s ” )
11 pvalue = 1 - cdfchi ( ”PQ” ,T (1) , 11) ;
12 disp ( pvalue , ” The p v a l u e i s ” )
13
14 X = [277 283 358 333];
15 pi = ones (4 ,1) ;
16 pi = pi /4;
17 new = X .^2;
18 npi = sum ( X ) * pi ;
19 T = sum ( new ) ;

105
20 T = T / npi ;
21 T = T - sum ( X ) ;
22 disp ( ”When t h e r e a r e 4 r e g i o n s ” )
23 disp ( T (1) , ” The t e s t s t a t i s t i c i s ” )
24 pvalue = 1 - cdfchi ( ”PQ” ,T (1) , 3) ;
25 disp ( pvalue , ” The p v a l u e i s ” )

Scilab code Exa 11.2b Quality of bulbs

1 X = [3 6 9 7 5];
2 p = [0.15 0.25 0.35 0.20 0.05];
3 T = 0;
4 n3 = sum ( X ) ;
5 np = p * n3 ;
6 Xsqu = (X - np ) .^2;
7 disp ( Xsqu ) ;
8 XT = Xsqu ./ np ;
9 T = sum ( XT ) ;
10
11 //T = T − sum (X) ;
12 // d i s p ( ” When t h e r e a r e 12 r e g i o n s ” )
13 disp (T , ” The t e s t s t a t i s t i c i s ” )
14 pvalue = 1 - cdfchi ( ”PQ” ,T (1) , 4) ;
15 // a= c d f c h i ( ”PQ” ,T ( 1 ) , 4 ) ;
16
17 disp ( pvalue , ” The p v a l u e i s ” )
18 disp ( ” Thus , t h e h y p o t h e s i s would n o t be r e j e c t e d a t
5% l e v e l o f s i g n i f i c a n c e ” )

Scilab code Exa 11.2d Six outcomes

1 X = [3 3 5 18 4 7];
2 p = [0.1 0.1 0.05 0.4 0.2 0.15];

106
3 psimu = 0.1843; // p−v a l u e o b t a i n e d by s i m u l a t i o n
4 num = 10000;
5 T = 0;
6 n = sum ( X ) ;
7 np = n * p ;
8 Xsqu = X .^2;
9 for i = 1:6
10 T = T + ( Xsqu ( i ) / np ( i ) ) ;
11 end
12 T = T - sum ( X ) ;
13
14 disp ( T (1) , ” The t e s t s t a t i s t i c i s ” )
15 pvalue = 1 - cdfchi ( ”PQ” ,T (1) , 5) ;
16 // d i s p ( p v a l u e , ” The p v a l u e i s ” )
17 int1 = psimu - (1.645* sqrt ( psimu *(1 - psimu ) / num ) ) ;
18 int2 = psimu + (1.645* sqrt ( psimu *(1 - psimu ) / num ) ) ;
19 disp ( ” With 90% c o n f i d e n c e p−v a l u e l i e s b e t w e e n ” )
20 disp ( int1 )
21 disp ( ” and ” )
22 disp ( int2 ) ;

Scilab code Exa 11.3a Weekly accidents

1 Y = [8 0 0 1 3 4 0 2 12 5 1 8 0 2 0 1 9 3 4 5 3 3 4
7 4 0 1 2 1 2];
2 weeks = 30;
3 lamda = sum ( Y ) / weeks ;
4 p = zeros (5 ,1) ;
5 p (1) = cdfpoi ( ”PQ” , 0 , lamda ) ;
6 p (2) = cdfpoi ( ”PQ” , 1 , lamda ) - p (1) ;
7 p (3) = cdfpoi ( ”PQ” , 3 , lamda ) - cdfpoi ( ”PQ” , 1 ,
lamda ) ;
8 p (4) = cdfpoi ( ”PQ” , 5 , lamda ) - cdfpoi ( ”PQ” , 3 ,
lamda ) ;
9 p (5) = 1 - cdfpoi ( ”PQ” , 5 , lamda ) ;

107
10 // d i s p ( p ) ;
11 X = zeros (5 ,1) ;
12 for i =1:30
13 if ( Y ( i ) ==0)
14 X (1) = X (1) +1;
15 end
16 if ( Y ( i ) ==1)
17 X (2) = X (2) +1;
18 end
19 if ( Y ( i ) ==2)
20 X (3) = X (3) +1;
21 end
22 if ( Y ( i ) ==3)
23 X (3) = X (3) +1;
24 end
25 if ( Y ( i ) ==4)
26 X (4) = X (4) +1;
27 end
28 if ( Y ( i ) ==5)
29 X (4) = X (4) +1;
30 end
31 if ( Y ( i ) >5)
32 X (5) = X (5) +1;
33 end
34 end
35 // d i s p (X) ;
36 T = 0;
37 npi = weeks * p ;
38 for i =1:5
39 T = T + (( X ( i ) - npi ( i ) ) ^2) / npi ( i ) ;
40 end
41 disp (T , ”T i s ” ) ;
42 pvalue = 1 - cdfchi ( ”PQ” , T , 3) ;
43 disp ( pvalue , ” The p−v a l u e i s ” )
44 disp ( ” H y p o t h e s i s o f an u n d e r l y i n g p o i s s o n
d i s t r i b u t i o n i s r e j e c t e d ”)

108
Scilab code Exa 11.4a Ploitical affiliation and Gender

1 Nij = [68 56 32; 52 72 20];


2 n = sum ( Nij ) ;
3 Ni = zeros (2 ,1) ;
4 Mj = zeros (3 ,1) ;
5 for i = 1:2
6 for j = 1:3
7 Ni ( i ) = Ni ( i ) + Nij (i , j ) ;
8 end
9 end
10 for j = 1:3
11 for i = 1:2
12 Mj ( j ) = Mj ( j ) + Nij (i , j ) ;
13 end
14 end
15 NM = ones (2 ,3) ;
16 for i =1:2
17 for j =1:3
18 NM (i , j ) = Ni ( i ) * Mj ( j ) ;
19 end
20 end
21 NM = NM / n ;
22 // d i s p (NM) ;
23 TS = 0
24 for i =1:2
25 for j = 1:3
26 TS = TS + (( Nij (i , j ) - NM (i , j ) ) ^2) / NM (i , j ) ;
27 end
28 end
29 disp ( TS , ” The t e s t s t a t i s t i c i s ” )
30 compare = cdfchi ( ”X” , 2 , 0.95 , 0.05) ;
31 // d i s p ( compare )
32 if ( TS > compare )

109
33 disp ( ” The n u l l h y p o t h e s i s i s r e j e c t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
34 else
35 disp ( ” The n u l l h y p o t h e s i s i s a c c e p t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
36 end

Scilab code Exa 11.4b Machine Breakdown and shift

1 Nij = [68 56 32; 52 72 20];


2 n3 = sum ( Nij ) ;
3 Ni = zeros (2 ,1) ;
4 Mj = zeros (3 ,1) ;
5 for i = 1:2
6 for j = 1:3
7 Ni ( i ) = Ni ( i ) + Nij (i , j ) ;
8 end
9 end
10 for j = 1:3
11 for i = 1:2
12 Mj ( j ) = Mj ( j ) + Nij (i , j ) ;
13 end
14 end
15 NM = ones (2 ,3) ;
16 for i =1:2
17 for j =1:3
18 NM (i , j ) = Ni ( i ) * Mj ( j ) ;
19 end
20 end
21 NM = NM / n3 ;
22 // d i s p (NM) ;
23 TS = 0
24 for i =1:2
25 for j = 1:3
26 TS = TS + (( Nij (i , j ) - NM (i , j ) ) ^2) / NM (i , j ) ;

110
27 end
28 end
29 disp ( TS , ” The t e s t s t a t i s t i c i s ” )
30 compare = cdfchi ( ”X” , 2 , 0.95 , 0.05) ;
31 // d i s p ( compare )
32 if ( TS > compare )
33 disp ( ” The n u l l h y p o t h e s i s i s r e j e c t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
34 else
35 disp ( ” The n u l l h y p o t h e s i s i s a c c e p t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
36 end

Scilab code Exa 11.5a Lung cancer and smoking

1 Nij = [62 14; 9938 19986];


2 n = sum ( Nij ) ;
3 Ni = zeros (2 ,1) ;
4 Mj = zeros (2 ,1) ;
5 for i = 1:2
6 for j = 1:2
7 Ni ( i ) = Ni ( i ) + Nij (i , j ) ;
8 end
9 end
10 for j = 1:2
11 for i = 1:2
12 Mj ( j ) = Mj ( j ) + Nij (i , j ) ;
13 end
14 end
15 NM = ones (2 ,2) ;
16 for i =1:2
17 for j =1:2
18 NM (i , j ) = Ni ( i ) * Mj ( j ) ;
19 end
20 end

111
21 NM = NM / n ;
22 disp ( NM ) ;
23 TS = 0
24 for i =1:2
25 for j = 1:2
26 TS = TS + (( Nij (i , j ) - NM (i , j ) ) ^2) / NM (i , j ) ;
27 end
28 end
29 disp ( TS , ” The t e s t s t a t i s t i c i s ” )
30 compare = cdfchi ( ”X” , 1 , 0.99 , 0.01) ;
31 // d i s p ( compare )
32 if ( TS > compare )
33 disp ( ” The n u l l h y p o t h e s i s i s r e j e c t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
34 else
35 disp ( ” The n u l l h y p o t h e s i s i s a c c e p t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
36 end

Scilab code Exa 11.5b Females reporting abuse

1 Nij = [28 30 58 55; 472 470 442 445];


2 n = sum ( Nij ) ;
3 Ni = zeros (2 ,1) ;
4 Mj = zeros (4 ,1) ;
5 for i = 1:2
6 for j = 1:4
7 Ni ( i ) = Ni ( i ) + Nij (i , j ) ;
8 end
9 end
10 for j = 1:4
11 for i = 1:2
12 Mj ( j ) = Mj ( j ) + Nij (i , j ) ;
13 end
14 end

112
15 NM = ones (2 ,4) ;
16 for i =1:2
17 for j =1:4
18 NM (i , j ) = Ni ( i ) * Mj ( j ) ;
19 end
20 end
21 NM = NM / n ;
22 // d i s p (NM) ;
23 TS = 0
24 for i =1:2
25 for j = 1:4
26 TS = TS + (( Nij (i , j ) - NM (i , j ) ) ^2) / NM (i , j ) ;
27 end
28 end
29 disp ( TS , ” The t e s t s t a t i s t i c i s ” )
30 compare = cdfchi ( ”X” , 3 , 0.99 , 0.01) ;
31 pvalue = 1 - cdfchi ( ”PQ” , TS , 3) ;
32 disp ( pvalue , ” The p−v a l u e i s ” )
33 // d i s p ( compare )
34 if ( TS > compare )
35 disp ( ” The n u l l h y p o t h e s i s i s r e j e c t e d a t t h e 1%
l e v e l o f s i g n i f i c a n c e ”);
36 else
37 disp ( ” The n u l l h y p o t h e s i s i s a c c e p t e d a t t h e 5%
l e v e l o f s i g n i f i c a n c e ”);
38 end

Scilab code Exa 11.6a Testing distribution of a population

1 X = [66 72 81 94 112 116 124 140 145 155];


2 D = 0.4831487;
3 n = 10;
4 Dgiven = 1.480;
5 Dstar = ( sqrt ( n ) + 0.12 + (0.11/ sqrt ( n ) ) ) * D ;
6 disp ( Dstar , ” D s t a r i s ” ) ;

113
7 if ( Dstar > Dgiven )
8 disp ( ” N u l l h y p o t h e s i s i s r e j e c t e d a t 2 . 5% l e v e l
of s i g n i f i c a n c e ”)
9 else
10 disp ( ” N u l l h y p o t h e s i s i s a c c e p t e d a t 2 . 5% l e v e l
of s i g n i f i c a n c e ”)
11 end

114
Chapter 12

Non parametric Hypothesis


Tests

Scilab code Exa 12.2a testing the median

1 n = 200;
2 v = 120;
3 p =0.5;
4 if ( v < ( n /2) )
5 pvalue = 2* cdfbin ( ”PQ” , v , n , p ,1 - p ) ;
6 else
7 pvalue = 2* cdfbin ( ”PQ” , n -v , n , p ,1 - p ) ;
8
9 end
10 disp ( pvalue , ” P v a l u e i s ” ) ;

Scilab code Exa 12.2b testing the median

1 n = 80;
2 v = 28;
3 p =0.5;

115
4
5 pvalue = cdfbin ( ”PQ” , v , n , p ,1 - p ) ;
6 disp ( pvalue , ” P v a l u e i s ” ) ;
7 disp ( ” Thus , t h e n u l l h y p o t h e s i s t h a t t h e median
i n c o m e i s l e s s t h a n o r e q u a l t o $90 , 0 0 0 i s
r e j e c t e d ”)

Scilab code Exa 12.3b Signed Rank Test

1 n =4;
2 mo = 2;
3 X = [4.2 1.8 5.3 1.7];
4 t =3;
5 tstar = min (t , ( n *( n +1) /2) - t ) ;
6 P = zeros (4 ,4) ;
7 P (1 ,1) = 0.5;
8 P (1 ,2) = 1;
9 P (1 ,3) = 1;
10 P (1 ,4) = 1;
11 for i =2:4
12 for j = 1:4
13 if (j - i <1)
14 P (i , j ) = 0.5* P (i -1 , j ) ;
15 // d i s p ( j , i ) ;
16 // d i s p (P( i , j ) )
17
18 else
19 P (i , j ) = 0.5*( P (i -1 ,j - i ) + P (i -1 , j ) ) ;
20 end
21 end
22 end
23 disp ( P )

116
Scilab code Exa 12.3c Determining Population Distribution

1 n =20;
2 t =142;
3 tstar = min (t , ( n *( n +1) /2) - t ) ;
4 P = ones (20 , tstar +1) ;
5 P (1 ,1) = 0.5;
6 P (1 ,2) = 1;
7 for i =2:20
8 for j = 1: tstar +1 if (j - i <1)
9 P (i , j ) = 0.5* P (i -1 , j ) ;
10 // d i s p ( j , i ) ;
11 // d i s p (P( i , j ) )
12
13 else
14 P (i , j ) = 0.5*( P (i -1 ,j - i ) + P (i -1 , j ) ) ;
15 end
16 end
17 end
18 // d i s p (P)
19 pvalue = 2* P (20 , tstar +1) ;
20 disp ( pvalue , ” P v a l u e i s ” )

Scilab code Exa 12.4a Treatments against corrosion

1 X = [65.2 67.1 69.4 78.2 74 80.3];


2 Y = [59.4 72.1 68 66.2 58.5];
3 Z = [ X Y ];
4 Z = gsort (Z , ’ g ’ , ’ i ’ ) ;
5 [ m n ]= size ( X ) ;
6 [ p q ] = size ( Z )
7 T = 0;
8 for i =1: n
9 test = X ( i ) ;
10 for j =1 : q

117
11 if ( test == Z ( j ) )
12 T = T+ j;
13 end
14 end
15 end
16 disp (T , ” The t e s t s t a t i s t i c i s ”)

Scilab code Exa 12.4b Determining P

1 function result = prob (N , M , K )


2 if ( N ==1 & M ==0)
3 if ( K >0)
4 result = 1;
5 else
6 result =0;
7 end
8
9 elseif ( N ==0 & M ==1)
10 if ( K <0)
11 result = 0;
12 else
13 result =1;
14 end
15 elseif ( N ==0 & M ==0 & K ==0)
16 result =1;
17 else
18 result = ( prob (N -1 , M , K -N - M ) * N /( N + M ) ) + (
prob (N , M -1 , K ) * M /( N + M ) ) ;
19 // r e s u l t = p r o b (N−1 , M, K−N−M) ∗N/ (N+M) +
p r o b (N, M−1 , K) ;
20 // r e s u l t = r e s u l t + p r o b (N, M−1 , K) ;
21 end
22 endfunction
23
24 function result = pval (n ,m , t )

118
25 result = 2* min ( prob (n ,m , t ) , 1 - prob (n ,m ,t -1) ) ;
26 endfunction

Scilab code Exa 12.4c Finding p value

1 ans = pval (5 ,6 ,21) ;


2 disp ( ans )

Scilab code Exa 12.4d Comparing production methods

1 ans = pval (9 ,13 ,72) ;


2 disp ( ans )

Scilab code Exa 12.4e Determining p value

1 n1 =5;
2 m1 = 6;
3
4 t1 =21;
5 num1 = n1 *( n1 + m1 +1) /2;
6 d1 = abs ( t1 - num1 ) ;
7 val = d1 / sqrt ( n1 * m1 *( n1 + m1 +1) /12) ;
8 // d i s p ( d1 , ” d i s ” )
9 // d i s p ( v a l , ” v a l i s ” )
10 pval = 2*(1 - cdfnor ( ”PQ” , val , 0 ,1) ) ;
11 disp ( pval , ” The p−v a l u e f o r e g 1 2 . 4 a i s ” )
12 n2 =9;
13 m2 = 13;
14 t2 =72;
15 d2 = abs ( t2 - n2 *( n2 + m2 +1) /2) ;

119
16
17 val = d2 / sqrt ( n2 * m2 *( n2 + m2 +1) /12) ;
18 pval = 2*(1 - cdfnor ( ”PQ” , val , 0 ,1) ) ;
19 disp ( pval , ” The p−v a l u e f o r e g 1 2 . 4 d i s ” )

Scilab code Exa 12.5a Testing randomness

1 function result = fact ( num )


2 if ( num <=0)
3 result = 1
4 else
5 result = factorial ( num )
6 end
7 endfunction
8 function result = proba (n ,m , k )
9 if ( pmodulo (k ,2) ==0)
10 k = k /2;
11 result = 2* fact (m -1) * fact (n -1) * fact ( n ) * fact (
m ) /( fact (k -1) ^2* fact (m - k ) * fact (n - k ) * fact (
n+m));
12 else
13 k = (k -1) /2;
14 result = fact (m -1) * fact (n -1) * fact ( n ) * fact ( m
) /( fact (k -1) * fact ( k ) * fact (m - k ) * fact (n -k
-1) * fact ( n + m ) ) + fact (m -1) * fact (n -1) *
fact ( n ) * fact ( m ) /( fact (k -1) * fact ( k ) * fact (
m -k -1) * fact (n - k ) * fact ( n + m ) ) ;
15 end
16 endfunction
17
18 r1 = 20;
19 n1 = 20;
20 m1 =10;
21 ans1 =0;
22 for i =1:19

120
23 ans1 = ans1 + proba ( n1 , m1 , i ) ;
24 // d i s p ( p r o b a ( n , m, i ) ) ;
25 // d i s p ( a n s 1 )
26 end
27 if ( ans1 <0.5)
28 pvalue1 = 2* ans1 ;
29 else
30 pvalue1 = 2*(1 - ans1 ) ;
31 end
32 disp ( pvalue1 , ”P−v a l u e i s ” )

Scilab code Exa 12.5c Determining p value

1 u = 61;
2 sigma = 5.454;
3 r =75;
4 val = cdfnor ( ”PQ” , (r - u ) / sigma , 0 ,1) ;
5 if ( val >0.5)
6 pvalue = 2*(1 - val ) ;
7 else
8 pvalue = 2* val ;
9 end
10 disp ( pvalue , ”P−v a l u e i s ” ) ;

121
Chapter 13

Quality Control

Scilab code Exa 13.2a Steel shaft diameter

1 X = [3.01 2.97 3.12 2.99 3.03 3.02 3.10 3.14 3.09


3.20];
2 Y = 1:1:10;
3 u = 3;
4 sigma = 0.1;
5 n =4;
6 ucl = u + (3* sigma / sqrt ( n ) ) ;
7 lcl = u - (3* sigma / sqrt ( n ) ) ;
8 Z = 0.1:0.1:10;
9 P = ones (1 ,100) ;
10 Q = ones (1 ,100) ;
11 P = P * ucl ;
12 Q = Q * lcl ;
13 plot2d (Y , X , -2) ;
14 plot2d (Z , P , 1) ;
15 plot2d (Z , Q , 1) ;
16 // d i s p ( s i z e ( Z ) ) ;
17 // d i s p ( s i z e (P) ) ;
18 disp ( ucl , ’ u c l i s ’ ) ;
19 disp ( lcl , ’ l c l i s ’ )

122
Scilab code Exa 13.2b unknown mean and variance

1 Xbar = [3.01 2.97 3.12 2.99 3.03 3.02 3.10 3.14 3.09
3.20];
2 S = [0.12 0.14 0.08 0.11 0.09 0.08 0.15 0.16 0.13
0.16];
3 c = [0.7978849 0.8862266 0.9213181 0.9399851
0.9515332 0.9593684 0.9650309 0.9693103
0.9726596];
4 n =4;
5 Xbarbar = mean ( Xbar ) ;
6 Sbar = mean ( S ) ;
7 lcl = Xbarbar - (3* Sbar /( sqrt ( n ) * c (n -1) ) ) ;
8 ucl = Xbarbar + (3* Sbar /( sqrt ( n ) * c (n -1) ) ) ;
9 // d i s p ( l c l , ”LCL i s ” )
10 // d i s p ( u c l , ”UCL i s ” )
11 u = Xbarbar ;
12 sigma = Sbar / c (n -1) ;
13 // d i s p ( u ) ;
14 // d i s p ( s i g m a ) ;
15 // d i s p ( Sbar , c ( 4 ) ) ;
16 prob = cdfnor ( ”PQ” , 3.1 , u , sigma ) - cdfnor ( ”PQ” ,
2.9 , u , sigma ) ;
17 disp ( prob *100 , ” P e r c e n t a g e o f t h e i t e m s t h a t w i l l
meet t h e s p e c i f i c a t i o n s i s ” )

Scilab code Exa 13.3a determining control limits

123
Figure 13.1: determining control limits

124
Figure 13.2: determining control limits

1 Xbar = [35.1 33.2 31.7 35.4 34.5 36.4 35.9 38.4


35.7 27.2 38.1 37.6 38.8 34.3 43.2 41.3 35.7 36.3
35.4 34.6];
2 S = [4.2 4.4 2.5 3.2 2.6 4.5 3.4 5.1 3.8 6.2 4.2 3.9
3.2 4 3.5 8.2 8.1 4.2 4.1 3.7];
3 c = [0.7978849 0.8862266 0.9213181 0.9399851
0.9515332 0.9593684 0.9650309 0.9693103
0.9726596];
4 Y = 1:1:20;
5 n =5;
6 Z = 0.1:0.1:20;
7 Xbarbar = mean ( Xbar ) ;
8 Sbar = mean ( S ) ;
9 lclX = Xbarbar - (3* Sbar /( sqrt ( n ) * c (n -1) ) ) ;
10 uclX = Xbarbar + (3* Sbar /( sqrt ( n ) * c (n -1) ) ) ;
11 val1 = 1/ c (n -1) ;
12 val1 = val1 ^2;
13 val1 = val1 - 1;

125
14 val = sqrt ( val1 ) ;
15 // v a l = s q r t ( ( 1 / c ( n−1) ˆ 2 ) ) − 1 ;
16 ucls = Sbar *(1+(3* val ) ) ;
17 lcls = Sbar *(1 -(3* val ) ) ;
18 // d i s p ( u c l s , l c l s )
19 plot2d (Y , Xbar , -2) ;
20 P = ones (1 , 200) ;
21 Q = ones (1 , 200) ;
22 P = P * lclX ;
23 Q = Q * uclX ;
24 disp ( uclX , ’UCL(X)= ’ ) ;
25 disp ( lclX , ’LCL(X)= ’ ) ;
26 plot2d (Z , P , 1) ;
27 plot2d (Z , Q , 1) ;
28 title ( ’ C o n t r o l Chart f o r X ’ )
29 scf (2) ;
30 disp ( uclX , ’UCL( S )= ’ ) ;
31 disp ( lclX , ’LCL( S )= ’ ) ;
32 // d i s p ( u c l s , l c l s )
33 plot2d (Y , S , -2) ;
34 P = P * lcls / lclX ;
35 Q = Q * ucls / uclX ;
36 plot2d (Z , P , 1) ;
37 plot2d (Z , Q , 1) ;
38 title ( ’ C o n t r o l Chart f o r S ’ )

Scilab code Exa 13.4a Defectives Screws

1 defect = [6 5 3 0 1 2 1 0 2 1 1 3 2 0 1 1 0 2 1 2];
2 F = [0.12 0.10 0.06 0.00 0.02 0.04 0.02 0.00 0.04
0.02 0.02 0.06 0.04 0.00 .02 0.02 0.00 0.04 0.02
0.04];
3 total = 1000;
4 Fbar = sum ( defect ) / total ;
5 n =50;

126
6 val = sqrt ( Fbar *(1 - Fbar ) / n ) ;
7 lcl = Fbar - (3* val ) ;
8 ucl = Fbar + (3* val ) ;
9 disp ( lcl , ”LCL i s ” ) ;
10 disp ( ucl , ”UCL i s ” ) ;
11 for i = 1:20
12 if ( F ( i ) > ucl | F ( i ) < lcl )
13 totald = sum ( defect ) - defect ( i ) ;
14 // t o t a l = t o t a l −50;
15 end
16 end
17 // d i s p ( t o t a l d ) ;
18 total = total - 50;
19 Fbar = totald / total ;
20 val = sqrt ( Fbar *(1 - Fbar ) / n ) ;
21 // d i s p ( Fbar ) ;
22 disp ( ” A f t e r r e c o m p u t a t i o n ” ) ;
23 lcl = Fbar - (3* val ) ;
24 ucl = Fbar + (3* val ) ;
25 disp ( lcl , ”LCL i s ” ) ;
26 disp ( ucl , ”UCL i s ” ) ;

Scilab code Exa 13.5a Control during production of cars

1 X = [141 162 150 111 92 74 85 95 76 68 63 74 103 81


94 68 95 81 102 73];
2 total = sum ( X ) ;
3 num = 20;
4 Xbar = mean ( X ) ;
5 lcl = Xbar - 3* sqrt ( Xbar ) ;
6 ucl = Xbar + 3* sqrt ( Xbar ) ;
7 disp ( ucl , ”UCL i s ” ) ;
8 disp ( lcl , ”LCL i s ” ) ;
9 for i =1:20
10 if ( X ( i ) > ucl )

127
11 total = total - X ( i ) ;
12 num = num -1;
13 end
14 end
15 Xbar = total / num ;
16
17 lcl = Xbar - 3* sqrt ( Xbar ) ;
18 ucl = Xbar + 3* sqrt ( Xbar ) ;
19 disp ( ” A f t e r r e c o m p u t a t i o n ” )
20 disp ( ucl , ”UCL i s ” ) ;
21 disp ( lcl , ”LCL i s ” ) ;
22 total = total - X (4) ;
23 num = num -1;
24 disp ( Xbar , ” Xbar i s ” ) ;
25 disp ( X (4) , ” i s ” ) ;
26 Xbar = total / num ;
27 lcl = Xbar - 3* sqrt ( Xbar ) ;
28 ucl = Xbar + 3* sqrt ( Xbar ) ;
29 disp ( ” A f t e r s e c o n d r e c o m p u t a t i o n ” )
30 disp ( ucl , ”UCL i s ” ) ;
31 disp ( lcl , ”LCL i s ” ) ;
32 disp ( Xbar , ” I t a p p e a r s t h a t t h e p r o c e s s i s i n
c o n t r o l w i t h mean ” ) ;
33
34 // The mean a f t e r t h e s e c o n d r e c o m p u t a t i o n i s
i n c o r e e c t l y c a l c u l a t e d in the textbook . I t should
be
35 ((17*84.41) -111 ) /16 = 82.748 whereas the value
given in the book is 82.56. The values of UCL and
LCL
36 change accordingly .

Scilab code Exa 13.6b Service Time

1 X = [48 52 70 62 57 81 56 59 77 82 78 80 74 82 68

128
84];
2 u = 62;
3 n = 4;
4 sigma = 24;
5 alpha = 0.25;
6 W = zeros (17) ;
7 W (1) = 60;
8 for i =2:17
9 W ( i ) = (0.25* X (i -1) ) + (0.75* W (i -1) ) ;
10 end
11 disp ( W , ” The v a l u e s o f W a r e ” )
12 val = 3* sigma * sqrt ( alpha /( n *(2 - alpha ) ) ) ;
13 lcl = u - val ;
14 ucl = u + val ;
15 disp ( lcl , ”LCL i s ” ) ;
16 disp ( ucl , ”UCL i s ” ) ;

Scilab code Exa 13.6c Exponentially weighted moving average control

1 X = [9.617728 10.25437 9.867195 10.79338 10.60699


10.48396 13.33961 9.462969 10.14556 11.66342
11.55484 11.26203 12.31473 9.220009 11.25206
10.48662 9.025091 9.693386 11.45989 12.44213
11.18981 11.56674 9.869849 12.11311 11.48656];
2 t = 1:1:26;
3 alpha = 2/9;
4 val = 9.915051 - ( alpha *9.617728) ;
5 val = val /(1 - alpha ) ;
6 disp ( val , ” v a l i s ” ) ;
7 u = 10;
8 n = 5;
9 sigma = 2;
10

129
Figure 13.3: Exponentially weighted moving average control

130
11 W = zeros (26) ;
12 W (1) = 10.;
13 for i =2:26
14 W ( i ) = ( alpha * X (i -1) ) + ((1 - alpha ) * W (i -1) ) ;
15 end
16 disp ( W , ” The v a l u e s o f W a r e ” ) ;
17 val = 3* sigma * sqrt ( alpha /( n *(2 - alpha ) ) ) ;
18 lcl = u - val ;
19 ucl = u + val ;
20 disp ( lcl , ”LCL i s ” ) ;
21 disp ( ucl , ”UCL i s ” ) ;
22 plot2d (t ,W , -2) ;
23 xlabel ( ” t ” ) ;
24 ylabel ( ”W” ) ;
25 nlcl = ones (1 , 26) ;
26 nlcl = nlcl .* lcl ;
27 plot2d (t , nlcl ) ;
28 nucl = ones (1 , 26) ;
29 nucl = nucl .* ucl ;
30 plot2d (t , nucl ) ;
31
32 // The a s y m p t p o t i c l i n e s f o r UCL and LCL have b e e n
plotted

Scilab code Exa 13.6d Finding control limit

1 X = [29 33 35 42 36 44 43 45];
2 u =30;
3 sig = 8;
4 d =0.5;
5 B =5;
6 Y = X - u - ( d * sig ) ;
7 S = zeros (9) ;
8 S (1) =0;
9 for i =2:9

131
10 S ( i ) = max ( S (i -1) + Y (i -1) , 0) ;
11 end
12 disp (S , ” S i s ” )
13 cl = B * sig ;
14 disp ( cl )
15 answer =100;
16 for i =1:9
17 if ( S ( i ) > cl )
18 answer = i ;
19 end
20 end
21 disp ( ” The mean h a s i n c r e a s e d a f t e r o b s e r v i n g t h e ” )
22 disp ( answer -1) ;
23 disp ( ” s u b g r o u p a v e r a g e ” ) ;

132
Chapter 14

Life Testing

Scilab code Exa 14.3a Lifetime of a transistor

1 total =50;
2 failure = 15;
3 alpha = 0.05;
4 t =525;
5 val1 = cdfchi ( ”X” , 2* failure , alpha /2 , 1 -( alpha /2) )
;
6 val2 = cdfchi ( ”X” , 2* failure , 1 - alpha /2 , ( alpha /2) )
;
7
8 int1 = 2* t / val1 ;
9 int2 = 2* t / val2 ;
10 disp ( ” The 95% c o n f i d e n c e i n t e r v a l i s ” ) ;
11 disp ( int2 ) ;
12 disp ( int1 , ” t o ” ) ;
13
14 // The c o n f i d e n c e i n t e r v a l i s from 2 2 . 3 5 t o 6 2 . 1 7
w h e r e a s my s o l u t i o n i n S c i l a b i s 2 2 . 3 5 t o 6 2 . 5 3
15 because of the difference in the value of chi - square
(0.975 , 30) . The textbook says the value is 16.89
16 whereas scilab calculates its value as 16.79

133
Scilab code Exa 14.3b Lifetime of Battery

1 t = 1800;
2 theta = 150;
3 r =20;
4 pvalue = cdfchi ( ”PQ” ,2* t / theta , 2* r ) ;
5 disp ( pvalue , ”P−v a l u e i s ” )

Scilab code Exa 14.3c One at a time sequential test

1 T = 500;
2 alpha = 0.05;
3 r = 10;
4 val1 = cdfchi ( ”X” , 2* r , 1 - alpha /2 , alpha /2) ;
5 val2 = cdfchi ( ”X” , 2* r , alpha /2 , 1 - alpha /2) ;
6 int1 = 2* T / val1 ;
7 int2 = 2* T / val2 ;
8 disp ( ” The 95% c o n f i d e n c e i n t e r v a l i s ” ) ;
9 disp ( int1 ) ;
10 disp ( int2 , ” t o ” ) ;
11
12 // The c o n f i d e n c e i n t e r v a l i s from 2 9 . 2 7 t o 1 0 3 . 5 2
w h e r e a s my s o l u t i o n i n S c i l a b i s 2 9 . 2 6 5 7 7 4 t o
13 104.26683 because of the difference in the value of
chi - square (0.975 , 30) . The textbook says the
value is
14 9.66 whereas scilab calculates its value as
9.5907774 .

134
Scilab code Exa 14.3d Lifetime of semiconductors

1 r = 30;
2 T = 600;
3 theta = 25;
4 val1 = cdfchi ( ”PQ” , 2* T / theta , 2* r ) ;
5 val2 = 1 - cdfchi ( ”PQ” , 2* T / theta , 2*( r +1) ) ;
6 pvalue = min ( val1 , val2 ) ;
7 disp ( pvalue , ” The p v a l u e i s ” ) ;
8 disp ( ”H0 would be a c c e p t e d when t h e s i g n i f i c a n c e
level i s 0 . 1 0 ”);

Scilab code Exa 14.3e Bayes estimator

1 X = [5 7 6.2 8.1 7.9 15 18 3.9 4.6 5.8];


2 Y = [3 3.2 4.1 1.8 1.6 2.7 1.2 5.4 10.3 1.5];
3 t = sum ( X ) + sum ( Y ) ;
4 R =10;
5 a = 20;
6 b = 2;
7 estimate = ( R + b ) /( a + t ) ;
8 disp ( estimate , ” Bayes e s t i m a t e o f lambda i s ” ) ;

Scilab code Exa 14.4a Lifetime of items produced by two plants

1 Xlife = 420;
2 Ylife = 510;
3 Xnum = 10;
4 Ynum =15;
5 ts = Xlife * Ynum /( Ylife * Xnum ) ;
6 disp ( ts , ” The v a l u e o f t h e t e s t s t a t i s t i c i s ”);
7 val = cdff ( ”PQ” , ts , Xnum , Ynum ) ;
8 pvalue = 2*(1 - val ) ;

135
9 disp ( pvalue , ” The p−v a l u e i s ” ) ;
10 disp ( ”We c a n n o t r e j e c t H0” ) ;

136

You might also like