# Modeling stock market signals (online changepoint detection)

**URL:** <https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757>\
**Category:** Questions\
**Created:** [March 29, 2020, 12:49am UTC](https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757 "2020-03-29T00:49:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![sharsenij14](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@sharsenij14](https://discourse.pymc.io/u/sharsenij14)\
**Post date:** [March 29, 2020, 12:49am UTC](https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757/1 "2020-03-29T00:49:39Z")

</div>

Hi everyone. I am working on a finance problem. I have a time-series for each stock that tells me how much of it I should buy or sell on any given day. The problem is that the signal is noisy and if I react to the noise, I am getting punished by the brokerage commissions without any perceptible reward. It seems like I need something to tell me whether the signal has really changed (and if so, then by how much). Then I can make an intelligent decision on whether I should adjust my portfolio, and if the expected reward exceeds the transaction costs.

In the Bayesian context, I see multiple references to this [2007 paper by Adams and MacKay](https://arxiv.org/pdf/0710.3742.pdf), in which they model the “run length” of a time-series regime. The paper is elegantly summarized in this [blog post](http://gregorygundersen.com/blog/2019/08/13/bocd/), as well as this [talk](https://youtu.be/cas__TaFk9U), which also proposes a computationally efficient way to calculate the most likely path through the time-series regimes. I believe the speaker is referencing this [GitHub](https://github.com/gwgundersen/bocd/), authored by the aforementioned blogger that contains a numpy/scipy implementation of the Adams / MacKay algorithm.

Anyway, I don’t have a strong quantitative background, so it is hard for me to understand the basic theoretical underpinnings, let alone to implement the code in PyMC3. However, I thought that online changepoint detection must be a frequent enough use case that perhaps others, who are more versed in Bayesian statistics, would be interested in taking a look and weighing in on what the PyMC3 framework would look like. I also thought that for my specific problem I can start with a simpler solution, like [GP smoothing](https://docs.pymc.io/notebooks/GP-smoothing.html). At this point, I feel that any advice from you guys would be super helpful. Thanks!

---

<div class="post-metadata">

**Author:** ![sharsenij14](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@sharsenij14](https://discourse.pymc.io/u/sharsenij14)\
**Post date:** [March 29, 2020, 6:09am UTC](https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757/2 "2020-03-29T06:09:30Z")

</div>

Here are a couple other GitHubs with numpy/scipy implementations: [one](https://github.com/hildensia/bayesian_changepoint_detection/blob/master/Example%20Code.ipynb) (notebook, seems informative), [two](https://github.com/dtolpin/bocd) (claims performance improvement over what is in the first link)

Also, here’s documentation for the graphlab implementation: [link](https://turi.com/learn/userguide/anomaly_detection/bayesian_changepoints.html)

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [March 29, 2020, 7:40am UTC](https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757/3 "2020-03-29T07:40:46Z")

</div>

Model like this is probably pretty difficult to implement in pymc3 as it involves dynamic expanding the parameter set. You can translate some of the component into pymc3 tho.

---

<div class="post-metadata">

**Author:** ![sharsenij14](https://avatars.discourse-cdn.com/v4/letter/s/a4c791/32.png) [@sharsenij14](https://discourse.pymc.io/u/sharsenij14)\
**Post date:** [March 29, 2020, 7:59am UTC](https://discourse.pymc.io/t/modeling-stock-market-signals-online-changepoint-detection/4757/4 "2020-03-29T07:59:40Z")

</div>

Understood. Thank you.

I’ll see what I can piece together in numpy/scipy. And if I can get an improvement with simpler solutions (smoothing) in pymc3.
