# Deterministic Variable inside logp

Hi Pymc3 Comunity,
I have a custom distribution defined as a `pm.DensityDist()`
There are a number of intermediate calculation steps inside the logp of this custom dist
I’d love to have some of these intermediate values included in a) the trace, and b) posterior predictive checks.
Essentially I’d like to have a deterministic variable defined inside a logp.
The naive approach of defining it dirrectly fails with a “ValueError: Variable name trans_p already exists.” (regardless of the var name).

Here is a toy example of what im trying to do:

`````` def logp(current_idx, next_idx):
alpha_t = alpha[current_idx, :]
trans_baserate_t = trans_baserate[current_idx, :]
trans_p = pm.Deterministic('trans_p', trans_baserate_t + alpha_t) #here
cat = pm.Categorical.dist(p=trans_p)
return cat.logp(next_idx)
``````

Is this possible? or will I have to recreate this deterministic variable from the trace object?

1 Like

It should be possible if you wrap it into a distribution (we done that before here https://github.com/pymc-devs/pymc3/blob/3873f3ce6daea604750b896d45bbf59f7cee72be/pymc3/distributions/multivariate.py#L957-L961), but I think an easier workaround would be create the Deterministic outside of the custom logp and pass it to the logp function instead.

Thanks!
I’m not sure how to create it outside the logp, as it depends on values inside the logp.
However, I moved everything outside the logp and it worked fine. Thanks!

``````    alpha_t = alpha[current_idx, :]
trans_baserate_t = trans_baserate[current_idx, :]
trans_p = pm.Deterministic('trans_p', trans_baserate_t + alpha_t) #here
obs = pm.Categorical('obs', p=trans_p, observed=next_idx)
``````