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.7.0.tar.gz (5.5 kB view details)

Uploaded Source

Built Distribution

cmdstanjupyter-0.7.0-py2.py3-none-any.whl (5.8 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file cmdstanjupyter-0.7.0.tar.gz.

File metadata

  • Download URL: cmdstanjupyter-0.7.0.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.5

File hashes

Hashes for cmdstanjupyter-0.7.0.tar.gz
Algorithm Hash digest
SHA256 e67229545508e3859f79a9a00212741fce3a8d46c18e5738c28e665edcfabe81
MD5 2084f0b78a2500827a70e5151bc81a31
BLAKE2b-256 ec2b768d15d5cdc1f7b8bc6d76234210070144aa8a0c207c949a239d688b532c

See more details on using hashes here.

File details

Details for the file cmdstanjupyter-0.7.0-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for cmdstanjupyter-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 a97a714191e9a14226f7cddff991bb9ed65702b517d5d7950462de5fd3fa1fa3
MD5 6d349eff4a65cee3bf433e86b47fe656
BLAKE2b-256 e28fcd91511e12bb60fdd3abfafb43db4a2397782b77c24ac38e37f8ee577075

See more details on using hashes here.

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