matlab累积概率分布,[转载]Matlab累积分布函数cdf与概率密度函数pdf

  • Post author:
  • Post category:其他

累积分布函数cdf (Cumulative

Distribution Function)

背景知识:http://www.lifelaf.com/blog/?p=746

语法

y =

cdf(‘name’,x,A,B)

y =

cdf(‘name’,x,A,B,C)

y =

cdf(pd,x)

y =

cdf(___,’upper’)

描述

y =

cdf(‘name’,x,A) 计算某种分布(由’name’定义,如’Normal’正态,

‘Poisson’泊松,

‘T’ t分布…)下,x值处的累计分布,A,B,C等为’name’函数的参数

y =

cdf(pd,x) 直接计算概率分布函数pd(probability

distribution) ,在x处的累计分布,实际上,这里的pd

已被’name’,

A定义好,举栗如下:

%

定义一个正态分布函数pd, 均值mu

= 0, 标准差sigma = 1.

mu = 0;

sigma = 1;

pd =

makedist(‘Normal’,mu,sigma);

%

定义x值

x = [-2,-1,0,1,2];

%

计算x值处的累计分布

y = cdf(pd,x)

y

=

0.0228 0.1587 0.5000 0.8413 0.9772

用第一种语句表达相同内容为:

y2 =

0.0228 0.1587 0.5000 0.8413 0.9772

t分布累积分布函数tcdf

(Student’stcumulative

distribution function)

%

事实上就是y = cdf(‘T’,x,A)函数

语法

p =

tcdf(x,nu)p =

tcdf(x,nu,’upper’)

描述

计算t分布在x值处的累积分布,nu是t分布的自由度

再举个栗子

mu = 1; % Population mean

sigma = 2;  % Population standard deviation

n = 100; % Sample size

x = normrnd(mu,sigma,n,1);  % Random sample from population

xbar = mean(x);  % Sample mean

s = std(x); % Sample standard deviation

t = (xbar – mu)/(s/sqrt(n)) %这里t分布出现了,正态分布总体与样本均值的差符合t分布

t =

1.0589

p = 1-tcdf(t,n-1) % Probability of larger t-statistic

p =

0.1461

该p值(即t函数的累积分布就是t检验在相同x值处的概率ptest)

[h,ptest] = ttest(x,mu,0.05,’right’)

h =

0

ptest =

0.1461

概率密度函数pdf (Probability

density functions)

搞懂了累积分布函数cdf,这个就没什么需要多说了

语法

y =

pdf(‘name’,x,A)

y =

pdf(‘name’,x,A,B)

y =

pdf(‘name’,x,A,B,C)

y =

pdf(pd,x)

举例

%

定义一个正态分布函数pd, 均值mu

= 0, 标准差sigma = 1.

mu =

0;

sigma =

1;

pd =

makedist(‘Normal’,mu,sigma);

%

定义x值

x = [-2

-1 0 1 2];

%

计算x值处的概率密度(cdf是累计分布)

y = pdf

(pd,x)

y

=

0.0540 0.2420 0.3989 0.2420 0.0540

同样,另一种表达

y = pdf(pd,x)

y =

0.0540 0.2420 0.3989 0.2420 0.0540

t分布概率密度函数tpdf(Student’s

t probability density function)

语法

y =

tpdf(x,nu)

举例

tpdf(0,1:6)

ans =

0.3183 0.3536 0.3676 0.3750 0.3796 0.3827

相反,还可以通过p求t分布的t值

tinv (Student’s t inverse cumulative distribution

function)

语法

x = tinv(p,nu)

举例

% the 99th percentile of the Student’s t distribution for one to

six degrees of freedom

percentile = tinv(0.99,1:6)

percentile =

31.8205 6.9646 4.5407 3.7469 3.3649 3.1427

有一个问题,Matlab有一个inv矩阵求逆函数,不知与tinv什么关系,莫非tinv是在t分布下调用了inv计算程序?但p并不等是t的逆矩阵啊(即t*p

= E)啊?求解答

inv是矩阵求逆的意思。具体用法A=inv(B),其中B是输入的可逆矩阵,输出A就是B的逆矩阵,逆矩阵满足性质 AB=BA=E

(E是单位阵)。如果输入的是不可逆矩阵会弹出警告,并返回inf。

调用举例:

>> inv([1 0;0 0])

警告: 矩阵为奇异工作精度。

ans =

Inf Inf

Inf Inf

>> inv(rand(2))

ans =

-13.0929 5.2640

12.0501 -3.3159

另附官方英文解释(输入doc inv也可以自己查看):

Y = inv(X) returns theinverse of the square matrix X. A warning

messageis printed if X is badly scaled or nearly singular.

In practice, it is seldom necessary to form the explicit

inverseof a matrix. A frequent misuse of inv arises whensolving the

system of linear equations Ax = b.One way to solve this is with x =

inv(A)*b.A better way, from both an execution time and numerical

accuracy standpoint,is to use the matrix division operator x =

Ab.This produces the solution using Gaussian elimination, without

formingthe inverse. See mldivide ()for further information.