Skip to content

Instantly share code, notes, and snippets.

@philip-sterne
Created September 14, 2018 13:08
Show Gist options
  • Save philip-sterne/73c10eed7f1d5301e3044964d562f7c0 to your computer and use it in GitHub Desktop.
Save philip-sterne/73c10eed7f1d5301e3044964d562f7c0 to your computer and use it in GitHub Desktop.
# This short script ues some hypothetical numbers to calculate whether or not
# to operate on a possibly-cancerous lump, given the results of a screening test
u = {
"healthy":100.0,
"late-stage cancer":-10000.0,
"surgery": -50.0,
}
p = {
"FP": 0.05, # False Positive
"TP": 0.01, # True Positive
"FN": 0.001, # False Negative
"TN": 0.939, # True Negative
}
assert 1.0 == p["FP"]+p["TP"]+p["FN"]+p["TN"]
# Given the various outcomes of the test, we can infer stats on the test:
p["positive"] = p["TP"] + p["FP"]
p["negative"] = p["TN"] + p["FN"]
print("The probability of a positive test result is: %0.3f"%p["positive"])
print("The probability of a negative test result is: %0.3f"%p["negative"])
# Calculate the expected utility of ignoring a positive test:
expected_positive_ignore_utility = p["FP"] * u["healthy"] + p["TP"]*u["late-stage cancer"]
# Calculate the expected utility of operating (given a positive test)
# Also we assume that the surgery is 100% reliable:
expected_positive_operate_utility = u["surgery"]
if expected_positive_ignore_utility > expected_positive_operate_utility:
print("It is better to ignore the positive test result!")
else:
print("It is better to operate with a positive test result!")
expected_positive = max(expected_positive_ignore_utility, expected_positive_operate_utility)
expected_negative_ignore_utility = p["FN"] * u["late-stage cancer"] + p["TN"]*u["healthy"]
# Calculate the expected utility of operating (given a negative test)
# Also we assume that the surgery is 100% reliable:
expected_negative_operate_utility = u["surgery"]
if expected_negative_ignore_utility > expected_negative_operate_utility:
print("It is better to ignore the negative test result!")
else:
print("It is better to operate with a negative test result!")
expected_negative = max(expected_negative_ignore_utility, expected_negative_operate_utility)
expected_test_utility = p["positive"]*expected_positive + p["negative"]*expected_negative
print("The expected utility of the test is: %f" % expected_test_utility)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment