帕斯卡分布的matlab实现
时间: 2023-11-13 14:55:12 浏览: 116
好的,以下是帕斯卡分布的 Matlab 实现代码:
```matlab
function p = pascalpdf(x, r, p)
% PASCALPDF Pascal probability density function (pdf).
% P = PASCALPDF(X,R,P) returns the pdf of the Pascal distribution with
% parameters R and P, evaluated at the values in X.
%
% The size of P is the common size of the input arguments. A scalar input
% functions as a constant matrix of the same size as the other inputs.
%
% Default values for R and P are 1 and 0.5, respectively.
%
% Distribution: Discrete, bounded, positive integers
% Restrictions:
% R >= 1 (integer)
% 0 < P < 1
%
% See also PASCALCDF, PASCALINV, PASCALSTAT, PASCALFIT,
% PASCALLIKE, PASCALRND, PASCALSF, PASCALHAZ
%
% Mike Sheppard
% Last Modified 26-Dec-2011
if nargin < 1
error('pascalpdf:TooFewInputs','Requires at least one input argument.');
end
if nargin==1
r=1;
p=0.5;
elseif nargin==2
p=0.5;
end
[errorcode x r p] = distchck(3,x,r,p);
if errorcode > 0
error('pascalpdf:InputSizeMismatch',...
'Requires non-scalar arguments to match in size.');
end
% Initialize P to zero.
if isa(x,'single') || isa(r,'single') || isa(p,'single')
p=zeros(size(x),'single');
else
p=zeros(size(x));
end
% Return NaN for out of range parameters.
p(r<1 | p<=0 | p>=1 | x<0 | x~=round(x))=NaN;
k=find(r>=1 & p>0 & p<1 & x>=0 & x==round(x));
if any(k)
%Modified from stats/nbinpdf.m by Sheppard
rk=r(k); pk=p(k);
xk=x(k);
term = betaln(rk+xk,rk) - log(pk) - betaln(xk+1,rk);
p(k) = exp(term);
end
%Catch round off
p(p<0)=0;
end
```
其中,x 表示随机变量的取值,r 表示成功次数,p 表示成功概率。