Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Posterior predictive of deterministics of observedRVs are wrong #3354

Closed
lucianopaz opened this issue Jan 24, 2019 · 0 comments
Closed

Posterior predictive of deterministics of observedRVs are wrong #3354

lucianopaz opened this issue Jan 24, 2019 · 0 comments

Comments

@lucianopaz
Copy link
Contributor

Following a discourse thread, sample_posterior_predictive does not give consistent results for deterministic nodes that depend on ObservedRV's.

Please provide a minimal, self-contained, and reproducible example.

import numpy as np
from theano import tensor as tt
import pymc3 as pm


def Model_1(meas_in_1, meas_in_2, meas_out):
    with pm.Model() as Assy_model_1:
        mu_in_1 = pm.Normal('mu_in_1', -5., 5.)
        sigma_in_1 = pm.HalfCauchy('sd_in_1', 5.)
        mu_in_2 = pm.Normal('mu_in_2', -5., 5.)
        sigma_in_2 = pm.HalfCauchy('sd__in_2', 5.)

        in_1 = pm.Normal('in_1', mu_in_1, sigma_in_1, observed=meas_in_1)
        in_2 = pm.Normal('in_2', mu_in_2, sigma_in_2, observed=meas_in_2)
        out_diff = in_1 + in_2
        pm.Deterministic('out', out_diff)
        return(Assy_model_1)


np.random.seed(23)
meas_in_1 = 2 + np.random.uniform(0., 1., size=100)
meas_in_2 = 5 + np.random.uniform(0., 1., size=100)
meas_out = meas_in_1 + meas_in_2

Model = Model_1(meas_in_1, meas_in_2, meas_out)

with Model:
    trace = pm.sample()

df_trace = pm.trace_to_dataframe(trace)
print(df_trace.head())

ppc_trace = pm.trace_to_dataframe(trace,
                                  varnames=[n for n in trace.varnames
                                            if n != 'out']).to_dict('records')

ppc = pm.sample_posterior_predictive(model=Model,
                                     trace=ppc_trace,
                                     samples=len(ppc_trace),
                                     vars=(Model.deterministics +
                                           Model.basic_RVs))
print(ppc.keys())
print("ppc['in_1'] + ppc['in_2']")
print(ppc['in_1'] + ppc['in_2'])
print("ppc['out']")
print(ppc['out'])
print("meas_in_1 + meas_in_2")
print(meas_in_1 + meas_in_2)

Please provide any additional information below.

The error is that ppc['out'] != ppc['in_1'] + ppc['in_2'].

The problem seems to come from draw_values. The in_1 and in_2 observed variables are theano.ViewOPs with TensorConstant ancestors:

In [1]: tt.printing.debugprint(Model['out'])
 Elemwise{identity}` [id A] 'out'   
 |Elemwise{add,no_inplace} [id B] ''   
   |ViewOp [id C] 'in_1'   
   | |TensorConstant{[2.5172978...43301769]} [id D]
   |ViewOp [id E] 'in_2'   
     |TensorConstant{[5.6434602...27628245]} [id F]

In draw_values' loop over the named_node relations (while stack:...), TensorConstant variables are ignored and they don't add their parents to the stack. Later on, in the while to_eval or missing_inputs:... loop, the to_eval parameter nodes may be in the wrong order, and out is compiled, and used with the view of the original TensorConstant.value, instead of with the values that get drawn from the posterior predictive distribution.

Versions and main components

  • PyMC3 Version: pymc3.6
  • Theano Version: 1.0.4
  • Python Version: 3.6
  • Operating system: Ubuntu
  • How did you install PyMC3: (conda/pip) git clone
twiecki pushed a commit that referenced this issue Jan 25, 2019
… `TensorConstant` or `SharedVariables` to the named relationship nodes stack, only if these descendants are `ObservedRV` or `MultiObservedRV` instances.

* Fix for 3354

* Fixed float32 precision error

* Added inline comment explaining why we must add observed RVs to the stack
@twiecki twiecki closed this as completed Jan 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants