-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfits.py
More file actions
210 lines (177 loc) · 7.01 KB
/
fits.py
File metadata and controls
210 lines (177 loc) · 7.01 KB
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
# Original code 2024-2025 by Oyin Adenekan
# edited by Peter Kasson
"""functions for MCMC sampling of hypoexponential distributions."""
import numpy as np
import scipy
# gamma pdf
def gamma(k, N, x):
return ((k**N)*(x**(N-1)))*np.exp(-k*x)/scipy.special.factorial(N) # formula for convolution of N exponentials, 1 k for each step
def simulate_gamma(k, N, num_times=300, upper_time_bound=500):
# set things up
lower_time_bound = 0
dwell_times = np.zeros((num_times))
prob_dwell_times = np.zeros((num_times))
successes = []
# perform simulations
total_iters = 0
idx = 0
while idx < num_times:
# R = np.random.rand(lower_time_bound, upper_time_bound) # propose a dwell time in the range of ks
R = np.random.random_sample()*upper_time_bound # propose a dwell time in the range of ks
prob_R = gamma(k, N, R) # get probability of dwell time according to pdf
# print(R, prob_R)
prob_accept = np.random.rand()
if prob_R > prob_accept:
# print(idx, 'success !')
dwell_times[idx] = R
prob_dwell_times[idx] = prob_R
idx = idx + 1
successes.append(True)
else:
successes.append(False)
# total_iters = total_iters + 1
return dwell_times
# calculate hyperexponential pdf
def conv_1_exps(rates, t):
return rates[0]*np.exp(-rates[0]*t)
def conv_2_exps(rates, t):
if rates[0] == rates[1]:
return rates[0]*rates[1]*t*np.exp(-rates[0]*t)
else:
return ((rates[0]*rates[1])/(rates[1]-rates[0]))*(np.exp(-rates[0]*t)-np.exp(-rates[1]*t))
def conv_3_exps(rates, t):
first_term = (rates[0]*rates[1]*rates[2])/(rates[1]-rates[0])
second_term_1 = (np.exp(-rates[0]*t)-np.exp(-rates[2]*t))/(-rates[0]+rates[2])
second_term_2 = (np.exp(-rates[1]*t)-np.exp(-rates[2]*t))/(-rates[1]+rates[2])
return first_term*(second_term_1 - second_term_2)
def conv_4_exps(rates, t):
k1 = rates[0]
k2 = rates[1]
k3 = rates[2]
k4 = rates[3]
pre_num = k1*k2*k3*k4
mid_term = (np.exp(-k1*t) - np.exp(-k3*t))/(k3 - k1) - (np.exp(-k2*t) - np.exp(-k3*t))/(k3 - k2)
k4_term = 1/k4 - np.exp(-k4*t)/k4
big_denom = k2-k1
return (pre_num*mid_term*k4_term)/big_denom
def conv_5_exps(rates, t):
k1 = rates[0]
k2 = rates[1]
k3 = rates[2]
k4 = rates[3]
k5 = rates[4]
pre_term = k1*k2*k3*k4*k5
first_mid = (np.exp(-k1*t) - np.exp(-k3*t))/(k3 - k1)
second_mid = (np.exp(-k2*t) - np.exp(-k3*t))/(k3 - k2)
k4_term = 1/k4 - np.exp(-k4*t)/k4
k5_term = 1/k5 - np.exp(-k5*t)/k5
big_denom = k2-k1
return pre_term* (first_mid - second_mid) * k4_term * k5_term / big_denom
# simulate dwell times for a given set of rates
def simulate_conv_exps(convolution_func, rates, num_times=300, upper_time_bound=3000):
# set things up
lower_time_bound = 0
dwell_times = np.zeros((num_times))
prob_dwell_times = np.zeros((num_times))
successes = []
total_iters = 0
idx = 0
while idx < num_times:
# R = np.random.rand(lower_time_bound, upper_time_bound) # propose a dwell time in the range of ks
R = np.random.random_sample()*upper_time_bound # propose a dwell time in the range of ks
prob_R = convolution_func(rates, R) # get probability of dwell time according to pdf
# print(R, prob_R)
prob_accept = np.random.rand()
if prob_R > prob_accept:
# print(idx, 'success !')
dwell_times[idx] = R
prob_dwell_times[idx] = prob_R
idx = idx + 1
successes.append(True)
else:
successes.append(False)
# total_iters = total_iters + 1
return dwell_times
# perform mcmc
def do_mcmc(num_iters, num_rates, datas, conv_func, bounds = [0, 1]):
# initialize values
low_bound = bounds[0]
high_bound = bounds[1]
k_current = np.random.uniform(low=low_bound, high=high_bound, size=num_rates)
temperature = 1
# variables for for loop
ks = np.zeros((num_rates, num_iters))
# tracking accepts
accepts = np.zeros((num_rates, num_iters))
accepts[:, 0] = 1
# accept_interval_size = 500
# delta_std_dev = 0.3
# tracking proposals
likelihood_curve = np.zeros((num_iters))
likelihood_curve[0] = np.sum(np.log(conv_func(k_current, datas)))
ks[:,0] = k_current
proposals = np.zeros((num_rates, num_iters))
proposals[:, 0] = k_current
# track accept probabilities
accept_probs = np.zeros((num_rates, num_iters-1))
print_flag = 1
for i in range(1, num_iters):
if i > 100:
print_flag = 0
if i % 100 == 0:
print_flag = 1
# if print_flag: print('iteration', i)
for idx in range(num_rates):
# if print_flag: print('which k: {}'.format(idx+1))
# calculate the likelihoods, for new and old
# if print_flag: print('current')
# if print_flag: print('k1: {}, k2: {}, k3: {}'.format(k_current[0], k_current[1], k_current[2]))
prev_likelihood = np.sum(np.log(conv_func(k_current, datas)))
# if print_flag: print('current likelihood:', prev_likelihood)
# propose a proposal
k_proposed = np.random.gamma(shape=1, scale=k_current[idx])
proposals[idx, i] = k_proposed
# update current list and keep pre proposal number
pre_proposal_rate = k_current[idx]
k_current[idx] = k_proposed
# k_proposed = np.random.gamma(this_k, 10)
# if print_flag: print('proposed')
# if print_flag: print('k1: {}, k2: {}, k3: {}'.format(k_current[0], k_current[1], k_current[2]))
new_likelihood = np.sum(np.log(conv_func(k_current, datas)))
# if print_flag: print('proposed likelihood:', new_likelihood)
accept_ratio = np.exp(new_likelihood - prev_likelihood)
# accept_ratio = new_likelihood/prev_likelihood
# if print_flag: print('accept ratio: {}'.format(accept_ratio))
prob_accept = accept_ratio if accept_ratio < 1 else 1
accept_probs[idx, i-1] = accept_ratio
# if print_flag: print('calculated accept prob: {}, actual accept prob: {}'.format(accept_ratio, prob_accept))
if accept_ratio**temperature >= np.random.uniform():
ks[idx, i] = k_proposed
likelihood_curve[i] = new_likelihood
# k_current[idx] = k_proposed
accepts[idx, i] = 1
else:
k_current[idx] = pre_proposal_rate
ks[idx, i] = k_current[idx]
likelihood_curve[i] = prev_likelihood
return ks, likelihood_curve
# dwell times to ecdf
def dwell_times_to_ecdf(dwell_times):
num_dwell_times = len(dwell_times)
dwell_times_sorted_args = np.argsort(dwell_times)
dwell_times_sorted = dwell_times[dwell_times_sorted_args]
# time_points = np.linspace(0, np.max(dwell_times), num_)
ecdf = np.zeros((num_dwell_times))
for idx in range(num_dwell_times):
num_fused = len(dwell_times_sorted[dwell_times_sorted <= dwell_times_sorted[idx]])
ecdf[idx] = num_fused/num_dwell_times
return ecdf
# dwell times, w independent max value, to ecdf
def dwell_times_to_ecdf_independent_max(dwell_times, max_time, length):
# create an x-axis based on given max time
x_axis = np.linspace(0, max_time, length)
ecdf = np.zeros((length))
for idx in range(len(x_axis)):
num_dwell_times_in_interval = len(dwell_times[dwell_times <= x_axis[idx]])
ecdf[idx] = num_dwell_times_in_interval/length
return x_axis, ecdf