Skip to content

Commit

Permalink
Fix edge case where cumprob[end] < 1
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesCranmer committed Mar 9, 2022
1 parent e792776 commit da3b197
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Population.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,15 @@ function bestOfSample(pop::Population, options::Options)::PopMember
prob_each = p * (1 - p) .^ k
prob_each /= sum(prob_each)
cumprob = cumsum(prob_each)
chosen_idx = sort_idx[findfirst(cumprob .> rand())]
raw_chosen_idx = findfirst(cumprob .> rand())

# Sometimes, due to precision issues, we might have cumprob[end] < 1,
# so we must check for nothing returned:
if raw_chosen_idx === nothing
chosen_idx = sort_idx[end]
else
chosen_idx = sort_idx[raw_chosen_idx]
end
end
return sample.members[chosen_idx]
end
Expand Down

0 comments on commit da3b197

Please sign in to comment.