Vous êtes sur la page 1sur 16

# MATLAB Workbook

CME106
Introduction to Probability and Statistics for Engineers

Second Edition

1. Random Number Generation 2. Probability Distributions 3. Parameter Estimation 4. Hypothesis Testing 5. Regression and Correlation Analyses

## 1. Random Number Generation

Commands: unidrnd unifrnd hist Generates a discrete random number or a random vector Generates a random number or a vector from a uniform distribution Creates a frequency plot (histogram)

We will attempt to solve one of the homework problems numerically by performing a virtual experiment using MATLABs random number generator. The problem is as follows. Two points a and b are selected at random along the x-axis such that and . Find the probability that the distance between a and b is greater than 3 by performing one million trials. Make a histogram of the generated distances between x and y.

SOLUTION
Two vectors of random numbers each with one million elements will be drawn from a uniform distribution and the distance between the corresponding elements compared to 3. See code below.
clear; x=unifrnd(-2,0,1,1000000); y=unifrnd(0,3,1,1000000); s=0; d=abs(x-y); for i=1:1000000 if d(i)>3 s=s+1; else end end hist(d,100) Probability=s/1000000 The exact analytical solution is 1/3

## Running the code:

Probability = 0.3345

Hint: generating a vector of random numbers all at once is computationally more efficient than generating random values one at a time within a for loop !

Using MATLAB, verify your answer to one other homework problem, which follows. A point is selected at random inside a circle. Find the probability that the point is closer to the center of the circle than to its circumference. Hint: Generate a random vector of x and y coordinates first, then consider only those points, which are inside the circle.

2. Probability Distributions
Commands: binopdf binocdf binoinv binornd PDF for the binominal distribution CDF for the binominal distribution Inverse CDF for the binomial distribution Generates random numbers from the binomial distribution

poisspdf PDF for the Poisson distribution poisscdf CDF for the Poisson distribution poissinv Inverse CDF for the Poisson distribution poissrnd Generates random numbers from the Poisson distribution normpdf normcdf norminv normrnd PDF for the normal distribution CDF for the normal distribution Inverse CDF for the normal distribution Generates random numbers from the normal distribution

a) A single lot containing 1000 chips manufactured by a semiconductor company is known to contain 1% of defective parts. Find the probability that at most 10 parts in the lot are defective, assuming: a) Binominal distribution and b) Poisson distribution.
b) The diameters of shafts manufactured by an automotive company are normally

distributed with the mean value of 1 inch and the standard deviation of 0.01 in. Each shaft is to be mounted into a bearing with an inner diameter of 1.025 in. A part is termed as defective if it does not fit into the bearings. Write a MATLAB script to estimate the proportion of defective parts out of 10,000. On the same set of axes, plot a histogram showing the observed frequencies of the shaft diameters and the scaled density function.

SOLUTION
Part a) >> binopdf(10,1000,0.01) ans = 0.1257 Part b) clear % x - observed number of defective parts % y - expected number of defective parts x=normrnd(1,0.01,1,10000); >> poisspdf(10,10) ans = 0.1251

s=0; for i=1:10000 if x(i)>1.025 s=s+1; else end end Probability=s/10000 figure(2) hist(x,100); xlabel('Shaft diameter, in'); ylabel('Frequency');

## Running the code:

Probability = 0.0063

1) You work for a VC firm, pitching ideas to different investors every day. While snooping around a rivals office, you find a report hes written to management bragging that he has an 85% chance of hitting his sales target this year. Assuming that you know his success rate is 30%, and assuming that you model his sales with a binomial distribution with 365 trials, what is his sales target? 2) Your zero-g-proof-pen manufacturing company is in dire straights, and you want to know what the probability is that you will have to declare bankruptcy. In order to stay afloat, you need each of the following three things to happen: Your factory needs to fill an order for 500 pens without making any defective pens (assume a binomial distribution with a 0.05% chance of error), you also need a space shuttle to be launched in the next month (consider space shuttle launch per month a Poisson random variable with rate 0.4, and you need exactly 1 launch), and you need your CEO to find at least 3 NASA secretaries who will take his phone calls (in the past this occurrence has been normally distributed with mean 2.6 and variance 1). Assuming that these three events are independent, what is the probability that your company will go bankrupt? 3) Write a Monte-Carlo simulation to test your analytical result in part 2), using poissrnd, binornd, and normrnd with the appropriate parameters. Approximately how many trials are needed before your Monte Carlo answer converges to within 0.001 of your analytical result in part 2)? 4) Write a script to simulate a sampling procedure from 1,000 identical lots using the binominal distribution with probability of a part being defective of 0.01. Create a histogram showing the frequencies of the number of observed defective parts in 1,000 lots using 10,000 trials. On the same set of axes, plot the scaled probability density function for the binominal distribution with the appropriate parameters. The density function can be plotted over the range from 0 to 25. Compare the empirical and the theoretical distributions. Note that the binomial distribution function binopdf returns non-zero values only when the argument is an integer.

