# Sample from prior?

**URL:** <https://discourse.pymc.io/t/sample-from-prior/1542>\
**Category:** Questions\
**Created:** [July 20, 2018, 4:14pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542 "2018-07-20T16:14:56Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 4:14pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/1 "2018-07-20T16:14:56Z")

</div>

Is it possible to generate samples from an **untrained** model? This seems useful for assessing a prior. However, when I try using `sample()` with a model I have created that has hyperparameters, I get this error:

```
Traceback (most recent call last):
 File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/joblib/externals/loky/backend/queues.py", line 151, in _feed
obj, reducers=reducers)
File "/opt/local/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/joblib/externals/loky/backend/reduction.py", line 145, in dumps
p.dump(obj)
ValueError: must use protocol 4 or greater to copy this object; since __getnewargs_ex__ returned keyword arguments.

```

I found lots of answers about prior _predictive_ sampling, but nothing for just prior sampling.

---

<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:** [July 20, 2018, 4:24pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/2 "2018-07-20T16:24:38Z")

</div>

prior predictive sampling (`pm.sample_prior_predictive`) is the thing you are looking for, it returns a dict which contains the prior samples from priors and the prior predictive.

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 4:35pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/3 "2018-07-20T16:35:07Z")

</div>

Sorry if I’m being stupid, but when I do `help(pm.sampling)` I don’t seem to have this method available. I have only `sample_ppc`. I’ve got pymc3 3.4.1 from pip installed. Am I using a version that’s too old?

---

<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:** [July 20, 2018, 5:28pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/4 "2018-07-20T17:28:33Z")

</div>

Ohhh right, it is on master (sorry about that). If you wait 1-2 days we are releasing 3.5 very soon.

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 5:29pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/5 "2018-07-20T17:29:46Z")

</div>

Thanks! I’ll try pip installing from git for now.

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 6:04pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/6 "2018-07-20T18:04:59Z")

</div>

A follow-up question: `sample_prior_predictive` is taking very long to run on my model. This surprises me, because my model is a causal, generative model. There’s one layer of hyper-parameters, then a layer of Gaussians whose parameters are weighted sums of the hyperparameters.  
It should be possible for PyMC3 to sample from this distribution very quickly, by simple forward sampling, so I’m surprised that it takes so long.  
Is there any way to “tell” PyMC3 to do simple forward sampling? Or do I need to write my own forward sampler for this model?  
Thanks for all of your help!

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 6:20pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/7 "2018-07-20T18:20:05Z")

</div>

P.S. I was thinking I could write my own forward sampler by looking at `model.vars[n].get_parents()`, but when I do that in my model I get `[]` for all of the variables. So is there something I should do to get the model’s links built?  
I was thinking I could topologically sort the variables and then generate from them in sort order, and that would be faster than the current method (for my particular model).

---

<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:** [July 20, 2018, 6:38pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/8 "2018-07-20T18:38:46Z")

</div>

Hand written forward stimulation is of course faster, as PyMC3 still need to walk the graph and get/set the state of the RV. But the current implementation is already pretty fast (you can have a look at some previous experiment/implementation [https://github.com/junpenglao/Planet\_Sakaar\_Data\_Science/blob/master/Miscellaneous/Test\_sample\_prior.ipynb](https://github.com/junpenglao/Planet_Sakaar_Data_Science/blob/master/Miscellaneous/Test_sample_prior.ipynb)).  
If you write you own forward sampler, you should not use the model.vars[n], as it would still be quite slow. You should just use the random generation from scipy/numpy, and do the forward yourself (again, see notebook above).

---

<div class="post-metadata">

**Author:** ![rpgoldman](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/rpgoldman/32/1800_2.png) [@rpgoldman](https://discourse.pymc.io/u/rpgoldman)\
**Post date:** [July 20, 2018, 7:36pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/9 "2018-07-20T19:36:58Z")

</div>

Thank you. I _think_ I have it working now.  
One note: I was misled by the `vars` argument to `sample_predictive_prior`. This parameter, as far as I can tell, cannot be passed a list of variables, but must be passed a list of variable _names_. That definitely cost me some time to figure out. The easy solution would be to change the parameter name to `varnames`, and change the documentation from

```
vars : iterable
    Variables for which to compute the posterior predictive samples.

```

to

```
varnames : iterable
    Names of the variables for which to compute the posterior predictive samples.

```

Alternatively, you could keep the name, and check to see if `vars` is already bound to a list of variables before treating it as a list of names and trying to look them up.

---

<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:** [July 20, 2018, 9:13pm UTC](https://discourse.pymc.io/t/sample-from-prior/1542/10 "2018-07-20T21:13:56Z")

</div>

Oh yes you are right - would you like to send a pull request to improve the docstring?
