Skip to main content

A JupyterLab extension for Stan models.

Project description

cmdstanjupyter

Github Actions Status

This extension provides syntax highlighting for Stan code in JupyterLab, as well as a %%stan magic command to define Stan models in Jupyter notebooks and build them with CmdStanPy.

Requirements

  • JupyterLab >= 4.0.0

Install

To install the extension, execute:

pip install cmdstanjupyter

Note: this does not install CmdStanPy for you, in case you are only interested in Stan syntax highlighting and not the %%stan magic!

Install it separately using pip or conda!

Usage

Screenshot of a notebook with Stan highlighting

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 equivalent:

%stanf births.stan paris_female_births

To use your compiled model:

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

Credits

The %magic is heavily based on the previous jupyterstan package. The highlighting code is inspired by jupyterlab-stata-highlight.

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

cmdstanjupyter-2.1.0-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: cmdstanjupyter-2.1.0.tar.gz
  • Upload date:
  • Size: 90.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for cmdstanjupyter-2.1.0.tar.gz
Algorithm Hash digest
SHA256 5e5fa21ecc73f842d6881cef4f379bd97c3ab97da94cb3b5f2a10df8c6c80bbf
MD5 49b2adcc7e0db35e2f2389af58a64e4f
BLAKE2b-256 fd6b8ed1917e5110aa79b142196c65a08127158056fc9803024d3a12f07aa850

See more details on using hashes here.

File details

Details for the file cmdstanjupyter-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: cmdstanjupyter-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 19.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for cmdstanjupyter-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1564121ac0a5e97ee49b68e8cc991da08976287c9fa4a9a9cccbf170a49715d4
MD5 881f3e4666db1c716c14990b62079903
BLAKE2b-256 4dbdfb0bd8723e699193a19d6b5cd11ead27cae12f7126da86dad9e795dc32a1

See more details on using hashes here.

Supported by

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