diff --git a/matlab/DsgeSmoother.m b/matlab/DsgeSmoother.m
index 0140ecf1286c78ba899cf5b4437430c33d954e97..2686345ed2d562f82d7ecabc9ae702e8b9516b16 100644
--- a/matlab/DsgeSmoother.m
+++ b/matlab/DsgeSmoother.m
@@ -93,14 +93,8 @@ else
 end
 trend_coeff = zeros(vobs,1);
 if bayestopt_.with_trend == 1
-    trend_coeff = zeros(vobs,1);
-    t = options_.trend_coeffs;
-    for i=1:length(t)
-        if ~isempty(t{i})
-            trend_coeff(i) = evalin('base',t{i});
-        end
-    end
-    trend = constant*ones(1,gend)+trend_coeff*(1:gend);
+    [trend_addition, trend_coeff] =compute_trend_coefficients(M_,options_,vobs,gend);
+    trend = constant*ones(1,gend)+trend_addition;
 else
     trend = constant*ones(1,gend);
 end
diff --git a/matlab/compute_trend_coefficients.m b/matlab/compute_trend_coefficients.m
new file mode 100644
index 0000000000000000000000000000000000000000..9686867e29c45822149facded5249ac32964eade
--- /dev/null
+++ b/matlab/compute_trend_coefficients.m
@@ -0,0 +1,49 @@
+function [trend_addition, trend_coeff]=compute_trend_coefficients(M_,DynareOptions,nvarobs,ntobs)
+% [trend_addition, trend_coeff]=compute_trend_coefficients(DynareOptions,nvarobs,ntobs)
+% Computes the trend coefficiencts and the trend, accounting for
+% prefiltering
+%
+% INPUTS
+%   M_              [structure] describing the model; called in the eval
+%                               statement
+%   DynareOptions   [structure] describing the options
+%   nvarobs         [scalar]    number of observed variables
+%   ntobs           [scalar]    length of data sample for estimation
+%
+% OUTPUTS
+%   trend_addition  [nvarobs by ntobs double] matrix storing deterministic
+%                               trend component
+%   trend_coeff     [nvarobs by 1] vector storing trend slope
+%
+% SPECIAL REQUIREMENTS
+%   none
+
+% Copyright (C) 2014 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/>.
+
+
+trend_coeff = zeros(nvarobs,1);
+t = DynareOptions.trend_coeffs;
+for i=1:length(t)
+    if ~isempty(t{i})
+        trend_coeff(i) = eval(t{i});
+    end
+end
+trend_addition=trend_coeff*[DynareOptions.first_obs:DynareOptions.first_obs+ntobs-1];
+if DynareOptions.prefilter
+    trend_addition = bsxfun(@minus,trend_addition,mean(trend_addition,2));
+end
diff --git a/matlab/dsge_likelihood.m b/matlab/dsge_likelihood.m
index ade4a0d0d682193cdc6ea63b6907ed9d75547b09..fb25f7250106cea7c79ebfafa5eb95fc8a3cbab3 100644
--- a/matlab/dsge_likelihood.m
+++ b/matlab/dsge_likelihood.m
@@ -307,14 +307,8 @@ end
 
 % Define the deterministic linear trend of the measurement equation.
 if BayesInfo.with_trend
-    trend_coeff = zeros(DynareDataset.vobs,1);
-    t = DynareOptions.trend_coeffs;
-    for i=1:length(t)
-        if ~isempty(t{i})
-            trend_coeff(i) = evalin('base',t{i});
-        end
-    end
-    trend = repmat(constant,1,DynareDataset.nobs)+trend_coeff*[1:DynareDataset.nobs];
+    [trend_addition, trend_coeff]=compute_trend_coefficients(Model,DynareOptions,DynareDataset.vobs,DynareDataset.nobs);
+    trend = repmat(constant,1,DynareDataset.nobs)+trend_addition;
 else
    trend_coeff = zeros(DynareDataset.vobs,1);
    trend = repmat(constant,1,DynareDataset.nobs);
diff --git a/matlab/dyn_forecast.m b/matlab/dyn_forecast.m
index 2d5a214676e7fe44e5fde02b1daefccdd159299f..c5edc21366c588230d23a46c4839728f05ce275e 100644
--- a/matlab/dyn_forecast.m
+++ b/matlab/dyn_forecast.m
@@ -92,12 +92,12 @@ switch task
             end
         end
         if ~isempty(trend_coeffs) 
