From 0459d1bcd4a669e42e1e9bf6005fdff131e7eaea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Hermes=29?= Date: Thu, 11 Feb 2016 21:35:29 +0100 Subject: [PATCH] Fixed bug in Weibull prior. Fixed inconsistencies in the ordering of the hyperparameters (shape and scale). --- matlab/distributions/compute_prior_mode.m | 12 +++++++----- matlab/distributions/gamma_specification.m | 4 ++-- matlab/distributions/lpdfgweibull.m | 4 ++-- matlab/distributions/weibull_specification.m | 6 +++--- matlab/lpdfgam.m | 4 ++-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/matlab/distributions/compute_prior_mode.m b/matlab/distributions/compute_prior_mode.m index 434fb6c4a..86ee121c0 100644 --- a/matlab/distributions/compute_prior_mode.m +++ b/matlab/distributions/compute_prior_mode.m @@ -58,6 +58,8 @@ switch shape 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 @@ -85,12 +87,12 @@ switch shape m = m + hyperparameters(3) ; end case 8 - % s = hyperparameters(1) [scale parameter] - % k = hyperparameters(2) [shape parameter] - if hyperparameters(2)<=1 + % k = hyperparameters(1) [shape parameter] + % s = hyperparameters(2) [scale parameter] + if hyperparameters(1)<=1 m = 0; else - m = hyperparameters(1)*((hyperparameters(2)-1)/hyperparameters(2))^(1/hyperparameters(2)); + m = hyperparameters(2)*((hyperparameters(1)-1)/hyperparameters(1))^(1/hyperparameters(1)); end if length(hyperparameters)>2 % Add location parameter @@ -212,7 +214,7 @@ end %$ % Weibull density %$ try %$ m1 = compute_prior_mode([1 1],8); -%$ m2 = compute_prior_mode([1 2 1],8); % Wolfram Alpha: WeibullDistribution[2,1] +%$ m2 = compute_prior_mode([2 1 1],8); % Wolfram Alpha: WeibullDistribution[2,1] %$ t(1) = true; %$ catch %$ t(1) = false; diff --git a/matlab/distributions/gamma_specification.m b/matlab/distributions/gamma_specification.m index 20f002975..f5bccc309 100644 --- a/matlab/distributions/gamma_specification.m +++ b/matlab/distributions/gamma_specification.m @@ -9,8 +9,8 @@ function [a, b] = gamma_specification(mu, sigma2, lb, name) % --*-- Unitary te % - name [string] Name of the parameter (or random variable). % % OUTPUTS -% - a [double] First hyperparameter of the Gamma density. -% - b [double] Second hyperparameter of the Gamma density. +% - a [double] First hyperparameter of the Gamma density (shape). +% - b [double] Second hyperparameter of the Gamma density (scale). % Copyright (C) 2015 Dynare Team % diff --git a/matlab/distributions/lpdfgweibull.m b/matlab/distributions/lpdfgweibull.m index cdfc4ef7e..bb8fbaa18 100644 --- a/matlab/distributions/lpdfgweibull.m +++ b/matlab/distributions/lpdfgweibull.m @@ -4,8 +4,8 @@ function [ldens,Dldens,D2ldens] = lpdfgweibull(x,a,b,c) % --*-- Unitary tests - % % INPUTS % - x [double] m*n matrix of points where the (logged) density will be evaluated, -% - a [double] m*n matrix of First Weibull distribution parameters, -% - b [double] m*n matrix of Second Weibull distribution parameters, +% - a [double] m*n matrix of First Weibull distribution parameters (shape parameter, k), +% - b [double] m*n matrix of Second Weibull distribution parameters (scale parameter, λ), % - c [double] m*n matrix of Third Weibull distribution parameters (location parameter, default is 0). % % OUTPUTS diff --git a/matlab/distributions/weibull_specification.m b/matlab/distributions/weibull_specification.m index 2b9136fc2..78b644d44 100644 --- a/matlab/distributions/weibull_specification.m +++ b/matlab/distributions/weibull_specification.m @@ -1,4 +1,4 @@ -function [scale, shape] = weibull_specification(mu, sigma2, lb, name) % --*-- Unitary tests --*-- +function [shape, scale] = weibull_specification(mu, sigma2, lb, name) % --*-- Unitary tests --*-- % Returns the hyperparameters of the Weibull distribution given the expectation and variance. % @@ -95,7 +95,7 @@ scale = mu/gamma(1+1/shape); %$ disp(sprintf('... mu=%s and s2=%s', num2str(mu(j,i)),num2str(s2(j,i)))) %$ end %$ if ~isnan(mu(j,i)) && ~isnan(s2(j,i)) && ~isinf(mu(j,i)) && ~isinf(s2(j,i)) -%$ [scale, shape] = weibull_specification(mu(j,i), s2(j,i)); +%$ [shape, scale] = weibull_specification(mu(j,i), s2(j,i)); %$ if isnan(scale) %$ t = false; %$ else @@ -105,7 +105,7 @@ scale = mu/gamma(1+1/shape); %$ t = false; %$ end %$ end -%$ if ~t +%$ if ~t && debug %$ failed1 = [failed1; mu(j,i) s2(j,i)]; %$ failed1_ = [failed1_; shapes(i) scales(j)]; %$ error('UnitTest','Cannot compute scale and shape hyperparameters for mu=%s and s2=%s', num2str(mu(j,i)), num2str(s2(j,i))) diff --git a/matlab/lpdfgam.m b/matlab/lpdfgam.m index 037284456..d9d7a3883 100644 --- a/matlab/lpdfgam.m +++ b/matlab/lpdfgam.m @@ -3,8 +3,8 @@ function [ldens,Dldens,D2ldens] = lpdfgam(x,a,b); % % INPUTS % x [double] m*n matrix of locations, -% a [double] m*n matrix or scalar, First GAMMA distribution parameters, -% b [double] m*n matrix or scalar, Second GAMMA distribution parameters, +% a [double] m*n matrix or scalar, First GAMMA distribution parameters (shape), +% b [double] m*n matrix or scalar, Second GAMMA distribution parameters (scale), % % OUTPUTS % ldens [double] m*n matrix of logged GAMMA densities evaluated at x.