Skip to content

Issues getting variable in hierarchical model to update #2271

Closed
@aspassiani

Description

@aspassiani

I have previously been working with OpenBUGS/WinBUGS to perform my Bayesian statistics but have decided to move over to using the PYMC3 package in Python. I believe I have correctly converted my code to be used with PYMC3 but I am having issues with getting my output from PYMC3 to match my output from BUGS, specifically one of my variables does not update from its initialized values. My code is written as follows:

model2 = pm.Model()
with model2:
    # Co-efficients
    alpha = pm.Normal('alpha', mu=0.0, sd=1000)
    beta = pm.Deterministic('beta', tt.exp(alpha))
    lamda = pm.Gamma('lamda', mu=1.0e-2, sd=100)
    # Population Effect
    pop_eff= pm.Deterministic('pop_eff', tt.exp((-1*beta)/tt.exp(pop_den_all))) 
    # Mean/Expected Event Occurrence Rate
    lamdal = pm.Deterministic('lamdal', lamda*5625)
    # Actual Occurrence of Events
    Tlatent_new = pm.Poisson('Tlatent_new', mu=lamdal, shape=56)
    # Observed Event Counts
    Tobs_new = pm.Binomial('Tobs_new', n=Tlatent_new, p=pop_eff, shape=56, observed=Tobs)
    # Initialization
    start1 = {'alpha': 0.5, 'lambda': 1., 'Tlatent_new': Tlatent}
    start2 = {'alpha': 1., 'lambda': 2., 'Tlatent_new': Tlatent}
    start = [start1, start2]
    step1 = pm.Metropolis([alpha, lamda])
    step2 = pm.BinaryGibbsMetropolis([Tlatent_new])

with model2:
    trace2 = merge_traces([pm.sample(2000, step=[step1, step2], tune=1000, start=start[i], chain=i)
                            for i in range(2)])

with sample data:

#Sample Data
Tobs = np.array([0,0,1,0,0,0,8,3,0,0,2,0,1,0,0,0,0,3,4,1,1,0,1,0,1,0,0,0,0,0,0,0,0,3,0,0,0,1,5,2,
                    27,17,26,9,6,0,1,3,4,5,1,4,16,73,26,3])
pop_den_all = np.array([0.01,0.69,1.03,0.12,0.22,0.59,26.60,9.76,0.0033,0.064,0.81,0.96,0.76,1.01,
                        0.20,0.79,4.47,12.21,18.56,1.68,0.33,0.001,0.22,0.046,0.11,0.065,0.33,0.002,
                        0.065,0.021,0.33,0.086,0.60,1.43,0.072,0.13,0.73,2.89,7.22,5.42,24.35,104.88,
                        122.54,17.71,5.13,1.70,0.037,2.05,5.43,2.83,1.61,3.00,20.19,284.83,98.69,2.67])
#Initialization Data
Tlatent = np.array([80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,
                    80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,80,
                    80,80,80,80,80,80])

When I run the model, the variable Tlatent_new does not update from the initialized values I give it in Tlatent. This occurs when I want to include the effects of population density in my model using this line Tobs_new = pm.Binomial('Tobs_new', n=Tlatent_new, p=pop_eff, shape=56, observed=Tobs). When I do not include this line the output of Tlatent_new differs from the initial guess Tlatent.

I cannot figure out what is wrong with that additional line. I think it might have something to do with the use of the Binomial distribution because if I change it to a different distribution I will see a change in Tlatent_new from the initial guess Tlatent. I don't see why the Binomial distribution would not work. I thought my choice of sampling method might be an issue but I have tried changing that as well with no luck.

Any help as to why this does not work is greatly appreciated.

Thanks in advance,

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions