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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  
     | 
    
      function m = compute_prior_mode(hyperparameters,shape) % --*-- Unitary tests --*--
% This function computes the mode of the prior distribution given the (two, three or four) hyperparameters
% of the prior distribution.
%
% INPUTS
%   hyperparameters     [double]    1*n vector of hyper parameters.
%   shape               [integer]   scalar specifying the prior shape:
%                                     shape=1 => Beta distribution,
%                                     shape=2 => Gamma distribution,
%                                     shape=3 => Gaussian distribution,
%                                     shape=4 => Inverse Gamma (type 1) distribution,
%                                     shape=5 => Uniform distribution,
%                                     shape=6 => Inverse Gamma (type 2) distribution,
%                                     shape=8 => Weibull distribution.
%
% OUTPUTS
%   m       [double]    scalar or 2*1 vector, the prior mode.
%
% REMARKS
% [1] The size of the vector of hyperparameters is 3 when the Gamma or Inverse Gamma is shifted and 4 when
%     the support of the Beta distribution is not [0,1].
% [2] The hyperparameters of the uniform distribution are the lower and upper bounds.
% [3] The uniform distribution has an infinity of modes. In this case the function returns the prior mean.
% [4] For the beta distribution we can have 1, 2 or an infinity of modes.
% Copyright (C) 2009-2017 Dynare Team
%
% This file is part of Dynare.
%
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
%
% You should have received a copy of the GNU General Public License
% along with Dynare.  If not, see <https://www.gnu.org/licenses/>.
m = NaN ;
switch shape
  case 1
    if (hyperparameters(1)>1 && hyperparameters(2)>1)
        m = (hyperparameters(1)-1)/(hyperparameters(1)+hyperparameters(2)-2) ;
    elseif (hyperparameters(1)<1 && hyperparameters(2)<1)
        m = [ 0 ; 1 ] ;
    elseif ( hyperparameters(1)<1 && hyperparameters(2)>1-eps ) || ( abs(hyperparameters(1)-1)<2*eps && hyperparameters(2)>1 )
        m = 0;
    elseif ( hyperparameters(1)>1 && hyperparameters(2)<1+eps ) || ( abs(hyperparameters(1)-1)<2*eps && hyperparameters(2)<1 )
        m = 1;
    elseif ( abs(hyperparameters(1)-1)<2*eps && abs(hyperparameters(2)-1)<2*eps )% Uniform distribution!
        m = .5 ;
    end
    if length(hyperparameters)==4
        m = m*(hyperparameters(4)-hyperparameters(3)) + hyperparameters(3) ;
    end
  case 2
    % a = hyperparameters(1) [shape parameter]
    % b = hyperparameters(2) [scale parameter]
    if hyperparameters(1)<1
        m = 0;
    else
        m = (hyperparameters(1)-1)*hyperparameters(2);
    end
    if length(hyperparameters)>2
        m = m + hyperparameters(3);
    end
  case 3
    m = hyperparameters(1);
  case 4
    % s  = hyperparameters(1)
    % nu = hyperparameters(2)
    m = 1/sqrt((hyperparameters(2)+1)/hyperparameters(1));
    if length(hyperparameters)>2
        m = m + hyperparameters(3);
    end
  case 5
    m = hyperparameters(1);
  case 6
    % s  = hyperparameters(1)
    % nu = hyperparameters(2)
    m = hyperparameters(1)/(hyperparameters(2)+2) ;
    if length(hyperparameters)>2
        m = m + hyperparameters(3) ;
    end
  case 8
    % k = hyperparameters(1) [shape parameter]
    % s = hyperparameters(2) [scale parameter]
    if hyperparameters(1)<=1
        m = 0;
    else
        m = hyperparameters(2)*((hyperparameters(1)-1)/hyperparameters(1))^(1/hyperparameters(1));
    end
    if length(hyperparameters)>2
        % Add location parameter
        m = m + hyperparameters(3) ;
    end
  otherwise
    error('Unknown prior shape!')
end
%@test:1
%$ % Beta density
%$ try
%$     m1 = compute_prior_mode([2 1],1);
%$     m2 = compute_prior_mode([2 5 1 4],1); % Wolfram Alpha: BetaDistribution[2,5]
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,1,1e-6);
%$     t(3) = dassert(m2,1/5*3+1,1e-6);
%$ end
%$ T = all(t);
%@eof:1
%@test:2
%$ % Gamma density
%$ try
%$     m1 = compute_prior_mode([1 2],2);
%$     m2 = compute_prior_mode([9 0.5 1],2);  % Wolfram Alpha: GammaDistribution[9,0.5]
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,0,1e-6);
%$     t(3) = dassert(m2,4+1,1e-6);
%$ end
%$ T = all(t);
%@eof:2
%@test:3
%$ % Normal density
%$ try
%$     m1 = compute_prior_mode([1 1],3);
%$     m2 = compute_prior_mode([2 5],3);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,1,1e-6);
%$     t(3) = dassert(m2,2,1e-6);
%$ end
%$ T = all(t);
%@eof:3
%@test:4
%$ % Inverse Gamma I density
%$ try
%$     m1 = compute_prior_mode([8 2],4);
%$     m2 = compute_prior_mode([8 2 1],4);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,1.632993161855452,1e-6);
%$     t(3) = dassert(m2,1.632993161855452+1,1e-6);
%$ end
%$ T = all(t);
%@eof:4
%@test:5
%$ % Uniform density
%$ try
%$     m1 = compute_prior_mode([0.5 1/sqrt(12)],5);
%$     m2 = compute_prior_mode([2 5 1 2],5);
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,0.5,1e-6);
%$     t(3) = dassert(m2,2,1e-6);
%$ end
%$ T = all(t);
%@eof:5
%@test:6
%$ % Inverse Gamma II density, parameterized with s and nu where  s=2*beta and nu=2*alpha
%$ try
%$     m1 = compute_prior_mode([8 2],6);  % Wolfram Alpha, parameterized with alpha beta: InversegammaDistribution[1,4]
%$     m2 = compute_prior_mode([8 4 1],6); % Wolfram Alpha, parameterized with alpha beta: InversegammaDistribution[2,4]
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,2,1e-6);
%$     t(3) = dassert(m2,1+4/3,1e-6);
%$ end
%$ T = all(t);
%@eof:6
%@test:7
%$ % Weibull density
%$ try
%$     m1 = compute_prior_mode([1 1],8);
%$     m2 = compute_prior_mode([2 1 1],8); % Wolfram Alpha: WeibullDistribution[2,1]
%$     t(1) = true;
%$ catch
%$     t(1) = false;
%$ end
%$
%$ % Check the results
%$ if t(1)
%$     t(2) = dassert(m1,0,1e-6);
%$     t(3) = dassert(m2,1+1/sqrt(2),1e-6);
%$ end
%$ T = all(t);
%@eof:7
%@test:8
%$ % Unknown density
%$ try
%$     m1 = compute_prior_mode([1 1],7);
%$     t(1) = false;
%$ catch
%$     t(1) = true;
%$ end
%$
%$ T = all(t);
%@eof:8
 
     |