1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/local/bin/python3

# In a group with an anomalous subgroup, calculate the ratio of the
# subgroup having some property to the rest of the group having that
# same property.

def calculate_ratio(odd_amount, odd_total, all_amount, all_total):
	odd_frac = odd_amount / odd_total
	rest_frac = (all_amount - odd_amount) / (all_total - odd_total)

	ratio = odd_frac / rest_frac

	print("""Odd ratio = {0:.3}%, Rest ratio = {1:.3f}%
Odd factor = {2:.2f}""".format(100 * odd_frac, 100 * rest_frac, ratio))

if __name__ == '__main__':
	# This example compares the odds of having died from coronavirus in
	# the US to the odds of having died from coronavirus in the rest of
	# the world as of 2021-04-27.

	# Deaths in the US and population of the US
	odd_amount, odd_total = 571_753, 3.29e8
	# Deaths in the whole world and world population
	all_amount, all_total = 3_110_124, 7.799e9
	calculate_ratio(odd_amount, odd_total, all_amount, all_total)

    # Results:
    # Odd ratio = 0.174%, Rest ratio = 0.034%
    # Odd factor = 5.11