# PyMC for Monte Carlo Simulation

**URL:** <https://discourse.pymc.io/t/pymc-for-monte-carlo-simulation/8744>\
**Category:** Questions\
**Tags:** modeling\
**Created:** [February 7, 2022, 11:28am UTC](https://discourse.pymc.io/t/pymc-for-monte-carlo-simulation/8744 "2022-02-07T11:28:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Morrr](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/morrr/32/4777_2.png) [@Morrr](https://discourse.pymc.io/u/Morrr)\
**Post date:** [February 7, 2022, 11:28am UTC](https://discourse.pymc.io/t/pymc-for-monte-carlo-simulation/8744/1 "2022-02-07T11:28:45Z")

</div>

I am new to Bayesian methods and PyMC, so forgive me if this question might sound odd:

For the sake of simplicity, suppose a linear model. However, the actual model might consist of several parameters.

```
y = pm.Normal(beta*X, sigma**2) 

beta = [pm.Normal(..., name=intercept), pm.Normal(..., name=slope)] 

```

X being a controllable physical parameter e.g. concentration, pressure etc. If I have allowed limits on y (e.g. toxicological limits), I would like to know which ranges of X would yield acceptable limits of y. Once I fitted the model parameters (beta), intuitively I would perform a Monte Carlo simulation if the problem cannot be solved analytically. Since PyMC already has efficient samplers built in, I was wondering if the following task or steps could be achieved using PyMC and if such an approach would make sense for you or if you would suggest a different approach?

1.) Fit the model and find the unknown parameters  
2.) Convert the input X to a probability distribution to sample from  
3.) Use PyMC.sample to draw samples and calculate y  
4.) Define an acceptable range for X which yields y values within the acceptable range (e.g. below toxic concentration)

---

<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:** [February 8, 2022, 6:19am UTC](https://discourse.pymc.io/t/pymc-for-monte-carlo-simulation/8744/2 "2022-02-08T06:19:45Z")

</div>

> [@Morrr](#):
>
> 1.) Fit the model and find the unknown parameters

when you do `pm.sample(...)` the resulting fit represent your unknown parameter as MCMC samples

> [@Morrr](#):
>
> 2.) Convert the input X to a probability distribution to sample from

you just need to more X as well in your model, this is an old blogpost but the idea stands [Motif of the Mind | Junpeng Lao, PhD](https://junpenglao.xyz/Blogs/posts/2017-10-23-OOS_missing.html)

> [@Morrr](#):
>
> 3.) Use PyMC.sample to draw samples and calculate y

for calculating y I suspect you are looking for `pm.sample_posterior_preditive`, which give you prediction _conditioned_ on your observation and inference result

> [@Morrr](#):
>
> 4.) Define an acceptable range for X which yields y values within the acceptable range (e.g. below toxic concentration)

You can also do that use posterior predictive sample, by finding all the y within the acceptable range, and index to X, and compute the range of `X[index, :]`
