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

Uploaded Source

Built Distribution

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

slimp-1.1.1-py3-none-any.whl (1.3 MB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for slimp-1.1.1.tar.gz
Algorithm Hash digest
SHA256 a964c0d3c4007c382a7f4432ac718625e79aa6a62409f762e484bae45e0e4e9a
MD5 e3f8fe364a7adc74dc5c67960ca17aeb
BLAKE2b-256 17141c0f32ebd14975af2c21eef44a44f2ff4ece09265fd30e5ea61de3d9cdbf

See more details on using hashes here.

File details

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

File metadata

  • Download URL: slimp-1.1.1-py3-none-any.whl
  • Upload date:
  • Size: 1.3 MB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.5

File hashes

Hashes for slimp-1.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 48722463d6440a3a09f8274719cbe3b8c1cd3331cdc77808646bbe9f510b9160
MD5 31610ea03bc7468b3d631be36731de23
BLAKE2b-256 7883b1c5166340043ca35d4e30dbbb7f093ef91df6f374372fb53a74603b2939

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