-
Notifications
You must be signed in to change notification settings - Fork 0
/
analyze-autosave-distn.py
116 lines (104 loc) · 2.92 KB
/
analyze-autosave-distn.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
"""
Do things with autosaved stationary distributions.
"""
import sys
import numpy as np
import matplotlib
import matplotlib.pyplot as plt
import multinomstate
def plot_reciprocal_variance_AB_given_AB_plus_Ab(rows):
filename = 'reciprocal_variance_AB_given_AB_plus_Ab.png'
N = sum(rows[0][:-1])
variances = []
for AB_plus_Ab in range(1, N+1):
m = np.zeros(AB_plus_Ab+1)
for AB, Ab, aB, ab, p in rows:
if AB + Ab == AB_plus_Ab:
m[AB] += p
m /= np.sum(m)
x_values = np.linspace(0, 1, AB_plus_Ab+1)
mu = np.dot(m, x_values)
if not np.allclose(mu, 0.5):
raise Exception
v = np.dot(m, (x_values - mu)**2)
variances.append(np.reciprocal(v))
xs = np.arange(1, N+1)
ys = variances
plt.plot(xs, ys)
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 2*y2))
plt.savefig(filename)
plt.close()
def plot_AB_given_half_AB_plus_Ab(rows):
N = sum(rows[0][:-1])
Nd2 = N // 2
filename = 'AB_given_%d_AB_plus_Ab.png' % Nd2
m = np.zeros(Nd2 + 1)
for AB, Ab, aB, ab, p in rows:
if AB + Ab == Nd2:
m[AB] += p
m /= np.sum(m)
xs = np.arange(Nd2 + 1)
ys = m
plt.plot(xs, ys)
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 2*y2))
plt.savefig(filename)
plt.close()
def plot_AB_given_only_AB_and_Ab(rows):
N = sum(rows[0][:-1])
filename = 'AB_given_%d_AB_plus_Ab.png' % N
m = np.zeros(N+1)
for AB, Ab, aB, ab, p in rows:
if AB + Ab == N:
m[AB] += p
m /= np.sum(m)
xs = np.arange(N+1)
ys = m
plt.plot(xs, ys)
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 2*y2))
plt.savefig(filename)
plt.close()
def plot_AB_plus_Ab(rows):
filename = 'AB_plus_Ab.png'
N = sum(rows[0][:-1])
m = np.zeros(N+1)
for AB, Ab, aB, ab, p in rows:
m[AB+Ab] += p
m /= np.sum(m)
xs = np.arange(N+1)
ys = m
plt.plot(xs, ys)
x1, x2, y1, y2 = plt.axis()
plt.axis((x1, x2, 0, 2*y2))
plt.savefig(filename)
plt.close()
def main():
k = 4
print 'reading state distribution from stdin...'
rows = []
for line in sys.stdin.readlines():
AB, Ab, aB, ab, p = line.split()
row = [int(AB), int(Ab), int(aB), int(ab), float(p)]
rows.append(row)
N = sum(rows[0][:-1])
print 'defining the state vectors...'
M = np.array(list(multinomstate.gen_states(N, k)), dtype=int)
m_AB_ab = np.zeros(N+1)
m_AB_Ab = np.zeros(N+1)
for AB, Ab, aB, ab, p in rows:
m_AB_ab[AB+ab] += p
m_AB_Ab[AB+Ab] += p
print 'marginal distribution of AB+ab:'
print m_AB_ab
print
print 'marginal distribution of AB+Ab:'
print m_AB_Ab
print
plot_AB_plus_Ab(rows)
plot_AB_given_only_AB_and_Ab(rows)
plot_AB_given_half_AB_plus_Ab(rows)
plot_reciprocal_variance_AB_given_AB_plus_Ab(rows)
if __name__ == '__main__':
main()