Why sample posterior predictive not work after load .nc file

Dear all,

good day. I faced some issue with sampel posterior predictive after load the model.
Can anyone please help? thanks in advance

Code is as below:
import numpy as np
import pandas as pd
import datetime as dt
from pandas_datareader import data
import matplotlib.pyplot as plt
%matplotlib inline

np.random.seed(1000000) #Who wants to be a millionaire

start, end = dt.datetime(2014, 1, 1), dt.datetime(2015, 12, 31)
sp500 = data.DataReader(’^GSPC’,‘yahoo’, start, end).loc[:,‘Close’]

#train, test = np.arange(0, 450), np.arange(451,509)

returns = np.log(sp500[1:] / sp500[0:-1].values) # Calculate log returns
n=len(returns)-1
plt.plot(returns)

import pymc3 as pm
with pm.Model() as model:

#y_obs_last = pm.Data('y_obs_last', returns.values[1:])

sigma = pm.Exponential('sigma', 1./.02, testval=.1)
mus = pm.Normal('mus', 0, sd=5, testval=.1)

nu = pm.Exponential('nu', 1./10)
corr = pm.GaussianRandomWalk('corr', tau=sigma**-2, shape=n)

#lam uses variance in pymc3, not sd like in scipy
r = pm.StudentT('r', nu, mu=mus, lam=1/np.exp(-2*corr), observed=returns.values[:-1]) 

with model:
start = pm.find_MAP(vars=[corr], method=“L-BFGS-B”)

with model:
step = pm.NUTS(vars=[mus, corr, nu,sigma],scaling=start, gamma=.25)
start2 = pm.sample(100, step, start=start, cores=8, chains=2, random_seed=42)[-1]

# Start next run at the last sampled position.
step = pm.NUTS(vars=[mus, corr, nu,sigma],scaling=start2, gamma=.55)
trace = pm.sample(2000, step, start=start2, cores=8, chains=2, random_seed=42)

with model:
y_test = pm.sample_posterior_predictive(trace, 500)

import arviz as az
model_output = az.from_pymc3(trace=trace, posterior_predictive=y_test, model=model)
model_output

model_output.to_netcdf(‘trace_sp500.nc’)

from arviz import from_netcdf
loaded_model = from_netcdf(‘trace_sp500.nc’)

with pm.Model() as new_model:

#y_obs_last = pm.Data('y_obs_last', returns.values[1:])

sigma = pm.Exponential('sigma', 1./.02, testval=.1)
mus = pm.Normal('mus', 0, sd=5, testval=.1)

nu = pm.Exponential('nu', 1./10)
corr = pm.GaussianRandomWalk('corr', tau=sigma**-2, shape=n)

#lam uses variance in pymc3, not sd like in scipy
r = pm.StudentT('r', nu, mu=mus, lam=1/np.exp(-2*corr), observed=returns.values[:-1]) 

# samples
trace2 = loaded_model.posterior

with pm.Model() as new_model:
y_test2 = pm.sample_posterior_predictive(loaded_model, samples=500, model=new_model)

y_test2 result is { }, nothing inside.
But the y_test result is an array.

May i know what’ wrong with my code?

Thank you

Dear all,

I think I made some small mistake and I found the solution.

r = pm.StudentT(‘r’, nu, mu=mus, lam=1/np.exp(-2*corr), observed=returns.values[:-1])

change the np.exp to pm.math.exp will solve the error of sample posterior predictive after load .nc file.
Maybe due to numpy and pymc compatibility, so better standardise and use all pymc functions if possible.

Besides that, I found another mistake that done by me is as follow:
with pm.Model() as new_model:
y_test2 = pm.sample_posterior_predictive(loaded_model, samples=500, model=new_model)

since I defined new_model after load the .nc file.
I once again define new model by pm.Model() as new_model which overwrote my defined model.
So, it is a silly mistake by myself.
correct is
with new_model:
y_test2 = pm.sample_posterior_predictive(loaded_model, samples=500, model=new_model)