From dcf49d606f657d467531efcadbac59b78dea370c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Charybdis=29?=
 <stephane.adjemian@ens.fr>
Date: Wed, 10 Feb 2010 14:20:28 +0100
Subject: [PATCH] Added the possibility of computing hp-filtered simulated
 moments.

---
 matlab/disp_moments.m     | 15 ++++++-------
 matlab/sample_hp_filter.m | 44 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 52 insertions(+), 7 deletions(-)
 create mode 100644 matlab/sample_hp_filter.m

diff --git a/matlab/disp_moments.m b/matlab/disp_moments.m
index 23af69f9eb..d2e51a435e 100644
--- a/matlab/disp_moments.m
+++ b/matlab/disp_moments.m
@@ -23,10 +23,6 @@ global M_ options_ oo_
 warning_old_state = warning;
 warning off
 
-if options_.hp_filter
-    error('STOCH_SIMUL: HP filter is not yet implemented for empirical moments')
-end
-
 if size(var_list,1) == 0
     var_list = M_.endo_names(1:M_.orig_endo_nbr, :);
 end
@@ -45,7 +41,13 @@ end
 y = y(ivar,options_.drop+M_.maximum_lag+1:end)';
 
 m = mean(y);
-y = y - repmat(m,size(y,1),1);
+
+if options_.hp_filter
+    [hptrend,y] = sample_hp_filter(y,options_.hp_filter);
+else
+    y = bsxfun(@minus, y, m);
+end
+
 s2 = mean(y.*y);
 s = sqrt(s2);
 oo_.mean = transpose(m);
@@ -54,8 +56,7 @@ oo_.var = y'*y/size(y,1);
 labels = deblank(M_.endo_names(ivar,:));
 
 if options_.nomoments == 0
-    z = [ m' s' s2' (mean(y.^3)./s2.^1.5)' (mean(y.^4)./(s2.*s2)-3)' ];
-    
+    z = [ m' s' s2' (mean(y.^3)./s2.^1.5)' (mean(y.^4)./(s2.*s2)-3)' ];    
     title='MOMENTS OF SIMULATED VARIABLES';
     if options_.hp_filter
         title = [title ' (HP filter, lambda = ' ...
diff --git a/matlab/sample_hp_filter.m b/matlab/sample_hp_filter.m
new file mode 100644
index 0000000000..fccb1633e7
--- /dev/null
+++ b/matlab/sample_hp_filter.m
@@ -0,0 +1,44 @@
+function [hptrend,hpcycle] = sample_hp_filter(y,s)
+% HP filters a collection of time series.
+% 
+% INPUTS 
+%   y                        [double]   T*n matrix of data (n is the number of variables)
+%   s                        [integer]  scalar, smoothing parameter.
+% 
+% OUTPUTS 
+%   hptrend                  [double]   T*n matrix, trend component of y.
+%   hpcycle                  [double]   T*n matrix, cycle component of y.  
+%               
+% SPECIAL REQUIREMENTS
+%    
+
+% Copyright (C) 2010 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 <http://www.gnu.org/licenses/>.
+    
+[T,n] = size(y);
+
+if nargin<2
+    s = 1600;
+end
+
+D = spdiags(repmat([s, -4.0*s, (1 + 6.0*s), -4.0*s, s], T, 1), -2:2, T, T);% Sparse matrix.
+D(1,1) = 1.0+s; D(T,T) = D(1,1);
+D(1,2) = -2.0*s; D(2,1) = D(1,2); D(T-1,T) = D(1,2); D(T,T-1) = D(1,2);
+D(2,2) = 1.0+5.0*s; D(T-1,T-1) = D(2,2);
+
+hptrend = D\y;
+hpcycle = y-hptrend;
\ No newline at end of file
-- 
GitLab