1function bounds = prior_bounds(bayestopt, prior_trunc)
2
3%@info:
4%! @deftypefn {Function File} {@var{bounds} =} prior_bounds (@var{bayesopt},@var{option})
5%! @anchor{prior_bounds}
6%! @sp 1
7%! Returns bounds for the prior densities. For each estimated parameter the lower and upper bounds
8%! are such that the defined intervals contains a probability mass equal to 1-2*@var{option}.prior_trunc. The
9%! default value for @var{option}.prior_trunc is 1e-10 (set in @ref{global_initialization}).
10%! @sp 2
11%! @strong{Inputs}
12%! @sp 1
13%! @table @ @var
14%! @item bayestopt
15%! Matlab's structure describing the prior distribution (initialized by @code{dynare}).
16%! @item option
17%! Matlab's structure describing the options (initialized by @code{dynare}).
18%! @end table
19%! @sp 2
20%! @strong{Outputs}
21%! @sp 1
22%! @table @ @var
23%! @item bounds
24%! A structure with two fields lb and up (p*1 vectors of doubles, where p is the number of estimated parameters) for the lower and upper bounds.
25%! @end table
26%! @sp 2
27%! @strong{This function is called by:}
28%! @sp 1
29%! @ref{get_prior_info}, @ref{dynare_estimation_1}, @ref{dynare_estimation_init}
30%! @sp 2
31%! @strong{This function calls:}
32%! @sp 1
33%! None.
34%! @end deftypefn
35%@eod:
36
37
38% function bounds = prior_bounds(bayestopt)
39% computes bounds for prior density.
40%
41% INPUTS
42%    bayestopt  [structure]  characterizing priors (shape, mean, p1..p4)
43%
44% OUTPUTS
45%    bounds     [double]      structure specifying prior bounds (lb and ub fields)
46%
47% SPECIAL REQUIREMENTS
48%    none
49
50% Copyright (C) 2003-2017 Dynare Team
51%
52% This file is part of Dynare.
53%
54% Dynare is free software: you can redistribute it and/or modify
55% it under the terms of the GNU General Public License as published by
56% the Free Software Foundation, either version 3 of the License, or
57% (at your option) any later version.
58%
59% Dynare is distributed in the hope that it will be useful,
60% but WITHOUT ANY WARRANTY; without even the implied warranty of
61% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
62% GNU General Public License for more details.
63%
64% You should have received a copy of the GNU General Public License
65% along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
66
67pshape = bayestopt.pshape;
68p3 = bayestopt.p3;
69p4 = bayestopt.p4;
70p6 = bayestopt.p6;
71p7 = bayestopt.p7;
72
73bounds.lb = zeros(length(p6),1);
74bounds.ub = zeros(length(p6),1);
75
76for i=1:length(p6)
77    switch pshape(i)
78      case 1
79        if prior_trunc == 0
80            bounds.lb(i) = p3(i);
81            bounds.ub(i) = p4(i);
82        else
83            bounds.lb(i) = betainv(prior_trunc,p6(i),p7(i))*(p4(i)-p3(i))+p3(i);
84            bounds.ub(i) = betainv(1-prior_trunc,p6(i),p7(i))*(p4(i)-p3(i))+p3(i);
85        end
86      case 2
87        if prior_trunc == 0
88            bounds.lb(i) = p3(i);
89            bounds.ub(i) = Inf;
90        else
91            try
92                bounds.lb(i) = gaminv(prior_trunc,p6(i),p7(i))+p3(i);
93                bounds.ub(i) = gaminv(1-prior_trunc,p6(i),p7(i))+p3(i);
94            catch
95                % Workaround for ticket #161, see http://savannah.gnu.org/bugs/?52569
96                if isoctave
97                    error(['Due to a computational limitation in Octave, the prior bounds cannot be computed. You must either use prior_trunc=0 or choose other values for mean and/or variance of your prior on ' bayestopt.name{i} ', or use another shape'])
98                else
99                    rethrow(lasterror)
100                end
101            end
102        end
103      case 3
104        if prior_trunc == 0
105            bounds.lb(i) = -Inf;
106            bounds.ub(i) = Inf;
107        else
108            bounds.lb(i) = norminv(prior_trunc,p6(i),p7(i));
109            bounds.ub(i) = norminv(1-prior_trunc,p6(i),p7(i));
110        end
111      case 4
112        if prior_trunc == 0
113            bounds.lb(i) = p3(i);
114            bounds.ub(i) = Inf;
115        else
116            try
117                bounds.lb(i) = 1/sqrt(gaminv(1-prior_trunc, p7(i)/2, 2/p6(i)))+p3(i);
118                bounds.ub(i) = 1/sqrt(gaminv(prior_trunc, p7(i)/2, 2/p6(i)))+p3(i);
119            catch
120                % Workaround for ticket #161, see http://savannah.gnu.org/bugs/?52569
121                if isoctave
122                    error(['Due to a computational limitation in Octave, the prior bounds cannot be computed. You must either use prior_trunc=0 or choose other values for mean and/or variance of your prior on ' bayestopt.name{i} ', or use another shape'])
123                else
124                    rethrow(lasterror)
125                end
126            end
127        end
128      case 5
129        if prior_trunc == 0
130            bounds.lb(i) = p6(i);
131            bounds.ub(i) = p7(i);
132        else
133            bounds.lb(i) = p6(i)+(p7(i)-p6(i))*prior_trunc;
134            bounds.ub(i) = p7(i)-(p7(i)-p6(i))*prior_trunc;
135        end
136      case 6
137        if prior_trunc == 0
138            bounds.lb(i) = p3(i);
139            bounds.ub(i) = Inf;
140        else
141            try
142                bounds.lb(i) = 1/gaminv(1-prior_trunc, p7(i)/2, 2/p6(i))+p3(i);
143                bounds.ub(i) = 1/gaminv(prior_trunc, p7(i)/2, 2/p6(i))+ p3(i);
144            catch
145                % Workaround for ticket #161, see http://savannah.gnu.org/bugs/?52569
146                if isoctave
147                    error(['Due to a computational limitation in Octave, the prior bounds cannot be computed. You must either use prior_trunc=0 or choose other values for mean and/or variance of your prior on ' bayestopt.name{i} ', or use another shape'])
148                else
149                    rethrow(lasterror)
150                end
151            end
152        end
153      case 8
154        if prior_trunc == 0
155            bounds.lb(i) = p3(i);
156            bounds.ub(i) = Inf;
157        else
158            bounds.lb(i) = p3(i)+wblinv(prior_trunc,p6(i),p7(i));
159            bounds.ub(i) = p3(i)+wblinv(1-prior_trunc,p6(i),p7(i));
160        end
161      otherwise
162        error(sprintf('prior_bounds: unknown distribution shape (index %d, type %d)', i, pshape(i)));
163    end
164end