https://github.com/williambdean/conjugate
Bayesian Conjugate Models in Python
https://github.com/williambdean/conjugate
bayesian-inference data-science probability-distribution python statistical-analysis statistics
Last synced: 9 months ago
JSON representation
Bayesian Conjugate Models in Python
- Host: GitHub
- URL: https://github.com/williambdean/conjugate
- Owner: williambdean
- License: mit
- Created: 2023-06-23T11:06:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-04-15T00:11:10.000Z (9 months ago)
- Last Synced: 2025-04-15T01:24:45.343Z (9 months ago)
- Topics: bayesian-inference, data-science, probability-distribution, python, statistical-analysis, statistics
- Language: Python
- Homepage: https://williambdean.github.io/conjugate/
- Size: 12 MB
- Stars: 25
- Watchers: 1
- Forks: 0
- Open Issues: 22
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- License: LICENSE
Awesome Lists containing this project
README
# Conjugate Models
[](https://github.com/astral-sh/ruff)
[](https://github.com/williambdean/conjugate/actions/workflows/tests.yml)
[](https://badge.fury.io/py/conjugate-models)
[](https://williambdean.github.io/conjugate/)
[](https://app.codecov.io/github/williambdean/conjugate)
Bayesian conjugate models in Python
## Installation
```bash
pip install conjugate-models
```
## Features
- [Connection to Scipy Distributions](https://williambdean.github.io/conjugate/examples/scipy-connection) with `dist` attribute
- [Built in Plotting](https://williambdean.github.io/conjugate/examples/plotting) with `plot_pdf`, `plot_pmf`, and `plot_cdf` methods
- [Vectorized Operations](https://williambdean.github.io/conjugate/examples/vectorized-inputs) for parameters and data
- [Indexing Parameters](https://williambdean.github.io/conjugate/examples/indexing) for subsetting and slicing
- [Generalized Numerical Inputs](https://williambdean.github.io/conjugate/examples/generalized-inputs) for any inputs that act like numbers
- Out of box compatibility with `polars`, `pandas`, `numpy`, and more.
- [Unsupported Distributions](https://williambdean.github.io/conjugate/examples/pymc-sampling) for sampling from unsupported distributions
## Supported Models
Many likelihoods are supported including
- `Bernoulli` / `Binomial`
- `Categorical` / `Multinomial`
- `Poisson`
- `Normal` (including linear regression)
- and [many more](https://williambdean.github.io/conjugate/models/)
## Basic Usage
1. Define prior distribution from `distributions` module
1. Pass data and prior into model from `models` modules
1. Analytics with posterior and posterior predictive distributions
```python
from conjugate.distributions import Beta, BetaBinomial
from conjugate.models import binomial_beta, binomial_beta_predictive
# Observed Data
x = 4
N = 10
# Analytics
prior = Beta(1, 1)
prior_predictive: BetaBinomial = binomial_beta_predictive(n=N, distribution=prior)
posterior: Beta = binomial_beta(n=N, x=x, prior=prior)
posterior_predictive: BetaBinomial = binomial_beta_predictive(n=N, distribution=posterior)
```
From here, do any analysis you'd like!
```python
# Figure
import matplotlib.pyplot as plt
fig, axes = plt.subplots(ncols=2)
ax = axes[0]
ax = posterior.plot_pdf(ax=ax, label="posterior")
prior.plot_pdf(ax=ax, label="prior")
ax.axvline(x=x/N, color="black", ymax=0.05, label="MLE")
ax.set_title("Success Rate")
ax.legend()
ax = axes[1]
posterior_predictive.plot_pmf(ax=ax, label="posterior predictive")
prior_predictive.plot_pmf(ax=ax, label="prior predictive")
ax.axvline(x=x, color="black", ymax=0.05, label="Sample")
ax.set_title("Number of Successes")
ax.legend()
plt.show()
```

More examples on in the [documentation](https://williambdean.github.io/conjugate/).
## Contributing
If you are interested in contributing, check out the [contributing guidelines](https://github.com/williambdean/conjugate/blob/main/CONTRIBUTING.md)