# Conjugate Prior
Python implementation of the conjugate prior table for Bayesian Statistics
[![Downloads](http://pepy.tech/badge/conjugate-prior)](http://pepy.tech/count/conjugate-prior)
See wikipedia page:
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.
1. `BetaBernoulli` - Same as above.
1. `GammaExponential` - Useful for churn-rate analysis, cost, dwell-time.
1. `GammaPoisson` - Useful for time passed until event, as above.
1. `NormalNormalKnownVar` - Useful for modeling a centralized distribution with constant noise.
1. `NormalLogNormalKnownVar` - Useful for modeling a Length of a support phone call.
1. `InvGammaNormalKnownMean` - Useful for modeling the effect of a noise.
1. `InvGammaWeibullKnownShape` - Useful for reasoning about particle sizes over time.
1. `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.
1. `model.pdf(x)` - Returns the probability-density-function of the prior function at `x`.
1. `model.cdf(x)` - Returns the cumulative-density-function of the prior function at `x`.
1. `model.mean()` - Returns the prior mean.
1. `model.plot(l, u)` - Plots the prior distribution between `l` and `u`.
1. `model.posterior(l, u)` - Returns the credible interval on `(l,u)` (equivalent to `cdf(u)-cdf(l)`).
1. `model.update(data)` - Returns a *new* model after observing `data`.
1. `model.predict(x)` - Predicts the likelihood of observing `x` (if a posterior predictive exists).
1. `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())
Raw data
{
"_id": null,
"home_page": "https://github.com/argmaxml/conjugate_prior",
"name": "conjugate-prior",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "conjugate, bayesian, stats, statistics, bayes, distribution, probability, hypothesis, modelling, thompson sampling",
"author": "Uri Goren",
"author_email": "conjugate@argmaxml.com",
"download_url": "https://files.pythonhosted.org/packages/25/ee/adc2b6949630036809cd13d4eacf250df3931598786fc830e5f8a65c84a3/conjugate_prior-0.85.tar.gz",
"platform": null,
"description": "# Conjugate Prior\nPython implementation of the conjugate prior table for Bayesian Statistics\n\n[![Downloads](http://pepy.tech/badge/conjugate-prior)](http://pepy.tech/count/conjugate-prior)\n\nSee wikipedia page:\n\nhttps://en.wikipedia.org/wiki/Conjugate_prior#Table_of_conjugate_distributions\n\n## Installation:\n`pip install conjugate-prior`\n\n## Supported Models:\n 1. `BetaBinomial` - Useful for independent trials such as click-trough-rate (ctr), web visitor conversion.\n 1. `BetaBernoulli` - Same as above.\n 1. `GammaExponential` - Useful for churn-rate analysis, cost, dwell-time.\n 1. `GammaPoisson` - Useful for time passed until event, as above.\n 1. `NormalNormalKnownVar` - Useful for modeling a centralized distribution with constant noise.\n 1. `NormalLogNormalKnownVar` - Useful for modeling a Length of a support phone call.\n 1. `InvGammaNormalKnownMean` - Useful for modeling the effect of a noise.\n 1. `InvGammaWeibullKnownShape` - Useful for reasoning about particle sizes over time.\n 1. `DirichletMultinomial` - Extension of BetaBinomial to more than 2 types of events (Limited support).\n\n## Basic API\n 1. `model = GammaExponential(a, b)` - A Bayesian model with an `Exponential` likelihood, and a `Gamma` prior. Where `a` and `b` are the prior parameters.\n 1. `model.pdf(x)` - Returns the probability-density-function of the prior function at `x`.\n 1. `model.cdf(x)` - Returns the cumulative-density-function of the prior function at `x`.\n 1. `model.mean()` - Returns the prior mean.\n 1. `model.plot(l, u)` - Plots the prior distribution between `l` and `u`.\n 1. `model.posterior(l, u)` - Returns the credible interval on `(l,u)` (equivalent to `cdf(u)-cdf(l)`).\n 1. `model.update(data)` - Returns a *new* model after observing `data`.\n 1. `model.predict(x)` - Predicts the likelihood of observing `x` (if a posterior predictive exists).\n 1. `model.sample()` - Draw a single sample from the posterior distribution.\n\n\n\n## Coin flip example:\n\n from conjugate_prior import BetaBinomial\n heads = 95\n tails = 105\n prior_model = BetaBinomial() # Uninformative prior\n updated_model = prior_model.update(heads, tails)\n credible_interval = updated_model.posterior(0.45, 0.55)\n print (\"There's {p:.2f}% chance that the coin is fair\".format(p=credible_interval*100))\n predictive = updated_model.predict(50, 50)\n print (\"The chance of flipping 50 Heads and 50 Tails in 100 trials is {p:.2f}%\".format(p=predictive*100))\n\n## Variant selection with Multi-armed-bandit\n\nAssume we have `10` creatives (variants) we can choose for our ad campaign, at first we start with the uninformative prior.\n\nAfter getting feedback (i.e. clicks) from displaying the ads, we update our model.\n\nThen we sample the `DirrechletMultinomial` model for the updated distribution.\n\n from conjugate_prior import DirichletMultinomial\n from collections import Counter\n # Assuming we have 10 creatives\n model = DirichletMultinomial(10)\n mle = lambda M:[int(r.argmax()) for r in M]\n selections = [v for k,v in sorted(Counter(mle(model.sample(100))).most_common())]\n print(\"Percentage before 1000 clicks: \",selections)\n # after a period of time, we got this array of clicks\n clicks = [400,200,100,50,20,20,10,0,0,200]\n model = model.update(clicks)\n selections = [v for k,v in sorted(Counter(mle(model.sample(100))).most_common())]\n print(\"Percentage after 1000 clicks: \",selections)\n\n## Naive Recommendation System with UCB\n\n from conjugate_prior import BetaBinomialRanker\n ranker = BetaBinomialRanker(prior=0.1) # 10% click-through-rate\n ranker[\"cmpgn1\"]+=(1,9) # 1 click, 9 skips\n ranker[\"cmpgn2\"]+=(10,90) # 10 click, 90 skips\n ranker[\"cmpgn3\"]+=(1,2) # 1 click, 3 skips\n # Balance exploration and exploitation w/UCB\n print(ranker.rank_by_ucb())\n",
"bugtrack_url": null,
"license": null,
"summary": "Bayesian Statistics conjugate prior distributions",
"version": "0.85",
"project_urls": {
"Homepage": "https://github.com/argmaxml/conjugate_prior"
},
"split_keywords": [
"conjugate",
" bayesian",
" stats",
" statistics",
" bayes",
" distribution",
" probability",
" hypothesis",
" modelling",
" thompson sampling"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "9337194d0f34ac789d9f8bbf31891e3d021052688b7474d74ffb7bdd3bf6d79e",
"md5": "0413660c9ae1584f04491022f553e703",
"sha256": "ec8f2474f76d30808b0bf31a330eb8e4afac3e819e9e9f016668f5b6b2da7477"
},
"downloads": -1,
"filename": "conjugate_prior-0.85-py3-none-any.whl",
"has_sig": false,
"md5_digest": "0413660c9ae1584f04491022f553e703",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 10236,
"upload_time": "2024-07-28T15:14:10",
"upload_time_iso_8601": "2024-07-28T15:14:10.753312Z",
"url": "https://files.pythonhosted.org/packages/93/37/194d0f34ac789d9f8bbf31891e3d021052688b7474d74ffb7bdd3bf6d79e/conjugate_prior-0.85-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "25eeadc2b6949630036809cd13d4eacf250df3931598786fc830e5f8a65c84a3",
"md5": "1dd8a9e5f0398f219c60ca59bbc396f9",
"sha256": "855d4ce90b40bee0f05b65fa6dca8c8baa7928584d41888e7fe058998dbcc88f"
},
"downloads": -1,
"filename": "conjugate_prior-0.85.tar.gz",
"has_sig": false,
"md5_digest": "1dd8a9e5f0398f219c60ca59bbc396f9",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 9179,
"upload_time": "2024-07-28T15:14:12",
"upload_time_iso_8601": "2024-07-28T15:14:12.111510Z",
"url": "https://files.pythonhosted.org/packages/25/ee/adc2b6949630036809cd13d4eacf250df3931598786fc830e5f8a65c84a3/conjugate_prior-0.85.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-07-28 15:14:12",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "argmaxml",
"github_project": "conjugate_prior",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "conjugate-prior"
}