Distributions
These are the supported distributions based on the conjugate models.
Many have the dist
attribute which is a scipy.stats distribution object. From there,
you can use the methods from scipy.stats to get the pdf, cdf, etc.
Distributions can be plotted using the plot_pmf
or plot_pdf
methods of the distribution.
from conjugate.distribution import Beta
beta = Beta(1, 1)
scipy_dist = beta.dist
print(scipy_dist.mean())
# 0.5
print(scipy_dist.ppf([0.025, 0.975]))
# [0.025 0.975]
samples = scipy_dist.rvs(100)
beta.plot_pmf(label="beta distribution")
Distributions like Poisson can be added with other Poissons or multiplied by numerical values in order to scale rate. For instance,
daily_rate = 0.25
daily_pois = Poisson(lam=daily_rate)
two_day_pois = daily_pois + daily_pois
weekly_pois = 7 * daily_pois
Below are the currently supported distributions
Beta
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Beta distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
shape parameter |
required |
Source code in conjugate/distributions.py
from_mean(mean, alpha)
classmethod
Alternative constructor from mean and alpha.
from_successes_and_failures(successes, failures)
classmethod
Alternative constructor based on hyperparameter interpretation.
Source code in conjugate/distributions.py
BetaBinomial
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Beta binomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of trials |
required |
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
shape parameter |
required |
Source code in conjugate/distributions.py
BetaGeometric
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Beta geometric distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
shape parameter |
required |
one_start
|
bool
|
whether to start at 1 or 0. Default is 1. |
True
|
Source code in conjugate/distributions.py
BetaNegativeBinomial
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Beta negative binomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of successes |
required |
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
shape parameter |
required |
Source code in conjugate/distributions.py
BetaProportional
dataclass
Beta proportional distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
NUMERIC
|
product of observations |
required |
q
|
NUMERIC
|
product of complements |
required |
k
|
NUMERIC
|
number of observations |
required |
Source code in conjugate/distributions.py
approx_log_likelihood(alpha, beta, ln=np.log, gammaln=gammaln)
Approximate log likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
shape parameter |
required |
ln
|
log function |
log
|
|
gammaln
|
log gamma function |
gammaln
|
Returns:
Type | Description |
---|---|
NUMERIC
|
log likelihood up to a constant |
Source code in conjugate/distributions.py
Binomial
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Binomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of trials |
required |
p
|
NUMERIC
|
probability of success |
required |
Source code in conjugate/distributions.py
CompoundGamma
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Compound gamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape |
required |
beta
|
NUMERIC
|
scale |
required |
lam
|
NUMERIC
|
rate |
required |
Source code in conjugate/distributions.py
Dirichlet
dataclass
Bases: DirichletPlotDistMixin
Dirichlet distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
Source code in conjugate/distributions.py
DirichletMultinomial
dataclass
Bases: SliceMixin
Dirichlet multinomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
n
|
NUMERIC
|
number of trials |
required |
Source code in conjugate/distributions.py
Exponential
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Exponential distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lam
|
NUMERIC
|
rate parameter |
required |
Source code in conjugate/distributions.py
Gamma
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Gamma distribution.
Gamma Distribution Scipy Docmentation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
rate parameter |
required |
Source code in conjugate/distributions.py
GammaKnownRateProportional
dataclass
Gamma known rate proportional distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
NUMERIC
|
prod of observations |
required |
b
|
NUMERIC
|
number of observations |
required |
c
|
NUMERIC
|
number of observations |
required |
Source code in conjugate/distributions.py
approx_log_likelihood(alpha, beta, ln=np.log, gammaln=gammaln)
Approximate log likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
known rate parameter |
required |
ln
|
log function |
log
|
|
gammaln
|
log gamma function |
gammaln
|
Returns:
Type | Description |
---|---|
NUMERIC
|
log likelihood up to a constant |
Source code in conjugate/distributions.py
GammaProportional
dataclass
Gamma proportional distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
NUMERIC
|
product of r observations |
required |
q
|
NUMERIC
|
sum of s observations |
required |
r
|
NUMERIC
|
number of observations for p |
required |
s
|
NUMERIC
|
number of observations for q |
required |
Source code in conjugate/distributions.py
approx_log_likelihood(alpha, beta, ln=np.log, gammaln=gammaln)
Approximate log likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape parameter |
required |
beta
|
NUMERIC
|
rate parameter |
required |
ln
|
log function |
log
|
|
gammaln
|
log gamma function |
gammaln
|
Returns:
Type | Description |
---|---|
NUMERIC
|
log likelihood up to a constant |
Source code in conjugate/distributions.py
Geometric
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Geometric distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
p
|
NUMERIC
|
probability of success |
required |
one_start
|
bool
|
whether to start at 1 or 0. Default is 1. |
True
|
Source code in conjugate/distributions.py
Hypergeometric
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Hypergeometric distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
N
|
NUMERIC
|
population size |
required |
k
|
NUMERIC
|
number of successes in the population |
required |
n
|
NUMERIC
|
number of draws |
required |
Source code in conjugate/distributions.py
InverseGamma
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
InverseGamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape |
required |
beta
|
NUMERIC
|
scale |
required |
Source code in conjugate/distributions.py
InverseWishart
dataclass
Inverse Wishart distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nu
|
NUMERIC
|
degrees of freedom |
required |
psi
|
NUMERIC
|
scale matrix |
required |
Source code in conjugate/distributions.py
LogNormal
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Log normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
sigma
|
NUMERIC
|
standard deviation |
required |
Source code in conjugate/distributions.py
Lomax
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Lomax distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
alpha
|
NUMERIC
|
shape |
required |
lam
|
NUMERIC
|
scale |
required |
Source code in conjugate/distributions.py
Multinomial
dataclass
Bases: SliceMixin
Multinomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of trials |
required |
p
|
NUMERIC
|
probability of success |
required |
Source code in conjugate/distributions.py
MultivariateNormal
dataclass
Multivariate normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
cov
|
NUMERIC
|
covariance matrix |
required |
Source code in conjugate/distributions.py
MultivariateStudentT
dataclass
MultivariateStudentT distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
sigma
|
NUMERIC
|
covariance matrix |
required |
nu
|
NUMERIC
|
degrees of freedom |
required |
Source code in conjugate/distributions.py
NegativeBinomial
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Negative binomial distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
NUMERIC
|
number of successes |
required |
p
|
NUMERIC
|
probability of success |
required |
Source code in conjugate/distributions.py
Normal
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
sigma
|
NUMERIC
|
standard deviation |
required |
Source code in conjugate/distributions.py
from_mean_and_precision(mean, precision)
classmethod
from_mean_and_variance(mean, variance)
classmethod
NormalGamma
dataclass
Normal gamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
lam
|
NUMERIC
|
precision |
required |
alpha
|
NUMERIC
|
shape |
required |
beta
|
NUMERIC
|
scale |
required |
Source code in conjugate/distributions.py
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 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 |
|
sample_beta(size, return_variance=False, random_state=None)
Sample beta from the normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC | tuple[NUMERIC, NUMERIC]
|
samples from the normal distribution |
Source code in conjugate/distributions.py
sample_mean(size, return_variance=False, random_state=None)
Sample mean from the normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC | tuple[NUMERIC, NUMERIC]
|
samples from the normal distribution |
Source code in conjugate/distributions.py
sample_variance(size, random_state=None)
Sample precision from gamma distribution and invert.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC
|
samples from the inverse gamma distribution |
Source code in conjugate/distributions.py
NormalInverseGamma
dataclass
Normal inverse gamma distribution.
Supports both 1 dimensional and multivariate cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
alpha
|
NUMERIC
|
shape |
required |
beta
|
NUMERIC
|
scale |
required |
delta_inverse
|
NUMERIC | None
|
covariance matrix, 2d array for multivariate case |
None
|
nu
|
NUMERIC | None
|
alternative precision parameter for 1 dimensional case |
None
|
Source code in conjugate/distributions.py
546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 |
|
sample_beta(size, return_variance=False, random_state=None)
Sample beta from the normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC | tuple[NUMERIC, NUMERIC]
|
samples from the normal distribution and optionally variance |
Source code in conjugate/distributions.py
sample_mean(size, return_variance=False, random_state=None)
Sample the mean from the normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC | tuple[NUMERIC, NUMERIC]
|
samples from the normal distribution and optionally variance |
Source code in conjugate/distributions.py
sample_variance(size, random_state=None)
Sample variance from the inverse gamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
random_state
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC
|
samples from the inverse gamma distribution |
Source code in conjugate/distributions.py
NormalInverseWishart
dataclass
Normal inverse Wishart distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
kappa
|
NUMERIC
|
precision |
required |
nu
|
NUMERIC
|
degrees of freedom |
required |
psi
|
NUMERIC
|
scale matrix |
required |
Source code in conjugate/distributions.py
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 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 |
|
inverse_wishart
property
Inverse wishart distribution.
sample_mean(size, return_variance=False, random_state=None)
Sample the mean from the normal distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
Generator | None
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC
|
samples from the normal distribution and optionally variance |
Source code in conjugate/distributions.py
sample_variance(size, random_state=None)
Sample precision from gamma distribution and invert.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
required |
random_state
|
Generator | None
|
random state |
None
|
Returns:
Type | Description |
---|---|
NUMERIC
|
samples from the inverse wishart distribution |
Source code in conjugate/distributions.py
NormalWishart
dataclass
Normal Wishart distribution.
Parameterization from Wikipedia.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
lam
|
NUMERIC
|
precision |
required |
W
|
NUMERIC
|
scale matrix |
required |
nu
|
NUMERIC
|
degrees of freedom |
required |
Source code in conjugate/distributions.py
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 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 |
|
sample_mean(size=1, return_variance=False, random_state=None)
Sample mean
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
1
|
return_variance
|
bool
|
whether to return variance as well |
False
|
random_state
|
Generator | None
|
random state |
None
|
Returns:
Type | Description |
---|---|
ndarray | tuple[ndarray, ndarray]
|
samples from the normal distribution and optionally variance |
Source code in conjugate/distributions.py
sample_variance(size=1, random_state=None, inv=np.linalg.inv)
Sample variance
Parameters:
Name | Type | Description | Default |
---|---|---|---|
size
|
int
|
number of samples |
1
|
random_state
|
Generator | None
|
random state |
None
|
inv
|
Callable
|
matrix inversion function |
inv
|
Returns:
Type | Description |
---|---|
ndarray
|
samples from the inverse wishart distribution |
Source code in conjugate/distributions.py
Pareto
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Pareto distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x_m
|
NUMERIC
|
minimum value |
required |
alpha
|
NUMERIC
|
scale parameter |
required |
Source code in conjugate/distributions.py
Poisson
dataclass
Bases: DiscretePlotMixin
, SliceMixin
Poisson distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lam
|
NUMERIC
|
rate parameter |
required |
Source code in conjugate/distributions.py
ScaledInverseChiSquared
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Scaled inverse chi squared distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nu
|
NUMERIC
|
degrees of freedom |
required |
sigma2
|
NUMERIC
|
scale parameter |
required |
Source code in conjugate/distributions.py
from_inverse_gamma(inverse_gamma)
classmethod
Alternative constructor from inverse gamma distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inverse_gamma
|
InverseGamma
|
inverse gamma distribution |
required |
Returns:
Type | Description |
---|---|
ScaledInverseChiSquared
|
scaled inverse chi squared distribution |
Source code in conjugate/distributions.py
to_inverse_gamma()
Convert to inverse gamma distribution.
Returns:
Type | Description |
---|---|
InverseGamma
|
inverse gamma distribution |
StudentT
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
StudentT distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
sigma
|
NUMERIC
|
standard deviation |
required |
nu
|
NUMERIC
|
degrees of freedom |
required |
Source code in conjugate/distributions.py
Uniform
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Uniform distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
low
|
NUMERIC
|
lower bound |
required |
high
|
NUMERIC
|
upper bound |
required |
Source code in conjugate/distributions.py
VectorizedDist
Vectorized distribution to handle scipy distributions that don't support vectorization.
Source code in conjugate/distributions.py
VonMises
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Von Mises distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
kappa
|
NUMERIC
|
concentration |
required |
Source code in conjugate/distributions.py
VonMisesKnownConcentration
dataclass
Von Mises known concentration distribution.
Taken from Section 2.13.1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
a
|
NUMERIC
|
positive value |
required |
b
|
NUMERIC
|
value between 0 and 2 pi |
required |
Source code in conjugate/distributions.py
log_likelihood(mu, cos=np.cos, ln=np.log, i0=i0)
Approximate log likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mu
|
NUMERIC
|
mean |
required |
cos
|
cosine function |
cos
|
|
ln
|
log function |
log
|
|
i0
|
modified bessel function of order 0 |
i0
|
Returns:
Type | Description |
---|---|
NUMERIC
|
log likelihood |
Source code in conjugate/distributions.py
VonMisesKnownDirectionProportional
dataclass
Von Mises known direction proportional distribution.
Taken from Section 2.13.2.
Args: c: NUMERIC r: NUMERIC
Source code in conjugate/distributions.py
approx_log_likelihood(kappa, ln=np.log, i0=i0)
Approximate log likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
kappa
|
NUMERIC
|
concentration |
required |
ln
|
log function |
log
|
|
i0
|
modified bessel function of order 0 |
i0
|
Returns:
Type | Description |
---|---|
NUMERIC
|
log likelihood up to a constant |
Source code in conjugate/distributions.py
Weibull
dataclass
Bases: ContinuousPlotDistMixin
, SliceMixin
Weibull distribution.
Parameterization from Section 2.11 of paper.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
beta
|
NUMERIC
|
shape parameter |
required |
theta
|
NUMERIC
|
scale parameter |
required |
Example
Recreation of the plot on Wikipedia.
import matplotlib.pyplot as plt
import numpy as np
from conjugate.distributions import Weibull
lam = 1
k = np.array([0.5, 1.0, 1.5, 5.0])
beta = k
theta = lam ** beta
distribution = Weibull(beta=beta, theta=theta)
ax = distribution.set_bounds(0, 2.5).plot_pdf(
label=["k=0.5", "k=1.0", "k=1.5", "k=5.0"],
color=["blue", "red", "pink", "green"],
)
ax.legend()
Source code in conjugate/distributions.py
Wishart
dataclass
Wishart distribution.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
nu
|
NUMERIC
|
degrees of freedom |
required |
V
|
NUMERIC
|
scale matrix |
required |