Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jmquintana79/38f2ca1f1b042448b9036cce6486431d to your computer and use it in GitHub Desktop.
Save jmquintana79/38f2ca1f1b042448b9036cce6486431d to your computer and use it in GitHub Desktop.
def ratio_lossed_gained_uncertainty(num_lossed:int,
num_gained:int,
alpha:float = 0.01,
verbose:bool = False) -> tuple[float, float]:
"""Estimate ratio lossed / gained with statistical uncertainty
Using confidence interval (CI) estimation for binomal estimation (loss, gain).
NOTE> In this case lossed is win and gained is loss, that is, ratio = lossed / gained.
Arguments:
num_lossed {int} -- Number of lossed.
num_gained {int} -- Number of gained.
Keyword Arguments:
alpha {float} -- Confidence (default: {0.01})
verbose {bool} -- Display or not (default, False)
Returns:
tuple[float, float] -- (ratio, relative uncertainty)
"""
# estimate total
total = num_gained + num_lossed
# validation
if total == 0 or num_gained == 0:
return (float('inf'), 0.)
# proportions
p = num_lossed / total
R = num_lossed / num_gained
# confidence interval for binomial distribution
ci_low, ci_high = proportion_confint(num_lossed, total, alpha=alpha, method='wilson')
# confidence interval to ratios interval
R_low = ci_low / (1 - ci_low)
R_high = ci_high / (1 - ci_high)
# relative uncertainty
uncertainty = (R_high - R_low) / R
# build results container
d_results = {
'ratio': R,
'probability lossed': p,
'CI ratio': (R_low, R_high),
'relative uncertainty': uncertainty
}
# display
if verbose:
print(d_results)
# return
return (d_results["ratio"], d_results['relative uncertainty'])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment