# Why sample posterior predictive not work after load .nc file

**URL:** <https://discourse.pymc.io/t/why-sample-posterior-predictive-not-work-after-load-nc-file/8043>\
**Category:** Questions\
**Created:** [September 17, 2021, 2:51pm UTC](https://discourse.pymc.io/t/why-sample-posterior-predictive-not-work-after-load-nc-file/8043 "2021-09-17T14:51:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![GMCobraz\_T](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gmcobraz_t/32/4165_2.png) [@GMCobraz\_T](https://discourse.pymc.io/u/GMCobraz_T)\
**Post date:** [September 17, 2021, 2:51pm UTC](https://discourse.pymc.io/t/why-sample-posterior-predictive-not-work-after-load-nc-file/8043/1 "2021-09-17T14:51:18Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GMCobraz\_T](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/gmcobraz_t/32/4165_2.png) [@GMCobraz\_T](https://discourse.pymc.io/u/GMCobraz_T)\
**Post date:** [September 18, 2021, 10:30am UTC](https://discourse.pymc.io/t/why-sample-posterior-predictive-not-work-after-load-nc-file/8043/2 "2021-09-18T10:30:39Z")

</div>

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)
