# Updating model with new data

**URL:** <https://discourse.mc-stan.org/t/updating-model-with-new-data/5532>\
**Category:** General\
**Created:** [September 7, 2018, 11:47am UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532 "2018-09-07T11:47:33Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 11:47am UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/1 "2018-09-07T11:47:33Z")

</div>

Hi everyone,

I was just wondering if it’s possible to update a model when new data arrives. So suppose I have some model X and build a model to obtain posterior estimates for some parameters, and I get some new data X’. Is it possible to rather than run the whole model again on all the data available X+X’, is it possible to just update the model for the new data X’ to get updated posterior estimates for the parameters?

Just thinking that this could save time if I didnt need to re-run the model again?

Might just be a stupid question but curious.

Thanks!  
Ryan

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 7, 2018, 12:40pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/2 "2018-09-07T12:40:17Z")

</div>

> [@Ryan\_Chan](#):
>
> Is it possible to rather than run the whole model again on all the data available X+X’

Yes

> [@Ryan\_Chan](#):
>
> is it possible to just update the model for the new data X’ to get updated posterior estimates for the parameters?

If you can obtain a parametric representation for the posterior conditional on X

---

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 12:53pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/3 "2018-09-07T12:53:59Z")

</div>

Thanks for the reply Ben!

So if I had a stanfit object called stanfit\_1 from fitting the model to X, how do I obtain the updated model stanfit\_2 with data X+X’ from this?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 7, 2018, 1:26pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/4 "2018-09-07T13:26:54Z")

</div>

You look at the parameters conditional on X, see what distribution they are closest to, and use that as your prior when conditioning on X’ to obtain the new posterior distribution.

---

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 2:58pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/5 "2018-09-07T14:58:29Z")

</div>

Can you explain how I can determine the closest distribution that the posterior distribution of the parameters is in Stan and RStan please?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 7, 2018, 3:21pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/6 "2018-09-07T15:21:33Z")

</div>

If it is anything other than multivariate normal, it is pretty difficult. But if it is multivariate normal, then you just need to estimate the mean vector and the covariance matrix (presumably with some regularization).

---

<div class="post-metadata">

**Author:** ![wds15](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/wds15/32/908_2.png) [@wds15](https://discourse.mc-stan.org/u/wds15)\
**Post date:** [September 7, 2018, 3:24pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/7 "2018-09-07T15:24:21Z")

</div>

The task usually is to find a good transformation which turns the parameters to become multivariate normal (ideally uncorrelated). This is anyway a good exercise to do for a given model (if possible). So as a bonus you get a better sampling model, usually.

---

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 4:16pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/8 "2018-09-07T16:16:51Z")

</div>

This makes sense but just not 100% clear how to implement this in practice.

I’ve implemented this football/soccer model by Baio and Blangiardo ([http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.182.8659&rep=rep1&type=pdf](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.182.8659&rep=rep1&type=pdf)) and my current stan code is:

data {  
int nteams;  
int ngames;  
int home\_team[ngames];  
int away\_team[ngames];  
int\<lower=0\> home\_goals[ngames];  
int\<lower=0\> away\_goals[ngames];  
}  
parameters {  
real home;  
real mu\_att;  
real mu\_def;  
real tau\_att;  
real tau\_def;

vector[nteams-1] att\_free;  
vector[nteams-1] def\_free;  
}  
transformed parameters {  
vector[nteams] att;  
vector[nteams] def;  
vector[ngames] log\_theta\_home;  
vector[ngames] log\_theta\_away;

// need to make sum(att)=sum(def)=0  
for (k in 1:(nteams-1)) {  
att[k] = att\_free[k];  
def[k] = def\_free[k];  
}  
att[nteams] = -sum(att\_free);  
def[nteams] = -sum(def\_free);

log\_theta\_home = home + att[home\_team] + def[away\_team];  
log\_theta\_away = att[away\_team] + def[home\_team];  
}  
model {  
home ~ normal(0, 10000);  
mu\_att ~ normal(0, 10000);  
mu\_def ~ normal(0, 10000);  
tau\_att ~ gamma(0.1, 0.1);  
tau\_def ~ gamma(0.1, 0.1);

att\_free ~ normal(mu\_att, 1/tau\_att);  
def\_free ~ normal(mu\_def, 1/tau\_def);

home\_goals ~ poisson\_log(log\_theta\_home);  
away\_goals ~ poisson\_log(log\_theta\_away);  
}

