# Think Bayes example cannot sample from observed data

**URL:** <https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975>\
**Category:** v5\
**Tags:** bug\
**Created:** [August 27, 2024, 9:49am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975 "2024-08-27T09:49:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattiadg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiadg/32/8449_2.png) [@mattiadg](https://discourse.pymc.io/u/mattiadg)\
**Post date:** [August 27, 2024, 9:49am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/1 "2024-08-27T09:49:14Z")

</div>

Hi everyone,  
I am following the great book Think Bayes and I got to the chapter introducing pymc for MCMC. This code is the last example in the chapter:

```auto
with pm.Model() as model9:
    p0 = pm.Beta('p0', alpha=1, beta=1)
    p1 = pm.Beta('p1', alpha=1, beta=1)
    N = pm.DiscreteUniform('N', num_seen, 350)
    
    q0 = 1-p0
    q1 = 1-p1
    ps = [q0*q1, q0*p1, p0*q1, p0*p1]
    
    k00 = N - num_seen
    data = pm.math.stack((k00, k01, k10, k11))
    y = pm.Multinomial('y', n=N, p=ps, observed=data)

```

and according to the book, it should produce the following warning:

```auto
Sampling 4 chains for 1_000 tune and 1_000 draw iterations (4_000 + 4_000 draws total) took 2 seconds.
/home/downey/miniconda3/envs/ThinkBayes2/lib/python3.10/site-packages/pymc/backends/arviz.py:65: UserWarning: Could not extract data from symbolic observation y
  warnings.warn(f"Could not extract data from symbolic observation {obs}")
The rhat statistic is larger than 1.01 for some parameters. This indicates problems during sampling. See https://arxiv.org/abs/1903.08008 for details
The effective sample size per chain is smaller than 100 for some parameters. A higher number is needed for reliable rhat and ess computation. See https://arxiv.org/abs/1903.08008 for details

```

but in many runs I always get the following

```auto
/usr/local/lib/python3.10/dist-packages/pymc/backends/arviz.py:60: UserWarning: Could not extract data from symbolic observation y
  warnings.warn(f"Could not extract data from symbolic observation {obs}")

```

and the distribution forms are quite different from the expected.

Link to the chapter: [MCMC — Think Bayes](https://allendowney.github.io/ThinkBayes2/chap19_v3.html#exercises)  
Package versions:  
pymc: 5.10.4  
arviz: 0.18.0  
pytensor: 2.18.6

---

<div class="post-metadata">

**Author:** ![Genesis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/genesis/32/6017_2.png) [@Genesis](https://discourse.pymc.io/u/Genesis)\
**Post date:** [August 28, 2024, 12:24pm UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/2 "2024-08-28T12:24:57Z")

</div>

What are `num_seen`, `k01`, `k10` and `k11`? Could it be that you are failing to define them?

---

<div class="post-metadata">

**Author:** ![mattiadg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiadg/32/8449_2.png) [@mattiadg](https://discourse.pymc.io/u/mattiadg)\
**Post date:** [August 28, 2024, 2:50pm UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/3 "2024-08-28T14:50:06Z")

</div>

They are integers:

```auto
k10 = 20 - 3
k01 = 15 - 3
k11 = 3
num_seen = k01 + k10 + k11

```

---

<div class="post-metadata">

**Author:** ![mattiadg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiadg/32/8449_2.png) [@mattiadg](https://discourse.pymc.io/u/mattiadg)\
**Post date:** [August 28, 2024, 3:06pm UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/4 "2024-08-28T15:06:43Z")

</div>

This is the full code:

```auto
import pymc as pm

k10 = 20 - 3
k01 = 15 - 3
k11 = 3
num_seen = k01 + k10 + k11

with pm.Model() as model9:
    p0 = pm.Beta('p0', alpha=1, beta=1)
    p1 = pm.Beta('p1', alpha=1, beta=1)
    N = pm.DiscreteUniform('N', num_seen, 350)
    
    q0 = 1-p0
    q1 = 1-p1
    ps = [q0*q1, q0*p1, p0*q1, p0*p1]
    
    k00 = N - num_seen
    data = pm.math.stack((k00, k01, k10, k11))
    y = pm.Multinomial('y', n=N, p=ps, observed=data)
    idata9 = pm.sample(1000, **options)
    az.plot_posterior(idata9)

```

---

<div class="post-metadata">

**Author:** ![Genesis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/genesis/32/6017_2.png) [@Genesis](https://discourse.pymc.io/u/Genesis)\
**Post date:** [August 29, 2024, 10:03am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/5 "2024-08-29T10:03:04Z")

</div>

I think it’s probably because you are setting a random variable as observations. To my knowledge that’s not supported ([Random variable as observation](https://discourse.pymc.io/t/random-variable-as-observation/8581))

---

<div class="post-metadata">

**Author:** ![mattiadg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiadg/32/8449_2.png) [@mattiadg](https://discourse.pymc.io/u/mattiadg)\
**Post date:** [August 29, 2024, 10:56am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/6 "2024-08-29T10:56:03Z")

</div>

So you think that the example has never worked?  
I thought that Think Bayes is a popular book, it is strange that nobody noticed this before.

---

<div class="post-metadata">

**Author:** ![Genesis](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/genesis/32/6017_2.png) [@Genesis](https://discourse.pymc.io/u/Genesis)\
**Post date:** [August 29, 2024, 11:06am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/7 "2024-08-29T11:06:03Z")

</div>

I am not sure to be honest. I am not familiar with the book (though have indeed heard of it) or with the history of PyMC - maybe the syntax at the time was different enough to support it, I am really not sure. Maybe one of the devs or other readers of the book can jump in.

If you use the following for `N` it does seem to no longer emit the warning, though I can’t speak to correctness:

```auto
N = pm.draw(pm.DiscreteUniform.dist(num_seen, 350))

```

---

<div class="post-metadata">

**Author:** ![mattiadg](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/mattiadg/32/8449_2.png) [@mattiadg](https://discourse.pymc.io/u/mattiadg)\
**Post date:** [August 29, 2024, 11:27am UTC](https://discourse.pymc.io/t/think-bayes-example-cannot-sample-from-observed-data/14975/8 "2024-08-29T11:27:00Z")

</div>

In the book there are the outputs for the commands, so he’s likely executed them.  
Your variant does not produce any warnings, but it does not really solve the problem because one of the goals of the exercise is to compute the posterior for N. It is not computed this way.
