Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
def calculate_character_probability(character, questions_so_far, answers_so_far):
# Prior
P_character = 1 / len(characters)
# Likelihood
P_answers_given_character = 1
P_answers_given_not_character = 1
for question, answer in zip(questions_so_far, answers_so_far):
P_answers_given_character *= max(
1 - abs(answer - character_answer(character, question)), 0.01)
P_answer_not_character = np.mean([1 - abs(answer - character_answer(not_character, question))
for not_character in characters
if not_character['name'] != character['name']])
P_answers_given_not_character *= max(P_answer_not_character, 0.01)
# Evidence
P_answers = P_character * P_answers_given_character + \
(1 - P_character) * P_answers_given_not_character
# Bayes Theorem
P_character_given_answers = (
P_answers_given_character * P_character) / P_answers
return P_character_given_answers
def character_answer(character, question):
if question in character['answers']:
return character['answers'][question]
return 0.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment