<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 https
Step2:  Eight schools example
https
Step4:  Optimization in Stan
https
<ASSISTANT_TASK:>
Python Code:
%matplotlib inline
from matplotlib import pyplot as plt
import pystan
import warnings
warnings.filterwarnings("ignore")
Explanation: https://pystan.readthedocs.org/
Install
conda install pystan
End of explanation
schools_code = 
data {
    int<lower=0> J; // number of schools
    real y[J]; // estimated treatment effects
    real<lower=0> sigma[J]; // s.e. of effect estimates
}
parameters {
    real mu;
    real<lower=0> tau;
    real eta[J];
}
transformed parameters {
    real theta[J];
    for (j in 1:J)
    theta[j] <- mu + tau * eta[j];
}
model {
    eta ~ normal(0, 1);
    y ~ normal(theta, sigma);
}
schools_dat = {'J': 8,
               'y': [28,  8, -3,  7, -1,  1, 18, 12],
               'sigma': [15, 10, 16, 11,  9, 11, 10, 18]}
fit = pystan.stan(model_code=schools_code, data=schools_dat,
                  iter=1000, chains=4)
fit
la = fit.extract(permuted=True)
la.keys()
for k,v in la.iteritems():
    print k,v.shape
fit.plot()
plt.gcf().set_size_inches(18.5, 10.5);
Explanation: Eight schools example
https://pystan.readthedocs.org/en/latest/getting_started.html
End of explanation
ocode = 
data {
    int<lower=1> N;
    real y[N];
}
parameters {
    real mu;
}
model {
    y ~ normal(mu, 1);
}
sm = pystan.StanModel(model_code=ocode)
y2 = np.random.normal(size=20)
np.mean(y2)
op = sm.optimizing(data=dict(y=y2, N=len(y2)))
op
Explanation: Optimization in Stan
https://pystan.readthedocs.org/en/latest/optimizing.html
End of explanation
<END_TASK>