Skip to content

Commit

Permalink
Fix model docs (#34)
Browse files Browse the repository at this point in the history
* add to docstrings of existing

* remove black since not used

* helper for running ruff

* fix typo

* up the version
  • Loading branch information
wd60622 authored Jan 9, 2024
1 parent 885beb4 commit 9da0132
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 131 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ cov:
poetry run pytest --cov-report html --cov=conjugate tests && open htmlcov/index.html

format:
poetry run black conjugate tests
poetry run pre-commit run --all-files

html:
open http://localhost:8000/
Expand Down
83 changes: 78 additions & 5 deletions conjugate/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,44 @@ def binomial_beta_posterior_predictive(n: NUMERIC, beta: Beta) -> BetaBinomial:
return BetaBinomial(n=n, alpha=beta.alpha, beta=beta.beta)


def bernoulli_beta(x: NUMERIC, beta_prior: Beta) -> Beta:
"""Posterior distribution for a bernoulli likelihood with a beta prior.
Args:
x: sucesses from that trials
beta_prior: Beta distribution prior
Returns:
Beta distribution posterior
"""
return binomial_beta(n=1, x=x, beta_prior=beta_prior)


def bernoulli_beta_posterior_predictive(beta: Beta) -> BetaBinomial:
"""Posterior predictive distribution for a bernoulli likelihood with a beta prior.
Args:
beta: Beta distribution
Returns:
BetaBinomial posterior predictive distribution
"""
return binomial_beta_posterior_predictive(n=1, beta=beta)


def negative_binomial_beta(r, n, x, beta_prior: Beta) -> Beta:
"""Posterior distribution for a negative binomial likelihood with a beta prior.
Args:
r: number of failures
n: number of trials
x: number of successes
beta_prior: Beta distribution prior
Returns:
Beta distribution posterior
"""
alpha_post = beta_prior.alpha + (r * n)
Expand Down Expand Up @@ -117,7 +151,16 @@ def get_categorical_dirichlet_posterior_params(


def categorical_dirichlet(x: NUMERIC, dirichlet_prior: Dirichlet) -> Dirichlet:
"""Posterior distribution of Categorical model with Dirichlet prior."""
"""Posterior distribution of Categorical model with Dirichlet prior.
Args:
x: counts
dirichlet_prior: Dirichlet prior on the counts
Returns:
Dirichlet posterior distribution
"""
alpha_post = get_dirichlet_posterior_params(dirichlet_prior.alpha, x)

return Dirichlet(alpha=alpha_post)
Expand Down Expand Up @@ -155,7 +198,17 @@ def get_poisson_gamma_posterior_params(


def poisson_gamma(x_total: NUMERIC, n: NUMERIC, gamma_prior: Gamma) -> Gamma:
"""Posterior distribution for a poisson likelihood with a gamma prior"""
"""Posterior distribution for a poisson likelihood with a gamma prior.
Args:
x_total: sum of all outcomes
n: total number of samples in x_total
gamma_prior: Gamma prior
Returns:
Gamma posterior distribution
"""
alpha_post, beta_post = get_poisson_gamma_posterior_params(
alpha=gamma_prior.alpha, beta=gamma_prior.beta, x_total=x_total, n=n
)
Expand Down Expand Up @@ -187,8 +240,18 @@ def poisson_gamma_posterior_predictive(
get_exponential_gamma_posterior_params = get_poisson_gamma_posterior_params


def exponetial_gamma(x_total: NUMERIC, n: NUMERIC, gamma_prior: Gamma) -> Gamma:
"""Posterior distribution for an exponential likelihood with a gamma prior"""
def exponential_gamma(x_total: NUMERIC, n: NUMERIC, gamma_prior: Gamma) -> Gamma:
"""Posterior distribution for an exponential likelihood with a gamma prior.
Args:
x_total: sum of all outcomes
n: total number of samples in x_total
gamma_prior: Gamma prior
Returns:
Gamma posterior distribution
"""
alpha_post, beta_post = get_exponential_gamma_posterior_params(
alpha=gamma_prior.alpha, beta=gamma_prior.beta, x_total=x_total, n=n
)
Expand Down Expand Up @@ -299,7 +362,17 @@ def linear_regression(
def linear_regression_posterior_predictive(
normal_inverse_gamma: NormalInverseGamma, X: NUMERIC, eye=np.eye
) -> MultivariateStudentT:
"""Posterior predictive distribution for a linear regression model with a normal inverse gamma prior."""
"""Posterior predictive distribution for a linear regression model with a normal inverse gamma prior.
Args:
normal_inverse_gamma: NormalInverseGamma posterior
X: design matrix
eye: function to get identity matrix, defaults to np.eye
Returns:
MultivariateStudentT posterior predictive distribution
"""
mu = X @ normal_inverse_gamma.mu
sigma = (normal_inverse_gamma.beta / normal_inverse_gamma.alpha) * (
eye(X.shape[0]) + (X @ normal_inverse_gamma.delta_inverse @ X.T)
Expand Down
Loading

0 comments on commit 9da0132

Please sign in to comment.