26
An Introduction to Statistics in Matlab This guide is derived from the help files within Matlab. It identifies important concepts when working with Matlab as well as statistical tools that one might commonly use. It is not intended to be all-inclusive or explain all functions in great detail, but to serve as a quick reference when working with statistics in Matlab. For more information regarding the concepts addressed in this guide or for Matlab operations that this guide omits, please refer to the Matlab help files, accessed through the menus within Matlab. Table of Contents 1. An Introduction to Matrices and Arithmetic Operators pg. 2 a. Matrices pg. 2 b. Operators pg. 4 2. Basic Statistical Commands pg. 7 3. Hypothesis Testing pg. 9 a. Z-test pg. 9 b. T-test pg. 10 c. Two-sample t-test pg. 10 d. ANOVA pg. 11 4. Bootstrap pg. 13 5. Plotting with Matlab pg. 14 6. Regression and Curve-fitting pg. 16 a. Command-based Regression pg. 16 i. Setting up the Design Matrix by Hand pg. 16 ii. Using polyfit pg. 17 b. Graphical-based Regression pg. 18 c. Curve fitting tool pg. 19 7. Working with Probability Distributions pg. 22 a. CDF pg. 22 b. Parameter Estimation pg. 23 c. Quantiles pg. 23 d. Loglikelihood pg. 24 e. PDF pg. 25 f. Random number generation pg. 25 1

Manual Matlab

Embed Size (px)

DESCRIPTION

Manual Matlab

Citation preview

Page 1: Manual Matlab

An Introduction to Statistics in MatlabThis guide is derived from the help files within Matlab. It identifies important concepts when

working with Matlab as well as statistical tools that one might commonly use. It is not intendedto be all-inclusive or explain all functions in great detail, but to serve as a quick reference whenworking with statistics in Matlab. For more information regarding the concepts addressed in thisguide or for Matlab operations that this guide omits, please refer to the Matlab help files, accessedthrough the menus within Matlab.

Table of Contents

1. An Introduction to Matrices and Arithmetic Operators pg. 2a. Matrices pg. 2b. Operators pg. 4

2. Basic Statistical Commands pg. 7

3. Hypothesis Testing pg. 9a. Z-test pg. 9b. T-test pg. 10c. Two-sample t-test pg. 10d. ANOVA pg. 11

4. Bootstrap pg. 13

5. Plotting with Matlab pg. 14

6. Regression and Curve-fitting pg. 16a. Command-based Regression pg. 16

i. Setting up the Design Matrix by Hand pg. 16ii. Using polyfit pg. 17

b. Graphical-based Regression pg. 18c. Curve fitting tool pg. 19

7. Working with Probability Distributions pg. 22a. CDF pg. 22b. Parameter Estimation pg. 23c. Quantiles pg. 23d. Loglikelihood pg. 24e. PDF pg. 25f. Random number generation pg. 25

1

Page 2: Manual Matlab

An Introduction to Matrices and Arithmetic Operators

Matrices

In Matlab, matrix operations are the basis for all calculations.A matrix is a two-dimensional array of real or complex numbers. Acolumn vector is an m-by-1 matrix, created by the statement:

c = [3;1;4];

A row vector is a 1-by-n matrix, created by the statement:

r = [3 1 4];

A scalar is a 1-by-1 matrix, created by the statement:

s = 7;

To create a m-by-n matrix, you may string together row vectors,separated by a semicolon:

m = [1 4 7 10; 2 5 8 11; 3 6 9 12];

Note that the use of the semicolon at the end of each line preventsthe answer from being immediately returned by Matlab. To immediatelyview the output, simply leave off the semicolon.

The dimensions of a matrix are displayed for each matrix (excludingvectors) in the workspace window. Alternatively, one can use thefollowing command:

a = size(A);

2

Page 3: Manual Matlab

To obtain the transpose of a matrix, use "’". For example,

g = [4 3 1];h = g’ (which returns h = 4

31)

The identity matrix may be obtained using the command "eye".

i = eye(3,2) (which returns i = 1 00 10 0)

i = eye(2) (which returns i = 1 00 1)

To get the inverse of a matrix, use "inv".

A = [1 1 1; 1 2 3; 1 3 6];X = inv(A) (which returns X = 3 -3 1

-3 5 -21 -2 1)

To get the determinant of a matrix, use "det".

d = det(A) (which returns d = 1)

This may be necessary when performing certain calculations, as thecommand may simply return NaN when you wished to disregard thesevalues.

To get a matrix of ones [or zeros], use the following commands:

a = ones(2,3);b = zeros(2,3);

which will return 2-by-3 matrices of ones [or zeros respectively].

3

Page 4: Manual Matlab

For simplicity (and the fact that one will need matrices of ones orzeros that are the same size as other matrices) use the command:

c = zeros(size(C));

to get a matrix of zeros that has the same dimensions as C.

Missing values may be indicated with the value NaN. To remove thesevalues from your named variable, use the following command:

x = x(˜isnan(x));

Operators

To add and subtract matrices, they must be of the same size (samenumber of rows and columns). Then, simply use "+" or "-". Forexample,

A = [2 3; 4 5];B = [2 1; 5 8];r = [3 1 4];s = 7;

C = A + B (which returns C = [4 49 13])

To do matrix multiplication, simply use "*". For example, using c andr as defined above,

w = r * c (which returns w = 26)

w = c * r (which returns w =[9 3 123 1 412 4 16])

Similarly, with scalars,

w = r * s (which returns w = [21 7 28])

4

Page 5: Manual Matlab

To raise a matrix to a power (using matrix multiplication), simply use"ˆ".

C = Aˆ2 (from above) (which returns C = [16 2128 37])

To do element-by-element multiplication, use the command immultiply.

x = [3 1 4];y = [5 6 4];g = immultiply(x,y) (which returns g = [15 6 16])

One can also use the command .*

g = x.*y (which returns g = [15 6 16])

Similarly, for element by element division, use the command ./

g = x./y (which returns g = [0.6000 0.1667 1.0000])

Similarly, for raising each element in a matrix to a power, use thecommand .ˆ

A = [1 2; 3 4];B = A.ˆ2 (which returns B = [1 4

9 16])

