# Single source of error seasonal local level model: is this correct?

**URL:** <https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928>\
**Category:** Modeling\
**Tags:** time-series, feedback-wanted, state-space, specification\
**Created:** [April 16, 2021, 8:08pm UTC](https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928 "2021-04-16T20:08:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JMalnight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jmalnight/32/9641_2.png) [@JMalnight](https://discourse.mc-stan.org/u/JMalnight)\
**Post date:** [April 16, 2021, 8:08pm UTC](https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928/1 "2021-04-16T20:08:20Z")

</div>

I am trying to gradually build up some single source of error state space models for revenue forecasting. I’m a relative newbie to STAN and want to ensure that my code is correct. I think it is, but I need feedback.

I’m building upon the multiple source of error (MSOE) state space code [here](https://github.com/sinhrks/stan-statespace/blob/master/models/fig04_10.stan):

```
data {
  int<lower=1> n;
  vector[n] y;
}
parameters {
  vector[n] mu;
  vector[n] seasonal;
  real<lower=0> sigma_level;
  real<lower=0> sigma_seas;
  real<lower=0> sigma_irreg;
}
transformed parameters {
  vector[n] yhat;
  yhat = mu + seasonal;
}
model {
  for(t in 4:n)
seasonal[t] ~ normal(- sum(seasonal[t-3:t-1]), sigma_seas);

  for(t in 2:n)
mu[t] ~ normal(mu[t-1], sigma_level);

  y ~ normal(yhat, sigma_irreg);
}

```

However, that does not use a single source of error, as I want. As a first pass, I want to use Hyndman’s exponential smoothing models as templates (e.g. [here](https://otexts.com/fpp2/expsmooth.html)). The key difference between the SSOE and MSOE models is that in the code above, sigma\_level and sigma\_seas are multiples of the residual sigma\_irreg. To use just a level example (omitting the seasons for space):

y\_t = l\_{t-1} + e\_t  
l\_t = l\_{t-1} + \alpha e\_t

So if we know what l\_{t-1} is, the expectation and variance of l\_t are l\_{t-1} and \alpha^2 \sigma^2, if e\_t \sim N(0, \sigma^2). I think. I hope.

Because it lets me update the above code to:

```
data {
  int<lower=1> n;
  vector[n] y;
  int<lower=1> S; // Seasons
}
parameters {
  vector[n] mu;
  vector[n] seasonal;
  real<lower=0, upper=1> alpha; // Level smoother
  real<lower=0, upper=1> gamma; // Seasonal smoother
  real<lower=0> sigma_irreg;
}
transformed parameters {
  real<lower=0> sigma_level;
  sigma_level = alpha * sigma_irreg;
  real<lower=0> sigma_seas;
  sigma_seas = gamma * sigma_irreg;
  vector[n] yhat;
  yhat = mu + seasonal;
}
model {
  // Priors
  alpha ~ beta(2, 1);
  gamma ~ beta(2, 2);
  // Model
  for(t in S:n)
    seasonal[t] ~ normal(- sum(seasonal[t-S+1:t-1]), sigma_seas);

  for(t in 2:n)
    mu[t] ~ normal(mu[t-1], sigma_level);

  y ~ normal(yhat, sigma_irreg);
}

```

Anyway, I would greatly appreciate feedback. I deeply hope this is correct, because there is no way in hell I’m ever going to program a Kalman filter and I would love to actually use Stan and Bayes in my work.

---

<div class="post-metadata">

**Author:** ![Ara\_Winter](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ara_winter/32/4284_2.png) [@Ara\_Winter](https://discourse.mc-stan.org/u/Ara_Winter)\
**Post date:** [April 20, 2021, 3:20pm UTC](https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928/2 "2021-04-20T15:20:08Z")

</div>

My first question would be does the local level and seasonal base model run with simulated data that looks like your real data and recovers the known parameters. Second would be does it run with your real data?

---

<div class="post-metadata">

**Author:** ![JMalnight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jmalnight/32/9641_2.png) [@JMalnight](https://discourse.mc-stan.org/u/JMalnight)\
**Post date:** [April 22, 2021, 3:48pm UTC](https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928/3 "2021-04-22T15:48:36Z")

</div>

Good question. I will create fake data with parameters and try to recover it today.

The model does run with real data and it does seem to fit well. I feel optimistic but want to check it to be sure. Worst case scenario, I might try to rewrite it in error-correction form.

I’ll update soon.

---

<div class="post-metadata">

**Author:** ![JMalnight](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jmalnight/32/9641_2.png) [@JMalnight](https://discourse.mc-stan.org/u/JMalnight)\
**Post date:** [June 23, 2022, 2:57pm UTC](https://discourse.mc-stan.org/t/single-source-of-error-seasonal-local-level-model-is-this-correct/21928/4 "2022-06-23T14:57:48Z")

</div>

Long story short, it does run and I think it is correct. I recently found a resource that uses a very similar approach ([here](https://openforecast.org/adam/ADAMETSPureAdditive.html). This seems kosher but does not rely on innovations so much as errors (innovations, as I understand it, being the error from the previous period’s forecast).
