Skip to content

Instantly share code, notes, and snippets.

@robdmc
Created November 9, 2015 19:58
Show Gist options
  • Select an option

  • Save robdmc/101b1c226e63cf33438d to your computer and use it in GitHub Desktop.

Select an option

Save robdmc/101b1c226e63cf33438d to your computer and use it in GitHub Desktop.
Beta Distribution Ranking
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