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

Uploaded Source

Built Distribution

slimp-0.5.0-py3-none-any.whl (1.2 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for slimp-0.5.0.tar.gz
Algorithm Hash digest
SHA256 bcaef21ea9efbc69f88ca63ca34652db33a4eb21c1914377aed66583718a18dc
MD5 5f998b469858386ee839db3cf92f6066
BLAKE2b-256 bf7ae3311086768b88bf846adddc37051b9c2f76b532bebb52594396d2d2fdc5

See more details on using hashes here.

File details

Details for the file slimp-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: slimp-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 1.2 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.2

File hashes

Hashes for slimp-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a64c7e80bb1826989a0d939f7f2397a2ecefb30291ce4baa86cf19dc24dee45
MD5 eebd6d7e8f3a8c60094355d79cfc19e5
BLAKE2b-256 ed7c6adf88bfd2da1bc20cfb7d909f06c8817fe56092fcc8a19bda8d455a9f72

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