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 anExponentiallikelihood, and aGammaprior. Whereaandbare 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 betweenlandu.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))
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)
Naive Recommendation System with UCB
from conjugate_prior import BetaBinomialRanker ranker = BetaBinomialRanker(prior=0.1) # 10% click-through-rate ranker["cmpgn1"]+=(1,9) # 1 click, 9 skips ranker["cmpgn2"]+=(10,90) # 10 click, 90 skips ranker["cmpgn3"]+=(1,2) # 1 click, 3 skips
Balance exploration and exploitation w/UCB
print(ranker.rank_by_ucb())
Metadata
Release files for conjugate-prior 0.85
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| conjugate_prior-0.85.tar.gz | 9.2 kB | Without provenance | Details for conjugate_prior-0.85.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| conjugate_prior-0.85-py3-none-any.whl | Python 3 | none | any | Details for conjugate_prior-0.85-py3-none-any.whl |
Total release size: 19.4 kB
Release files / conjugate_prior-0.85.tar.gz
| Download URL | conjugate_prior-0.85.tar.gz |
|---|---|
| Size | 9.2 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
855d4ce90b40bee0f05b65fa6dca8c8baa7928584d41888e7fe058998dbcc88f
|
|
BLAKE2b-256 checksum How to use checksums |
25eeadc2b6949630036809cd13d4eacf250df3931598786fc830e5f8a65c84a3
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Release files / conjugate_prior-0.85-py3-none-any.whl
| Download URL | conjugate_prior-0.85-py3-none-any.whl |
|---|---|
| Size | 10.2 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
ec8f2474f76d30808b0bf31a330eb8e4afac3e819e9e9f016668f5b6b2da7477
|
|
BLAKE2b-256 checksum How to use checksums |
9337194d0f34ac789d9f8bbf31891e3d021052688b7474d74ffb7bdd3bf6d79e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.12.4
|
Security for conjugate-prior 0.85
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.