

total = 1000000
f_luke = 5/1000
f_leuk = 1.4/100

tot_luke = round(f_luke * total)
tot_not_luke = round((1 - f_luke) * total)

tot_leuk = round(f_leuk * total)
tot_no_leuk = round((1 - f_leuk) * total)

tot_luke_leuk = round(f_leuk * tot_luke )
tot_luke_no_leuk = round((1 - f_leuk) * tot_luke )

tot_not_luke_leuk = round(f_leuk * tot_not_luke )
tot_not_luke_no_leuk = round((1 - f_leuk) * tot_not_luke )

print()
print("               Leukemia     no Leukemia     total")
print("   Luke       ", tot_luke_leuk, "         ", tot_luke_no_leuk, 
      "          ", tot_luke)
print("   Not Luke   ", tot_not_luke_leuk, "      ", tot_not_luke_no_leuk, 
      "        ", tot_not_luke)
print("   total      ", tot_leuk, "      ", tot_no_leuk, 
      "        ", total)



def accuracy(tp, fp, fn, tn):
    correct = tp + tn
    total = tp + fp + fn + tn
    return correct / total

print()
print("Accuracy = ", end="")
print( accuracy(tot_luke_leuk, tot_luke_no_leuk,
                tot_not_luke_leuk, tot_not_luke_no_leuk)
     )


def precision(tp, fp, fn, tn):
    return tp / (tp + fp)

print()
print("Precision = ", end="")
print( precision(tot_luke_leuk, tot_luke_no_leuk,
                 tot_not_luke_leuk, tot_not_luke_no_leuk)
     )


# A high recall value means there were very few false negatives and 
# that the classifier is more permissive in the criteria for classifying 
# something as positive.

# Models need high recall when you need output-sensitive predictions. 
# For example, predicting cancer or predicting terrorists needs a high recall, 
# in other words, you need to cover false negatives VERY WELL.

def recall(tp, fp, fn, tn):
    return tp / (tp + fn)

print()
print("Recall = ", end="")
print( recall(tot_luke_leuk, tot_luke_no_leuk,
              tot_not_luke_leuk, tot_not_luke_no_leuk)
     )

# This is the harmonic mean of precision and recall and 
# the value lies between them.
def f1_score(tp, fp, fn, tn):
    p = precision(tp, fp, fn, tn)
    r = recall(tp, fp, fn, tn)
    return 2 * p * r / (p + r)

print()
print("f1-score = ", end="")
print( f1_score(tot_luke_leuk, tot_luke_no_leuk,
                tot_not_luke_leuk, tot_not_luke_no_leuk)
     )


print("high recall but a low precision ==> ")
print('     A model that predicts “yes” when it is even a little bit confident')


