Conjugate Prior
Python implementation of the conjugate prior table for Bayesian Statistics
See wikipedia page:
https://en.wikipedia.org/wiki/Conjugate_prior#Table_of_conjugate_distributions
Installation:
pip install conjugate-prior
Supported Models:
BetaBinomial
- Useful for independent trials such as click-trough-rate (ctr), web visitor conversion.BetaBernoulli
- Same as above.GammaExponential
- Useful for churn-rate analysis, cost, dwell-time.GammaPoisson
- Useful for time passed until event, as above.NormalNormalKnownVar
- Useful for modeling a centralized distribution with constant noise.NormalLogNormalKnownVar
- Useful for modeling a Length of a support phone call.InvGammaNormalKnownMean
- Useful for modeling the effect of a noise.InvGammaWeibullKnownShape
- Useful for reasoning about particle sizes over time.DirichletMultinomial
- Extension of BetaBinomial to more than 2 types of events (Limited support).
Basic API
model = GammaExponential(a, b)
- A Bayesian model with anExponential
likelihood, and aGamma
prior. Wherea
andb
are the prior parameters.model.pdf(x)
- Returns the probability-density-function of the prior function atx
.model.cdf(x)
- Returns the cumulative-density-function of the prior function atx
.model.mean()
- Returns the prior mean.model.plot(l, u)
- Plots the prior distribution betweenl
andu
.model.posterior(l, u)
- Returns the credible interval on(l,u)
(equivalent tocdf(u)-cdf(l)
).model.update(data)
- Returns a new model after observingdata
.model.predict(x)
- Predicts the likelihood of observingx
(if a posterior predictive exists).model.sample()
- Draw a single sample from the posterior distribution.
Coin flip example:
from conjugate_prior import BetaBinomial
heads = 95
tails = 105
prior_model = BetaBinomial() # Uninformative prior
updated_model = prior_model.update(heads, tails)
credible_interval = updated_model.posterior(0.45, 0.55)
print ("There's {p:.2f}% chance that the coin is fair".format(p=credible_interval*100))
predictive = updated_model.predict(50, 50)
print ("The chance of flipping 50 Heads and 50 Tails in 100 trials is {p:.2f}%".format(p=predictive*100))