Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Oct 27, 2024
1 parent 297b2d3 commit e1e0f8a
Show file tree
Hide file tree
Showing 29 changed files with 172 additions and 178 deletions.
2 changes: 1 addition & 1 deletion .github/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@ changelog:
- docs
- title: Maintenance 🔧
labels:
- "*"
- "*"
6 changes: 3 additions & 3 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: Workflow for Codecov conjugate

on:
push:
paths:
paths:
- "conjugate/**"
- "pyproject.toml"
- "tests/**"

pull_request:
paths:
paths:
- "conjugate/**"
- "pyproject.toml"
- "tests/**"
Expand All @@ -24,7 +24,7 @@ jobs:
with:
python-version: '3.10'
- name: Install dependencies
run: pip install . pytest pytest-cov pytest-mpl pypika
run: pip install . pytest pytest-cov pytest-mpl pypika
- name: Run tests and collect coverage
run: pytest
- name: Upload coverage to Codecov
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: Docs
on:
push:
branches:
- master
- master
- main
permissions:
contents: write
Expand All @@ -11,7 +11,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/sync-closing-labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
uses: actions/checkout@v2
- name: Sync labels with closing issues
uses: wd60622/closing-labels@v0.0.3
with:
with:
exclude: "duplicate,help wanted,question"
env:
GH_TOKEN: ${{ github.token }}
4 changes: 2 additions & 2 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ name: Tests

on:
push:
paths:
paths:
- "conjugate/**"
- "pyproject.toml"
- "tests/**"
pull_request:
paths:
paths:
- "conjugate/**"
- "pyproject.toml"
- "tests/**"
Expand Down
19 changes: 9 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
# Guidelines for Contributing

Contributions are welcomed in all forms. These may be bugs, feature requests, documentation, or examples. Please feel free to:
Contributions are welcomed in all forms. These may be bugs, feature requests, documentation, or examples. Please feel free to:

- Submit an issue
- Open a pull request
- Help with outstanding issues and pull requests

## Open an Issue

If you find a bug or have a feature request, please [open an issue](https://github.com/wd60622/conjugate/issues/new) on GitHub.
If you find a bug or have a feature request, please [open an issue](https://github.com/wd60622/conjugate/issues/new) on GitHub.
Please check that it is not one of the [open issues](https://github.com/wd60622/conjugate/issues).

## Local Development Steps

### Create a forked branch of the repo

Do this once but keep it up to date
Do this once but keep it up to date

1. [Fork wd60622/conjugate GitHub repo](https://github.com/wd60622/conjugate/fork)
1. Clone forked repo and set upstream

```bash
```bash
git clone git@github.com:<your-username>/conjugate.git
cd conjugate
git remote add upstream git@github.com:wd60622/conjugate.git
Expand All @@ -36,15 +36,15 @@ In the root of the repo, run:
poetry install
```

And also install the [pre-commit](https://pre-commit.com/) hooks with:
And also install the [pre-commit](https://pre-commit.com/) hooks with:

```bash
pre-commit install
```bash
pre-commit install
```

## Pull Request Checklist

Please check that your pull request meets the following criteria:
Please check that your pull request meets the following criteria:

- Unit tests pass
- pre-commit hooks pass
Expand All @@ -64,5 +64,4 @@ Tests will run on each pull request.

Documentation will be updated with each merge to `main` branch.

Package release to PyPI on every GitHub Release.

Package release to PyPI on every GitHub Release.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

.DEFAULT_GOAL := help

help:
help:
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z0-9_-]+:.*?## / {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}' $(MAKEFILE_LIST) | sort

test-generate-baseline: ## Generate baseline images for tests
Expand All @@ -12,7 +12,7 @@ test: ## Run tests
poetry run pytest tests

cov: ## Run tests and generate coverage report
poetry run pytest tests
poetry run pytest tests
coverage html
open htmlcov/index.html

Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Conjugate Models

[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
[![Tests](https://github.com/wd60622/conjugate/actions/workflows/tests.yml/badge.svg)](https://github.com/wd60622/conjugate/actions/workflows/tests.yml)
[![PyPI version](https://badge.fury.io/py/conjugate-models.svg)](https://badge.fury.io/py/conjugate-models)
[![Tests](https://github.com/wd60622/conjugate/actions/workflows/tests.yml/badge.svg)](https://github.com/wd60622/conjugate/actions/workflows/tests.yml)
[![PyPI version](https://badge.fury.io/py/conjugate-models.svg)](https://badge.fury.io/py/conjugate-models)
[![docs](https://github.com/wd60622/conjugate/actions/workflows/docs.yml/badge.svg)](https://wd60622.github.io/conjugate/)
[![codecov](https://codecov.io/github/wd60622/conjugate/branch/main/graph/badge.svg)](https://app.codecov.io/github/wd60622/conjugate)

Expand All @@ -11,11 +11,11 @@ Bayesian conjugate models in Python

## Installation

```bash
```bash
pip install conjugate-models
```

## Features
## Features

- [Connection to Scipy Distributions](https://wd60622.github.io/conjugate/examples/scipy-connection) with `dist` attribute
- [Built in Plotting](https://wd60622.github.io/conjugate/examples/plotting) with `plot_pdf`, `plot_pmf`, and `plot_cdf` methods
Expand All @@ -25,7 +25,7 @@ pip install conjugate-models
- Out of box compatibility with `polars`, `pandas`, `numpy`, and more.
- [Unsupported Distributions](https://wd60622.github.io/conjugate/examples/pymc-sampling) for sampling from unsupported distributions

## Supported Models
## Supported Models

Many likelihoods are supported including

Expand All @@ -41,7 +41,7 @@ Many likelihoods are supported including
1. Pass data and prior into model from `models` modules
1. Analytics with posterior and posterior predictive distributions

```python
```python
from conjugate.distributions import Beta, BetaBinomial
from conjugate.models import binomial_beta, binomial_beta_predictive

Expand All @@ -54,7 +54,7 @@ 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)
posterior_predictive: BetaBinomial = binomial_beta_predictive(n=N, distribution=posterior)
```

From here, do any analysis you'd like!
Expand Down
4 changes: 2 additions & 2 deletions docs/distributions.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
comments: true
comments: true
---
# Distributions
# Distributions

::: conjugate.distributions
55 changes: 26 additions & 29 deletions docs/examples/bayesian-update.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
---
comments: true
comments: true
---

Easy to use Bayesian inference incrementally by setting the posterior to the prior for the next set of data points.
Easy to use Bayesian inference incrementally by setting the posterior to the prior for the next set of data points.

Abstractly, something like this:
Abstractly, something like this:

```python
prior = ...
Expand All @@ -30,11 +30,11 @@ import matplotlib.pyplot as plt
from conjugate.distributions import NormalInverseGamma
from conjugate.models import normal

def create_sampler(mu, sigma, rng):
def create_sampler(mu, sigma, rng):
"""Generate a sampler from a normal distribution with mean `mu` and standard deviation `sigma`."""
def sample(n: int):
def sample(n: int):
return rng.normal(loc=mu, scale=sigma, size=n)

return sample


Expand All @@ -45,9 +45,9 @@ sample = create_sampler(mu=mu, sigma=sigma, rng=rng)


prior = NormalInverseGamma(
mu=0,
alpha=1, beta=1,
nu=1,
mu=0,
alpha=1, beta=1,
nu=1,
)

cumsum = 0
Expand All @@ -57,9 +57,9 @@ for batch_size in batch_sizes:
data = sample(n=batch_size)

posterior = normal(
x_total=data.sum(),
x2_total=(data ** 2).sum(),
n=batch_size,
x_total=data.sum(),
x2_total=(data ** 2).sum(),
n=batch_size,
prior=prior
)

Expand All @@ -69,14 +69,14 @@ for batch_size in batch_sizes:
label = f"n={cumsum}"
ax.scatter(variance_samples ** 0.5, beta_samples, alpha=0.25, label=label)

prior = posterior
prior = posterior

ax.scatter(sigma, mu, color="black", label="true")
ax.set(
xlabel="$\sigma$",
ylabel="$\mu$",
xlim=(0, None),
ylim=(0, None),
xlabel="$\sigma$",
ylabel="$\mu$",
xlim=(0, None),
ylim=(0, None),
title="Updated posterior samples of $\mu$ and $\sigma$"
)
ax.legend()
Expand All @@ -91,16 +91,16 @@ plt.show()
With a Binomial model, we can assume as Beta prior


```python
```python
import numpy as np

import matplotlib.pyplot as plt

from conjugate.distributions import Beta
from conjugate.models import binomial_beta

def create_sampler(p, rng):
def sampler(n: int):
def create_sampler(p, rng):
def sampler(n: int):
return rng.binomial(n=n, p=p)

return sampler
Expand All @@ -114,13 +114,13 @@ sample = create_sampler(p=p, rng=rng)
ax = plt.gca()
cumsum = 0
batch_sizes = [5, 25, 50]
for batch_size in batch_sizes:
for batch_size in batch_sizes:
x = sample(n=batch_size)

posterior = binomial_beta(
x=x,
n=batch_size,
prior=prior,
x=x,
n=batch_size,
prior=prior,
)

cumsum += batch_size
Expand All @@ -132,14 +132,11 @@ for batch_size in batch_sizes:

ax.axvline(p, label="true p", color="black", ymax=0.05)
ax.set(
xlabel="p",
ylim=(None, 10),
xlabel="p",
ylim=(None, 10),
)
ax.legend()
plt.show()
```

![Binomial Model](../images/bayesian-update-binomial.png)


Loading

0 comments on commit e1e0f8a

Please sign in to comment.