Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syncing sweeps implementation in algorithms.py #2247

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 19 additions & 8 deletions algorithms.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ def _reset(self):
self._times = []

def _genic_selection_stochastic_forwards(self, dt, freq, alpha):
ux = (alpha * freq * (1 - freq)) / np.tanh(alpha * freq)
ux = (alpha / 2.0 * freq * (1 - freq)) / np.tanh(alpha / 2.0 * freq)
sign = 1 if random.random() < 0.5 else -1
freq += (ux * dt) + sign * np.sqrt(freq * (1.0 - freq) * dt)
return freq
Expand Down Expand Up @@ -1022,36 +1022,47 @@ def single_sweep_simulate(self):

# main loop time
t_inc_orig = self.time_slice
t_start = self.t
e_time = 0.0
while self.ancestors_remain() and sweep_traj_step < len(times) - 1:
self.verify()
event_prob = 1.0
while event_prob > random.random() and sweep_traj_step < len(times) - 1:
sweep_traj_step += 1
x = allele_freqs[sweep_traj_step]
e_time += times[sweep_traj_step]
e_time = times[sweep_traj_step] * 2 * self.P[0].start_size
# self.t = self.t + times[sweep_traj_step]
sweep_pop_sizes = [
self.P[0].get_num_ancestors(label=0),
self.P[0].get_num_ancestors(label=1),
]
p_rec_b = self.get_total_recombination_rate(0) * t_inc_orig
p_rec_B = self.get_total_recombination_rate(1) * t_inc_orig
p_rec_b = (
self.get_total_recombination_rate(0)
* t_inc_orig
* 2
* self.P[0].start_size
)
p_rec_B = (
self.get_total_recombination_rate(1)
* t_inc_orig
* 2
* self.P[0].start_size
)

# JK NOTE: We should probably factor these pop size calculations
# into a method in Population like get_common_ancestor_waiting_time().
# That way we can handle exponentially growing populations as well?
p_coal_b = (
(sweep_pop_sizes[0] * (sweep_pop_sizes[0] - 1))
* 0.5
/ (1.0 - x)
* t_inc_orig
/ self.P[0].start_size
)
p_coal_B = (
(sweep_pop_sizes[1] * (sweep_pop_sizes[1] - 1))
* 0.5
/ x
* t_inc_orig
/ self.P[0].start_size
)
sweep_pop_tot_rate = p_rec_b + p_rec_B + p_coal_b + p_coal_B

Expand All @@ -1062,12 +1073,12 @@ def single_sweep_simulate(self):

if total_rate == 0:
break
if self.t + e_time > self.modifier_events[0][0]:
if t_start + e_time > self.modifier_events[0][0]:
t, func, args = self.modifier_events.pop(0)
self.t = t
func(*args)
else:
self.t += e_time
self.t = t_start + e_time
# choose which event happened
if random.random() < sweep_pop_tot_rate / total_rate:
# even in sweeping pop, choose which kind
Expand Down
Loading