diff --git a/matlab/histvalf.m b/matlab/histvalf.m
index f15e72511e4c2bd269c7075929c367f33669de40..07a6a25a7f5367cf86e80b7078fd32739f3841ac 100644
--- a/matlab/histvalf.m
+++ b/matlab/histvalf.m
@@ -30,29 +30,6 @@ function [endo_histval, exo_histval, exo_det_histval] = histvalf(M, options)
 % You should have received a copy of the GNU General Public License
 % along with Dynare.  If not, see <http://www.gnu.org/licenses/>.
 
-if ~isfield(options, 'nobs') || isempty(options.nobs)
-    options.nobs = M.orig_maximum_lag;
-end
-
-if ~isfield(options, 'first_obs') || isempty(options.first_obs)
-    if isfield(options, 'first_simulation_period')
-        options.first_obs = options.first_simulation_period ...
-            - options.nobs;
-    else
-        options.first_obs = 1;
-    end
-elseif isfield(options, 'first_simulation_period')
-    nobs = options.first_simulation_period - opions_.first_obs;
-    if options.nobs ~= nobs
-        error(sprintf(['HISTVALF: first_obs = %d and', ...
-                       ' first_simulation_period = %d', ...
-                       ' don''t provide for the number of' ...
-                       ' lags in the model.'], ...
-                      options.first_obs, ...
-                      options.first_simulation_period))
-    end
-end
-
 series = histvalf_initvalf('HISTVAL', M, options);
 % capture the difference between stochastic and
 % perfect foresight setup
diff --git a/matlab/histvalf_initvalf.m b/matlab/histvalf_initvalf.m
index e65779c493129a3d732c9ecd4e4afca72421f4c0..3a2610e5156a0b566eac1888d75c3a347b047391 100644
--- a/matlab/histvalf_initvalf.m
+++ b/matlab/histvalf_initvalf.m
@@ -129,6 +129,24 @@ nobs0 = series.nobs;
 
 first_obs_ispresent = false;
 last_obs_ispresent = false;
+
+if ~isfield(options, 'first_obs') || isempty(options.first_obs)
+    if isfield(options, 'first_simulation_period')
+        options.first_obs = options.first_simulation_period ...
+            - M.orig_maximum_lag;
+    end
+elseif isfield(options, 'first_simulation_period')
+    nobs = options.first_simulation_period - opions_.first_obs;
+    if M.orig_maximum_lag ~= nobs
+        error(sprintf(['HISTVALF: first_obs = %d and', ...
+                       ' first_simulation_period = %d', ...
+                       ' don''t provide for the number of' ...
+                       ' lags in the model.'], ...
+                      options.first_obs, ...
+                      options.first_simulation_period))
+    end
+end
+
 if isfield(options, 'first_obs')
     i = options.first_obs;
     if i < 1
diff --git a/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod b/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod
index e74b6c9b27a3721bd0a0ab10330f292b9be7ddaa..200734a963b408ce4b27071fc2f16a92898a5675 100644
--- a/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod
+++ b/tests/histval_initval_file/sim_exo_lead_lag_initvalf.mod
@@ -24,6 +24,12 @@ model;
 end;
 
 initval_file(series = ds);
+if oo_.initval_series.dates(1) ~= dates('1Y');
+  error("Wrong initial date in oo_.initval_series");
+end;
+if oo_.initval_series{'x'}.data(6) ~= 0.9;
+  error("Wrond value for x"); 
+end;
 
 perfect_foresight_setup(periods=200);
 perfect_foresight_solver(maxit=100);
@@ -42,6 +48,55 @@ data1(8, 6) = 0.9; //shock to x in period 2
 ds1 = dseries(data1, '1Y', {'c', 'cmav', 'k', 'z_backward', 'z_forward', 'x'});
 
 initval_file(series = ds1, first_obs = 3, last_obs = 210, nobs = 208);
+if oo_.initval_series.dates(1) ~= dates('1Y');
+  error("Wrong initial date in oo_.initval_series");
+end;
+if oo_.initval_series{'x'}.data(6) ~= 0.9;
+  error("Wrond value for x"); 
+end;
+
+
+perfect_foresight_setup(periods=200);
+perfect_foresight_solver(maxit=100);
+
+if ~oo_.deterministic_simulation.status
+   error('Perfect foresight simulation failed');
+end
+
+base_results=load('sim_exo_lead_lag_results.mat');
+if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
+    error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
+end
+
+initval_file(series = ds1, first_obs = 3Y, last_obs = 210Y, nobs = 208);
+if oo_.initval_series.dates(1) ~= dates('3Y');
+  error("Wrong initial date in oo_.initval_series");
+end;
+if oo_.initval_series{'x'}.data(6) ~= 0.9;
+  error("Wrond value for x"); 
+end;
+
+
+perfect_foresight_setup(periods=200);
+perfect_foresight_solver(maxit=100);
+
+if ~oo_.deterministic_simulation.status
+   error('Perfect foresight simulation failed');
+end
+
+base_results=load('sim_exo_lead_lag_results.mat');
+if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
+    error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
+end
+
+initval_file(series = ds1, first_simulation_period = 7);
+if oo_.initval_series.dates(1) ~= dates('1Y');
+  error("Wrong initial date in oo_.initval_series");
+end;
+if oo_.initval_series{'x'}.data(6) ~= 0.9;
+  error("Wrond value for x"); 
+end;
+
 
 perfect_foresight_setup(periods=200);
 perfect_foresight_solver(maxit=100);
@@ -54,3 +109,25 @@ base_results=load('sim_exo_lead_lag_results.mat');
 if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
     error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
 end
+
+initval_file(series = ds1, first_simulation_period = 7Y);
+if oo_.initval_series.dates(1) ~= dates('1Y');
+  error("Wrong initial date in oo_.initval_series");
+end;
+if oo_.initval_series{'x'}.data(6) ~= 0.9;
+  error("Wrond value for x"); 
+end;
+
+
+perfect_foresight_setup(periods=200);
+perfect_foresight_solver(maxit=100);
+
+if ~oo_.deterministic_simulation.status
+   error('Perfect foresight simulation failed');
+end
+
+base_results=load('sim_exo_lead_lag_results.mat');
+if max(max(abs(base_results.oo_.endo_simul(1:5,:) - oo_.endo_simul(1:5,:)))) > 1e-8
+    error('Simulation with leads and lags doesn''t match the one with auxiliary variables')
+end
+