# Up-dating parameters for every iteration of a Stan model (MGARCH-Dirichlet Process Mixture)

**URL:** <https://discourse.mc-stan.org/t/up-dating-parameters-for-every-iteration-of-a-stan-model-mgarch-dirichlet-process-mixture/28451>\
**Category:** Modeling\
**Tags:** discrete-parameters, bmgarch, dirichlet-multinomial, rstan\
**Created:** [August 5, 2022, 12:16pm UTC](https://discourse.mc-stan.org/t/up-dating-parameters-for-every-iteration-of-a-stan-model-mgarch-dirichlet-process-mixture/28451 "2022-08-05T12:16:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![F\_Louisa](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/f_louisa/32/14816_2.png) [@F\_Louisa](https://discourse.mc-stan.org/u/F_Louisa)\
**Post date:** [August 5, 2022, 12:16pm UTC](https://discourse.mc-stan.org/t/up-dating-parameters-for-every-iteration-of-a-stan-model-mgarch-dirichlet-process-mixture/28451/1 "2022-08-05T12:16:04Z")

</div>

Dear Stan Community,

My question in short:  
**Is it possible to update the parameters of a stan model with every iteration?**

My question:  
I am a student trying to implement the MGARCH-DPM methodology of Maheu and Shamsi’s Nonparametric Dynamic Conditional Beta (see attachments). It involves a hierarchical model where the returns are modeled by an infinite normal mixture model with a mixed \mu parameter and mixed component B, the latter component mixes the the covariance matrix H\_t. The mixing parameters, \mu and B, are generated by a Dirichlet process.  
p\left(r\_{t} \mid \mu, B, W, H\_{t}\right)=\sum\_{j=1}^{\infty} \omega\_{j} N\left(r\_{t} \mid \mu\_{j}, H\_{t}^{1 / 2} B\_{j}\left(H\_{t}^{1 / 2}\right)^{\prime}\right)

The hierarchical model:  
 \begin{array}{ll} r\_{t} & \mid \phi\_{t}, H\_{t} \sim N\left(\xi\_{t}, H\_{t}^{1 / 2} \Lambda\_{t}\left(H\_{t}^{1 / 2}\right)^{\prime}\right), t=1, \ldots, T \\ \phi\_{t} & \equiv\left\{\xi\_{t}, \Lambda\_{t}\right\} \mid G \sim G, \\ G \mid & \alpha, G\_{0} \sim D P\left(\alpha, G\_{0}\right) \\ G\_{0} & \equiv N\left(\mu\_{0}, D\right) \times \mathcal{W}^{-1}\left(B\_{0}, \nu\_{0}\right) \\ H\_{t} & =\Gamma\_{0}+\Gamma\_{1} \odot\left(r\_{t-1}-\eta\right)\left(r\_{t-1}-\eta\right)^{\prime}+\Gamma\_{2} \odot H\_{t-1} . \end{array}

In order to implement this I want to make use of Philippe Rast’s **bmgarch** package to handle the Multivariate Garch process (I am opting for a DCC parametrization, deviating from the attached paper). The **bmgarch** package models the MGARCH process using stan (see attachments).

It would be ideal to run the entire MGARCH-DPM model in Stan, but I did find out how stan does not handle discrete sampling, which is needed for a Dirichlet Process Mixture.

Hence to optimize both the MGARCH parameters and the Dirichlet process, I thought of using Ross’ and Markwick’s **dirichletprocess** package to optimize for \mu and B. I wrote my own distribution ‘_dirichletprocess\_mvnormalgarchdpm.R_’ (attached) to work with the package. This distribution takes, in addition to the regular multivariate normal prior parameters, also the covariance matrix \mathbf{H} as input as generated by **bmgarch**.

However, with the many iterations and samples, optimizing both processes separately is not the most efficient.

Thus I think a better approach would be to nest both processes so that for every iteration of the **bmgarch** optimization it can use updated parameters generated by the **dirichletprocess** \mu and B for the next iteration round, and vice versa. Hence, I would really like to know if updating the parameters would be possible in stan!

Any suggestions how to approach this issue are greatly appreciated.

Regards,  
Floor

P.s. tagging @Bob_Carpenter as kindly suggested by @ph-rast :)

Attachments:  
[maheu\_paper.pdf](https://discourse.mc-stan.org/uploads/short-url/qiLqKSG7eiXrQgqvvXId9JqNlfq.pdf) (1.7 MB)

[Package **bmgarch** (specifically to the _DCCMGARCH.stan_)](https://github.com/ph-rast/bmgarch/blob/master/inst/stan/DCCMGARCH.stan)  
[Package **dirichletprocess**](https://github.com/dm13450/dirichletprocess)  
[dirichletprocess\_mvnormalgarchdpm.R](https://discourse.mc-stan.org/uploads/short-url/ntX6w6dM6Dq49FP2ELEngezFRn4.R) (7.4 KB)

---

<div class="post-metadata">

**Author:** ![Stephen\_Martin](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/stephen_martin/32/7879_2.png) [@Stephen\_Martin](https://discourse.mc-stan.org/u/Stephen_Martin)\
**Post date:** [August 6, 2022, 2:52am UTC](https://discourse.mc-stan.org/t/up-dating-parameters-for-every-iteration-of-a-stan-model-mgarch-dirichlet-process-mixture/28451/2 "2022-08-06T02:52:30Z")

</div>

I don’t think this would work particularly well. As much as I love DPs, and as much as I love Stan, the two don’t work well together. I also don’t know if the ‘bouncing back in forth’ between stan and dirichletprocess would work well; I suspect it would not.

You could try just using some identified mixture within Stan. In practice, DPs are often fit by just using a very large ‘max number of groups’ and fitting a particular mixture model (e.g., with a stick-breaking prior on the weights). This _can_ be done in Stan, though it is very slow [You do not need discrete sampling for mixtures; you can marginalize out the discrete params, just as you specified in your post], and identification for mixtures within Stan can be very problem/data specific. Sometimes it takes a lot of tinkering.

I would suggest _either_ using a non-DP mixture, and finding a way to identify it well for your data. Or choose an alternative to DPs for non-parametric models.

---

<div class="post-metadata">

**Author:** ![jsocolar](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/jsocolar/32/2486_2.png) [@jsocolar](https://discourse.mc-stan.org/u/jsocolar)\
**Post date:** [August 6, 2022, 2:54am UTC](https://discourse.mc-stan.org/t/up-dating-parameters-for-every-iteration-of-a-stan-model-mgarch-dirichlet-process-mixture/28451/3 "2022-08-06T02:54:34Z")

</div>

Some possibly helpful discussions of related topics:  
Old google group thread  
[https://groups.google.com/g/stan-users/c/uo2Y4x05hwU?pli=1](https://groups.google.com/g/stan-users/c/uo2Y4x05hwU?pli=1)

Old discourse thread

> [@Bayesian nonparametric modeling](https://discourse.mc-stan.org/t/bayesian-nonparametric-modeling/2674):
>
> Hi, I just have a general question about the capability of stan doing nonparametric Bayesian modeling. So far in the user guide it is pretty much parametric. Is there any chance to handle for example CRP/IBP in stan? Or are there any plan to move in that direction? Thanks! jicun
