Skip to content

Instantly share code, notes, and snippets.

@tommylees112
Last active April 12, 2022 13:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tommylees112/5ac7a4973d57147c2b8f5fda477b929c to your computer and use it in GitHub Desktop.
Save tommylees112/5ac7a4973d57147c2b8f5fda477b929c to your computer and use it in GitHub Desktop.
New Pystan Environment that works on M1 Mac
conda create -n stan --yes
conda activate stan
conda install pip --yes
# Install httpstan from source (this will take a while).
pip install -v git+https://github.com/tillahoffmann/httpstan@pip
# Install pystan.
pip install 'pystan>=3'
# Try a dummy program (this should report build and sampling progress).
python -c 'import stan; stan.build("parameters { real x; } model { x ~ normal(0, 1); }").sample()'
conda install -c conda-forge ipython arviz scikit-learn seaborn jupyterlab nest-asyncio --yes
# test https://pystan.readthedocs.io/en/latest/
@tommylees112
Copy link
Author

@tommylees112
Copy link
Author

Extra information about setting up your environment here:
https://gist.github.com/tommylees112/21d9f928dc63693181c8634e085d4084

  1. install xcode
  2. run xcode-select --install to install build-essential on Mac

@tommylees112
Copy link
Author

Test the 8 schools model: https://pystan.readthedocs.io/en/latest/

@tommylees112
Copy link
Author

tommylees112 commented Feb 16, 2022

Pystan Test:

import stan
import numpy as np
import arviz as az

schools_code = """
data {
  int<lower=0> J;         // number of schools
  real y[J];              // estimated treatment effects
  real<lower=0> sigma[J]; // standard error of effect estimates
}
parameters {
  real mu;                // population treatment effect
  real<lower=0> tau;      // standard deviation in treatment effects
  vector[J] eta;          // unscaled deviation from mu by school
}
transformed parameters {
  vector[J] theta = mu + tau * eta;        // school treatment effects
}
model {
  target += normal_lpdf(eta | 0, 1);       // prior log-density
  target += normal_lpdf(y | theta, sigma); // log-likelihood
}
"""

schools_data = {"J": 8,
                "y": [28,  8, -3,  7, -1,  1, 18, 12],
                "sigma": [15, 10, 16, 11,  9, 11, 10, 18]}

posterior = stan.build(schools_code, data=schools_data)
fit = posterior.sample(num_chains=4, num_samples=1000)
eta = fit["eta"]  # array with shape (8, 4000)
df = fit.to_frame()  # pandas `DataFrame, requires pandas

# requires arviz and numpy
schools = ['Choate', 'Deerfield', 'Phillips Andover', 'Phillips Exeter', 'Hotchkiss', 'Lawrenceville', "St. Paul's", 'Mt. Hermon']
data = az.from_pystan(fit, coords={"school": schools}, dims={"eta": ["school"], "theta": ["school"]})
posterior = data.posterior

print(posterior)

To check plotting:

az.plot_pair(
    posterior,
    coords={"school": ["Choate", "Deerfield", "Phillips Andover"]},
    divergences=True,
)

@tommylees112
Copy link
Author

tommylees112 commented Feb 16, 2022

@tommylees112
Copy link
Author

tommylees112 commented Apr 7, 2022

# issue when running code inside a jupyter notebook
https://stackoverflow.com/questions/55409641/asyncio-run-cannot-be-called-from-a-running-event-loop

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
/var/folders/q3/0lmt64ld10s14_0n0vxpt_m00000gp/T/ipykernel_28231/803591416.py in <module>
----> 1 posterior = stan.build(model_code, data=data)

~/miniconda3/envs/stan/lib/python3.9/site-packages/stan/model.py in build(program_code, data, random_seed)
    515 
    516     try:
--> 517         return asyncio.run(go())
    518     except KeyboardInterrupt:
    519         return  # type: ignore

~/miniconda3/envs/stan/lib/python3.9/asyncio/runners.py in run(main, debug)
     31     """
     32     if events._get_running_loop() is not None:
---> 33         raise RuntimeError(
     34             "asyncio.run() cannot be called from a running event loop")
     35 

RuntimeError: asyncio.run() cannot be called from a running event loop
​

From the Pystan FAQs

# How can I use PyStan with Jupyter Notebook or JupyterLab?
Use nest-asyncio. This package is needed because Jupter Notebook blocks the use of certain asyncio functions. (To verify this, try running asyncio.run(asyncio.sleep(1)) in a notebook.) If you would like to learn more about the problem, see the following issue: ipython/ipykernel#548. This problem only affects Jupyter Notebook and derivatives. It does not affect IPython.

Add the following lines to the opening of jupyter notebook github page for nest_asyncio:

import nest_asyncio
nest_asyncio.apply()

@tommylees112
Copy link
Author

tommylees112 commented Apr 12, 2022

Initialisation Errors are caused by data not meeting the constraints: here

---------------------------------------------------------------------------
RemoteTraceback                           Traceback (most recent call last)
RemoteTraceback: 
"""
Traceback (most recent call last):
  File "/home/thomas_lees112/miniconda3/envs/stan/lib/python3.7/multiprocessing/pool.py", line 121, in worker
    result = (True, func(*args, **kwds))
  File "/home/thomas_lees112/miniconda3/envs/stan/lib/python3.7/multiprocessing/pool.py", line 44, in mapstar
    return list(map(*args))
  File "stanfit4anon_model_4f4910bcf40488584c481cb91fedfe57_36726699539089669.pyx", line 371, in stanfit4anon_model_4f4910bcf40488584c481cb91fedfe57_36726699539089669._call_sampler_star
  File "stanfit4anon_model_4f4910bcf40488584c481cb91fedfe57_36726699539089669.pyx", line 404, in stanfit4anon_model_4f4910bcf40488584c481cb91fedfe57_36726699539089669._call_sampler
RuntimeError: Initialization failed.
"""

But there is a problem with empty int lists: Github Issue here

This was discussed Discourse here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment