@@ -8,23 +8,36 @@ nutpie can be installed using conda or mamba from conda-forge with
88mamba install -c conda-forge nutpie pymc
99```
1010
11- To install it from source, install a rust compiler (eg using rustup) and run
11+ Or using pip:
12+
13+ ```
14+ pip install nutpie
15+ ```
16+
17+ To install it from source, install a rust compiler and maturin and then
1218
1319```
1420maturin develop --release
1521```
1622
1723If you want to use the nightly simd implementation for some of the math functions,
18- switch to rust nightly and then install with the ` simd_support ` feature in the nutpie dir:
24+ switch to rust nightly and then install with the ` simd_support ` feature in then
25+ nutpie directory:
1926
2027```
2128rustup override set nightly
2229maturin develop --release --features=simd_support
2330```
2431
25- ## Usage
32+ ## Usage with PyMC
33+
34+ First, PyMC and numba need to be installed, for example using
35+
36+ ```
37+ mamba install pymc numba
38+ ```
2639
27- First, we need to create a model, for example using pymc :
40+ We need to create a model:
2841
2942``` python
3043import pymc as pm
@@ -75,30 +88,57 @@ We then compile this model and sample form the posterior:
7588
7689``` python
7790compiled_model = nutpie.compile_pymc_model(pymc_model)
78- trace_pymc = nutpie.sample(compiled_model, chains = 10 )
91+ trace_pymc = nutpie.sample(compiled_model)
7992```
8093
8194` trace_pymc ` now contains an arviz ` InferenceData ` object, including sampling
8295statistics and the posterior of the variables defined above.
8396
84- For more details, see the example notebook ` pytensor_logp `
97+ ## Usage with Stan
98+
99+ In order to sample from stan model, ` bridgestan ` needs to be installed.
100+ A pip package is available, but right now this can not be installed using conda.
101+
102+ ```
103+ pip install bridgestan
104+ ```
105+
106+ When we install nutpie with pip, we can also specify that we want optional
107+ dependencies for Stan models using
108+
109+ ```
110+ pip install 'bridgestan[stan]'
111+ ```
112+
113+ In addition, a C++ compiler needs to be available. For details see
114+ [ the stan docs] ( https://mc-stan.org/docs/cmdstan-guide/cmdstan-installation.html#cpp-toolchain ) .
115+
116+ We can then compile a Stan model, and sample using nutpie:
85117
86- nutpie can also sample from stan models, it currently needs a patched version of httpstan do so so however.
87- The required version can be found [ here] ( https://github.com/stan-dev/httpstan/pull/600 ) .
88- Make sure to follow the development
89- [ installation instructions for httpstan] ( https://httpstan.readthedocs.io/en/latest/installation.html#installation-from-source ) .
118+ ```
119+ import nutpie
120+
121+ code = """
122+ data {
123+ real mu;
124+ }
125+ parameters {
126+ real x;
127+ }
128+ model {
129+ x ~ normal(mu, 1);
130+ }
131+ """
132+
133+ compiled = nutpie.compile_stan_model(code=code)
134+ # Provide data
135+ compiled = compiled.with_data(mu=3.)
136+ trace = nutpie.sample(compiled)
137+ ```
90138
91139## Advantages
92140
93141nutpie uses ` nuts-rs ` , a library written in rust, that implements NUTS as in
94142pymc and stan, but with a slightly different mass matrix tuning method as
95143those. It often produces a higher effective sample size per gradient
96144evaluation, and tends to converge faster and with fewer gradient evaluation.
97-
98- From the benchmarks I did, it seems to be the fastest CPU based sampler I could
99- find, outperforming cmdstan and numpyro.
100-
101- Unfortunately performance on pymc models is currently somewhat limited by an
102- [ issue in numba] ( https://github.com/numba/numba/issues/8156 ) , which hopefully
103- will be fixed soon. Without the patch mentioned in the issue the model above
104- samples in about 2s on my machine, with the patch it finished is about 700ms.
0 commit comments