# PyMC3 for physics

**URL:** <https://discourse.pymc.io/t/pymc3-for-physics/275>\
**Category:** Development\
**Created:** [August 22, 2017, 1:05pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275 "2017-08-22T13:05:02Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![owlas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/owlas/32/33_2.png) [@owlas](https://discourse.pymc.io/u/owlas)\
**Post date:** [August 22, 2017, 1:05pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/1 "2017-08-22T13:05:02Z")

</div>

Hi team,

After a brief chat with @fonnesbeck in Southampton, UK a few months back, I thought I would try using the `pm.Potential` function to help teach MCMC in physics and it seems to work excellently.

I’d be keen to write up a notebook for the documentation if you think it would fit?

Here’s a notebook I produced for our group [link](https://gist.github.com/owlas/c4dec8ee1d4c9a7be870467abf9e34c7). It would need adapting a little but I think an example of the Heisenberg model would be a nice example of how pm.Potential can be used.

I think the important concepts for those looking to use PyMC3 for physics problems would be:

- Showing how energy and the probability distribution link
- Explaining the necessity of the prior distributions
- Plotting the energy and probability surfaces

Any other suggestions?

---

<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:** [August 22, 2017, 1:14pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/2 "2017-08-22T13:14:46Z")

</div>

This looks awesome! Yes definitely PR a notebook.  
My only nitpick: you should rewrite the `tot` function using theano ops for the sampling in PyMC3.

---

<div class="post-metadata">

**Author:** ![aseyboldt](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aseyboldt/32/5795_2.png) [@aseyboldt](https://discourse.pymc.io/u/aseyboldt)\
**Post date:** [August 22, 2017, 1:27pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/3 "2017-08-22T13:27:12Z")

</div>

Cool!  
One problem that could come up here is that the sampler might get stuck in one of the local minima. So I would add a traceplot and also run several chains and check the rhat (`pm.gelman_rubin`). Our current hamiltonian methods don’t deal with multimodal posteriors particularly well. SMC might help with that, but I don’t know much about it.  
Also, with nuts you don’t usually need that many samples (that is, as long as the sampler doesn’t get stuck around one mode for long time)

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [August 22, 2017, 4:00pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/4 "2017-08-22T16:00:22Z")

</div>

Very nice example of using PyMC3 (and `pm.Potential`) to simulate physical systems!

I think one nice extension to your example will be to include a “kT- term” and suggest the student/user to test values like 0.1, 1 and 10 to explore the role of temperature in the system.

Regarding @aseyboldt comment I want to add that I am using PyMC3 with the SMC sampler to simulate (small) biomolecules with very good results so far 🙂 Maybe the energy barrier in your example is relatively low, but for the systems I am using SMC is crucial to avoid getting trapped in local minima.

---

<div class="post-metadata">

**Author:** ![owlas](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/owlas/32/33_2.png) [@owlas](https://discourse.pymc.io/u/owlas)\
**Post date:** [August 22, 2017, 4:22pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/5 "2017-08-22T16:22:51Z")

</div>

All, thanks for suggestions so far (keep them coming!) - I’ll get to work on these.

I haven’t used the sequential MC step in PyMC3 and I couldn’t find any documentation other than the docstring. Is there an example of smc in recent docs build?

---

<div class="post-metadata">

**Author:** ![AustinRochford](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/austinrochford/32/92_2.png) [@AustinRochford](https://discourse.pymc.io/u/AustinRochford)\
**Post date:** [August 22, 2017, 4:25pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/6 "2017-08-22T16:25:04Z")

</div>

This is awesome, thanks for writing it up!

---

<div class="post-metadata">

**Author:** ![aloctavodia](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/aloctavodia/32/8642_2.png) [@aloctavodia](https://discourse.pymc.io/u/aloctavodia)\
**Post date:** [August 22, 2017, 5:20pm UTC](https://discourse.pymc.io/t/pymc3-for-physics/275/7 "2017-08-22T17:20:37Z")

</div>

Take a look at this [notebook](https://github.com/pymc-devs/pymc3/blob/96e3600770c567439958258b86102c32f5030edc/docs/source/notebooks/SMC2_gaussians.ipynb)