-          trend = trend_coeffs*(gend+(1-M.maximum_lag:horizon)); 
+          trend = trend_coeffs*(options.first_obs+gend-1+(1-M.maximum_lag:horizon)); 
         end
     end
-    global bayestopt_
-    if isfield(bayestopt_,'mean_varobs')
-        trend = trend + repmat(bayestopt_.mean_varobs,1,horizon+M.maximum_lag);
+    if options.prefilter
+        global bayestopt_
+        trend = trend - repmat(mean(trend_coeffs*[options.first_obs:options.first_obs+gend-1],2),1,horizon+1); %subtract mean trend
     end
   otherwise
     error('Wrong flag value')
@@ -125,7 +125,14 @@ if ~isscalar(trend)
 end
 
 if options.loglinear == 1
-    yf=yf-oo.dr.ys(i_var)*ones(1,horizon+1)+log(oo.dr.ys(i_var))*ones(1,horizon+1); %take care of logged steady state in this case; above the unlogged one was added
+    yf=yf-oo.dr.ys(i_var)*ones(1,horizon+M.maximum_lag)+log(oo.dr.ys(i_var))*ones(1,horizon+M.maximum_lag); %take care of logged steady state in this case; above the unlogged one was added    
+    if options.prefilter == 1 %subtract steady state and add mean for observables
+        yf(i_var_obs,:)=yf(i_var_obs,:)-repmat(log(oo.dr.ys(i_var_obs)),1,horizon+M.maximum_lag)+ repmat(bayestopt_.mean_varobs,1,horizon+M.maximum_lag);
+    end
+else
+    if options.prefilter == 1 %subtract steady state and add mean for observables
+        yf(i_var_obs,:)=yf(i_var_obs,:)-repmat(oo.dr.ys(i_var_obs),1,horizon+M.maximum_lag)+ repmat(bayestopt_.mean_varobs,1,horizon+M.maximum_lag);
+    end    
 end
 
 for i=1:n_var
