Skip to content

Commit

Permalink
Modifying test simulations
Browse files Browse the repository at this point in the history
  • Loading branch information
John Hawkins authored and John Hawkins committed Apr 13, 2021
1 parent 0bdcf90 commit daf3946
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
8 changes: 4 additions & 4 deletions minvime/estimator_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ def estimate_binary_model_requirements(tp, fp, tn, fn, cases, baserate, minroi=0
current_min_roi = roi
tprs = y
combinations = combinations + 1
print("Tested ", combinations, " different AUC plots")
print("Number of Exponents", len(beta_range))
print("Number of Alpha Weights", len(alpha_range))
#print("Tested ", combinations, " different AUC plots")
#print("Number of Exponents", len(beta_range))
#print("Number of Alpha Weights", len(alpha_range))
return min_auc, min_precision, min_recall, np.array(fprates), tprs

######################################################################
Expand All @@ -91,7 +91,7 @@ def generate_roc_auc(fprates, alpha, beta):

def calculate_peak_roi(fprates, tprates, tp, fp, tn, fn, num_pos, num_neg):
""" Calculate the maximal ROI for a given ROC curve (defined by vectors of FPR and TPR) """
roi = -999999
roi = -99999999999999
result_precision = 0.0
result_recall = 0.0
for index in range(len(fprates)):
Expand Down
54 changes: 54 additions & 0 deletions scripts/run_churn_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Churn Simulation
We explore properties of a minimum viable model for churn problems with different criteria.
In a churn situation every False Negative involves the cost of losing a customer.
The cost of a False Positive is the cost of an uneccessary intervention.
A True Positive means that you have a chance of mitigating the churn.
Therefore the impact of a model making a True Positive is a reduction in the expected
loss of a Churn.
For example, if churn involves a $10,000 loss per customer, and the
probability of an intervention working is 20%. Then True Positive reduces the expected
loss from $10,000 to $8,000. Or the model delivered $2,000 worth of value.
This script will produce a table of model performance criteria for problems of
varying definitions in terms of the costs of both churn and intervention.
"""

import sys
import pandas as pd

sys.path.append('../minvime')
import estimator_classification as esti

fns = [-10000,-8000,-6000,-4000,-2000,-1000]
fps = [-500,-400,-300,-200,-100]
success_rate = 0.2

tn = 0
roi = 500000
cases = 1000000
baserate = 0.005

rez = pd.DataFrame()

for fn in fns:
for fp in fps:
# A true positive is a reduction in expected loss (after considering intervention cost)
tp = fn - (fn * success_rate) + fp
current_churn_loss = cases * baserate * fn
# To deliver ROI we need to lift ROI from the current churn losses.
minroi = current_churn_loss + roi
auc, prec, recall, fprs, tprs = esti.estimate_binary_model_requirements(
tp=tp, fp=fp, tn=tn, fn=fn, cases=cases,
baserate=baserate, minroi=minroi
)
rez = rez.append({'Total Loss':current_churn_loss, 'Loss p Cust':fn, 'Intvn Cost':fp, 'auc':auc, 'precision':prec, 'recall':recall}, ignore_index=True)

print("Churn Model Requirements")
print("Criteria | Min ROI $%i | Cases %i | Base Churn Rate %f " % (roi,cases,baserate) )
print(rez)

12 changes: 6 additions & 6 deletions scripts/run_simulation_one.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
sys.path.append('../minvime')
import estimator_classification as esti # The file ../minvime/estimator_classification.py

tps = [20000,10000,8000,6000,4000,2000,1000,500]
fps = [-1000,-900,-800,-600,-500,-400,-200,-100]
tps = [20000,10000,8000,6000,4000,2000,1000]
fps = [-900,-800,-600,-500,-400,-200,-100]

tn = 0
fn = 0
Expand All @@ -17,12 +17,12 @@

for tp in tps:
for fp in fps:
auc, prec, recall = esti.estimate_binary_model_requirements(
auc, prec, recall, fprs, tprs = esti.estimate_binary_model_requirements(
tp=tp, fp=fp, tn=tn, fn=fn, cases=cases,
baserate=baserate, minroi=minroi
)
print({'tp':tp, 'fp':fp, 'auc':auc, 'precision':prec, 'recall':recall})
rez = rez.append({'tp':tp, 'fp':fp, 'auc':auc, 'precision':prec, 'recall':recall}, ignore_index=True)

rez = rez.append({'TP Benefit':tp, 'FP Cost':fp, 'AUC':auc, 'precision':prec, 'recall':recall}, ignore_index=True)
print("Model Requirements")
print("Criteria | Min ROI $%i | Cases %i | Base Churn Rate %f " % (minroi,cases,baserate) )
print(rez)

0 comments on commit daf3946

Please sign in to comment.