Debian Codegraph @

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

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