To find the minimum [or maximum] of a vector, use the command min(a)[or max(a) respectively]. If a is a matrix, the command will returna vector of the minimum [or maximum] of each column of a. To returnthe minimum [or maximum] of each row, use min(a,[],2) [or max(a,[],2)respectively.

To obtain the difference between successive elements of a vector (forexample, to find the interspike time from a series of spike times),

5

Page 6: Manual Matlab

use the command diff.

d = diff(a);

To find e raised to a power, use the command:

h = exp(x);

To find the logarithm base e of a variable, use the command:

h = log(x);

To find the logarithm base 10 of a variable, use the command:

h = log10(x);

6

Page 7: Manual Matlab

Basic statistical commands

Several of the most common statistical operators are briefly describedhere.

mean (average of vectors and matrices)

m = mean(X)

calculates the sample average of a vector, or the mean of eachcolumn of a matrix

median (median of vectors and matrices)

m = median(X)

calculates the median value of a vector, of the median of eachcolumn of a matrix

std (standard deviation of a sample)

y = std(X)

computes the sample standard deviation of the data in X

var (variance of a sample)

y = var(X)

computes the variance of the data in X

iqr (interquartile range of a sample)

y = iqr(X)

7

Page 8: Manual Matlab

computes the difference between the 75th and 25th percentiles of thesample X

cov (covariance matrix)

C = cov(X)C = cov(x,y)

computes the covariance matrix

corr (linear or rank correlation)

RHO = corr(X)RHO = corr(X,Y,...)[RHO, PVAL] = corr(X,Y,...)

returns a matrix containing pairwise correlation between each pairof columns in the matrix X (or the pairs of columns in the matricesX and Y). If PVAL is saved (using the last command), it also returnsthe matrix of p-values for testing the null hypothesis of zerocorrelation.

corrcoef (correlation coefficients)

R = corrcoef(X)R = corrcoef(x,y)[R,P] = corrcoef(x,y)

returns a matrix of correlation coefficients (and p-values testingfor presence of non-zero correlation if desired)

8

Page 9: Manual Matlab

Hypothesis Testing

The following is an example of simulated data used to illustrateseveral common statistical hypothesis tests.

Macaque monkeys were studied in an experiment in which a stimulus waspresented to them, and the time between the stimulus presentation andthe hand movement (hand) was recorded. In addition, the time betweenthe presentation of the stimulus and an increase in neural activity(neural) was also recorded.

neural = [17.1335 12.1186 15.8087 11.3353 14.5888 17.327614.4286 20.2471 12.6164 10.7160 20.508019.8125 15.1181 20.1423 23.2002]

hand = [28.3377 32.5013 37.1507 28.4141 38.7637 44.756732.8495 38.3092 34.6986 28.2532 40.903240.6467 46.7327 38.4544 44.4557]

Z-test

This is used to test the null hypothesis that the mean of a batch ofdata is equal to a particular value mu, assuming the variance isknown.

Suppose that in our experiment, we know that the standard devation ofthe time to move the hand is 6. We wish to know whether or not themean time to move the hand is different from 34. We also decide thatwe would claim that the mean is different from 34 if, if we sampledover and over and the mean was truly 34, we would expect a value forour sample mean as extreme or more extreme than what we obtained nomore than five percent of the time (our alpha value or level ofsignificance). To perform the test, use the command:

[h,sig,ci,zval] = ztest(x,m,sigma,alpha)

Applied to our data and test:

[h, sig, ci, zval] = ztest(hand, 34, 6, 0.05)

The values returned are h (0 if we should not reject the idea the mean

9

Page 10: Manual Matlab

is different from 34, 1 if we should), sig (the probability that theobserved value of Z could be as large or larger by chance under thenull hypothesis, i.e. the p-value), ci (a (1-alpha)*100% confidenceinterval for the mean), and zval (the value of the z-statistic).

Here, we would not reject the null hypothesis that the mean isdifferent from 34.

T-test

Often the true standard deviation is not known and we must work withthe sample standard deviation. In this case, we use a t-test. Thecommand is similar to that of the z-test.

[h,p,ci] = ttest(x,m,alpha)

Again, testing the mean to be 34 or not, we run the command:

[h, p, sig] = ttest(hand,34,0.05)

Once again, we would fail to reject our null hypothesis that the meanis actually 34.

Both Z and t-tests may be performed using one-sided hypotheses aswell, as opposed to the two-sided tests that were performed above.This is done by using the command

[h,sig,ci,zval] = ztest(x,m,sigma,alpha,tail)

[h,p,ci] = ttest(x,m,alpha,tail)

and using ’right’ for the alternative mu>m and ’left’ for thealternative mu<m in place of tail.

Two-sample t-test

To test the null hypothesis that two samples have the same mean, onemay use a two-sampled t-test. In Matlab, the assumption is made that

10

Page 11: Manual Matlab

the standard deviations for the two samples are equal. To perform thetest, use the command:

[h,p,ci] = ttest2(x,y,alpha,tail)

Suppose, for the sake of an example, we wished to verify that the timeto hand movement and the time to neural response were in factdifferent. In our case, the command would be:

[h,p,ci] = ttest2(neural,hand,0.05) (with the default tail settingbeing ’both’ for testing two-sided hypotheses)

The result clearly states that we should reject the hypothesis thatthe two means are the same, with a p-value of 7.83 x 10ˆ-12. Theconfidence interval given is a 95% confidence interval for thedifference of the means.

ANOVA

One use of ANOVA (analysis of variance) is to test the null hypothesisthat the means of several groups are all the same. The alternativehypothesis is that at least one group has a different mean from theothers. The command to perform this test is:

[p,table,stats] = anova1(X)

where X is matrix where the different columns represent differentgroups.

For example, suppose we have 3 monkeys doing the aforementioned task.The time for each monkey to move its hand is recorded as follows:

A = [24 26 35 42 15 26 24 35 25 21];B = [19 25 34 16 24 34 26 25 14 18];C = [32 31 42 25 28 27 34 32 29 28];

To perform the test to see if all of the means are equal, use thefollowing series of commands:

X = [A;B;C]’;[p,table,stats] = anova1(X)

11

Page 12: Manual Matlab

This will return an ANOVA table, showing the value of the F-statisticand p-value (which is greater than 0.05, so we fail to reject thehypothesis that the means are different) as well as a boxplot of thethree different groups.

12

Page 13: Manual Matlab

Bootstrap

The bootstrap involves choosing random samples with replacement from adata set and applying a statistical function to each sample the sameway. Sampling with replacement means that every sample is returned tothe data set after sampling, so a particular data vector from theoriginal data set could appear multiple times in a given bootstrapsample. The number of elements in each bootstrap sample equals thenumber of elements in the original data set.

To perform this test, we can use the bootstrap as follows:

[bootstat,bootsam] = bootstrp(nboot,bootfun,d1,d2,...)

Here, nboot is the number of times we are going to sample, bootfun isthe function we are interested in, and d1, etc. are the input data.

Each row of the output, bootstat, contains the results of applyingbootfun to one bootstrap sample. If bootfun returns multiple outputarguments, only the first is stored in bootstat. If the first outputfrom bootfun is a matrix, the matrix is reshaped to a row vector forstorage in bootstat. Each column in bootsam contains indices of thevalues that were drawn from the original data sets to constitute thecorresponding bootstrap sample.

13

Page 14: Manual Matlab

Plotting with Matlab

Some of the most commonly used methods to create plots are listedbelow:

boxplot (boxplots of a data sample)

boxplot(X)

returns a boxplot of each column of X

hist (plot histograms)

hist(y,nb)[n,x] = hist(y,nb)

draws a histogram with nb bins (default is 10). You may have thefrequency counts (n) and bin locations (x) returned as well.

hist3 (3-dimensional histogram of bivariate data)

hist3(X,nbins)hist3(X,’Edges’,edges)

draws a 3-D histogram with nbins(1) x nbins(2) bins (default is10x10). The edges of the dimensions may be specified.

plot (linear 2-D plot)

plot(X,Y)

creates a linear plot, connecting the points defined by the vectorsX and Y (if X and Y are matrices, columns or rows of the twomatrices are matched up, depending upon the dimensions of thematrices). Plot may be modified to create scatter plots and otherplots by adjusting the options available (see help menu for moreinformation).

plot3 (3-D line plot)

14

Page 15: Manual Matlab

plot3(X,Y,Z)

creates a line plot, connecting the points defined by the vectorsX, Y, and Z (which may be matrices, as discussed above)

scatter (2-D scatter/bubble graph)

scatter(X,Y)

creates a 2-D scatter plot, with bubbles at the locations specifiedby the vectors X and Y (which must be of the same size)

scatter3 (3-D scatter plot)

scatter(X,Y,Z)

creates a 3-D scatter plot, with bubbles at the locations specifiedby the vectors X, Y, and Z (which must be of the same size)

normplot (normal probability plot)

normplot(X)

displays a normal probability plot of the data in X. For matrices,normplot displys a line for each column of X.

probplot (probability plot)

probplot(’distname’,Y)

creates a probability plot for the specified distribution

qqplot (quantile-quantile plot of two samples)

qqplot(X,Y)

displays a quantile-quantile plot of two samples. If the twosamples do come from the same distribution, the plot will belinear.

15

Page 16: Manual Matlab

Regression and curve-fitting

Matlab has both a command based system to perform regression andcurve-fitting as well as a graphical interface, allowing the user topoint-and-click to perform the analysis.

Command based Regression

Setting up the Design Matrix by Hand

Suppose one wishes to model the data y using the polynomial function:

y = a + b*t + c*tˆ2

To perform this regression, one first needs to create the designmatrix X, which consists of a column of ones and two columnscorresponding to t and tˆ2.

X = [ones(size(t)) t t.ˆ2];

To perform the regression, use the backslash operator (do not confusewith the division operator):

r = X\y;

r will now contain three values, corresponding to a, b, and c in theform above.

This can also be done for linear-in-the-parameters regression.

Suppose one wishes to model the data y using the function:

y = a + b*exp(-t) + c*t*exp(-t)

Again, one creates the design matrix and then proceeds with theregression:

X = [ones(size(t)) exp(-t) t.*exp(-t)];r = X\y;

16

Page 17: Manual Matlab

To perform multiple regression, a similar procedure is followed.

Suppose one wishes to model the data y using the function:

y = a + b*x + c*z

Again, one creates the design matrix and then proceeds with theregression:

X = [ones(size(x)) x z];r = X\y;

Note that x, y, and z must be of the same size for the regression towork.

Using polyfit

Alternatively, one can also perform polynomial regression withoutfirst setting up the design matrix.

Again, suppose one wishes to model the data y using the polynomial function:

y = a + b*t + c*tˆ2

The command polyfit will set up the design matrix and perform theregression in one step.

p = polyfit(t,y,2); (where 2 is the degree of the polynomial beingfitted)

Suppose one now wishes to evaluate the fit at a series of points s.The command polyval allows this evaluation.

fit = polyval(p,s); (where p is the fit from polyfit)

To use the same commands to make exponential fits, merely transformthe variables.

Suppose one wishes to model the data y using the function:

17

Page 18: Manual Matlab

y = a + b*exp(t) + c*exp(t.ˆ2)

Then, one would use the command:

p = polyfit(t,log(y),2);

To get confidence intervals using polyfit, use the following set ofcommands:

Returning to our example where we are using the model:

y = a + b*t + c*tˆ2

First, one performs the regression, but also saves the standarderrors (as d):

[p,d] = polyfit(t,y,2);

One then evalutates along the fitted curve as before (using the pointss):

[fit,del] = polyval(p,s,d);

A 95% point-wise confidence interval at each point s can be obtainedfrom (fit-1.96*del, fit+1.96*del).

Graphical based regression

A quick way to perform regressions is through the basic fitting toolthat Matlab provides. Its options are limited, but it provides amethod to perform regressions and immediately view the results.

To begin, suppose we have a set of data y which we would like toregress on the variable x.

First, plot the data using the command:

plot(x,y)

Now, on the resulting figure, click on Tools in the menu bar and

18

Page 19: Manual Matlab

select Basic Fitting. This will open another window. On this window,check the fit you wish to use on the data (choosing from interpolatingsplines, linear regression, quadratic regression, and multiplepolynomial regression). You can choose multiple types at the sametime. The resulting fit will be plotted along with your data. Toobtain residual plots, simply check off "Plot residuals" with theoptions of choosing a bar or scatter plot. You can also include theregression equation in your plot by checking the "Show equations" box.

Pressing the arrow on the bottom of the menu will open up anotherpanel in the window, showing the coefficients used to fit the model.Clicking "Save to workspace" will allow you to save the fit as avariable name.

Pressing the arrow pointing right on the bottom of the menu again willopen up another panel in the window. This panel allows you to inputvalues of x at which to evaluate the fit. These fitted values mayalso be saved to the workspace, similar to the fit itself, by pressing"Save to workspace."

Curve fitting tool

Another graphical based tool to perform regression is the Curvefitting tool. This tool has more options than the Basic fitting tool,including more possible fits. These fits include polynomial fits,Gaussian fits, interpolating fits (including cubic splines), powers,rational expressions of polynomials, smoothing splines, trigonometricfits, and Weibull. Also included in the curve fitting tool are theability to exclude outliers and other points while performing theregression and the inclusion of point-wise confidence bands.

The following is step-by-step directions when working with the curvefitting tool:

Store data to be analyzed in variables x,y,....

Next, type in the command:

cftool

This command will open the curve fitting tool.

19

Page 20: Manual Matlab

Click on Data... Button. Data screen will open.

Import workspace vectors using the dropdown menu for X Data and YData. Note that variables must be of the same class. For example,for neuronal spike data, the y variable is often an integer. If the xvariable is of the class double, change the y variable to the classdouble in order to proceed.

When X and Y Data are selected (and weights if desired), click onCreate data set. Note that smoothed data sets may also be created byclicking on the Smooth tab on the top of the Data screen. Only asmall variety of smoothing options are available. Choosing a methodand clicking on Create smoothed data will create a data set of datasmoothed using the appropriate method.

On the Curve Fitting Tool window, the button Plotting... will allowyou to choose which data sets you wish to be displayed on a graphat the same time. Click the checkmark next to the dataset to showthe plot; toggle the check off to remove that dataset from theplot.

On the Curve Fitting Tool window, the button Fitting... will allowyou to create a fit for the data.

First, click on the New fit button.

Choose the appropriate dataset and the type of fit fromthe drop down menu boxes. The different types of fits will alsohave a set of choices from which to choose a fit. For example,polynomial allows one to choose linear, quadratic, cubic,...9thdegree polynomial.

After choosing the appropriate type of fit, hit apply to calculate thefit. The fit will be plotted in the Curve Fitting Tool window andwill be saved as the chosen Fit Name, the default being "fit 1". TheTable of Fits in the Fitting window will give appropriate informationabout the fit including SSE and R-squared. Additional information maybe added to the table by pressing the Table options... button and

20

Page 21: Manual Matlab

checking the desired quantities.

For additional analysis of the fit, select the Analysis... buttonon the Curve Fitting Tool. This will open the Analysis window.

The first option gives the fit to analyze and the values of X atwhich to analyze the fit. (Notation is starting X:distancebetween X values: ending X). Checking "Evalute fit at Xi" willallow you to calculate prediction or confidence bands at each valueof X indicated above. The other options allow calculations of thefirst and second derivatives of the fit and an integral of thefit.

By choosing "Plot results," the confidence intervals and/orderivatives will be plotted in a separate window, "Curve FittingAnalysis." Note that some fits, namely smoothing splines, will notallow calculations of prediction or confidence bands.

The Exclude... button on the Curve Fitting Tool allows for removalof particular points or regions when calculating a fit.

Define a new Exclusion rule name and select a data set. To excludeparticular points, check next to the index number of thecorresponding X/Y pair. To exclude sections of data, enter thevalue(s) that you do not wish to consider in the Exclude Sectionsarea. When finished, press Create exclusion rule. This exclusionrule may be accessed when using the Fitting... option by choosingthe appropriate Exclusion rule in the dropdown menu in the Fittingwindow.

21

Page 22: Manual Matlab

Working with Probability Distributions

There are several distinct functions dealing with differentdistribution functions. They are virtually identical except indistribution and necessary parameters.

CDF

The cumulative density function (or cdf) returns the integrated (orsummed) density from negative infinity to the specified value X. Thebasic form for the command is ****cdf(X,param1,param2,...), where ****is substituted with the appropriate abbreviation for the density beingused (listed below) and the necessary parameters are given.

p = ****cdf(X,param1,param2,...)computes cdf at each value in X using parameters specified

cdf(’name’,X,A1,A2,A3)betacdf(X,A,B) - betabinocdf(X,N,P) - binomialchi2cdf(X,V) - chi-squaredecdf(y) - empirical cdfexpcdf(X,MU) - exponentialfcdf(X,V1,V2) - Fgamcdf(X,A,B) - gammageocdf(X,P) - geometrichygecdf(X,M,K,N) - hypergeometriclogncdf(X,MU,SIGMA) - lognormalnbincdf(X,R,P) - negative binomialncfcdf(X,NU1,NU2,DELTA) - noncentral Fnctcdf(X,NU,DELTA) - noncentral Tncx2cdf(X,V,DELTA) - noncentral chi-squarednormcdf(X,MU,SIGMA) - normalpoiscdf(X,LAMBDA) - poissonraylcdf(X,B) - Rayleightcdf(X,V) - Student’s tunidcdf(X,N) - discrete uniformunifcdf(X,A,B) - continuous uniformwblcdf(X,A,B) - Weibull

22

Page 23: Manual Matlab

Parameter Estimation

Supposing one knows the appropriate distribution from which data wasobtained, one can estimate the appropriate parameters for thatdistribution. The command is ****fit(data,alpha), again substitutingthe appropriate abbreviation for ****, and alpha is provided so as toobtain a confidence interval for the estimates. Note that theabbreviations for the densities are the same as in CDF, although notall densities are available.

[phat,pci] = ****fit(data,alpha)estimates parameters for a distribution for the data; pcicontains the confidence intervals for the estimates, using theappropriate value of alpha (default = 0.05)

mle(data,’distribution’,dist)betafit(data,alpha)binofit(data,alpha)expfit(data,alpha)gamfit(data,alpha)lognfit(data,alpha)nbinfit(data,alpha)normfit(data,alpha)poisfit(data,alpha)raylfit(data,alpha)unifit(data,alpha) - for continuous uniformwblfit(data,alpha)

Quantiles

Quantiles can be obtained from a particular density, given theappropriate parameters. Given a value (or vector) P, the quantilesare calculated for each number in P using the command****inv(P,param1,param2,...) where **** and the parameters are asbefore.

q = ****inv(P,param1,param2,...)computes the inverse of the cdf for the probabilities in Pusing parameters specified

icdf(’name’,P,A1,A2,A3)betainv(P,A,B)

23

Page 24: Manual Matlab

chi2inv(P,V)expinv(P,MU)finv(P,V1,V2)gaminv(P,A,B)geoinv(Y,P)hygeinv(P,M,K,N)logninv(P,MU,SIGMA)nbininv(Y,R,P)ncfinv(P,NU1,NU2,DELTA)nctinv(P,NU,DELTA)ncx2inv(P,V,DELTA)norminv(P,MU,SIGMA)poisinv(P,LAMBDA)raylinv(P,B)tinv(P,V)unidinv(P,N)unifinv(P,A,B)wblinv(P,A,B)

Loglikelihood

The value of the negative loglikelihood, evaluated at given parametersand data, can be obtained using the command ****like(params,data).Note that this option is available for only a certain number of selectdistriibutions.

nlogL = ****like(params,data)returns the negative of the log-likelihood function for theparameters specified in the vector params for theobservations in the vector data

betalike(params,data)explike(param,data)gamlike(params,data)lognlike(params,data)normlike(params,data)wbllike(parmas,data)

24

Page 25: Manual Matlab

PDF

Like the CDF, the probability density function (PDF) returns the valueof the density function at a particular value of X, given thenecessary parameters. The command is ****pdf(X,param1,param2,...)where the substitutions are made as before.

Y = ****pdf(X,param1,param2,...)computes pdf at each value in X using parameters specified

pdf(’name’,X,A1,A2,A3)betapdf(X,A,B)chi2pdf(X,V)exppdf(X,MU)fpdf(X,V1,V2)gampdf(X,A,B)geopdf(X,P)hygepdf(X,M,K,N)lognpdf(X,MU,SIGMA)mvnpdf(X,mu,SIGMA) - multivariate normalnbinpdf(X,R,P)ncfpdf(X,NU1,NU2,DELTA)nctpdf(X,V,DELTA)ncx2pdf(X,V,DELTA)normpdf(X,MU,SIGMA)poispdf(X,LAMBDA)raylpdf(X,B)tpdf(X,V)unidpdf(X,N)unifpdf(X,A,B)wblpdf(X,A,B)

Random number generation

Probably one of the most useful commands when performing simulations,the random number generator allows one to generate data from adistribution, given the necessary parameters. The commands for thisfunction are of the form ****rnd(param1,param2,...,m,n) where m and nindicate the dimensions of the matrix that is being generated. ****,param1, param2,... are as before.

R = ****rnd(param1,param2,m,n)

25

Page 26: Manual Matlab

returns an m x n matrix of random numbers from adistribution with parameters specified

random(’name’,A1,A2,A3,m,n)betarnd(A,B,m,n)chi2rnd(V,m,n)exprnd(MU,m,n)frnd(V1,V2,m,n)gamrnd(A,B,m,n)geornd(P,m,n)hygernd(M,K,N,m,n)iwishrnd(SIGMA,df) - inverse Wishartlognrnd(MU,SIGMA,m,n)mvnrnd(mu,SIGMA)mvtrnd(C,df,cases) - multivariate t (C = correlation matrix, output

has ’cases’ rows and p (col of C) columns)nbinrnd(R,P,m,n)ncfrnd(NU1,NU2,DELTA,m,n)nctrnd(V,DELTA,m,n)ncx2rnd(V,DELTA,m,n)normrnd(MU,SIGMA,m,n)poisrnd(LAMBDA,m,n)raylrnd(B,m,n)trnd(V,m,n)unidrnd(N,m,n)unifrnd(A,B,m,n)wblrnd(A,B,m,n)wishrnd(SIGMA,df) - Wishart

26