Skip to main content

Linear models with Stan and Pandas

Project description

slimp: linear models with Stan and Pandas

slimp estimates linear models using Stan and Pandas. Think rstanarm or brms, but in Python and faster.

Create the model:

import matplotlib.pyplot
import numpy
import pandas
import slimp

y, x = numpy.mgrid[0:10, 0:10]
z = 10 + x + 2*y + numpy.random.normal(0, 2, (10, 10))
data = pandas.DataFrame({"x": x.ravel(), "y": y.ravel(), "z": z.ravel()})

model = slimp.Model("z ~ 1 + x + y", data, num_chains=4)
# Also possible to specify random seed
# model = slimp.Model("z ~ 1 + x + y", data, seed=42)

Sample the parameters, check the results:

model.sample()
print(model.hmc_diagnostics)
print(model.summary()[["N_Eff", "R_hat"]].describe().loc[["min", "max"], :])
r_squared = slimp.r_squared(model)
print(r_squared.quantile([0.05, 0.95]))

Plot prior and posterior predictive checks:

figure, plots = matplotlib.pyplot.subplots(1, 2, layout="tight", figsize=(8, 4))
slimp.predictive_plot(model, use_prior=True, plot_kwargs={"ax":plots[0]})
slimp.predictive_plot(model, use_prior=False, plot_kwargs={"ax":plots[1]})

Plot the credible intervals of the parameters and their distributions:

slimp.parameters_plot(model, include=["x", "y"])
slimp.KDEPlot(model.draws["sigma"], prob=0.90)

Use a custom Stan model: have a look here

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

slimp-1.0.0.tar.gz (25.8 kB view details)

Uploaded Source

Built Distribution

slimp-1.0.0-py3-none-manylinux_2_34_x86_64.whl (1.8 MB view details)

Uploaded Python 3manylinux: glibc 2.34+ x86-64

File details

Details for the file slimp-1.0.0.tar.gz.

File metadata

  • Download URL: slimp-1.0.0.tar.gz
  • Upload date:
  • Size: 25.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for slimp-1.0.0.tar.gz
Algorithm Hash digest
SHA256 697f619e128dd9a25dd85e156ea361160a8880452094a3ce8e1eac039d8725f9
MD5 4761b7fb8ce4acd918e6c5c4d5b12988
BLAKE2b-256 7d6db21d2266604a84f3dcbac488829c7e3b6c419dedc661e3470f0cecae3375

See more details on using hashes here.

File details

Details for the file slimp-1.0.0-py3-none-manylinux_2_34_x86_64.whl.

File metadata

File hashes

Hashes for slimp-1.0.0-py3-none-manylinux_2_34_x86_64.whl
Algorithm Hash digest
SHA256 625c3f718a7f854536cb6c99b242277bc8b3f793a831297742f91a23aa3054c1
MD5 a975fa9bc450ff17b56b697f607fe1f6
BLAKE2b-256 b2d059938ffe84593d5650e2292ed9e2489cdfe26d3cdd0ac687a6f663e68d99

See more details on using hashes here.

Supported by

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