-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmatrix_method.py
210 lines (144 loc) · 8.08 KB
/
matrix_method.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import numpy as np
from scipy.linalg import solve_banded
########## Fixation probability ##########
def create_transition_matrix(pop_size, beta, a, b, c, d):
"""
Parameters
----------
beta
pop_size
a, b, c, d: game
"""
super_diagonal = np.zeros(pop_size - 1, dtype='float')
sub_diagonal = np.zeros(pop_size - 1, dtype='float')
diagonal = np.zeros(pop_size - 1, dtype='float')
for i in range(1, pop_size): # Start at 1 because T_0^+ is zero anyway
idx = i - 1
payoff_A = (a * (i - 1) + b * (pop_size - i)) / (pop_size - 1)
payoff_B = (c * i + d * (pop_size - i - 1)) / (pop_size - 1)
t_plus = i * np.exp(beta * payoff_A) / (i * np.exp(beta * payoff_A) + (pop_size - i) * np.exp(beta * payoff_B)) * (pop_size - i) / pop_size
t_minus = (pop_size - i) * np.exp(beta * payoff_B) / (i * np.exp(beta * payoff_A) + (pop_size - i) * np.exp(beta * payoff_B)) * i / pop_size
super_diagonal[idx] = t_plus
sub_diagonal[idx] = t_minus
diagonal[idx] = -(t_plus + t_minus) # 1 - (t_plus + t_minus)
# put a zero at first position and drop the last element
super_diagonal = np.insert(super_diagonal, 0, 0)
super_diagonal = super_diagonal[0:len(super_diagonal) - 1]
# drop first element and insert zero at the end
sub_diagonal = sub_diagonal[1:len(sub_diagonal)]
sub_diagonal = np.insert(sub_diagonal, len(sub_diagonal), 0)
return np.matrix([super_diagonal, diagonal, sub_diagonal])
def fixation_probability_matrix_based(pop_size, beta, a, b, c, d):
'''
Calculates the fixation probability for a given transition matrix.
Parameters
----------
beta
pop_size
a, b, c, d: game
'''
transition_matrix = create_transition_matrix(pop_size, beta, a, b, c, d)
# last element T_{N-1}^{+} (pop_size-1)
fixating_column = np.zeros(pop_size - 1)
# because j=N-1: (a * ((pop_size - 1) - 1) + b * (pop_size - (pop_size - 1))) / (pop_size - 1)
payoff_A = (a * (pop_size - 2) + b) / (pop_size - 1)
# because j=N-1: (c * (pop_size - 1) + d * (pop_size - (pop_size - 1) - 1)) / (pop_size - 1)
payoff_B = (c * (pop_size - 1)) / (pop_size - 1)
#because (pop_size - (pop_size - 1)) = 1
fixating_column[-1] = -1 * ((pop_size - 1) * np.exp(beta * payoff_A) / ((pop_size - 1) * np.exp(beta * payoff_A) + np.exp(beta * payoff_B)) / (pop_size))
# Careful: solve_banded only for well-mixed, linear complexity. Use
# linalg.solve for other cases
fixation_probability_all_transient = solve_banded((1, 1), transition_matrix, fixating_column)
fixation_probability = fixation_probability_all_transient[0]
return fixation_probability
########## Fixation time ##########
def create_conditional_transition_matrix(pop_size, beta, a, b, c, d,
fixation_probability_all_transient):
"""
This function creates a conditional transition matrix. Each transition probability is weighted by the
ratio of the fixation probabilities of the incoming and outgoing state.
"""
# Insert a zero at the beginning and a 1 at the end
fixation_probability_array = np.insert(fixation_probability_all_transient, 0, 0)
fixation_probability_array = np.insert(fixation_probability_array, len(fixation_probability_array), 1)
super_diagonal = np.zeros(pop_size - 1, dtype='float')
sub_diagonal = np.zeros(pop_size - 1, dtype='float')
diagonal = np.zeros(pop_size - 1, dtype='float')
for i in range(1, pop_size): # Start at 1 because T_0^+ is zero anyway
idx = i - 1
payoff_A = (a * (i - 1) + b * (pop_size - i)) / (pop_size - 1)
payoff_B = (c * i + d * (pop_size - i - 1)) / (pop_size - 1)
t_plus = i * np.exp(beta * payoff_A) / (i * np.exp(beta * payoff_A) + (pop_size - i) * np.exp(beta * payoff_B)) * (pop_size - i) / pop_size
t_minus = (pop_size - i) * np.exp(beta * payoff_B) / (i * np.exp(beta * payoff_A) + (pop_size - i) * np.exp(beta * payoff_B)) * i / pop_size
phi_plus = fixation_probability_array[i + 1]
phi_current = fixation_probability_array[i]
phi_minus = fixation_probability_array[i - 1]
super_diagonal[idx] = t_plus * phi_plus / phi_current
sub_diagonal[idx] = t_minus * phi_minus / phi_current
diagonal[idx] = -(t_plus + t_minus) # 1 - (t_plus + t_minus)
# put a zero at first position and drop the last element
super_diagonal = np.insert(super_diagonal, 0, 0)
super_diagonal = super_diagonal[0:len(super_diagonal) - 1]
# drop first element and insert zero at the end
sub_diagonal = sub_diagonal[1:len(sub_diagonal)]
sub_diagonal = np.insert(sub_diagonal, len(sub_diagonal), 0)
return np.matrix([super_diagonal, diagonal, sub_diagonal])
def transition_matrix_conditional_fixation_time(pop_size, beta, a, b, c, d):
'''
Calculates the conditional fixation time for a given transition matrix.
'''
transition_matrix = create_transition_matrix(
pop_size, beta, a, b, c, d)
fixating_column = np.zeros(pop_size - 1)
payoff_A = (a * ((pop_size - 1) - 1) + b) / (pop_size - 1)
payoff_B = c * (pop_size - 1) / (pop_size - 1)
fixating_column[-1] = -1 * ((pop_size - 1) * np.exp(beta * payoff_A) / ((pop_size - 1) * np.exp(beta * payoff_A) + np.exp(beta * payoff_B)) / pop_size)
fixation_probability_all_transient = solve_banded((1, 1), transition_matrix, fixating_column)
conditional_transition_matrix = create_conditional_transition_matrix(pop_size, beta, a, b, c, d, fixation_probability_all_transient)
solve_column = (-1) * np.ones(pop_size - 1)
fixation_time_all_transient = solve_banded((1, 1), conditional_transition_matrix, solve_column)
fixation_time = fixation_time_all_transient[0]
return fixation_time
def transition_matrix_unconditional_fixation_time(pop_size, beta, a, b, c, d):
'''
Calculates the unconditional fixation time for a given transition matrix.
'''
transition_matrix = create_transition_matrix(pop_size, beta, a, b, c, d)
solve_column = (-1) * np.ones(pop_size - 1)
fixation_time_all_transient = solve_banded((1, 1), transition_matrix, solve_column)
fixation_time = fixation_time_all_transient[0]
return fixation_time
########## Stationary distribution ##########
def create_full_banded_transition_matrix(pop_size, beta, a, b, c, d, mu):
"""
Creates the full transition matrix (i.e. with mutation) in a banded form.
"""
super_diagonal = np.zeros(pop_size + 1, dtype='float')
sub_diagonal = np.zeros(pop_size + 1, dtype='float')
diagonal = np.zeros(pop_size + 1, dtype='float')
N = pop_size
for i in range(0, pop_size+1):
pi_A = (i - 1) / (N - 1) * a + (N - i) / (N - 1) * b
pi_B = i / (N - 1) * c + (N - i - 1) / (N - 1) * d
fit_A = np.exp(beta * pi_A)
fit_B = np.exp(beta * pi_B)
t_plus = (i*fit_A) / (i*fit_A+(N-i)*fit_B) * (N-i) / N * (1-mu) + (N-i)*fit_B / (i*fit_A+(N-i)*fit_B) * (N-i)/N * mu
t_minus = ((N-i)*fit_B) / (i*fit_A+(N-i)*fit_B) * i / N * (1-mu) + i*fit_A / (i*fit_A+(N-i)*fit_B) * i/N * mu
super_diagonal[i] = t_plus
sub_diagonal[i] = t_minus
diagonal[i] = -(t_plus + t_minus)
matrix_transposed = np.matrix([sub_diagonal, diagonal, super_diagonal])
return matrix_transposed
def matrix_stationary_remove_equation(pop_size, beta, a, b, c, d, mu):
full_banded_transition_matrix = create_full_banded_transition_matrix(pop_size, beta, a, b, c, d, mu)
# remove first row and column (=remove first column of band matrix and set
# first super_diagnoal to zero):
cropped_matrix = full_banded_transition_matrix[:, 1:pop_size+1]
cropped_matrix[0, 0] = 0
solving_column = np.zeros(pop_size, dtype='float')
solving_column[0] = -mu
cropped_distribution = solve_banded((1, 1), cropped_matrix, solving_column)
stationary_distribution = np.concatenate((np.array([1.0]), cropped_distribution), axis=0)
sum_stationary = np.sum(stationary_distribution)
stationary_distribution = stationary_distribution/sum_stationary
return stationary_distribution