3. Parameter Estimation
Commands: normfit poissfit expfit Estimate parameters of a normal distribution Estimate parameters of a Poisson distribution Estimate parameters of an exponential distribution

The time to failure of field-effect transistors (FETs) is known to be exponentially distributed. Data (in hours) has been collected for 10 samples. Using the data in the table below, compute the maximum likelihood estimate and the 95% confidence interval for the mean time to failure. 2540 2650 2420 2580 2470 2800 2550 2300 2460 2490

SOLUTION
>> x=[2540, 2650, 2420, 2580, 2470, 2800, 2550, 2300, 2460, 2490]; >> [mu_hat, mu_confidence]=expfit(x,0.05) mu_hat = 2526 mu_confidence = 1.0e+003 * 1.2113 4.3156

1) A traffic engineer would like to make an assessment of traffic flow at a busy intersection during weekday rush hour. The number of arrivals is believed to satisfy the conditions for the Poisson distribution. The engineer records the number of cars arriving at the intersection between the hours of 8am and 9am over a period of two weeks (10 business days). Using the data in the table (in cars/hr), compute the maximum likelihood estimate and the 95% confidence interval for the average number of car arrivals.

## 205 275 320 210 240

2) A product is tested for levels of different chemicals Xi, where i = 1,2,3,4,5,6. The value of each level is an integer ranging from 1 to 5. Depending on the observed levels of the chemicals, the product is either accepted (Y = 1) or not (Y = 0). Given a 1 x 6 input vector of measurements of the levels of chemicals and the output Y, the parameters P(Xi = j|Y = 1) where i = 1,2,3,4,5,6 and j = 1,2,3,4,5 can be estimated using Maximum Likelihood Estimation analytically. The idea behind maximum likelihood parameter estimation is to determine the parameters that maximize the probability (likelihood) of the sample data. In our problem, each Xi = j|Y=1 is a random variable with parameters pij, which can be estimated as the ratio of the number of observations, in which both Y is 1 and Xi is j, to the number of observations in which Y is 1. We can do similar for Y=0. The given data (see file data.xls) consists of 9000 tests described above. a) Use the first 6000 tests to create a model for the given system, i.e. estimate the parameters P(Xi = j|Y = 1) for i = 1,2,3,4,5,6 and j = 1,2,3,4,5. b) Using the next 3000 test points, calculate P(Y = 1|X1 = i1, X2 = i2, X3 = i3, X4 = i4, X5 = i5, X6 = i6). From Bayes formula, we know that

## We will predict an outcome as which is equivalent to

if

If we make the additional Nave Bayes assumption that then our formula simplifies more and can use our calculations from part a). For the last 3000 entries of the data (from 6001 to 9000), predict the data as Y=1 or Y=0 by computing for j=1 and j=0, and choosing Y=1 or Y=0 based on which value is larger. Evaluate this method of prediction by comparing to the actual values for Y given in the data set. Compute the number of correct predictions divided by the number of predictions (3000).

4. Hypothesis Testing
Commands: ztest z-test for the mean of a normal distribution with known variance ttest t-test for the mean of normal distribution with unknown variance ttest2 t-test for the difference of two means of normal distribution with unknown but equal variances signtest sign test for the mean of an arbitrary distribution

1) The time to failure, T, of a part is known to be approximately normally distributed with a mean of 1000 hours and a standard deviation 20 hours. A new manufacturing process is proposed to delay the failure time by well over 200 hours. Data collected on 10 sample parts from the new manufacturing process yields the following times in hours (See table below). The new process will be implemented at the factory only if it can be shown to result in an increase in T of at least 300 hours. Use a hypothesis test to determine if the new process should be adopted.

Failure Times for New Parts 1350 1320 1290 1280 1340 1210 1290 1350 1300 1310

SOLUTION
The proper hypothesis test is

