diff --git a/mex/sources/estimation/EstimatedParametersDescription.cc b/mex/sources/estimation/EstimatedParametersDescription.cc
index 6141f6b46b9cd214358e92fc970905c0d0347fc7..69d302f821064fdb23d54ed07ad3b30247496257 100644
--- a/mex/sources/estimation/EstimatedParametersDescription.cc
+++ b/mex/sources/estimation/EstimatedParametersDescription.cc
@@ -34,3 +34,14 @@ EstimatedParametersDescription::EstimatedParametersDescription(std::vector<Estim
 {
 }
 
+size_t
+EstimatedParametersDescription::getNumberOfPeriods() const
+{
+  size_t r = 0;
+  for (size_t i = 0; i < estSubsamples.size(); i++)
+    {
+      size_t r2 = estSubsamples[i].endPeriod + 1;
+      if (r2 > r)
+        r = r2;
+    }
+}
diff --git a/mex/sources/estimation/EstimatedParametersDescription.hh b/mex/sources/estimation/EstimatedParametersDescription.hh
index 8d6aaf0092284c24bb226c5b987ac3cc64a5ef4f..0e4a14fac672406ae228f6660ff1d17d23466ad2 100644
--- a/mex/sources/estimation/EstimatedParametersDescription.hh
+++ b/mex/sources/estimation/EstimatedParametersDescription.hh
@@ -49,6 +49,7 @@ public:
   EstimatedParametersDescription(std::vector<EstimationSubsample> &estSubsamples, std::vector<EstimatedParameter> &estParams);
   std::vector<EstimationSubsample> estSubsamples;
   std::vector<EstimatedParameter> estParams;
+  size_t getNumberOfPeriods() const;
 };
 
 #endif // !defined(E8F2C096_A301_42e8_80FF_A643291BF995__INCLUDED_)
diff --git a/mex/sources/estimation/EstimationSubsample.hh b/mex/sources/estimation/EstimationSubsample.hh
index 41e56009869b57d1c5f33057d5e6280b01523c31..ab3ed3c7a1e4bd3ca66c9e7d104dd199abded0c2 100644
--- a/mex/sources/estimation/EstimationSubsample.hh
+++ b/mex/sources/estimation/EstimationSubsample.hh
@@ -53,7 +53,7 @@
  * class LogPosteriorDensity on as needed basis and updates all parameters (inc. H
  * and Q) when time slot xparam1 is supplied.
  *
- *
+ * Time indices follow C convention: first period has index 0.
  */
 class EstimationSubsample {
 public:
diff --git a/mex/sources/estimation/LogLikelihoodMain.cc b/mex/sources/estimation/LogLikelihoodMain.cc
index 272030078a7833f703368c2f7a0ba9137ddfeb02..29ce8c07766e30063aa31c7c30dd0238d9d472ba 100644
--- a/mex/sources/estimation/LogLikelihoodMain.cc
+++ b/mex/sources/estimation/LogLikelihoodMain.cc
@@ -34,8 +34,7 @@ LogLikelihoodMain::LogLikelihoodMain( //const Matrix &data_arg, Vector &deepPara
   : estSubsamples(estiParDesc.estSubsamples),
   logLikelihoodSubSample(dynamicDllFile, estiParDesc, n_endo, n_exo, zeta_fwrd_arg, zeta_back_arg, zeta_mixed_arg, zeta_static_arg, qz_criterium,
                          varobs, riccati_tol, lyapunov_tol, info_arg),
-  //deepParams(deepParams_arg),
-  vll(estSubsamples[estSubsamples.size()-1].endPeriod) // time dimension size of data
+    vll(estiParDesc.getNumberOfPeriods()) // time dimension size of data
 
 {
 
@@ -49,7 +48,7 @@ LogLikelihoodMain::~LogLikelihoodMain()
 double
 LogLikelihoodMain::compute(Matrix &steadyState, const Vector &estParams, Vector &deepParams, const MatrixConstView &data, Matrix &Q, Matrix &H, size_t start, int &info)
 {
-  for (size_t i = 1; i <= (size_t) estSubsamples.size(); ++i)
+  for (size_t i = 0; i < estSubsamples.size(); ++i)
     {
       VectorView vSteadyState = mat::get_col(steadyState, i);