diff --git a/matlab/dynare_estimation_1.m b/matlab/dynare_estimation_1.m
index 58ee4fef466c2ca46dd8cf11fdc98d2cec48f6be..d126587808965286f03f3e30ece820f7d0f5a3d9 100644
--- a/matlab/dynare_estimation_1.m
+++ b/matlab/dynare_estimation_1.m
@@ -619,14 +619,16 @@ if (~((any(bayestopt_.pshape > 0) && options_.mh_replic) || (any(bayestopt_.psha
     %%
     %%  Smooth observational errors...
     %%
-    if options_.prefilter == 1
-        yf = atT(bayestopt_.mf,:)+repmat(dataset_info.descriptive.mean',1,gend);
+    if options_.prefilter == 1 %as mean is taken after log transformation, no distinction is needed here
+        yf = atT(bayestopt_.mf,:)+repmat(dataset_info.descriptive.mean',1,gend)+...
+             trend_coeff*[options_.first_obs:options_.first_obs+gend-1]-...
+             repmat(mean(trend_coeff*[options_.first_obs:options_.first_obs+gend-1],2),1,gend);
     elseif options_.loglinear == 1
         yf = atT(bayestopt_.mf,:)+repmat(log(ys(bayestopt_.mfys)),1,gend)+...
-             trend_coeff*[1:gend];
+             trend_coeff*[options_.first_obs:options_.first_obs+gend-1];
     else
         yf = atT(bayestopt_.mf,:)+repmat(ys(bayestopt_.mfys),1,gend)+...
-             trend_coeff*[1:gend];
+             trend_coeff*[options_.first_obs:options_.first_obs+gend-1];
     end
     if nvn
         number_of_plots_to_draw = 0;
diff --git a/matlab/dynare_estimation_init.m b/matlab/dynare_estimation_init.m
index 64390686dacc25a2e230f340261a780f6ff73e45..e652c748e03f1cfb56d2f96c755a050643deef63 100644
--- a/matlab/dynare_estimation_init.m
+++ b/matlab/dynare_estimation_init.m
@@ -375,13 +375,11 @@ if ~isfield(options_,'trend_coeffs') % No!
 else% Yes!
     bayestopt_.with_trend = 1;
     bayestopt_.trend_coeff = {};
-    trend_coeffs = options_.trend_coeffs;
-    nt = length(trend_coeffs);
     for i=1:options_.number_of_observed_variables
-        if i > length(trend_coeffs)
+        if i > length(options_.trend_coeffs)
             bayestopt_.trend_coeff{i} = '0';
         else
-            bayestopt_.trend_coeff{i} = trend_coeffs{i};
+            bayestopt_.trend_coeff{i} = options_.trend_coeffs{i};
         end
     end
 end
diff --git a/matlab/non_linear_dsge_likelihood.m b/matlab/non_linear_dsge_likelihood.m
index 22e421824a58e902227f3eae293f0095d7810f3b..5c53bfe58041e6e632e78a15f815b63a94c7f2a0 100644
--- a/matlab/non_linear_dsge_likelihood.m
+++ b/matlab/non_linear_dsge_likelihood.m
@@ -227,6 +227,21 @@ end
 % Define a vector of indices for the observed variables. Is this really usefull?...
 BayesInfo.mf = BayesInfo.mf1;
 
+% Define the deterministic linear trend of the measurement equation.
+if DynareOptions.noconstant
+    constant = zeros(DynareDataset.vobs,1);
+else
+    constant = SteadyState(BayesInfo.mfys);
+end
+
+% Define the deterministic linear trend of the measurement equation.
+if BayesInfo.with_trend
+    [trend_addition, trend_coeff]=compute_trend_coefficients(Model,DynareOptions,DynareDataset.vobs,DynareDataset.nobs);
+    trend = repmat(constant,1,DynareDataset.info.ntobs)+trend_addition;
+else
+    trend = repmat(constant,1,DynareDataset.nobs);
+end
+
 % Get needed informations for kalman filter routines.
 start = DynareOptions.presample+1;
 np    = size(T,1);
diff --git a/matlab/prior_posterior_statistics_core.m b/matlab/prior_posterior_statistics_core.m
index 26b793c08cf05a923554c2b81e8c0cb311378b0f..1ea51e238b6debdb3edd21a938350fb136db9870 100644
--- a/matlab/prior_posterior_statistics_core.m
+++ b/matlab/prior_posterior_statistics_core.m
@@ -191,11 +191,17 @@ for b=fpar:B
         if horizon
             yyyy = alphahat(iendo,i_last_obs);
             yf = forcst2a(yyyy,dr,zeros(horizon,exo_nbr));
-            if options_.prefilter
+            if options_.prefilter 
+                % add mean
                 yf(:,IdObs) = yf(:,IdObs)+repmat(bayestopt_.mean_varobs', ...
                                                  horizon+maxlag,1);
+                % add trend
+                yf(:,IdObs) = yf(:,IdObs)+(options_.first_obs+gend+[1-maxlag:horizon]')*trend_coeff'-...
+                             repmat(mean(trend_coeff*[options_.first_obs:options_.first_obs+gend-1],2)',length(1-maxlag:horizon),1); %center trend
+            else
+                % add trend
+                    yf(:,IdObs) = yf(:,IdObs)+(options_.first_obs+gend+[1-maxlag:horizon]')*trend_coeff';                
             end
-            yf(:,IdObs) = yf(:,IdObs)+(gend+[1-maxlag:horizon]')*trend_coeff';
             if options_.loglinear
                 yf = yf+repmat(log(SteadyState'),horizon+maxlag,1);
             else
@@ -203,8 +209,15 @@ for b=fpar:B
             end
             yf1 = forcst2(yyyy,horizon,dr,1);
             if options_.prefilter == 1
+                % add mean
                 yf1(:,IdObs,:) = yf1(:,IdObs,:)+ ...
                     repmat(bayestopt_.mean_varobs',[horizon+maxlag,1,1]);
+                % add trend
+                yf1(:,IdObs) = yf1(:,IdObs)+(options_.first_obs+gend+[1-maxlag:horizon]')*trend_coeff'-...
+                             repmat(mean(trend_coeff*[options_.first_obs:options_.first_obs+gend-1],2)',length(1-maxlag:horizon),1); %center trend
+            else
+                yf1(:,IdObs,:) = yf1(:,IdObs,:)+repmat((options_.first_obs+gend+[1-maxlag:horizon]')* ...
+                                                       trend_coeff',[1,1,1]);
             end
             yf1(:,IdObs,:) = yf1(:,IdObs,:)+repmat((gend+[1-maxlag:horizon]')* ...
                                                    trend_coeff',[1,1,1]);