Created
November 9, 2015 19:58
-
-
Save robdmc/101b1c226e63cf33438d to your computer and use it in GitHub Desktop.
Beta Distribution Ranking
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import numpy as np | |
| from scipy import stats | |
| import pandas as pd | |
| def score_for_confidence(alpha, beta, confidence=.95): | |
| """ | |
| alpha: 1 + num_positive | |
| beta: 1 + num_negative | |
| confidence: confidence you can have that positive fraction is greater than this | |
| """ | |
| quantile = 1 - confidence | |
| return stats.distributions.beta(a=alpha, b=beta).ppf(quantile) | |
| def example(): | |
| N = 10 | |
| wins = np.random.poisson(2, size=N) | |
| totals = wins + np.random.poisson(3, size=N) | |
| losses = totals - wins | |
| alpha = wins + 1 | |
| beta = losses + 1 | |
| confidence = .9 | |
| df = pd.DataFrame({'wins': np.random.poisson(2, size=N),'losses': np.random.poisson(3, size=N)}) | |
| df['alpha'] = df.wins + 1 | |
| df['beta'] = df.losses + 1 | |
| df['score'] = score_for_confidence(df.alpha, df.beta, confidence) | |
| return df | |
| example() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment