Skip to content

Commit

Permalink
Merge pull request #9 from wd60622/add-normal-known-mean
Browse files Browse the repository at this point in the history
Add normal known mean
  • Loading branch information
wd60622 authored Oct 19, 2023
2 parents 8031764 + 0fbf870 commit 0025d71
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 42 deletions.
18 changes: 18 additions & 0 deletions .github/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# This file has been mostly taken verbatim from https://github.com/pymc-devs/pymc/blob/main/.github/release.yml
changelog:
exclude:
labels:
- no releasenotes
categories:
- title: New Features 🎉
labels:
- enhancement
- title: Bugfixes 🐛
labels:
- bug
- title: Documentation 📖
labels:
- docs
- title: Maintenance 🔧
labels:
- "*"
14 changes: 14 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
name: Python package
on:
push:
tags:
- "v*.*.*"
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Build and publish to pypi
uses: JRubics/poetry-publish@v1.17
with:
pypi_token: ${{ secrets.PYPI_TOKEN }}
39 changes: 0 additions & 39 deletions .github/workflows/python-publish.yml

This file was deleted.

31 changes: 29 additions & 2 deletions conjugate/distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,6 @@ class BetaNegativeBinomial(SliceMixin):
n: number of successes
alpha: shape parameter
"""

n: NUMERIC
Expand Down Expand Up @@ -369,7 +368,15 @@ def dist(self):

@dataclass
class NormalInverseGamma:
"""Normal inverse gamma distribution."""
"""Normal inverse gamma distribution.
Args:
mu: mean
delta_inverse: covariance matrix
alpha: shape
beta: scale
"""

mu: NUMERIC
delta_inverse: NUMERIC
Expand Down Expand Up @@ -433,3 +440,23 @@ def sample_beta(
return beta, variance

return beta


@dataclass
class StudentT(ContinuousPlotDistMixin, SliceMixin):
"""StudentT distribution.
Args:
mu: mean
sigma: standard deviation
nu: degrees of freedom
"""

mu: NUMERIC
sigma: NUMERIC
nu: NUMERIC

@property
def dist(self):
return stats.t(self.nu, self.mu, self.sigma)
50 changes: 50 additions & 0 deletions conjugate/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
NegativeBinomial,
BetaNegativeBinomial,
BetaBinomial,
InverseGamma,
NormalInverseGamma,
StudentT,
)
from conjugate._typing import NUMERIC

Expand Down Expand Up @@ -193,6 +195,54 @@ def exponetial_gamma(x_total: NUMERIC, n: NUMERIC, gamma_prior: Gamma) -> Gamma:
return Gamma(alpha=alpha_post, beta=beta_post)


def normal_known_mean(
x_total: NUMERIC,
x2_total: NUMERIC,
n: NUMERIC,
mu: NUMERIC,
inverse_gamma_prior: InverseGamma,
) -> InverseGamma:
"""Posterior distribution for a normal likelihood with a known mean and a variance prior.
Args:
x_total: sum of all outcomes
x2_total: sum of all outcomes squared
n: total number of samples in x_total
mu: known mean
inverse_gamma_prior: InverseGamma prior for variance
Returns:
InverseGamma posterior distribution for the variance
"""
alpha_post = inverse_gamma_prior.alpha + (n / 2)
beta_post = inverse_gamma_prior.beta + (
0.5 * (x2_total - (2 * mu * x_total) + (n * (mu**2)))
)

return InverseGamma(alpha=alpha_post, beta=beta_post)


def normal_known_mean_posterior_predictive(
mu: NUMERIC, inverse_gamma: InverseGamma
) -> StudentT:
"""Posterior predictive distribution for a normal likelihood with a known mean and a variance prior.
Args:
mu: known mean
inverse_gamma: InverseGamma prior
Returns:
StudentT posterior predictive distribution
"""
return StudentT(
n=2 * inverse_gamma.alpha,
mu=mu,
sigma=(inverse_gamma.beta / inverse_gamma.alpha) ** 0.5,
)


def linear_regression(
X: NUMERIC,
y: NUMERIC,
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "conjugate-models"
version = "0.1.5"
version = "0.1.6"
description = "Bayesian Conjugate Models in Python"
authors = ["Will Dean <wd60622@gmail.com>"]
license = "MIT"
Expand Down

0 comments on commit 0025d71

Please sign in to comment.