Debian Codegraph @

contrib/ms-sbvar/TZcode/MatlabFiles/find_betapar.m

raw
1 function [a, b, XLO, XUP] = find_betapar(XLO, XUP, PLO, PUP, a0, b0);
2
3 % This function takes as inputs the bounds [XLO, XUP] in the support of the
4 % Beta distribution (with unknown parameters a and b), the
5 % probabilities of the bounds [PLO, PUP], and the initial values for ab=[a0, b0]
6 % and returns the estimates of a and b (as well as XLO and XUP)
7 % by solving the non-linear functions in betapar(ab, XLO, XUP, PLO, PUP).
8
9 %-----------------------------------------------------------------------------------
10 %------------------------------- Beta distribution --------------------------------%
11 %--- p(x) = ( Gamma(a+b)/(Gamma(a)*Gamma(b)) ) x^(a-1) (1-x)^(b-1)
12 % = (1/Beta(a, b))x^(a-1) (1-x)^(b-1) for a>0 and b>0, and
13 % 0<=x<=1.
14 %--- E(x) = a/(a+b); var(x) = a*b/( (a+b)^2*(a+b+1) );
15 %--- The density is finite if a,b>=1.
16 %--- Noninformative density: (1) a=b=1; (2) a=b=0.5; or (3) a=b=0.
17 %-----------------------------------------------------------------------------------
18 %
19 % Copyright (C) 1997-2012 Tao Zha
20 %
21 % This free software: you can redistribute it and/or modify
22 % it under the terms of the GNU General Public License as published by
23 % the Free Software Foundation, either version 3 of the License, or
24 % (at your option) any later version.
25 %
26 % It is distributed in the hope that it will be useful,
27 % but WITHOUT ANY WARRANTY; without even the implied warranty of
28 % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 % GNU General Public License for more details.
30 %
31 % If you did not received a copy of the GNU General Public License
32 % with this software, see <http://www.gnu.org/licenses/>.
33 %
34
35
36 if XLO >= XUP;
37 error('the lower bound needs to be smaller than the upper bound')
38 elseif XLO<=0 or XUP >=1;
39 error('the support for Beta distribution needs to be between 0 and 1');
40 end;
41
42 if a0 <= 0 || b0 <= 0;
43 error('the values for a and b need to be positive');
44 end;
45
46 disp(' ')
47 disp('*************** Convergence results for beta density ***************')
48 options = optimset('Display', 'on','TolFun', 1.0e-10, 'TolX', 1.0e-10);
49 ab_values = fsolve('betapar', [a0, b0], options, XLO, XUP, PLO, PUP);
50 a = ab_values(1); b = ab_values(2);
51
52 % Alternatively, it is possible to constrain the search for the values of a
53 % and b in the positive range (with 0 being the explicit lower bound) by
54 % using the lsqnonlin function (see below) instead of the fsolve. The
55 % tradeoff is that lsqnonlin is typically slower than fsolve.
56 % LB_a = 0; LB_b = 0; UB_a = Inf; UB_b = Inf;
57 % ab_values = lsqnonlin('betapar', [a0, b0], [LB_a, LB_b], [UB_a, UB_b],...
58 % options, XLO, XUP, PLO, PUP);
59 % a = ab_values(1); b = ab_values(2);