# Update model when new observations are available

**URL:** <https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520>\
**Category:** Modeling\
**Created:** [December 1, 2021, 11:13am UTC](https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520 "2021-12-01T11:13:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![HJAM24](https://avatars.discourse-cdn.com/v4/letter/h/ce73a5/32.png) [@HJAM24](https://discourse.mc-stan.org/u/HJAM24)\
**Post date:** [December 1, 2021, 11:13am UTC](https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520/1 "2021-12-01T11:13:29Z")

</div>

Hi,

I have the following Stan-code

```

data {
  int<lower=1> N; // number of observations
  int<lower=1> K; // dimension of observations
  vector[K] y[N]; // observations: 
  vector[K] mu_x;
  vector[K] mu_y;
}

parameters {
  vector<lower=0>[K] sigma_x;
  vector<lower=0>[K] sigma_y;
  vector<lower=-1, upper=1>[K] rho;
}

model {
  for k in (1:K) {
    vector[2] mu = [mu_x[k], mu_y[k]]';
    // cholesky decomposition of covariance matrix
    matrix[2, 2] L_Sigma = [[sigma_x[k], 0], [rho[k] * sigma_y[k], sigma_y[k] * sqrt(1 - rho[k] * rho[k])]];
    sigma_x[k] ~ normal(0, 20);
    sigma_y[k] ~ normal(0, 20);

    y[k] ~ multi_normal(mu[k], L_Sigma);
}

```

After running this, I get the posteriors of _sigma\_x_, _sigma\_y_ and _rho_. When new data arrives I want to update the model. I believe the model is suited fort this because of the presence of the multivariate normal distribution. My question is how does the new _update-Stan_ code looks? Do I read the three variables one-by-one as vectors in the _data_-section? Is the below code the correct way to go?

```
data {
  int<lower=1> N; // number of observations
  int<lower=1> K; // dimension of observations
  vector[K] y[N]; // observations: a list of K vectors
  vector[K] mu_x;
  vector[K] mu_y;
  vector[K] sigma_x;
  vector[K] sigma_y;
  vector[K] rho;
}

parameters {
  vector<lower=0>[K] sigma_x;
  vector<lower=0>[K] sigma_y;
  vector<lower=-1, upper=1>[K] rho;
}

model {
  for k in (1:K) {
    vector[2] mu = [mu_x[k], mu_y[k]]';
    // cholesky decomposition of covariance matrix
    matrix[2, 2] L_Sigma = [[sigma_x[k], 0], [rho[k] * sigma_y[k], sigma_y[k] * sqrt(1 - rho[k] * rho[k])]];
    sigma_x[k] ~ normal(0, 20);
    sigma_y[k] ~ normal(0, 20);

    y[k] ~ multi_normal(mu, L_Sigma);
}

```

Thanks in advance

---

<div class="post-metadata">

**Author:** ![mike-lawrence](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/mike-lawrence/32/59_2.png) [@mike-lawrence](https://discourse.mc-stan.org/u/mike-lawrence)\
**Post date:** [December 1, 2021, 1:57pm UTC](https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520/2 "2021-12-01T13:57:48Z")

</div>

Sequential inference of this kind is not directly possible in Stan. There are ad hoc ways to attempt it regardless, but [see here](https://twitter.com/betanalpha/status/1425938076644347904?s=21) for a thread on why this can be folly.

---

<div class="post-metadata">

**Author:** ![spinkney](https://avatars.discourse-cdn.com/v4/letter/s/dec6dc/32.png) [@spinkney](https://discourse.mc-stan.org/u/spinkney)\
**Post date:** [December 1, 2021, 1:59pm UTC](https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520/3 "2021-12-01T13:59:51Z")

</div>

Also see a work in progress for nonparametric updating [Non-parametric Bayesian updating](https://discourse.mc-stan.org/t/non-parametric-bayesian-updating/25428) by @robertgrant

---

<div class="post-metadata">

**Author:** ![yizhang](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/yizhang/32/15673_2.png) [@yizhang](https://discourse.mc-stan.org/u/yizhang)\
**Post date:** [December 1, 2021, 4:28pm UTC](https://discourse.mc-stan.org/t/update-model-when-new-observations-are-available/25520/4 "2021-12-01T16:28:34Z")

</div>

See also [Using posteriors as new priors](https://discourse.mc-stan.org/t/using-posteriors-as-new-priors/12745).
