Skip to main content

Magics for defining and running stan code in Jupyter.

Project description

CmdStanJupyter

CmdStanJupyter is a package to help development of Stan models (using CmdStanPy) in jupyter notebooks.

Use it with jupyterlab-stan-highlight to recieve highlighting for your %%stan blocks in python notebooks!

The package is heavily based on Arvinds-ds jupyterstan package, but provides an interface that simply returns a cmdstanpy.CmdStanModel object.

Features

  • Compile a stan model and save it as a cmdstanpy variable by running a %%stan cell
  • Display and load an existing stan file with %stanf

Installation

To install the library:

pip install cmdstanjupyter

This does not install cmdstanpy by default, as the recommended installation for that package is via conda. If you want to install cmdstanpy via pip alongside this package, use

pip install cmdstanjupyter[all]

Usage

To use the magic in your notebook, you need to lead the extension:

%load_ext cmdstanjupyter

To define a stan model inside a jupyter notebook, start a cell with the %%stan magic. You can also provide a variable name, which is the variable name that the cmdstanpy.CmdStanModel object will be assigned to. For example:

%%stan paris_female_births
data {
    int male;
    int female;
}

parameters {
    real<lower=0, upper=1> p;
}

model {
    female ~ binomial(male + female, p);
}

When you run this cell, cmdstanjupyter will create a cmdstanpy CmdStanModel object, which will compile your model and allow you to sample from it.

If the above code was stored in a file births.stan, the following is also possible:

%stanf paris_female_births births.stan
data {
    int male;
    int female;
}

parameters {
    real<lower=0, upper=1> p;
}

model {
    female ~ binomial(male + female, p);
}

To use your compiled model:

fit = paris_female_births.sample(
    data={'male': 251527, 'female': 241945},
)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cmdstanjupyter-0.5.0.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

cmdstanjupyter-0.5.0-py2.py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page