-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbetapdf.m
45 lines (38 loc) · 1.55 KB
/
betapdf.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function y = betapdf(x,a,b)
%BETAPDF Beta probability density function.
% Y = BETAPDF(X,A,B) returns the beta probability density
% function with parameters A and B at the values in X.
%
% The size of Y is the common size of the input arguments. A scalar input
% functions as a constant matrix of the same size as the other inputs.
%
% See also BETACDF, BETAFIT, BETAINV, BETALIKE, BETARND, BETASTAT, PDF,
% BETA, BETALN.
% References:
% [1] M. Abramowitz and I. A. Stegun, "Handbook of Mathematical
% Functions", Government Printing Office, 1964, 26.1.33.
% Copyright 1993-2009 The MathWorks, Inc.
% $Revision: 2.11.2.8 $ $Date: 2009/03/09 19:49:31 $
if nargin < 3
error('stats:betapdf:TooFewInputs','Requires three input arguments.');
end
% Return NaN for out of range parameters.
a(a<=0) = NaN;
b(b<=0) = NaN;
% Out of range x could create a spurious NaN*i part to y, prevent that.
% These entries will get set to zero later.
xOutOfRange = (x<0) | (x>1);
x(xOutOfRange) = .5;
try
% When a==1, the density has a limit of beta(a,b) at x==0, and
% similarly when b==1 at x==1. Force that, instead of 0*log(0) = NaN.
logkerna = (a-1).*log(x); logkerna(a==1 & x==0) = 0;
logkernb = (b-1).*log(1-x); logkernb(b==1 & x==1) = 0;
y = exp(logkerna+logkernb - betaln(a,b));
catch
error('stats:betapdf:InputSizeMismatch',...
'Non-scalar arguments must match in size.');
end
% Fill in for the out of range x values, but don't overwrite NaNs from
% nonpositive params.
y(xOutOfRange & ~isnan(a) & ~isnan(b)) = 0;