The reason why I want to update the model is when testing the performance of it I’ve just kept increasing the data and running this whole model on the dataset again as more games come in.

The parameters I’m specifically interested in are att, def and home.

If the posterior distribution of the parameters after seeing a first set of games X is approximately multivariate normal, how do I make adjustments to this stan code so that I can obtain new posterior distribution by running it on the next set of games X’? So far, I’ve just been re-running the code for X+X’ when new games come in.

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 7, 2018, 4:44pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/9 "2018-09-07T16:44:06Z")

</div>

Pass in the posterior mean vector and the posterior precision matrix as data. Eliminate all the old priors and use the multivariate normal instead.

Except your original posterior is going to be messed up because you did not constrain `tau_att` and `tau_def` to be positive. It would probably be better to declare them in log form in the parameters block and then antilog them in the transformed parameters block. And use real priors.

```
data {
  int nteams;
  int ngames;
  int home_team[ngames];
  int away_team[ngames];
  int<lower=0> home_goals[ngames];
  int<lower=0> away_goals[ngames];
  
  vector[5 * nteams - 2] mu;
  cov_matrix[rows(mu), rows(mu)] precision;
}
parameters {
  real home;
  real mu_att;
  real mu_def;
  real tau_att;
  real tau_def;

  vector[nteams-1] att_free;
  vector[nteams-1] def_free;
}
transformed parameters {
  vector[nteams] att;
  vector[nteams] def;
  vector[ngames] log_theta_home;
  vector[ngames] log_theta_away;

  // need to make sum(att)=sum(def)=0
  for (k in 1:(nteams-1)) {
    att[k] = att_free[k];
    def[k] = def_free[k];
  }
  att[nteams] = -sum(att_free);
  def[nteams] = -sum(def_free);

  log_theta_home = home + att[home_team] + def[away_team];
  log_theta_away = att[away_team] + def[home_team];
}
model {
  vector[5 + 2 * nteams - 2] theta = append_row([home, mu_att, mu_def, tau_att, tau_def]'
                                     append_row(att_free, def_free));
  theta ~ multi_normal_prec(mu, precision);
  home_goals ~ poisson_log(log_theta_home);
  away_goals ~ poisson_log(log_theta_away);
}

```

---

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 4:51pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/10 "2018-09-07T16:51:41Z")

</div>

Thanks Ben, appreciate your patience, this is great help!

---

<div class="post-metadata">

**Author:** ![Ryan\_Chan](https://avatars.discourse-cdn.com/v4/letter/r/6f9a4e/32.png) [@Ryan\_Chan](https://discourse.mc-stan.org/u/Ryan_Chan)\
**Post date:** [September 7, 2018, 5:54pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/11 "2018-09-07T17:54:37Z")

</div>

> [@bgoodri](#):
>
> vector[5 \* nteams - 2] mu; cov\_matrix[rows(mu), rows(mu)] precision;

> [@bgoodri](#):
>
> vector[5 + 2 \* nteams - 2] theta = append\_row([home, mu\_att, mu\_def, tau\_att, tau\_def]’ append\_row(att\_free, def\_free));

Shouldn’t the dimensions of these two vectors be the same? Is it meant to be vector[5 + 2 \* nteams - 2] for both mu and theta?

---

<div class="post-metadata">

**Author:** ![bgoodri](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/bgoodri/32/4451_2.png) [@bgoodri](https://discourse.mc-stan.org/u/bgoodri)\
**Post date:** [September 7, 2018, 6:01pm UTC](https://discourse.mc-stan.org/t/updating-model-with-new-data/5532/12 "2018-09-07T18:01:08Z")

</div>

Yes
