# Model for finding arbitrary number of changepoints

**URL:** <https://discourse.mc-stan.org/t/model-for-finding-arbitrary-number-of-changepoints/13973>\
**Category:** General\
**Created:** [March 31, 2020, 8:44pm UTC](https://discourse.mc-stan.org/t/model-for-finding-arbitrary-number-of-changepoints/13973 "2020-03-31T20:44:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramiro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ramiro/32/7587_2.png) [@ramiro](https://discourse.mc-stan.org/u/ramiro)\
**Post date:** [March 31, 2020, 8:44pm UTC](https://discourse.mc-stan.org/t/model-for-finding-arbitrary-number-of-changepoints/13973/1 "2020-03-31T20:44:05Z")

</div>

Hello,

I am trying to implement a changepoint model. Perhaps a time series example would make it clearer:

 ![Screen Shot 2020-03-31 at 4.47.08 PM](https://canada1.discourse-cdn.com/flex030/uploads/mc_stan/original/2X/6/6525ff4836e427e68b55dfd3506a61f7de53ef97.png)

Each color is a different segment, where each segment is modeled as a normal distribution with mean zero and a different standard deviation (in the picture, the y-axis are the displacements sampled from such normal distribution for the segment). The number of segments is arbitrary.

I implemented a model for this using maximum likelihood based on literature specific for my problem. However, I need to add a bit more complexity to the model so I want to move on to a Bayesian framework.

In the past, I’ve implemented change points using reversible-jump mcmc. I don’t think this is possible with Stan correct? Any suggestions?

Thank you.

---

<div class="post-metadata">

**Author:** ![asael\_am](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/asael_am/32/5377_2.png) [@asael\_am](https://discourse.mc-stan.org/u/asael_am)\
**Post date:** [March 31, 2020, 9:38pm UTC](https://discourse.mc-stan.org/t/model-for-finding-arbitrary-number-of-changepoints/13973/2 "2020-03-31T21:38:45Z")

</div>

Hi!  
Have you tried the [prophet](https://facebook.github.io/prophet/docs/quick_start.html#r-api) package?! It has some methods for estimating change points! And You can always use their [stan code](https://github.com/facebook/prophet/blob/master/R/inst/stan/prophet.stan) is a bit tricky to follow and personalize but maybe the package itself is a good start :)

---

<div class="post-metadata">

**Author:** ![ramiro](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.mc-stan.org/ramiro/32/7587_2.png) [@ramiro](https://discourse.mc-stan.org/u/ramiro)\
**Post date:** [April 1, 2020, 4:17pm UTC](https://discourse.mc-stan.org/t/model-for-finding-arbitrary-number-of-changepoints/13973/3 "2020-04-01T16:17:43Z")

</div>

Thanks @asael_am! I will look into it. I am just concerned that it would not be flexible enough for what I need but will check it out. I am just at a loss as to whether just try to implement a model using plain RJ-MCMC in R, or whether there is a package out there that takes advantage of HMC for these kinds of problems.
