Change observed data without redefining model

Given a model, I’d like to change the observed data to infer model parameters for a different dataset.
Usually I specify the observed data as part of the model definition, such as

import numpy as np
import pymc3 as pm

mean1 = 3
N = 1000
data1 = np.random.normal(loc=mean1, size=N)

with pm.Model() as model:
    mean = pm.Flat('mean')
    obs = pm.Normal('obs', mu=mean, sd=1, observed=data1)

with model:
    map_s = pm.find_MAP()

This will correctly give something close to 3 for mean

But now I want to infer on a different dataset

mean2 = 10
data2 = np.random.normal(loc=mean2, size=N)

How do I specify this as the new observed dataset? I want to keep the model as it is otherwise.

I tried something like

obs.observations = data2
obs.tag.test_value = data2

but find_map() still returns 3 for the mean.

You can use theano.shared when defining the observation:

import numpy as np
import pymc3 as pm
import theano

mean1 = 3
N = 1000
data1 = np.random.normal(loc=mean1, size=N)
data = theano.shared(data1)

with pm.Model() as model:
    mean = pm.Flat('mean')
    obs = pm.Normal('obs', mu=mean, sd=1, observed=data)

with model:
    map_s1 = pm.find_MAP()
# change data
mean2 = 10
data2 = np.random.normal(loc=mean2, size=N)
data.set_value(data2)
with model:
    map_s2 = pm.find_MAP()

More information could be found on the PyMC3 doc

2 Likes

Hi there! my code is like:

import pandas as pd
import pymc3 as pm
import theano
import numpy as np

df = pd.read_csv("../input/attemptclasstime.csv", index_col = None, na_values = ['NA'])
df = df[df.studentclass == 5]
df = df.sort_values('question', ascending=[1])
unique_qid = df["question"].unique()
for j in unique_qid:
    nndf = df[df.question == j]
    data1 = nndf["status"].tolist()
    data = theano.shared(data1)

And is generating the following error, please help!

    AttributeError                            Traceback (most recent call last)
/opt/conda/lib/python3.6/site-packages/pymc3/theanof.py in floatX(X)
     64     try:
---> 65         return X.astype(theano.config.floatX)
     66     except AttributeError:

AttributeError: 'SharedVariable' object has no attribute 'astype'