# Using a random variable as observed

**URL:** <https://discourse.pymc.io/t/using-a-random-variable-as-observed/7184>\
**Category:** Questions\
**Created:** [April 6, 2021, 2:17pm UTC](https://discourse.pymc.io/t/using-a-random-variable-as-observed/7184 "2021-04-06T14:17:32Z")\
**Posts on this page:** 1\
**Showing post:** 5

<div class="post-metadata">

**Author:** ![OriolAbril](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/oriolabril/32/2497_2.png) [@OriolAbril](https://discourse.pymc.io/u/OriolAbril)\
**Post date:** [April 8, 2021, 1:02pm UTC](https://discourse.pymc.io/t/using-a-random-variable-as-observed/7184/5 "2021-04-08T13:02:59Z")

</div>

I have two ideas on how to trick pymc3 into using an observed variable as observed, I am not sure if it’s a good idea though. It may also be important to note that if PyMC3 were to allow random variable as observed, that would break after sampling when trying to convert to ArviZ and compute convergence chechs.

### Option 1

Use a `pm.Potential`. I believe something like `pm.Potential("wpp", pm.Beta.dist(alpha=alpha, beta=beta).logp(raw_with_slight_adjustment))` will add the right term to the joint probability.

### Option 2

Use the cursed `DensityDist`. There are at least 5 options to do that with a densitydist, 3 of which should work in this situation, afaik, all 5 should work when using proper observed values (and many won’t need the density\_dist\_obs thingy when doing so). Remember to use `idata_kwargs={"density_dist_obs": False}`

##### 2.1

```auto
DensityDist(
    "wpp", 
    pm.Beta.dist(alpha=alpha, beta=beta).logp, 
    observed=raw_with_slight_adjustment
)

```

This won’t work because if observed is not a dict, DensityDist has the same check all other distributions do, and doesn’t allow `FreeRVs` to be passed.

##### 2.2

```auto
DensityDist(
    "wpp", 
    pm.Beta.dist(alpha=alpha, beta=beta).logp, 
    observed={"value": raw_with_slight_adjustment}
)

```

This should now trigger the dict checks, and avoid the FreeRV one. I still don’t know if this is a feature or a bug, at the very least feels wrong to me. One can see in the [docs](https://docs.pymc.io/api/distributions/continuous.html#pymc3.distributions.continuous.Beta.logp) that the logp method of beta takes a `value` argument, so this should be public knowledge, what I don’t think is documented anywhere official is the array vs dict input trick. I have lost count of how many discourse posts and issues I have answered explaining that and saying `density_dist_obs=False` should be used.

##### 2.3

```auto
DensityDist(
    "wpp", 
    lambda a, b, raw: pm.Beta.dist(alpha=a, beta=b).logp(raw), 
    observed={"raw": raw_with_slight_adjustment, "a": alpha, "b": beta}
)

```

##### 2.4

```auto
def logp_fun(raw):
    pm.Beta.dist(alpha=alpha, beta=beta).logp(raw)
pm.DensityDist("wpp", logp_fun, observed=raw_with_slight_adjustment)

```

Again, won’t work because no dict input

##### 2.5

```auto
def logp_fun(raw):
    pm.Beta.dist(alpha=alpha, beta=beta).logp(raw)
pm.DensityDist(
    "wpp", logp_fun, observed={"raw": raw_with_slight_adjustment}
)

```

* * *

Disclaimer: I don’t have the slightest idea of which of these approaches is the one DensityDist was designed for, if there are any differences between them when it comes to preformance nor if one of these (or a 6th alternative) should be the “recommended” approach. I personally prefer the `Potential` option, but this opinion is only based on style and conceptual coherence in the use of `observed`

---

_[View the full topic](https://discourse.pymc.io/t/using-a-random-variable-as-observed/7184)._
