Updating this mark and recapture question to PyMC

From this question: Question: capture and recapture with hypergeometric

I have updated the code to PyMC:

K = 100 #marked animals in first round
n = 100 #captured animals in second round
obs = 10 #captured and marked in second round

with pm.Model() as my_model:
    N = pm.DiscreteUniform("N", lower=K, upper=10000)

    likelihood = pm.HyperGeometric('likelihood', N=N, k=K, n=n, observed=obs)

    trace = pm.sample(10000, tune=2000)


    ppc = pm.sample_posterior_predictive(trace)

But when it comes to plotting

az.plot_ppc(ppc, kind="scatter", figsize=(6, 2), num_pp_samples=100)

I get TypeError: expected dtype object, got 'numpy.dtype[int64]'. I think this could be related to what Oriol Abril commented in the first question, of a probable inconsistency between scalars and length 1 vectors. What can I do? Thx.

Creating some placeholder data (i.e., obs = [1,2,3,4]), your code runs fine for me. Not sure if that;'s because your observed data is to blame or not. Can you provide the entire error message/traceback?

Writing as you said obs=[1,2,3,4] also shows me the same error. I am with Python 3.7, PyMC 4.0.1 and arviz 0.12.1.

The entire error message for obs = 10:

TypeError                                 Traceback (most recent call last)
TypeError: expected dtype object, got 'numpy.dtype[int64]'

The above exception was the direct cause of the following exception:

SystemError                               Traceback (most recent call last)
<ipython-input-18-e50dab2a9772> in <module>
      1 #data_ppc.posterior_predictive = data_ppc.posterior_predictive.squeeze()
----> 2 az.plot_ppc(trace, kind="scatter")

/usr/local/apps/gcc/7.3.0/python/ in plot_ppc(data, kind, alpha, mean, observed, color, colors, grid, figsize, textsize, data_pairs, var_names, filter_vars, coords, flatten, flatten_pp, num_pp_samples, random_seed, jitter, animated, animation_kwargs, legend, labeller, ax, backend, backend_kwargs, group, show)
    354     # TODO: Add backend kwargs
    355     plot = get_plotting_function("plot_ppc", "ppcplot", backend)
--> 356     axes = plot(**ppcplot_kwargs)
    357     return axes

/usr/local/apps/gcc/7.3.0/python/ in plot_ppc(ax, length_plotters, rows, cols, figsize, animated, obs_plotters, pp_plotters, predictive_dataset, pp_sample_ix, kind, alpha, colors, textsize, mean, observed, jitter, total_pp_samples, legend, labeller, group, animation_kwargs, num_pp_samples, backend_kwargs, show)
    288                     vals = pp_vals.flatten()
    289                     bins = get_bins(vals)
--> 290                     _, hist, bin_edges = histogram(vals, bins=bins)
    291                     hist = np.concatenate((hist[:1], hist))
    292                     ax_i.plot(

/usr/local/apps/gcc/7.3.0/python/ in __call__(self, *args, **kwargs)
    193         """Call the jitted function or normal, depending on flag."""
    194         if Numba.numba_flag:
--> 195             return self.numba_fn(*args, **kwargs)
    196         else:
    197             return self.function(*args, **kwargs)

SystemError: CPUDispatcher(<function histogram at 0x7f523df178c0>) returned a result with an error set

Can you update to the most recent version of PyMC? We are currently at 4.1.7.

I can run your example (including the plot) without error:

1 Like

Thanks, updating does not depend on me but I’ll try.