From 7aadec0ec542c7f277a32e793d665a39748bd390 Mon Sep 17 00:00:00 2001 From: adjemian Date: Tue, 9 Sep 2008 21:47:13 +0000 Subject: [PATCH] v4.1: + Moved functions located in ./distribution to ./distributions/toolbox + Moved distribution related functions to ./distributions git-svn-id: https://www.dynare.org/svn/dynare/dynare_v4@2055 ac1d8469-bf42-47a9-8791-bf33cf982152 --- matlab/distributions/{ => toolbox}/betacdf.m | 0 matlab/distributions/{ => toolbox}/betainv.m | 0 matlab/distributions/{ => toolbox}/betapdf.m | 0 matlab/distributions/{ => toolbox}/betarnd.m | 0 matlab/distributions/{ => toolbox}/chi2inv.m | 0 .../distributions/{ => toolbox}/common_size.m | 0 matlab/distributions/{ => toolbox}/gamcdf.m | 0 matlab/distributions/{ => toolbox}/gaminv.m | 0 matlab/distributions/{ => toolbox}/gampdf.m | 0 matlab/distributions/{ => toolbox}/gamrnd.m | 0 matlab/distributions/{ => toolbox}/normcdf.m | 0 matlab/distributions/{ => toolbox}/norminv.m | 0 matlab/distributions/{ => toolbox}/normpdf.m | 0 .../{ => toolbox}/stdnormal_cdf.m | 0 .../{ => toolbox}/stdnormal_inv.m | 0 .../{ => toolbox}/stdnormal_pdf.m | 0 matlab/dynare_config.m | 3 +- matlab/inverse_gamma_specification.m | 76 ------------------- matlab/multivariate_normal_pdf.m | 36 --------- matlab/multivariate_student_pdf.m | 36 --------- matlab/rand_inverse_wishart.m | 53 ------------- matlab/rand_matrix_normal.m | 43 ----------- matlab/rand_multivariate_normal.m | 34 --------- matlab/rand_multivariate_student.m | 39 ---------- 24 files changed, 2 insertions(+), 318 deletions(-) rename matlab/distributions/{ => toolbox}/betacdf.m (100%) rename matlab/distributions/{ => toolbox}/betainv.m (100%) rename matlab/distributions/{ => toolbox}/betapdf.m (100%) rename matlab/distributions/{ => toolbox}/betarnd.m (100%) rename matlab/distributions/{ => toolbox}/chi2inv.m (100%) rename matlab/distributions/{ => toolbox}/common_size.m (100%) rename matlab/distributions/{ => toolbox}/gamcdf.m (100%) rename matlab/distributions/{ => toolbox}/gaminv.m (100%) rename matlab/distributions/{ => toolbox}/gampdf.m (100%) rename matlab/distributions/{ => toolbox}/gamrnd.m (100%) rename matlab/distributions/{ => toolbox}/normcdf.m (100%) rename matlab/distributions/{ => toolbox}/norminv.m (100%) rename matlab/distributions/{ => toolbox}/normpdf.m (100%) rename matlab/distributions/{ => toolbox}/stdnormal_cdf.m (100%) rename matlab/distributions/{ => toolbox}/stdnormal_inv.m (100%) rename matlab/distributions/{ => toolbox}/stdnormal_pdf.m (100%) delete mode 100644 matlab/inverse_gamma_specification.m delete mode 100644 matlab/multivariate_normal_pdf.m delete mode 100644 matlab/multivariate_student_pdf.m delete mode 100644 matlab/rand_inverse_wishart.m delete mode 100644 matlab/rand_matrix_normal.m delete mode 100644 matlab/rand_multivariate_normal.m delete mode 100644 matlab/rand_multivariate_student.m diff --git a/matlab/distributions/betacdf.m b/matlab/distributions/toolbox/betacdf.m similarity index 100% rename from matlab/distributions/betacdf.m rename to matlab/distributions/toolbox/betacdf.m diff --git a/matlab/distributions/betainv.m b/matlab/distributions/toolbox/betainv.m similarity index 100% rename from matlab/distributions/betainv.m rename to matlab/distributions/toolbox/betainv.m diff --git a/matlab/distributions/betapdf.m b/matlab/distributions/toolbox/betapdf.m similarity index 100% rename from matlab/distributions/betapdf.m rename to matlab/distributions/toolbox/betapdf.m diff --git a/matlab/distributions/betarnd.m b/matlab/distributions/toolbox/betarnd.m similarity index 100% rename from matlab/distributions/betarnd.m rename to matlab/distributions/toolbox/betarnd.m diff --git a/matlab/distributions/chi2inv.m b/matlab/distributions/toolbox/chi2inv.m similarity index 100% rename from matlab/distributions/chi2inv.m rename to matlab/distributions/toolbox/chi2inv.m diff --git a/matlab/distributions/common_size.m b/matlab/distributions/toolbox/common_size.m similarity index 100% rename from matlab/distributions/common_size.m rename to matlab/distributions/toolbox/common_size.m diff --git a/matlab/distributions/gamcdf.m b/matlab/distributions/toolbox/gamcdf.m similarity index 100% rename from matlab/distributions/gamcdf.m rename to matlab/distributions/toolbox/gamcdf.m diff --git a/matlab/distributions/gaminv.m b/matlab/distributions/toolbox/gaminv.m similarity index 100% rename from matlab/distributions/gaminv.m rename to matlab/distributions/toolbox/gaminv.m diff --git a/matlab/distributions/gampdf.m b/matlab/distributions/toolbox/gampdf.m similarity index 100% rename from matlab/distributions/gampdf.m rename to matlab/distributions/toolbox/gampdf.m diff --git a/matlab/distributions/gamrnd.m b/matlab/distributions/toolbox/gamrnd.m similarity index 100% rename from matlab/distributions/gamrnd.m rename to matlab/distributions/toolbox/gamrnd.m diff --git a/matlab/distributions/normcdf.m b/matlab/distributions/toolbox/normcdf.m similarity index 100% rename from matlab/distributions/normcdf.m rename to matlab/distributions/toolbox/normcdf.m diff --git a/matlab/distributions/norminv.m b/matlab/distributions/toolbox/norminv.m similarity index 100% rename from matlab/distributions/norminv.m rename to matlab/distributions/toolbox/norminv.m diff --git a/matlab/distributions/normpdf.m b/matlab/distributions/toolbox/normpdf.m similarity index 100% rename from matlab/distributions/normpdf.m rename to matlab/distributions/toolbox/normpdf.m diff --git a/matlab/distributions/stdnormal_cdf.m b/matlab/distributions/toolbox/stdnormal_cdf.m similarity index 100% rename from matlab/distributions/stdnormal_cdf.m rename to matlab/distributions/toolbox/stdnormal_cdf.m diff --git a/matlab/distributions/stdnormal_inv.m b/matlab/distributions/toolbox/stdnormal_inv.m similarity index 100% rename from matlab/distributions/stdnormal_inv.m rename to matlab/distributions/toolbox/stdnormal_inv.m diff --git a/matlab/distributions/stdnormal_pdf.m b/matlab/distributions/toolbox/stdnormal_pdf.m similarity index 100% rename from matlab/distributions/stdnormal_pdf.m rename to matlab/distributions/toolbox/stdnormal_pdf.m diff --git a/matlab/dynare_config.m b/matlab/dynare_config.m index 5892ea0a4..81fff689b 100644 --- a/matlab/dynare_config.m +++ b/matlab/dynare_config.m @@ -37,10 +37,11 @@ if nargin end dynareroot = strrep(which('dynare.m'),'dynare.m',''); +addpath([dynareroot '/distributions/']) % Add path to distribution-related function if under Matlab % without the statistics toolbox if ~exist('OCTAVE_VERSION') && isempty(ver('stats')) - addpath([dynareroot '/distributions/']) + addpath([dynareroot '/distributions/toolbox/']) end if exist([dynareroot '/AIM'])==7 % Add path to G.Anderson AIM solver (added by GP July'08) diff --git a/matlab/inverse_gamma_specification.m b/matlab/inverse_gamma_specification.m deleted file mode 100644 index f2b858595..000000000 --- a/matlab/inverse_gamma_specification.m +++ /dev/null @@ -1,76 +0,0 @@ -function [s,nu] = inverse_gamma_specification(mu,sigma,type) - -% function [s,nu] = inverse_gamma_specification(mu,sigma,type) -% Specification of the inverse Gamma function parameters -% X ~ IG(s,nu) -% -% INPUTS -% mu: expectation -% sigma: standard deviation -% type=1: inverse Gamma 1 -% type=2: inverse Gamma 2 - -% OUTPUTS -% s: shape parameter -% nu: scale parameter -% -% SPECIAL REQUIREMENTS -% none - -% Copyright (C) 2003-2008 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 . - -sigma2 = sigma^2; -mu2 = mu^2; - -if type == 2; % Inverse Gamma 2 - nu = 2*(2+mu2/sigma2); - s = 2*mu*(1+mu2/sigma2); -elseif type == 1; % Inverse Gamma 1 - if sigma2 < Inf; - nu = sqrt(2*(2+mu2/sigma2)); - nu2 = 2*nu; - nu1 = 2; - err = 2*mu2*gamma(nu/2)^2-(sigma2+mu2)*(nu-2)*gamma((nu-1)/2)^2; - while abs(nu2-nu1) > 1e-12 - if err > 0 - nu1 = nu; - if nu < nu2 - nu = nu2; - else - nu = 2*nu; - nu2 = nu; - end - else - nu2 = nu; - end - nu = (nu1+nu2)/2; - err = 2*mu2*gamma(nu/2)^2-(sigma2+mu2)*(nu-2)*gamma((nu-1)/2)^2; - end - s = (sigma2+mu2)*(nu-2); - else; - nu = 2; - s = 2*mu2/pi; - end; -else; - s = -1; - nu = -1; -end; - -% 01/18/2004 MJ replaced fsolve with secant -% suppressed chck -% changed order of output parameters \ No newline at end of file diff --git a/matlab/multivariate_normal_pdf.m b/matlab/multivariate_normal_pdf.m deleted file mode 100644 index 66565f19d..000000000 --- a/matlab/multivariate_normal_pdf.m +++ /dev/null @@ -1,36 +0,0 @@ -function density = multivariate_normal_pdf(X,Mean,Sigma_upper_chol,n); -% Evaluates the density of a multivariate gaussian, with expectation Mean -% and variance Sigma_upper_chol'*Sigma_upper_chol, at X. -% -% -% INPUTS -% -% X [double] 1*n vector -% Mean [double] 1*n vector, expectation of the multivariate random variable. -% Sigma_upper_chol [double] n*n matrix, upper triangular Cholesky decomposition of Sigma (the covariance matrix). -% n [integer] dimension. -% -% OUTPUTS -% density [double] density -% -% SPECIAL REQUIREMENTS - -% Copyright (C) 2003-2008 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 . - density = (2*pi)^(-.5*n) * ... - prod(diag(Sigma_upper_chol))^(-1) * ... - exp(-.5*(X-Mean)*(Sigma_upper_chol\(transpose(Sigma_upper_chol)\transpose(X-Mean)))); \ No newline at end of file diff --git a/matlab/multivariate_student_pdf.m b/matlab/multivariate_student_pdf.m deleted file mode 100644 index 1865b41ae..000000000 --- a/matlab/multivariate_student_pdf.m +++ /dev/null @@ -1,36 +0,0 @@ -function density = multivariate_student_pdf(X,Mean,Sigma_upper_chol,df); -% Evaluates the density of a multivariate student, with expectation Mean, -% variance Sigma_upper_chol'*Sigma_upper_chol and degrees of freedom df, at X. -% -% INPUTS -% -% X [double] 1*n vector -% Mean [double] 1*n vector, expectation of the multivariate random variable. -% Sigma_upper_chol [double] n*n matrix, upper triangular Cholesky decomposition of Sigma (the "covariance matrix"). -% df [integer] degrees of freedom. -% -% OUTPUTS -% density [double] density. -% -% SPECIAL REQUIREMENTS - -% Copyright (C) 2003-2008 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 . - nn = length(X); - t1 = gamma( .5*(nn+df) ) / ( gamma( .5*nn ) * (df*pi)^(.5*nn) ) ; - t2 = t1 / prod(diag(Sigma_upper_chol)) ; - density = t2 / ( 1 + (X-Mean)*(Sigma_upper_chol\(transpose(Sigma_upper_chol)\transpose(X-Mean)))/df )^(.5*(nn+df)); \ No newline at end of file diff --git a/matlab/rand_inverse_wishart.m b/matlab/rand_inverse_wishart.m deleted file mode 100644 index f6de993e0..000000000 --- a/matlab/rand_inverse_wishart.m +++ /dev/null @@ -1,53 +0,0 @@ -function G = rand_inverse_wishart(m, v, H_inv_upper_chol) - -% function G = rand_inverse_wishart(m, v, H_inv_upper_chol) -% rand_inverse_wishart Pseudo random matrices drawn from an -% inverse Wishart distribution -% G = rand_inverse_wishart(m, v, H_inv_upper_chol) -% Returns an m-by-m matrix drawn from an inverse-Wishart distribution. -% -% INPUTS: -% m: dimension of G and H_inv_upper_chol. -% v: degrees of freedom, greater or equal than m. -% H_inv_chol: upper cholesky decomposition of the inverse of the -% matrix parameter. -% The upper cholesky of the inverse is requested here -% in order to avoid to recompute it at every random draw. -% H_inv_upper_chol = chol(inv(H)) -% OUTPUTS: -% G: G ~ IW(m, v, H) where H = inv(H_inv_upper_chol'*H_inv_upper_chol) -% or, equivalently, using the correspondence between Wishart and -% inverse-Wishart: inv(G) ~ W(m, v, S) where -% S = H_inv_upper_chol'*H_inv_upper_chol = inv(H) -% -% SPECIAL REQUIREMENT -% none -% - -% Copyright (C) 2003-2008 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 . - - X = randn(v, m) * H_inv_upper_chol; - - - % At this point, X'*X is Wishart distributed - % G = inv(X'*X); - - % Rather compute inv(X'*X) using the SVD - [U,S,V] = svd(X, 0); - SSi = 1 ./ (diag(S) .^ 2); - G = (V .* repmat(SSi', m, 1)) * V'; \ No newline at end of file diff --git a/matlab/rand_matrix_normal.m b/matlab/rand_matrix_normal.m deleted file mode 100644 index 32aa4bafd..000000000 --- a/matlab/rand_matrix_normal.m +++ /dev/null @@ -1,43 +0,0 @@ -function B = rand_matrix_normal(n, p, M, Omega_lower_chol, Sigma_lower_chol) - -% function B = rand_matrix_normal(n, p, M, Omega_lower_chol, Sigma_lower_chol) -% Pseudo random matrices drawn from a matrix-normal distribution -% B ~ MN_n*p(M, Omega, Sigma) -% Equivalent to vec(B) ~ N(vec(Mu), kron(Omega, Sigma)) -% -% INPUTS -% n: row -% p: column -% M: (n*p) matrix, mean -% Omega_lower_chol: (p*p), lower Cholesky decomposition of Omega, -% (Omega_lower_chol = chol(Omega, 'lower')) -% Sigma_lower_chol: (n*n), lower Cholesky decomposition of Sigma, -% (Sigma_lower_chol = chol(Sigma, 'lower')) -% -% OUTPUTS -% B: (n*p) matrix drawn from a Matrix-normal distribution -% -% SPECIAL REQUIREMENTS -% Same notations than: http://en.wikipedia.org/wiki/Matrix_normal_distribution - -% Copyright (C) 2003-2008 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 . - - B1 = randn(n * p, 1); - B2 = kron(Omega_lower_chol, Sigma_lower_chol) * B1; - B3 = reshape(B2, n, p); - B = B3 + M; diff --git a/matlab/rand_multivariate_normal.m b/matlab/rand_multivariate_normal.m deleted file mode 100644 index bd70c51df..000000000 --- a/matlab/rand_multivariate_normal.m +++ /dev/null @@ -1,34 +0,0 @@ -function draw = rand_multivariate_normal(Mean,Sigma_upper_chol,n) -% Pseudo random draws from a multivariate normal distribution, -% \mathcal N_n(Mean,Sigma), with expectation Mean and variance Sigma. -% -% INPUTS -% -% Mean [double] 1*n vector, expectation of the multivariate random variable. -% Sigma_upper_chol [double] n*n matrix, upper triangular Cholesky decomposition of Sigma (the covariance matrix). -% n [integer] dimension. -% -% OUTPUTS -% draw [double] 1*n vector drawn from a multivariate normal distribution with expectation Mean and -% covariance Sigma -% -% SPECIAL REQUIREMENTS - -% Copyright (C) 2003-2008 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 . - - draw = Mean + randn(1,n) * Sigma_upper_chol; diff --git a/matlab/rand_multivariate_student.m b/matlab/rand_multivariate_student.m deleted file mode 100644 index cc4fc0e75..000000000 --- a/matlab/rand_multivariate_student.m +++ /dev/null @@ -1,39 +0,0 @@ -function draw = rand_multivariate_student(Mean,Sigma_upper_chol,df) -% Pseudo random draws from a multivariate student distribution, -% with expectation Mean, variance Sigma*df/(df-2) and degrees of freedom df>0. -% -% INPUTS -% -% Mean [double] 1*n vector, expectation of the multivariate random variable. -% Sigma_upper_chol [double] n*n matrix, upper triangular Cholesky decomposition of Sigma -% (the covariance matrix up to a factor df/(df-2)). -% df [integer] degrees of freedom. -% -% OUTPUTS -% draw [double] 1*n vector drawn from a multivariate normal distribution with expectation Mean and -% covariance Sigma. -% -% REMARK This is certainly not the most efficient way... -% -% NOTE See Zellner (appendix B.2, 1971) for a definition. -% - -% Copyright (C) 2003-2008 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 . - - n = length(Mean); - draw = Mean + randn(1,n) * Sigma_upper_chol * sqrt(df/sum(randn(df,1).^2));