Models
For more on these models, check out the Conjugate Prior Wikipedia Table
Supported Likelihoods
Discrete
- Bernoulli / Binomial
- Categorical / Multinomial
- Geometric
- Hypergeometric
- Negative Binomial
- Poisson
Continuous
- Beta
- Exponential
- Gamma
- Inverse Gamma
- Linear Regression (Normal)
- Log Normal
- Multivariate Normal
- Normal
- Pareto
- Uniform
- Von Mises
- Weibull
Model Functions
Below are the supported models:
bernoulli_beta(*, x, prior)
Posterior distribution for a bernoulli likelihood with a beta prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
NUMERIC
|
successes from a single trial |
required |
prior
|
Beta
|
Beta distribution prior |
required |
Returns:
Type | Description |
---|---|
Beta
|
Beta distribution posterior |
Examples:
Information gain from a single coin flip
from conjugate.distributions import Beta
from conjugate.models import bernoulli_beta
prior = Beta(1, 1)
# Positive outcome
x = 1
posterior = bernoulli_beta(x=x, prior=prior)
posterior.dist.ppf([0.025, 0.975])
# array([0.15811388, 0.98742088])
Source code in conjugate/models.py
bernoulli_beta_predictive(*, distribution)
Predictive distribution for a bernoulli likelihood with a beta prior.
Use for either prior or posterior predictive distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Beta
|
Beta distribution |
required |
Returns:
Type | Description |
---|---|
BetaBinomial
|
BetaBinomial predictive distribution |
Source code in conjugate/models.py
beta(*, x_prod, one_minus_x_prod, n, prior)
Posterior distribution for a Beta likelihood.
Inference on alpha and beta
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_prod
|
NUMERIC
|
product of all outcomes |
required |
one_minus_x_prod
|
NUMERIC
|
product of all (1 - outcomes) |
required |
n
|
NUMERIC
|
total number of samples in x_prod and one_minus_x_prod |
required |
prior
|
BetaProportional
|
BetaProportional prior |
required |
Returns:
Type | Description |
---|---|
BetaProportional
|
BetaProportional posterior distribution |
Source code in conjugate/models.py
binomial_beta(*, n, x, prior)
Posterior distribution for a binomial likelihood with a beta prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
total number of trials |
required |
x
|
NUMERIC
|
successes from that trials |
required |
prior
|
Beta
|
Beta distribution prior |
required |
Returns:
Type | Description |
---|---|
Beta
|
Beta distribution posterior |
Examples:
A / B test example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Beta
from conjugate.models import binomial_beta
impressions = np.array([100, 250])
clicks = np.array([10, 35])
prior = Beta(1, 1)
posterior = binomial_beta(n=impressions, x=clicks, prior=prior)
ax = plt.subplot(111)
posterior.set_bounds(0, 0.5).plot_pdf(ax=ax, label=["A", "B"])
prior.set_bounds(0, 0.5).plot_pdf(ax=ax, label="prior")
ax.legend()
Source code in conjugate/models.py
binomial_beta_predictive(*, n, distribution)
Posterior predictive distribution for a binomial likelihood with a beta prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of trials |
required |
distribution
|
Beta
|
Beta distribution |
required |
Returns:
Type | Description |
---|---|
BetaBinomial
|
BetaBinomial predictive distribution |
Examples:
A / B test example with 100 new impressions
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Beta
from conjugate.models import binomial_beta, binomial_beta_predictive
impressions = np.array([100, 250])
clicks = np.array([10, 35])
prior = Beta(1, 1)
posterior = binomial_beta(n=impressions, x=clicks, prior=prior)
posterior_predictive = binomial_beta_predictive(n=100, distribution=posterior)
ax = plt.subplot(111)
ax.set_title("Posterior Predictive Distribution with 100 new impressions")
posterior_predictive.set_bounds(0, 50).plot_pmf(
ax=ax,
label=["A", "B"],
)
Source code in conjugate/models.py
categorical_dirichlet(*, x, prior)
Posterior distribution of Categorical model with Dirichlet prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
NUMERIC
|
counts |
required |
prior
|
Dirichlet
|
Dirichlet prior on the counts |
required |
Returns:
Type | Description |
---|---|
Dirichlet
|
Dirichlet posterior distribution |
Source code in conjugate/models.py
categorical_dirichlet_predictive(*, distribution, n=1)
Predictive distribution of Categorical model with Dirichlet distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Dirichlet
|
Dirichlet distribution |
required |
n
|
NUMERIC
|
Number of trials for each sample, defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
DirichletMultinomial
|
DirichletMultinomial distribution related to predictive |
Source code in conjugate/models.py
exponential_gamma(*, x_total, n, prior)
Posterior distribution for an exponential likelihood with a gamma prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
prior
|
Gamma
|
Gamma prior |
required |
Returns:
Type | Description |
---|---|
Gamma
|
Gamma posterior distribution |
Source code in conjugate/models.py
exponential_gamma_predictive(*, distribution)
Predictive distribution for an exponential likelihood with a gamma distribution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Gamma
|
Gamma distribution |
required |
Returns:
Type | Description |
---|---|
Lomax
|
Lomax distribution related to predictive |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Exponential, Gamma
from conjugate.models import exponential_gamma, exponential_gamma_predictive
true = Exponential(1)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Gamma(1, 1)
posterior = exponential_gamma(n=n_samples, x_total=data.sum(), prior=prior)
prior_predictive = exponential_gamma_predictive(distribution=prior)
posterior_predictive = exponential_gamma_predictive(distribution=posterior)
ax = plt.subplot(111)
prior_predictive.set_bounds(0, 2.5).plot_pdf(ax=ax, label="prior predictive")
true.set_bounds(0, 2.5).plot_pdf(ax=ax, label="true distribution")
posterior_predictive.set_bounds(0, 2.5).plot_pdf(ax=ax, label="posterior predictive")
ax.legend()
Source code in conjugate/models.py
gamma(*, x_total, x_prod, n, prior)
Posterior distribution for a gamma likelihood.
Inference on alpha and beta
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
x_prod
|
NUMERIC
|
product of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total and x_prod |
required |
prior
|
GammaProportional
|
GammaProportional prior |
required |
Returns:
Type | Description |
---|---|
GammaProportional
|
GammaProportional posterior distribution |
Source code in conjugate/models.py
gamma_known_rate(*, x_prod, n, beta, prior)
Posterior distribution for a gamma likelihood.
The rate beta is assumed to be known.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_prod
|
NUMERIC
|
product of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_prod |
required |
beta
|
NUMERIC
|
known rate parameter |
required |
Returns:
Type | Description |
---|---|
GammaKnownRateProportional
|
GammaKnownRateProportional posterior distribution |
Source code in conjugate/models.py
gamma_known_shape(*, x_total, n, alpha, prior)
Gamma likelihood with a gamma prior.
The shape parameter of the likelihood is assumed to be known.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
alpha
|
NUMERIC
|
known shape parameter |
required |
prior
|
Gamma
|
Gamma prior |
required |
Returns:
Type | Description |
---|---|
Gamma
|
Gamma posterior distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Gamma
from conjugate.models import gamma_known_shape
known_shape = 2
unknown_rate = 5
true = Gamma(known_shape, unknown_rate)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Gamma(1, 1)
posterior = gamma_known_shape(
n=n_samples,
x_total=data.sum(),
alpha=known_shape,
prior=prior,
)
bound = 10
ax = plt.subplot(111)
posterior.set_bounds(0, bound).plot_pdf(ax=ax, label="posterior")
prior.set_bounds(0, bound).plot_pdf(ax=ax, label="prior")
ax.axvline(unknown_rate, color="black", linestyle="--", label="true rate")
ax.legend()
Source code in conjugate/models.py
gamma_known_shape_predictive(*, distribution, alpha)
Predictive distribution for a gamma likelihood with a gamma distribution
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Gamma
|
Gamma distribution |
required |
alpha
|
NUMERIC
|
known shape parameter |
required |
Returns:
Type | Description |
---|---|
CompoundGamma
|
CompoundGamma distribution related to predictive |
Source code in conjugate/models.py
geometric_beta(*, x_total, n, prior, one_start=True)
Posterior distribution for a geometric likelihood with a beta prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
sum of all trials outcomes |
required | |
n
|
total number of trials |
required | |
prior
|
Beta
|
Beta distribution prior |
required |
one_start
|
bool
|
whether to outcomes start at 1, defaults to True. False is 0 start. one_start is equivalent to number of Bernoulli trails before the first success. |
True
|
Returns:
Type | Description |
---|---|
Beta
|
Beta distribution posterior |
Examples:
Number of usages until user has good experience
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Beta
from conjugate.models import geometric_beta
data = np.array([3, 1, 1, 3, 2, 1])
prior = Beta(1, 1)
posterior = geometric_beta(x_total=data.sum(), n=data.size, prior=prior)
ax = plt.subplot(111)
posterior.set_bounds(0, 1).plot_pdf(ax=ax, label="posterior")
prior.set_bounds(0, 1).plot_pdf(ax=ax, label="prior")
ax.legend()
ax.set(xlabel="chance of good experience")
Source code in conjugate/models.py
geometric_beta_predictive(*, distribution, one_start=True)
Predictive distribution for a geometric likelihood with a beta prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Beta
|
Beta distribution |
required |
one_start
|
bool
|
whether to outcomes start at 1, defaults to True. False is 0 start. one_start is equivalent to number of Bernoulli trails before the first success. |
True
|
Returns:
Type | Description |
---|---|
BetaGeometric
|
BetaGeometric predictive distribution |
Source code in conjugate/models.py
hypergeometric_beta_binomial(*, x_total, n, prior)
Hypergeometric likelihood with a BetaBinomial prior.
The total population size is N and is known. Encode it in the BetaBinomial prior as n=N
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all trials outcomes |
required |
n
|
NUMERIC
|
total number of trials |
required |
prior
|
BetaBinomial
|
BetaBinomial prior n is the known N / total population size |
required |
Returns:
Type | Description |
---|---|
BetaBinomial
|
BetaBinomial posterior distribution |
Source code in conjugate/models.py
inverse_gamma_known_rate(*, reciprocal_x_total, n, alpha, prior)
Inverse Gamma likelihood with a known rate and unknown inverse scale.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reciprocal_x_total
|
NUMERIC
|
sum of all outcomes reciprocals |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
alpha
|
NUMERIC
|
known rate parameter |
required |
prior
|
Gamma
|
Gamma prior |
required |
Returns:
Type | Description |
---|---|
Gamma
|
Gamma posterior distribution |
Source code in conjugate/models.py
linear_regression(*, X, y, prior, inv=np.linalg.inv)
Posterior distribution for a linear regression model with a normal inverse gamma prior.
Derivation taken from this blog here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
NUMERIC
|
design matrix |
required |
y
|
NUMERIC
|
response vector |
required |
prior
|
NormalInverseGamma
|
NormalInverseGamma prior |
required |
inv
|
function to invert matrix, defaults to np.linalg.inv |
inv
|
Returns:
Type | Description |
---|---|
NormalInverseGamma
|
NormalInverseGamma posterior distribution |
Source code in conjugate/models.py
linear_regression_predictive(*, distribution, X, eye=np.eye)
Predictive distribution for a linear regression model with a normal inverse gamma prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
NormalInverseGamma
|
NormalInverseGamma posterior |
required |
X
|
NUMERIC
|
design matrix |
required |
eye
|
function to get identity matrix, defaults to np.eye |
eye
|
Returns:
Type | Description |
---|---|
MultivariateStudentT
|
MultivariateStudentT predictive distribution |
Source code in conjugate/models.py
log_normal(*, ln_x_total, ln_x2_total, n, prior)
Log normal likelihood.
By taking the log of the data, we can use the normal inverse gamma posterior.
Reference: Section 1.2.1
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ln_x_total
|
NUMERIC
|
sum of the log of all outcomes |
required |
ln_x2_total
|
NUMERIC
|
sum of the log of all outcomes squared |
required |
n
|
NUMERIC
|
total number of samples in ln_x_total and ln_x2_total |
required |
prior
|
NormalInverseGamma | NormalGamma
|
NormalInverseGamma or NormalGamma prior |
required |
Returns:
Type | Description |
---|---|
NormalInverseGamma | NormalGamma
|
NormalInverseGamma or NormalGamma posterior distribution |
Example
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import NormalInverseGamma, LogNormal
from conjugate.models import log_normal_normal_inverse_gamma
true_mu = 0
true_sigma = 2.5
true = LogNormal(true_mu, true_sigma)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
ln_data = np.log(data)
prior = NormalInverseGamma(mu=1, nu=1, alpha=1, beta=1)
posterior = log_normal_normal_inverse_gamma(
ln_x_total=ln_data.sum(), ln_x2_total=(ln_data**2).sum(), n=n_samples, prior=prior
)
fig, axes = plt.subplots(ncols=2)
mean, variance = posterior.sample_mean(4000, return_variance=True, random_state=42)
ax = axes[0]
ax.hist(mean, bins=20)
ax.axvline(true_mu, color="black", linestyle="--", label="true mu")
ax = axes[1]
ax.hist(variance, bins=20)
ax.axvline(true_sigma**2, color="black", linestyle="--", label="true sigma^2")
Source code in conjugate/models.py
2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 |
|
multinomial_dirichlet(*, x, prior)
Posterior distribution of Multinomial model with Dirichlet prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x
|
NUMERIC
|
counts |
required |
prior
|
Dirichlet
|
Dirichlet prior on the counts |
required |
Returns:
Type | Description |
---|---|
Dirichlet
|
Dirichlet posterior distribution |
Examples:
Personal preference for ice cream flavors
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Dirichlet
from conjugate.models import multinomial_dirichlet
kinds = ["chocolate", "vanilla", "strawberry"]
data = np.array(
[
[5, 2, 1],
[3, 1, 0],
[3, 2, 0],
]
)
prior = Dirichlet([1, 1, 1])
posterior = multinomial_dirichlet(x=data.sum(axis=0), prior=prior)
ax = plt.subplot(111)
posterior.plot_pdf(ax=ax, label=kinds)
ax.legend()
ax.set(xlabel="Flavor Preference")
Source code in conjugate/models.py
multinomial_dirichlet_predictive(*, distribution, n=1)
Predictive distribution of Multinomial model with Dirichlet distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Dirichlet
|
Dirichlet distribution |
required |
n
|
NUMERIC
|
Number of trials for each sample, defaults to 1. |
1
|
Returns:
Type | Description |
---|---|
DirichletMultinomial
|
DirichletMultinomial distribution related to predictive |
Source code in conjugate/models.py
multivariate_normal(*, X, prior, outer=np.outer)
Multivariate normal likelihood with normal inverse wishart prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
NUMERIC
|
design matrix |
required |
mu
|
known mean |
required | |
prior
|
NormalInverseWishart
|
NormalInverseWishart prior |
required |
outer
|
function to take outer product, defaults to np.outer |
outer
|
Returns:
Type | Description |
---|---|
NormalInverseWishart
|
NormalInverseWishart posterior distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import NormalInverseWishart
from conjugate.models import multivariate_normal
true_mean = np.array([1, 5])
true_cov = np.array(
[
[1, 0.5],
[0.5, 1],
]
)
n_samples = 100
rng = np.random.default_rng(42)
data = rng.multivariate_normal(
mean=true_mean,
cov=true_cov,
size=n_samples,
)
prior = NormalInverseWishart(
mu=np.array([0, 0]),
kappa=1,
nu=3,
psi=np.array(
[
[1, 0],
[0, 1],
]
),
)
posterior = multivariate_normal(
X=data,
prior=prior,
)
Source code in conjugate/models.py
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 |
|
multivariate_normal_known_covariance(*, n, x_bar, cov, prior, inv=np.linalg.inv)
Multivariate normal likelihood with known covariance and multivariate normal prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of samples |
required |
x_bar
|
NUMERIC
|
mean of samples |
required |
cov
|
NUMERIC
|
known covariance |
required |
prior
|
MultivariateNormal
|
MultivariateNormal prior for the mean |
required |
inv
|
function to invert matrix, defaults to np.linalg.inv |
inv
|
Returns:
Type | Description |
---|---|
MultivariateNormal
|
MultivariateNormal posterior distribution |
Source code in conjugate/models.py
multivariate_normal_known_covariance_predictive(*, distribution, cov)
Predictive distribution for a multivariate normal likelihood with known covariance and a multivariate normal prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
MultivariateNormal
|
MultivariateNormal distribution |
required |
cov
|
NUMERIC
|
known covariance |
required |
Returns:
Type | Description |
---|---|
MultivariateNormal
|
MultivariateNormal predictive distribution |
Source code in conjugate/models.py
multivariate_normal_known_mean(*, X, mu, prior)
Multivariate normal likelihood with known mean and inverse wishart prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
NUMERIC
|
design matrix |
required |
mu
|
NUMERIC
|
known mean |
required |
prior
|
InverseWishart
|
InverseWishart prior |
required |
Returns:
Type | Description |
---|---|
InverseWishart
|
InverseWishart posterior distribution |
Source code in conjugate/models.py
multivariate_normal_known_precision(*, n, x_bar, precision, prior, inv=np.linalg.inv)
Multivariate normal likelihood with known precision and multivariate normal prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of samples |
required |
x_bar
|
NUMERIC
|
mean of samples |
required |
precision
|
NUMERIC
|
known precision |
required |
prior
|
MultivariateNormal
|
MultivariateNormal prior for the mean |
required |
inv
|
function to invert matrix, defaults to np.linalg.inv |
inv
|
Returns:
Type | Description |
---|---|
MultivariateNormal
|
MultivariateNormal posterior distribution |
Source code in conjugate/models.py
multivariate_normal_known_precision_predictive(*, distribution, precision, inv=np.linalg.inv)
Predictive distribution for a multivariate normal likelihood with known precision and a multivariate normal prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
MultivariateNormal
|
MultivariateNormal distribution |
required |
precision
|
NUMERIC
|
known precision |
required |
inv
|
Callable
|
function to invert matrix, defaults to np.linalg.inv |
inv
|
Returns:
Type | Description |
---|---|
MultivariateNormal
|
MultivariateNormal predictive distribution |
Source code in conjugate/models.py
multivariate_normal_predictive(*, distribution)
Multivariate normal likelihood with normal inverse wishart distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
NormalInverseWishart
|
NormalInverseWishart distribution |
required |
Returns:
Type | Description |
---|---|
MultivariateStudentT
|
MultivariateStudentT predictive distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import NormalInverseWishart, MultivariateNormal
from conjugate.models import multivariate_normal, multivariate_normal_predictive
mu_1 = 10
mu_2 = 5
sigma_1 = 2.5
sigma_2 = 1.5
rho = -0.65
true_mean = np.array([mu_1, mu_2])
true_cov = np.array(
[
[sigma_1**2, rho * sigma_1 * sigma_2],
[rho * sigma_1 * sigma_2, sigma_2**2],
]
)
true = MultivariateNormal(true_mean, true_cov)
n_samples = 100
rng = np.random.default_rng(42)
data = true.dist.rvs(size=n_samples, random_state=rng)
prior = NormalInverseWishart(
mu=np.array([0, 0]),
kappa=1,
nu=2,
psi=np.array(
[
[5**2, 0],
[0, 5**2],
]
),
)
posterior = multivariate_normal(
X=data,
prior=prior,
)
prior_predictive = multivariate_normal_predictive(distribution=prior)
posterior_predictive = multivariate_normal_predictive(distribution=posterior)
ax = plt.subplot(111)
xmax = mu_1 + 3 * sigma_1
ymax = mu_2 + 3 * sigma_2
x, y = np.mgrid[-xmax:xmax:0.1, -ymax:ymax:0.1]
pos = np.dstack((x, y))
z = true.dist.pdf(pos)
# z = np.where(z < 0.005, np.nan, z)
contours = ax.contour(x, y, z, alpha=0.55, color="black")
for label, dist in zip(
["prior", "posterior"], [prior_predictive, posterior_predictive]
):
X = dist.dist.rvs(size=1000)
ax.scatter(X[:, 0], X[:, 1], alpha=0.15, label=f"{label} predictive")
ax.axvline(0, color="black", linestyle="--")
ax.axhline(0, color="black", linestyle="--")
ax.scatter(data[:, 0], data[:, 1], label="data", alpha=0.5)
ax.scatter(mu_1, mu_2, color="black", marker="x", label="true mean")
ax.set(
xlabel="x1",
ylabel="x2",
title=f"Posterior predictive after {n_samples} samples",
xlim=(-xmax, xmax),
ylim=(-ymax, ymax),
)
ax.legend()
Source code in conjugate/models.py
1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 |
|
negative_binomial_beta(*, r, n, x, prior)
Posterior distribution for a negative binomial likelihood with a beta prior.
Assumed known number of failures r
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r
|
NUMERIC
|
number of failures |
required |
n
|
NUMERIC
|
number of trials |
required |
x
|
NUMERIC
|
number of successes |
required |
prior
|
Beta
|
Beta distribution prior |
required |
Returns:
Type | Description |
---|---|
Beta
|
Beta distribution posterior |
Source code in conjugate/models.py
negative_binomial_beta_predictive(*, r, distribution)
Predictive distribution for a negative binomial likelihood with a beta prior
Assumed known number of failures r
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r
|
NUMERIC
|
number of failures |
required |
distribution
|
Beta
|
Beta distribution |
required |
Returns:
Type | Description |
---|---|
BetaNegativeBinomial
|
BetaNegativeBinomial predictive distribution |
Source code in conjugate/models.py
normal(*, x_total, x2_total, n, prior)
Posterior distribution for a normal likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
x2_total
|
NUMERIC
|
sum of all outcomes squared |
required |
n
|
NUMERIC
|
total number of samples in x_total and x2_total |
required |
prior
|
NormalInverseGamma | NormalGamma
|
NormalInverseGamma or NormalGamma prior |
required |
Returns:
Type | Description |
---|---|
NormalInverseGamma | NormalGamma
|
NormalInverseGamma or NormalGamma posterior distribution |
Source code in conjugate/models.py
normal_known_mean(*, x_total, x2_total, n, mu, prior)
Posterior distribution for a normal likelihood with a known mean and a variance prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
x2_total
|
NUMERIC
|
sum of all outcomes squared |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
mu
|
NUMERIC
|
known mean |
required |
prior
|
InverseGamma | ScaledInverseChiSquared
|
InverseGamma or ScaledInverseChiSquared prior for variance |
required |
Returns:
Type | Description |
---|---|
InverseGamma | ScaledInverseChiSquared
|
InverseGamma or ScaledInverseChiSquared posterior for variance |
Source code in conjugate/models.py
normal_known_mean_predictive(*, mu, distribution)
Predictive distribution for a normal likelihood with a known mean and a variance prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
known mean |
required |
distribution
|
InverseGamma | ScaledInverseChiSquared
|
InverseGamma or ScaledInverseChiSquared prior |
required |
Returns:
Type | Description |
---|---|
StudentT
|
StudentT predictive distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Normal, InverseGamma
from conjugate.models import normal_known_mean, normal_known_mean_predictive
unknown_var = 2.5
known_mu = 0
true = Normal(known_mu, unknown_var**0.5)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = InverseGamma(1, 1)
posterior = normal_known_mean(
n=n_samples,
x_total=data.sum(),
x2_total=(data**2).sum(),
mu=known_mu,
prior=prior,
)
bound = 5
ax = plt.subplot(111)
prior_predictive = normal_known_mean_predictive(
mu=known_mu,
distribution=prior,
)
prior_predictive.set_bounds(-bound, bound).plot_pdf(ax=ax, label="prior predictive")
true.set_bounds(-bound, bound).plot_pdf(ax=ax, label="true distribution")
posterior_predictive = normal_known_mean_predictive(
mu=known_mu,
distribution=posterior,
)
posterior_predictive.set_bounds(-bound, bound).plot_pdf(
ax=ax, label="posterior predictive"
)
ax.legend()
Source code in conjugate/models.py
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 |
|
normal_known_precision(*, x_total, n, precision, prior)
Posterior distribution for a normal likelihood with known precision and a normal prior on mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
precision
|
NUMERIC
|
known precision |
required |
prior
|
Normal
|
Normal prior for mean |
required |
Returns:
Type | Description |
---|---|
Normal
|
Normal posterior distribution for the mean |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Normal
from conjugate.models import normal_known_precision
unknown_mu = 0
known_precision = 0.5
true = Normal.from_mean_and_precision(unknown_mu, known_precision**0.5)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Normal(0, 10)
posterior = normal_known_precision(
n=n_samples, x_total=data.sum(), precision=known_precision, prior=prior
)
bound = 5
ax = plt.subplot(111)
posterior.set_bounds(-bound, bound).plot_pdf(ax=ax, label="posterior")
prior.set_bounds(-bound, bound).plot_pdf(ax=ax, label="prior")
ax.axvline(unknown_mu, color="black", linestyle="--", label="true mu")
ax.legend()
Source code in conjugate/models.py
normal_known_precision_predictive(*, precision, distribution)
Predictive distribution for a normal likelihood with known precision and a normal prior on mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
precision
|
NUMERIC
|
known precision |
required |
distribution
|
Normal
|
Normal posterior distribution for the mean |
required |
Returns:
Type | Description |
---|---|
Normal
|
Normal predictive distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Normal
from conjugate.models import normal_known_precision, normal_known_precision_predictive
unknown_mu = 0
known_precision = 0.5
true = Normal.from_mean_and_precision(unknown_mu, known_precision)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Normal(0, 10)
posterior = normal_known_precision(
n=n_samples, x_total=data.sum(), precision=known_precision, prior=prior
)
prior_predictive = normal_known_precision_predictive(
precision=known_precision,
distribution=prior,
)
posterior_predictive = normal_known_precision_predictive(
precision=known_precision,
distribution=posterior,
)
bound = 5
ax = plt.subplot(111)
true.set_bounds(-bound, bound).plot_pdf(ax=ax, label="true distribution")
posterior_predictive.set_bounds(-bound, bound).plot_pdf(
ax=ax, label="posterior predictive"
)
prior_predictive.set_bounds(-bound, bound).plot_pdf(ax=ax, label="prior predictive")
ax.legend()
Source code in conjugate/models.py
1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 |
|
normal_known_variance(*, x_total, n, var, prior)
Posterior distribution for a normal likelihood with known variance and a normal prior on mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
var
|
NUMERIC
|
known variance |
required |
prior
|
Normal
|
Normal prior for mean |
required |
Returns:
Type | Description |
---|---|
Normal
|
Normal posterior distribution for the mean |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Normal
from conjugate.models import normal_known_variance
unknown_mu = 0
known_var = 2.5
true = Normal(unknown_mu, known_var**0.5)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Normal(0, 10)
posterior = normal_known_variance(
n=n_samples,
x_total=data.sum(),
var=known_var,
prior=prior,
)
bound = 5
ax = plt.subplot(111)
posterior.set_bounds(-bound, bound).plot_pdf(ax=ax, label="posterior")
prior.set_bounds(-bound, bound).plot_pdf(ax=ax, label="prior")
ax.axvline(unknown_mu, color="black", linestyle="--", label="true mu")
ax.legend()
Source code in conjugate/models.py
normal_known_variance_predictive(*, var, distribution)
Predictive distribution for a normal likelihood with known variance and a normal distribution on mean.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
var
|
NUMERIC
|
known variance |
required |
distribution
|
Normal
|
Normal distribution for the mean |
required |
Returns:
Type | Description |
---|---|
Normal
|
Normal predictive distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Normal
from conjugate.models import normal_known_variance, normal_known_variance_predictive
unknown_mu = 0
known_var = 2.5
true = Normal(unknown_mu, known_var**0.5)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Normal(0, 10)
posterior = normal_known_variance(
n=n_samples, x_total=data.sum(), var=known_var, prior=prior
)
prior_predictive = normal_known_variance_predictive(
var=known_var,
distribution=prior,
)
posterior_predictive = normal_known_variance_predictive(
var=known_var,
distribution=posterior,
)
bound = 5
ax = plt.subplot(111)
true.set_bounds(-bound, bound).plot_pdf(ax=ax, label="true distribution")
posterior_predictive.set_bounds(-bound, bound).plot_pdf(
ax=ax, label="posterior predictive"
)
prior_predictive.set_bounds(-bound, bound).plot_pdf(ax=ax, label="prior predictive")
ax.legend()
Source code in conjugate/models.py
normal_normal_inverse_gamma(*, x_total, x2_total, n, prior)
Posterior distribution for a normal likelihood with a normal inverse gamma prior.
Derivation from paper here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
x2_total
|
NUMERIC
|
sum of all outcomes squared |
required |
n
|
NUMERIC
|
total number of samples in x_total and x2_total |
required |
prior
|
NormalInverseGamma
|
NormalInverseGamma prior |
required |
Returns:
Type | Description |
---|---|
NormalInverseGamma
|
NormalInverseGamma posterior distribution |
Source code in conjugate/models.py
normal_normal_inverse_gamma_predictive(*, distribution)
Predictive distribution for Normal likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
NormalInverseGamma
|
NormalInverseGamma distribution |
required |
Returns:
Type | Description |
---|---|
StudentT
|
StudentT predictive distribution |
Source code in conjugate/models.py
normal_predictive(*, distribution)
Predictive distribution for Normal likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
NormalInverseGamma | NormalGamma
|
NormalInverseGamma or NormalGamma distribution |
required |
Returns:
Type | Description |
---|---|
StudentT
|
StudentT predictive distribution |
Source code in conjugate/models.py
pareto_gamma(*, n, ln_x_total, x_m, prior, ln=np.log)
Posterior distribution for a pareto likelihood with a gamma prior.
The parameter x_m is assumed to be known.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of samples |
required |
ln_x_total
|
NUMERIC
|
sum of the log of all outcomes |
required |
x_m
|
NUMERIC
|
The known minimum value |
required |
prior
|
Gamma
|
Gamma prior |
required |
ln
|
function to take the natural log, defaults to np.log |
log
|
Returns:
Type | Description |
---|---|
Gamma
|
Gamma posterior distribution |
Examples:
Constructed example
import numpy as np
import matplotlib.pyplot as plt
from conjugate.distributions import Pareto, Gamma
from conjugate.models import pareto_gamma
x_m_known = 1
true = Pareto(x_m_known, 1)
n_samples = 15
data = true.dist.rvs(size=n_samples, random_state=42)
prior = Gamma(1, 1)
posterior = pareto_gamma(
n=n_samples,
ln_x_total=np.log(data).sum(),
x_m=x_m_known,
prior=prior,
)
ax = plt.subplot(111)
posterior.set_bounds(0, 2.5).plot_pdf(ax=ax, label="posterior")
prior.set_bounds(0, 2.5).plot_pdf(ax=ax, label="prior")
ax.axvline(x_m_known, color="black", linestyle="--", label="true x_m")
ax.legend()
Source code in conjugate/models.py
1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 |
|
poisson_gamma(*, x_total, n, prior)
Posterior distribution for a poisson likelihood with a gamma prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_total
|
NUMERIC
|
sum of all outcomes |
required |
n
|
NUMERIC
|
total number of samples in x_total |
required |
prior
|
Gamma
|
Gamma prior |
required |
Returns:
Type | Description |
---|---|
Gamma
|
Gamma posterior distribution |
Source code in conjugate/models.py
poisson_gamma_predictive(*, distribution, n=1)
Predictive distribution for a poisson likelihood with a gamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
distribution
|
Gamma
|
Gamma distribution |
required |
n
|
NUMERIC
|
Number of trials for each sample, defaults to 1. Can be used to scale the distributions to a different unit of time. |
1
|
Returns:
Type | Description |
---|---|
NegativeBinomial
|
NegativeBinomial distribution related to predictive |
Source code in conjugate/models.py
uniform_pareto(*, x_max, n, prior, max_fn=np.maximum)
Posterior distribution for a uniform likelihood with a pareto prior.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_max
|
NUMERIC
|
maximum value |
required |
n
|
NUMERIC
|
number of samples |
required |
prior
|
Pareto
|
Pareto prior |
required |
max_fn
|
elementwise max function, defaults to np.maximum |
maximum
|
Returns:
Type | Description |
---|---|
Pareto
|
Pareto posterior distribution |
Examples:
Get the posterior for this model with simulated data:
from conjugate.distributions import Uniform, Pareto
from conjugate.models import uniform_pareto
true_max = 5
true = Uniform(0, true_max)
n_samples = 10
data = true.dist.rvs(size=n_samples)
prior = Pareto(1, 1)
posterior = uniform_pareto(x_max=data.max(), n=n_samples, prior=prior)
Source code in conjugate/models.py
von_mises_known_concentration(*, cos_total, sin_total, n, kappa, prior, sin=np.sin, cos=np.cos, arctan2=np.arctan2)
VonMises likelihood with known concentration parameter.
Taken from Section 2.13.1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cos_total
|
NUMERIC
|
sum of all cosines |
required |
sin_total
|
NUMERIC
|
sum of all sines |
required |
n
|
NUMERIC
|
total number of samples in cos_total and sin_total |
required |
kappa
|
NUMERIC
|
known concentration parameter |
required |
prior
|
VonMisesKnownConcentration
|
VonMisesKnownConcentration prior |
required |
Returns:
Type | Description |
---|---|
VonMisesKnownConcentration
|
VonMisesKnownConcentration posterior distribution |
Source code in conjugate/models.py
von_mises_known_direction(*, centered_cos_total, n, prior)
VonMises likelihood with known direction parameter.
Taken from Section 2.13.2
Parameters:
Name | Type | Description | Default |
---|---|---|---|
centered_cos_total
|
NUMERIC
|
sum of all centered cosines. sum cos(x - known direction)) |
required |
n
|
NUMERIC
|
total number of samples in centered_cos_total |
required |
prior
|
VonMisesKnownDirectionProportional
|
VonMisesKnownDirectionProportional prior |
required |
Source code in conjugate/models.py
weibull_inverse_gamma_known_shape(*, n, x_beta_total, prior)
Posterior distribution for a Weibull likelihood with an inverse gamma prior on shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
total number of samples |
required |
x_beta_total
|
NUMERIC
|
sum of all x^beta |
required |
prior
|
InverseGamma
|
InverseGamma prior |
required |
Returns:
Type | Description |
---|---|
InverseGamma
|
InverseGamma posterior distribution |