From 329606cf57a1f58cf723ab9ea488e4e678fc1e8d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?St=C3=A9phane=20Adjemian=20=28Scylla=29?=
 <stephane.adjemian@univ-lemans.fr>
Date: Thu, 21 Sep 2017 13:40:31 +0200
Subject: [PATCH] Added option for initialisation of the one sided hp filter.

Can initialise the trend in the one sided HP filter with the trend
estimated with the standard HP filter.
---
 src/@dseries/onesidedhpcycle.m  | 15 +++++++++---
 src/@dseries/onesidedhpcycle_.m | 41 +++++++++++++++++++++++++++------
 src/@dseries/onesidedhptrend.m  | 13 +++++++++--
 src/@dseries/onesidedhptrend_.m | 37 +++++++++++++++++++++++++----
 src/@dseries/subsref.m          |  4 ++--
 5 files changed, 91 insertions(+), 19 deletions(-)

diff --git a/src/@dseries/onesidedhpcycle.m b/src/@dseries/onesidedhpcycle.m
index 34044ff..a9f2dca 100644
--- a/src/@dseries/onesidedhpcycle.m
+++ b/src/@dseries/onesidedhpcycle.m
@@ -1,4 +1,4 @@
-function o = onesidedhpcycle(o, lambda) % --*-- Unitary tests --*--
+function o = onesidedhpcycle(o, lambda, init) % --*-- Unitary tests --*--
 
 % Extracts the cycle component from a dseries object using the one sided HP filter.
 %
@@ -28,14 +28,23 @@ function o = onesidedhpcycle(o, lambda) % --*-- Unitary tests --*--
 
 if nargin>1
     if lambda<=0
-        error(['dseries::onesidedhptrend: Lambda must be a positive integer!'])
+        error(['dseries::onesidedhpcycle: Lambda must be a positive integer!'])
+    end
+    if nargin>2
+        if ~isequal(init, 'hpfilter')
+            error('dseries::onesidedhpcycle: Unknown option!')
+        end
     end
 else
     lambda = [];
 end
 
 o = copy(o);
-o.onesidedhpcycle_(lambda);
+if nargin<3
+    o.onesidedhpcycle_(lambda);
+else
+    o.onesidedhpcycle_(lambda, init);
+end
 
 return
 
diff --git a/src/@dseries/onesidedhpcycle_.m b/src/@dseries/onesidedhpcycle_.m
index f948721..61d89b8 100644
--- a/src/@dseries/onesidedhpcycle_.m
+++ b/src/@dseries/onesidedhpcycle_.m
@@ -1,4 +1,4 @@
-function o = onesidedhpcycle_(o, lambda) % --*-- Unitary tests --*--
+function o = onesidedhpcycle_(o, lambda, init) % --*-- Unitary tests --*--
 
 % Extracts the cycle component from a dseries object using a one sided HP filter.
 %
@@ -28,7 +28,12 @@ function o = onesidedhpcycle_(o, lambda) % --*-- Unitary tests --*--
 
 if nargin>1
     if lambda<=0
-        error(['dseries::onesidedhpcycle: Lambda must be a positive integer!'])
+        error('dseries::onesidedhpcycle: Lambda must be a positive integer!')
+    end
+    if nargin>2
+        if ~isequal(init, 'hpfilter')
+            error('dseries::onesidedhpcycle: Unknown option!')
+        end
     end
 else
     lambda = [];
@@ -37,20 +42,42 @@ end
 for i=1:vobs(o)
     if isempty(o.ops{i})
         if isempty(lambda)
-            o.ops(i) = {sprintf('onesidedhpcycle(%s, [])', o.name{i})};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, [], ''%s'')', o.name{i}, init)};
+            else
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, [])', o.name{i})};
+            end
         else
-            o.ops(i) = {sprintf('onesidedhpcycle(%s, %s)', o.name{i}, num2str(lambda))};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, %s, ''%s'')', o.name{i}, num2str(lambda), init)};
+            else
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, %s)', o.name{i}, num2str(lambda))};
+            end
         end
     else
         if isempty(lambda)
-            o.ops(i) = {sprintf('onesidedhpcycle(%s, [])', o.ops{i})};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, [], ''%s'')', o.ops{i}, init)};
+            else
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, [])', o.ops{i})};
+            end
         else
-            o.ops(i) = {sprintf('onesidedhpcycle(%s, %s)', o.ops{i}, num2str(lambda))};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, %s, ''%s'')', o.ops{i}, num2str(lambda), init)};
+            else
+                o.ops(i) = {sprintf('onesidedhpcycle(%s, %s)', o.ops{i}, num2str(lambda))};
+            end
         end
     end
 end
 
-[~, o.data] = one_sided_hp_filter(o.data, lambda);
+if nargin>2
+    trend = o.hptrend(lambda);
+    x0 = trend.data(1:2,:);
+    [~, o.data] = one_sided_hp_filter(o.data, lambda, x0);
+else    
+    [~, o.data] = one_sided_hp_filter(o.data, lambda);
+end
 
 return
 