## This calls for a one-sided Z-test, in particular, the right-tailed Z-test.

>> x=[1350, 1320, 1290, 1280, 1340, 1210, 1290, 1350, 1300, 1310]; >> mean = 1300; >> sigma = 20; >> alpha = 0.05; >> tail = right; >>[hypothesis,p_value]=ztest(x,me an,sigma,alpha,tail) hypothesis= 0 p_value = 0.2635 Since the p_value > 0.05, the null hypothesis is accepted and the new process is not adopted

2) It is desired to compare the performance of two types of gasoline engines by comparing their fuel consumption for a certain number of miles traveled. No historical data is available from which to determine the variance of the fuel consumption. However, is it known that the two engines have similar characteristics and that any variability in the fuel usage will be the same for both types of engines. It is also assumed that the fuel consumption if approximately normally distributed. The fuel consumption data in gallons for 3 engines of the first type and 4 engines of the second type are shown in the table below. Can it be concluded that the two types of engines demonstrate different performance at the 10% significance level? Type 1 usage (in gallons) 540 520 535 ---Type 2 usage (in gallons) 575 540 560 545

SOLUTION
The proper hypothesis test is

## Since the variance is unknown, we shall use a two-sided t-test here.

% This is a two-sided test, since the question is to compare two populations without any prior knowledge which of the two means is larger >> x=[540, 520, 535];

>> y=[575, 540, 560, 545]; >> [hypothesis,p_value]=ttest2(x,y,0.1) hypothesis = 1 p_value = 0.0794 % since the p_value < 0.1, the null hypothesis is rejected and it is concluded that the fuel consumptions are not the same at the 10% significance level % We can perform a hand calculation to compare the results with the results of ttest2 function

>> s_squared=(3-1)/(3+42)*var(x)+(4-1)/(3+4-2)*var(y) s_squared = 193.3333 >> test_statistic=(mean(x)mean(y))/sqrt(s_squared)/sqrt(1/3+1/ 4) test_statistic = -2.1972 >> p_value=2*tcdf(test_statistic,3+42) p_value = 0.0794 % The p-value is in agreement to the output from the ttest2 function

1) Repeat the test from example 1 in this section, but now assuming that the standard deviation for the distribution of the failure times is not known and must be estimated from the data. 2) Reconsider the fuel consumption problem from the Example 2 above. In this problem, the fuel consumption for the two types of engines has been determined pair-wise over different distances traveled; a type 1 engine and a type 2 engine were paired and driven the same distance (but different from the other pairs) to determine fuel consumption. Fuel consumption is again assumed to be normally distributed. Mean fuel consumption changes with distance traveled and possibly with engine type. Variability of fuel consumption is the same regardless of distance traveled or engine type. Because of this setup, it is believed that a paired t-test would be more appropriate. For the paired fuel consumption data provided in the table below, can it be concluded at the 10% significance level that the two engines consume fuel at different rates?

Type 1 usage (in gallons) Pair 1: Pair 2: Pair 3: 540 520 580

## Type 2 usage (in gallons) 555 515 585

Pair 4:

500

505

3) Suppose that because of the lack of historical data, test engineers are not certain that the fuel consumption in the previous exercise is normally distributed. Rather than using a paired t-test, they are considering using a distribution-free sign test. Using the data in the table above, what conclusion is reached at the 5% significance level? 4) In a conversation with a colleague regarding investing, you are told that you should invest in value stocks because they outperform growth stocks in the long run. Your goal is to test the validity of this statement. Historically, its been shown that the quantity , known as logreturn, is approximately normally distributed and independent over time. Here and are stock prices in consecutive years. In the file index_data.txt (available on Coursework), you will find annual prices of two stock indexes from 1926 until 2008. Calculate the log-returns for each index and test the hypothesis that the value index has the same mean logreturn as the growth index at the 10% significance level. 5) A claim is made that the amount of energy saved from a facility per week after installing new solar panels is 550 kilowatt-hours more than previous attempts to reduce energy usage. Suppose that technicians run weekly experiments to determine energy savings for both the solar panels and previous methods. Suppose further that the energy savings from the experiments are normally distributed. Unknown to the technicians, however, the data of energy savings (in kilowatt-hours) from the new solar panels has mean and standard deviation 800 while the data of energy saving (in kilowatt-hours) from previous methods of reducing energy usage has mean and standard deviation 800. The hypothesis test the technicians consider is Because the alternative hypothesis is actually true, we would like to know about how many weekly experiments the technicians need to run so that the probability of detection using a one-side t-test at the 5% level of significance is at least 95%, i.e. the probability of rejecting the null hypothesis is at least 95%. Run 1000 one-sided t-tests over a range of sample sizes to estimate the probability of detection for each sample size and to estimate the required sample size to reject the null hypothesis 95% of the time.

## 5. Regression and Correlation Analyses

Commands: polyfit estimates coefficients for a polynomial fit to a set of paired data lsline superimposes a best-fit line to a set of scattered data corrcoef sample correlation coefficient polyval evaluates a polynomial found with polyfit using test data

Thermistors are passive devices frequently used to measure temperature. They are resistive elements whose resistance increases as a function of temperature. To calibrate a thermistor, a voltage is applied across its terminals and the value of the current is recorded for a given temperature. The data collected is shown in the table below (current in amperes and voltage in volts). a) For each value of the temperature plot on the same set of axes a linear curve fit of the voltage versus current through the thermistor and separately a calibration curve (resistance versus temperature). Compute the coefficient of determination for each of the five sets of data.

b)

## T=50C I 0.02 0.05 0.07 0.08 V 1 2 3 4

SOLUTION
% Input data >> V=[1 2 3 4]; >> I10=[0.11 0.21 0.32 0.42]; >> I20=[0.08 0.15 0.23 0.33]; >> I30=[0.07 0.11 0.17 0.23]; >> I40=[0.04 0.07 0.11 0.15]; >> I50=[0.02 0.05 0.07 0.08]; % Compute regression coefficients >> [p10,s10]=polyfit(I10,V,1); >> [p20,s20]=polyfit(I20,V,1); >> [p30,s30]=polyfit(I30,V,1); >> [p40,s40]=polyfit(I40,V,1); >> [p50,s50]=polyfit(I50,V,1); % Plot scatter data and regression lines >> figure(1) >> grid on >> hold on >> plot(I10,V,'.'); >> lsline >> plot(I20,V,'.'); >> lsline >> plot(I30,V,'.'); >> lsline >> plot(I40,V,'.'); >> lsline >> plot(I50,V,'.'); >> lsline >> hold off >> xlabel('Current, amperes'); >> ylabel('Voltage, volts'); % Plot calibration curve, i.e. slope of regression lines versus temperature >> figure(2) >> T=[10 20 30 40 50]; >> resistance=[p10(1) p20(1) p30(1) p40(1) p50(1)]; >> plot(T,resistance,'+') >> axis([5,55,5,50]); >> grid on >> xlabel('Temperature, C'); >> ylabel('Resistance, Ohm'); % The coefficient of determination is equal to the square of the sample correlation coefficient. The diagonal elements are scaled variances (all equal to 1). The off- diagonal elements give the correlation coefficient. >> rho10=corrcoef(V,I_10) rho10 = 1.0000 0.9998 0.9998 1.0000 >> rho20=corrcoef(V,I_20) rho20 = 1.0000 0.9967 0.9967 1.0000 >> rho30=corrcoef(V,I_30) rho30 = 1.0000 0.9959 0.9959 1.0000 >> rho40=corrcoef(V,I_40) rho40 = 1.0000 0.9978 0.9978 1.0000 >> rho50=corrcoef(V,I_50) rho50 = 1.0000 0.9759 0.9759 1.0000

1) The voltage-current characteristic of a newly developed non-linear electronic

device is thought to have a quadratic dependence of the form: . A set of measurements was performed to estimate the regression coefficients . The measurements of the voltage and current are provided in the table below. Determine the regression coefficients. Plot on the same set of axes the current-voltage data and the quadratic curve fit.

V (volts) 1 2 3 4 5 6 7 8

## 2) Calculate a 50% confidence interval for future prediction for the

polynomial, and plot the data and curve again with the confidence interval included (Hint: Look up the polyval function in Matlab using: >>help polyval).

## 3) A companys production volume is thought to depend quadratically on

its raw material intake: . The measurements of the production volume in different months are given on the next page, along with the raw material intake for that month. Determine the coefficients for the model.

M (Raw Material in Tons) 1.82 1.40 2.93 1.36 1.52 1.45 1.92 1.81

P (Production Volume in Tons) 0.9267 0.6245 2.1614 0.5999 0.7026 0.6563 1.0110 0.9185