Debian Codegraph @

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

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