Tips for Stan installation for Windows Users¶
This recitation was written by Julian Wagner.
Here are two big issues I encountered when trying to install cmdstanpy on my Windows machines.
A space in the username kills the install¶
Windows is terrible about keeping the user from introducing a space into the username, e.g. Julian Wagner
. A space in the username leads to a space in the user folder, e.g. /c/Users/Julian Wagner/
; I myself ended up with a space in the username for one of my Windows machines (curse you Windows for using a space!!), and it killed the install. I tried to make a workaround for this, but was not able to solve it. If you have a space in your user folder (you can check in
File explorer > Local disk (C:) > Users
and look at the folder there), I suggest you make a new user login for your machine that has no space. If you make this new user an admin, you will be able to access the files from your existing login on the new login. To make a new user login, you can do Settings > Accounts > Other Users > Add someone else to this PC > I don't have this person's sign-in information > Add a user without a Microsoft account
. Now you can enter a new username and the
key is to pick a new username without a space!!, e.g. jwagner
. With that done, you can click on the new user and do Change account type > Administrator
if you want to access files from your existing user with the new user. From the start menu, you can click on the little icon that looks like a person and click on the new username to switch to the new user login. It will take a bit to get set up for logging in. If you did a local install of Git Bash/Anaconda, then you will need to
redo the setup from bebi103a and bebi103b to do installs for the new user login. As irksome as it is, I am not aware of how to succeed with the install with a User folder with a space in it.
Justin’s test code¶
To test your installation of CmdStanPy, you can run this test code on the famous “schools” hierarchical model.
[1]:
import numpy as np
import cmdstanpy
import arviz as az
import bokeh.plotting
import bokeh.io
bokeh.io.output_notebook()
schools_data = {
"J": 8,
"y": [28, 8, -3, 7, -1, 1, 18, 12],
"sigma": [15, 10, 16, 11, 9, 11, 10, 18],
}
schools_code = """
data {
int<lower=0> J; // number of schools
vector[J] y; // estimated treatment effects
vector<lower=0>[J] sigma; // s.e. of effect estimates
}
parameters {
real mu;
real<lower=0> tau;
vector[J] eta;
}
transformed parameters {
vector[J] theta = mu + tau * eta;
}
model {
eta ~ normal(0, 1);
y ~ normal(theta, sigma);
}
"""
with open("schools_code.stan", "w") as f:
f.write(schools_code)
sm = cmdstanpy.CmdStanModel(stan_file="schools_code.stan")
samples = sm.sample(data=schools_data, output_dir="./")
samples = az.from_cmdstanpy(samples)
# Make a plot of samples
p = bokeh.plotting.figure(
frame_height=250, frame_width=250, x_axis_label="μ", y_axis_label="τ"
)
p.circle(
np.ravel(samples.posterior["mu"]),
np.ravel(samples.posterior["tau"]),
alpha=0.1
)
bokeh.io.show(p)
INFO:cmdstanpy:compiling stan program, exe file: /Users/bois/Dropbox/git/bebi103_course/2021/b/content/recitations/04/schools_code
INFO:cmdstanpy:compiler options: stanc_options=None, cpp_options=None
INFO:cmdstanpy:compiled model file: /Users/bois/Dropbox/git/bebi103_course/2021/b/content/recitations/04/schools_code
INFO:cmdstanpy:start chain 1
INFO:cmdstanpy:start chain 2
INFO:cmdstanpy:start chain 3
INFO:cmdstanpy:start chain 4
INFO:cmdstanpy:finish chain 3
INFO:cmdstanpy:finish chain 1
INFO:cmdstanpy:finish chain 4
INFO:cmdstanpy:finish chain 2
[2]:
%load_ext watermark
%watermark -v -p numpy,bokeh,cmdstanpy,arviz,jupyterlab
CPython 3.8.5
IPython 7.19.0
numpy 1.19.2
bokeh 2.2.3
cmdstanpy 0.9.67
arviz 0.11.0
jupyterlab 2.2.6