# Sampling Deterministic Random Variables

I have a very simple model:

I have a Uniform prior, p; a Binomial Likelihood that uses the prior as parameter p. I make an observation (e.g. a success) to obtain my posterior belief of p.

This is easily achieved in PyMC3. However, I wish to define a new Random Variable, say, q, that is the reciprocal of p.

I (naively) thought I could just define a new variable `q = p**-1`, but this does not allow me to see the trace or sample from q when I call

`trace = pm.sample(500)`

Is there a way for me to take samples from a RV that is not explicitly used in the model but is deterministically related to a RV that IS in the model?

``````import pymc3 as pm
from scipy import optimize

basic_model = pm.Model()
with basic_model:
p = pm.Uniform('p', lower=0, upper=1)
q = p**-1       # THIS IS WHAT I WISH TO BE ABLE TO SEE IN THE TRACE
out = pm.Binomial('out', n=1, p=p, observed=1)
trace = pm.sample(500)
_ = pm.traceplot(trace)``````
1 Like

`pm.Deterministic` exists for just that!

``````with pm.Model() as basic_model:
p = pm.Uniform('p', lower=0, upper=1)
q = pm.Deterministic('q', 1 / p)
out = pm.Binomial('out', n=1, p=p, observed=1)
trace = pm.sample(500)
pm.traceplot(trace)
``````

3 Likes

Wow, that’s an awful lot simpler than I was expecting! Classic newbie overcomplicating things Thanks!