Skip to content

Instantly share code, notes, and snippets.

@fasiha
Created September 2, 2020 02:20
Show Gist options
  • Save fasiha/f201c0631c34bffd6c6f6be98b32dc5e to your computer and use it in GitHub Desktop.
Save fasiha/f201c0631c34bffd6c6f6be98b32dc5e to your computer and use it in GitHub Desktop.
import ebisu
import numpy
import random
exp = numpy.exp
model = (3, 3, 1)
m2pd = ebisu.modelToPercentileDecay
ur = ebisu.updateRecall
def update_growth(growth_model, successful, attempts, test_time):
original_model = growth_model[:3]
original_updated_model = ur(original_model, successful, attempts, test_time)
adjustment = m2pd(original_updated_model, 0.5) / m2pd(original_model, 0.5)
new_a = original_updated_model[0]
new_b = original_updated_model[1]
new_growth_coefficient = growth_model[3] * adjustment
new_t = new_growth_coefficient * growth_model[2]
new_growth_model = (new_a, new_b, new_t, new_growth_coefficient)
return new_growth_model
import numpy as np
from scipy.special import betaln
def _meanVarToBeta(mean, var):
"This function was stolen from ebisu.py"
tmp = mean * (1 - mean) / var - 1
alpha = mean * tmp
beta = (1 - mean) * tmp
return alpha, beta
def setHalflife(prior, newHalflife):
"Given an Ebisu model, safely time-shift it to any new halflife"
# Step 1: time-shift `prior` to its halflife via GB1 and Beta-fit there
(alpha, beta, t) = prior
oldHalflife = ebisu.modelToPercentileDecay(prior)
dt = oldHalflife / t
logDenominator = betaln(alpha, beta)
logMean = betaln(alpha + dt, beta) - logDenominator
logm2 = betaln(alpha + 2 * dt, beta) - logDenominator
mean = np.exp(logMean)
var = np.exp(logm2) - np.exp(2 * logMean)
newAlpha, newBeta = _meanVarToBeta(mean, var)
# Step 2: <indiana-jones.gif> just swap the true old halflife for new
return (newAlpha, newBeta, newHalflife)
def update_growth2(growth_model, successful, attempts, test_time):
original_model = growth_model[:3]
original_updated_model = ur(original_model, successful, attempts, test_time)
adjustment = m2pd(original_updated_model, 0.5) / m2pd(original_model, 0.5)
new_growth_coefficient = growth_model[3] * adjustment
new_t = new_growth_coefficient * growth_model[2]
return setHalflife(original_updated_model, new_t) + (new_growth_coefficient,)
new_model = model + (2.1,)
last_test_time = 0
rng = random.Random(123)
for i in range(30):
test_time = m2pd(new_model[:3], 0.85)
success = (rng.random() < 0.85)
new_model = update_growth2(new_model, success, 1, test_time)
if True or i > 2:
print(f"{i}: {success} at {round(test_time,3)} :: {new_model}")
last_test_time = test_time
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment