Docsity

Descriptive Statistics and Probability Distribution | STAT 2, Study notes of Statistics

Partial preview of the text

Download Descriptive Statistics and Probability Distribution | STAT 2 and more Study notes Statistics in PDF only on Docsity!

Matlab Workshop MFE 2006

Lecture 2

Stefano Corradin

Peng Liu

http://faculty.haas.berkeley.edu/peliu/computing

Statistical Analysis

2.1 Descriptive statistics.

2.2 Probability distributions.

2.3 Loading and saving data.

2.4 Multiple linear regression.

2.5 Stochastic processes.

Let construct a function to compute sample mean x¯, standard deviation, s, skewness, sk, and kurtosis, k, for each column of a matrix

function [xbar,s,sk,k] = statistics(X)

[n,m] = size(X); xbar = sum(X)/n; % alternative way to compute the mean X = X - ones(n,1)xbar; s = sqrt(sum(X.^2)/n); sk = sum(X.^3)./(ns.^3); k = sum(X.^4)./(n*s.^4);

now you can type the following statement.

x = randn(100); [xbar,s,sk,k] = statistics(X)

You can use the Statistics toolbox to test our previous results.

mean(X) std(X,1) skewness(X) kurtosis(X)

Some useful functions to produce graphics in descriptive statistics.

hist(x) % histogram boxplot(x) % boxplots of a data matrix cdfplot(x) % plot of empirical cumulative distribution function normplot(x) % normal probability plot qqplot(x,y) % quantile-quantile plot

For example

x = rand(1000,1); y = rand(1000,1); qqplot(x,y)

x5 = 0:5; y5 = binomialpdf(x5,5,.2); x10 = 0:10; y10 = binomialpdf(x10,10,.2); x20 = 0:20; y20 = binomialpdf(x20,20,.2); plot(x5,y5,’.’,x10,y10,’+’,x20,y20,’*’) legend(‘n=5,p.2’,‘n=10,p.2’,‘n=20,p.2’)

Let generate m random numbers from a Binomial distribution with parameters n and p:

function y = binomialrnd(m,n,p)

prob = rand(n,m); successes = prob < p; y = sum(successes);

we can try this function with.

y10 = binomialrnd(10,100,.1); mean(y10); var(y10); y100 = binomialrnd(100,100,.1); mean(y100); var(y100);

The Statistics Toolbox provides five functions

  • pdf probability density function
  • cdf cumulative distribution function
  • inv inverse cumulative distribution function
  • rnd random number generator
  • stat mean and variance as a function of parameters

for example for the normal distribution:

normpdf(x,0,1), normcdf(x,0,1), norminv(normcdf(x,0,1),0,1), normrnd(0,1), normstat(0,1).

Loading and saving data

Import data:

  • text data format .txt: you can import them using the command load. For example load ‘c:...\name-file’.
  • matlab data format .mat: still you can use the command load.
  • excel data format .xls: go to File / Import Data.

Export data:

  • you can export your data using the command save. For example save ‘c...\name-file’.

The predicted values are given by

Y^ ˆ = Xβ

the residuals are e = Y − Yˆ

and the residual variance is

s^2 R =

∑n i e

2 i n − k − 1

We can write a function to solve the regression problem!!

function [beta,var_beta,resid,ypred,sR2] = myregress(X,Y)

% X matrix of regressors with a column of ones % Y vector of data from the dependent variable

[n,k] = size(X); k = k - 1; beta = inv(X’X)(X’Y); ypred = Xbeta; resid = Y - ypred; sR2 = sum(resid.^2)/(n-k-1); var_beta = sR2inv(X’X);

var_beta =

0.0169 0.0007 -0.0014 -0.0027 -0. 0.0007 0.0164 0.0040 0.0030 -0. -0.0014 0.0040 0.0166 0.0011 -0. -0.0027 0.0030 0.0011 0.0119 0. -0.0026 -0.0017 -0.0003 0.0016 0.

We can produce some plots to analyze

  • the residuals;
  • the residuals versus the predicted values.

plot(resid,’o’); title(’Residuals versus row number’);