Skip to content

Instantly share code, notes, and snippets.

@elisohl-ncc
Created June 14, 2023 00:00
Show Gist options
  • Save elisohl-ncc/14d9df54616e43063266717650a4be63 to your computer and use it in GitHub Desktop.
Save elisohl-ncc/14d9df54616e43063266717650a4be63 to your computer and use it in GitHub Desktop.
@staticmethod
def bayes(h, e_given_h, e_given_not_h):
"""Update the posterior probability of h given e.
e: evidence
h: hypothesis
e_given_h: probability of e given h
e_given_not_h: probability of e given not h
"""
return e_given_h * h / (e_given_h * h + e_given_not_h * (1 - h))
@staticmethod
def get_updated_confidences(confidences, index, result):
new_confidences = confidences[:] # shallow copy
for j in range(256):
p_h = confidences[j]
if index == j:
p_e_given_h = 1 - FN_RATE if result else FN_RATE
p_e_given_not_h = FP_RATE if result else 1 - FP_RATE
else:
p_e_given_h = FP_RATE if result else 1 - FP_RATE
p_hi_given_not_hj = confidences[index] / (1 - confidences[j])
p_not_hi_given_not_hj = 1 - p_hi_given_not_hj
if result:
p_e_given_not_h = p_hi_given_not_hj * (1 - FN_RATE) + p_not_hi_given_not_hj * FP_RATE
else:
p_e_given_not_h = p_hi_given_not_hj * FN_RATE + p_not_hi_given_not_hj * (1 - FP_RATE)
new_confidences[j] = ByteSearch.bayes(p_h, p_e_given_h, p_e_given_not_h)
return new_confidences
@staticmethod
def get_entropy(dist):
return -sum(p * log2(p) for p in dist if p)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment