Bayesian Statistics conjugate prior distributions

# Conjugate Prior

Python implementation of the conjugate prior table for Bayesian Statistics https://en.wikipedia.org/wiki/Conjugate_prior#Table_of_conjugate_distributions

## Installation:

pip install conjugate-prior

## Supported Models:

1. BetaBinomial - Useful for independent trials such as click-trough-rate (ctr), web visitor conversion.
2. BetaBernoulli - Same as above.
3. GammaExponential - Useful for churn-rate analysis, cost, dwell-time.
4. GammaPoisson - Useful for time passed until event, as above.
5. NormalNormalKnownVar - Useful for modeling a centralized distribution with constant noise.
6. NormalLogNormalKnownVar - Useful for modeling a Length of a support phone call.
7. InvGammaNormalKnownMean - Useful for modeling the effect of a noise.
8. InvGammaWeibullKnownShape - Useful for reasoning about particle sizes over time.
9. DirichletMultinomial - Extension of BetaBinomial to more than 2 types of events (Limited support).

## Basic API

1. model = GammaExponential(a, b) - A Bayesian model with an Exponential likelihood, and a Gamma prior. Where a and b are the prior parameters.
2. model.pdf(x) - Returns the probability-density-function of the prior function at x.
3. model.cdf(x) - Returns the cumulative-density-function of the prior function at x.
4. model.mean() - Returns the prior mean.
5. model.plot(l, u) - Plots the prior distribution between l and u.
6. model.posterior(l, u) - Returns the credible interval on (l,u) (equivalent to cdf(u)-cdf(l)).
7. model.update(data) - Returns a new model after observing data.
8. model.predict(x) - Predicts the likelihood of observing x (if a posterior predictive exists).
9. model.sample() - Draw a single sample from the posterior distribution.

## Coin flip example:

from conjugate_prior import BetaBinomial
tails = 105
prior_model = BetaBinomial() # Uninformative prior
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))


## Variant selection with Multi-armed-bandit

Assume we have 10 creatives (variants) we can choose for our ad campaign, at first we start with the uninformative prior.

After getting feedback (i.e. clicks) from displaying the ads, we update our model.

Then we sample the DirrechletMultinomial model for the updated distribution.

from conjugate_prior import DirichletMultinomial
from collections import Counter
# Assuming we have 10 creatives
model = DirichletMultinomial(10)
mle = lambda M:[int(r.argmax()) for r in M]
selections = [v for k,v in sorted(Counter(mle(model.sample(100))).most_common())]
print("Percentage before 1000 clicks: ",selections)
# after a period of time, we got this array of clicks
clicks = [400,200,100,50,20,20,10,0,0,200]
model = model.update(clicks)
selections = [v for k,v in sorted(Counter(mle(model.sample(100))).most_common())]
print("Percentage after 1000 clicks: ",selections)


## Project details

This version 0.71 0.64 0.63 0.62 0.61 0.56 0.55 0.38 0.36 0.35 0.34 0.33 0.32 0.31 0.29 0.28 0.27 0.26 0.25 0.24 0.23 0.21 0.7 0.6 0.5 0.4 0.3 0.2 0.1

Uploaded source