diff --git a/src/@dseries/onesidedhptrend.m b/src/@dseries/onesidedhptrend.m
index 033588c..cb0e2fc 100644
--- a/src/@dseries/onesidedhptrend.m
+++ b/src/@dseries/onesidedhptrend.m
@@ -1,4 +1,4 @@
-function o = onesidedhptrend(o, lambda) % --*-- Unitary tests --*--
+function o = onesidedhptrend(o, lambda, init) % --*-- Unitary tests --*--
 
 % Extracts the trend component from a dseries object using the one sided HP filter.
 %
@@ -30,12 +30,21 @@ if nargin>1
     if lambda<=0
         error(['dseries::onesidedhptrend: Lambda must be a positive integer!'])
     end
+    if nargin>2
+        if ~isequal(init, 'hpfilter')
+            error('dseries::onesidedhpcycle: Unknown option!')
+        end
+    end
 else
     lambda = [];
 end
 
 o = copy(o);
-o.onesidedhptrend_(lambda);
+if nargin<3
+    o.onesidedhptrend_(lambda);
+else
+    o.onesidedhptrend_(lambda, init);
+end
 
 return
 
diff --git a/src/@dseries/onesidedhptrend_.m b/src/@dseries/onesidedhptrend_.m
index 620f742..eb881ce 100644
--- a/src/@dseries/onesidedhptrend_.m
+++ b/src/@dseries/onesidedhptrend_.m
@@ -30,6 +30,11 @@ if nargin>1
     if lambda<=0
         error(['dseries::onesidedhptrend: Lambda must be a positive integer!'])
     end
+    if nargin>2
+        if ~isequal(init, 'hpfilter')
+            error('dseries::onesidedhptrend: Unknown option!')
+        end
+    end
 else
     lambda = [];
 end
@@ -37,20 +42,42 @@ end
 for i=1:vobs(o)
     if isempty(o.ops{i})
         if isempty(lambda)
-            o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.name{i})};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhptrend(%s, [], ''%s'')', o.name{i}, init)};
+            else
+                o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.name{i})};
+            end
         else
-            o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.name{i}, num2str(lambda))};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhptrend(%s, %s, ''%s'')', o.name{i}, num2str(lambda), init)};
+            else
+                o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.name{i}, num2str(lambda))};
+            end
         end
     else
         if isempty(lambda)
-            o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.ops{i})};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhptrend(%s, [], ''%s'')', o.ops{i}, init)};
+            else
+                o.ops(i) = {sprintf('onesidedhptrend(%s, [])', o.ops{i})};
+            end
         else
-            o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.ops{i}, num2str(lambda))};
+            if nargin>2
+                o.ops(i) = {sprintf('onesidedhptrend(%s, %s, ''%s'')', o.ops{i}, num2str(lambda), init)};
+            else
+                o.ops(i) = {sprintf('onesidedhptrend(%s, %s)', o.ops{i}, num2str(lambda))};
+            end
         end
     end
 end
 
-o.data = one_sided_hp_filter(o.data, lambda);
+if nargin>2
+    trend = o.hptrend(lambda);
+    x0 = trend.data(1:2,:);
+    o.data = one_sided_hp_filter(o.data, lambda, x0);
+else    
+    o.data = one_sided_hp_filter(o.data, lambda);
+end
 
 return
 
diff --git a/src/@dseries/subsref.m b/src/@dseries/subsref.m
index ec8b082..22d127a 100644
--- a/src/@dseries/subsref.m
+++ b/src/@dseries/subsref.m
@@ -90,7 +90,7 @@ switch S(1).type
       case 'freq'
         % Returns an integer characterizing the data frequency (1, 4, 12 or 52)
         B = A.dates.freq;
-      case {'lag','lag_','lead','lead_','hptrend','hptrend_','hpcycle','hpcycle_','onesidedhptrend','onesidedhptrend_','onesidedhpcycle','onesidedhpcycle_','chain','chain_','detrend','detrend_','exist','mean','std','center','center_'} % Methods with less than two arguments.
+      case {'lag','lag_','lead','lead_','hptrend','hptrend_','hpcycle','hpcycle_','chain','chain_','detrend','detrend_','exist','mean','std','center','center_'} % Methods with less than two arguments.
         if length(S)>1 && isequal(S(2).type,'()')
             if isempty(S(2).subs)
                 B = feval(S(1).subs,A);
@@ -105,7 +105,7 @@ switch S(1).type
         else
             B = feval(S(1).subs,A);
         end
-      case {'cumsum','cumsum_','insert','pop','pop_','cumprod','cumprod_','remove','remove_'} % Methods with less than three argument.
+      case {'cumsum','cumsum_','insert','pop','pop_','cumprod','cumprod_','remove','remove_','onesidedhptrend','onesidedhptrend_','onesidedhpcycle','onesidedhpcycle_'} % Methods with less than three argument.
         if length(S)>1 && isequal(S(2).type,'()')
             if isempty(S(2).subs)
                 B = feval(S(1).subs,A);
-- 
GitLab