-
Notifications
You must be signed in to change notification settings - Fork 0
/
heredity.py
177 lines (148 loc) · 4.87 KB
/
heredity.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import csv
import itertools
import sys
PROBS = {
"gene": {
2: 0.01,
1: 0.03,
0: 0.96
},
"trait": {
2: {
True: 0.65,
False: 0.35
},
1: {
True: 0.56,
False: 0.44
},
0: {
True: 0.01,
False: 0.99
}
},
"mutation": 0.01
}
def main():
if len(sys.argv) != 2:
sys.exit("Usage: python heredity.py data.csv")
people = load_data(sys.argv[1])
probabilities = {
person: {
"gene": {
2: 0,
1: 0,
0: 0
},
"trait": {
True: 0,
False: 0
}
}
for person in people
}
names = set(people)
for have_trait in powerset(names):
fails_evidence = any(
(people[person]["trait"] is not None and
people[person]["trait"] != (person in have_trait))
for person in names
)
if fails_evidence:
continue
for one_gene in powerset(names):
for two_genes in powerset(names - one_gene):
p = joint_probability(people, one_gene, two_genes, have_trait)
update(probabilities, one_gene, two_genes, have_trait, p)
normalize(probabilities)
for person in people:
print(f"{person}:")
for field in probabilities[person]:
print(f" {field.capitalize()}:")
for value in probabilities[person][field]:
p = probabilities[person][field][value]
print(f" {value}: {p:.4f}")
def load_data(filename):
data = dict()
with open(filename) as f:
reader = csv.DictReader(f)
for row in reader:
name = row["name"]
data[name] = {
"name": name,
"mother": row["mother"] or None,
"father": row["father"] or None,
"trait": (True if row["trait"] == "1" else
False if row["trait"] == "0" else None)
}
return data
def powerset(s):
s = list(s)
return [
set(s) for s in itertools.chain.from_iterable(
itertools.combinations(s, r) for r in range(len(s) + 1)
)
]
def joint_probability(people, one_gene, two_genes, have_trait):
probability = 1
for person in people:
genes = (
2 if person in two_genes else
1 if person in one_gene else
0
)
trait = person in have_trait
if people[person]["mother"] is None and people[person]["father"] is None:
gene_prob = PROBS["gene"][genes]
else:
mother = people[person]["mother"]
father = people[person]["father"]
mother_genes = (
2 if mother in two_genes else
1 if mother in one_gene else
0
)
father_genes = (
2 if father in two_genes else
1 if father in one_gene else
0
)
def get_gene_prob(parent_genes, is_mutation):
if parent_genes == 2:
return 1 - PROBS["mutation"] if not is_mutation else PROBS["mutation"]
elif parent_genes == 1:
return 0.5
else:
return PROBS["mutation"] if not is_mutation else 1 - PROBS["mutation"]
if genes == 2:
gene_prob = get_gene_prob(mother_genes, False) * get_gene_prob(father_genes, False)
elif genes == 1:
gene_prob = (
get_gene_prob(mother_genes, False) * get_gene_prob(father_genes, True) +
get_gene_prob(mother_genes, True) * get_gene_prob(father_genes, False)
)
else:
gene_prob = get_gene_prob(mother_genes, True) * get_gene_prob(father_genes, True)
trait_prob = PROBS["trait"][genes][trait]
probability *= gene_prob * trait_prob
return probability
def update(probabilities, one_gene, two_genes, have_trait, p):
for person in probabilities:
genes = (
2 if person in two_genes else
1 if person in one_gene else
0
)
trait = person in have_trait
probabilities[person]["gene"][genes] += p
probabilities[person]["trait"][trait] += p
def normalize(probabilities):
for person in probabilities:
gene_total = sum(probabilities[person]["gene"].values())
trait_total = sum(probabilities[person]["trait"].values())
for gene in probabilities[person]["gene"]:
probabilities[person]["gene"][gene] /= gene_total
for trait in probabilities[person]["trait"]:
probabilities[person]["trait"][trait] /= trait_total
if __name__ == "__main__":
main()