Created
April 9, 2018 21:09
-
-
Save seanlynch/f2c70e2c7f1ae072f7f8efa91878e6c7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import random | |
def truth(): | |
return random.randrange(3) == 2 | |
# A, B and C toot the truth with a probability of 1/3 and lie with a probability of 2/3. | |
# A makes a toot and B toots an observation on whether or not A was tooting the truth or not. C toots that B confirmed A was truthful. | |
# What is the actual probability that A's toot was truthful? | |
def simulate(rounds=10000000): | |
# All probabilities are probability of truthfulness | |
count_total = count_true_when_c = count_false_when_c = 0 | |
for i in range(rounds): | |
a_is_truthful = truth() | |
b_is_truthful = truth() | |
c_is_truthful = truth() | |
# Did B confirm what A said? | |
b_confirmed_a = (a_is_truthful and b_is_truthful) or (not a_is_truthful and not b_is_truthful) | |
# Did C say B confirmed what A said? | |
c_said_it = (b_confirmed_a and c_is_truthful) or (not b_confirmed_a and not c_is_truthful) | |
count_total += 1 | |
if c_said_it: | |
if a_is_truthful: | |
count_true_when_c += 1 | |
else: | |
count_false_when_c += 1 | |
return count_total, count_true_when_c, count_false_when_c | |
def main(): | |
print(simulate()) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment