Skip to content

Commit 572bf46

Browse files
committed
Rename "reduce" to "ratio"
To reflect that we are actually calculating likelihood ratios here (dividing in linear space)
1 parent 4e5de92 commit 572bf46

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

tsdate/core.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,10 @@ def n_breaks(self, edge):
344344
def combine(self, lik_1, lik_2):
345345
return lik_1 * lik_2
346346

347-
def reduce(self, lik_1, lik_2, div_0_null=False):
347+
def ratio(self, lik_1, lik_2, div_0_null=False):
348348
"""
349-
In linear space, this divides lik_1 by lik_2
349+
Return the ratio of lik_1 to lik_2. In linear space, this divides lik_1 by lik_2
350350
If div_0_null==True, then 0/0 is set to the null_constant
351-
352-
NB: "reduce" is not a very good name for the function: can we think of
353-
something better that will also be meaningful in log space?
354351
"""
355352
with np.errstate(divide="ignore", invalid="ignore"):
356353
ret = lik_1 / lik_2
@@ -487,9 +484,9 @@ def _recombination_loglik(self, edge, fixed=True):
487484
def combine(self, loglik_1, loglik_2):
488485
return loglik_1 + loglik_2
489486

490-
def reduce(self, loglik_1, loglik_2, div_0_null=False):
487+
def ratio(self, loglik_1, loglik_2, div_0_null=False):
491488
"""
492-
In log space, loglik_1 - loglik_2
489+
In log space, likelihood ratio is loglik_1 - loglik_2
493490
If div_0_null==True, then if either is -inf it returns -inf (the null_constant)
494491
"""
495492
with np.errstate(divide="ignore", invalid="ignore"):
@@ -686,9 +683,9 @@ def inside_pass(self, *, standardize=True, cache_inside=False, progress=None):
686683
denominator[parent] = (
687684
np.max(val) if standardize else self.lik.identity_constant
688685
)
689-
inside[parent] = self.lik.reduce(val, denominator[parent])
686+
inside[parent] = self.lik.ratio(val, denominator[parent])
690687
if cache_inside:
691-
self.g_i = self.lik.reduce(
688+
self.g_i = self.lik.ratio(
692689
g_i, denominator[self.ts.tables.edges.child, None]
693690
)
694691
# Keep the results in this object
@@ -749,16 +746,16 @@ def outside_pass(
749746
# but is an approximation when times are unknown.
750747
spanfrac = edge.span / self.spans[child]
751748
try:
752-
inside_div_gi = self.lik.reduce(
749+
inside_div_gi = self.lik.ratio(
753750
self.inside[edge.parent], self.g_i[edge.id], div_0_null=True
754751
)
755752
except AttributeError: # we haven't cached g_i so we recalculate
756753
daughter_val = self.lik.scale_geometric(
757754
spanfrac, self.lik.make_lower_tri(self.inside[edge.child])
758755
)
759756
edge_lik = self.lik.get_inside(daughter_val, edge)
760-
cur_g_i = self.lik.reduce(edge_lik, self.denominator[child])
761-
inside_div_gi = self.lik.reduce(
757+
cur_g_i = self.lik.ratio(edge_lik, self.denominator[child])
758+
inside_div_gi = self.lik.ratio(
762759
self.inside[edge.parent], cur_g_i, div_0_null=True
763760
)
764761
parent_val = self.lik.scale_geometric(
@@ -768,15 +765,15 @@ def outside_pass(
768765
),
769766
)
770767
if standardize:
771-
parent_val = self.lik.reduce(parent_val, np.max(parent_val))
768+
parent_val = self.lik.ratio(parent_val, np.max(parent_val))
772769
edge_lik = self.lik.get_outside(parent_val, edge)
773770
val = self.lik.combine(val, edge_lik)
774771

775772
# vv[0] = 0 # Seems a hack: internal nodes should be allowed at time 0
776773
assert self.denominator[edge.child] > self.lik.null_constant
777-
outside[child] = self.lik.reduce(val, self.denominator[child])
774+
outside[child] = self.lik.ratio(val, self.denominator[child])
778775
if standardize:
779-
outside[child] = self.lik.reduce(val, np.max(val))
776+
outside[child] = self.lik.ratio(val, np.max(val))
780777
self.outside = outside
781778
posterior = outside.clone_with_new_data(
782779
grid_data=self.lik.combine(self.inside.grid_data, outside.grid_data),
@@ -829,7 +826,7 @@ def outside_maximization(self, *, eps, progress=None):
829826
* self.lik.mut_rate
830827
* edge.span,
831828
)
832-
result = self.lik.reduce(ll_mut, np.max(ll_mut))
829+
result = self.lik.ratio(ll_mut, np.max(ll_mut))
833830
else:
834831
cur_parent_index = maximized_node_times[edge.parent]
835832
if cur_parent_index < youngest_par_index:
@@ -846,7 +843,7 @@ def outside_maximization(self, *, eps, progress=None):
846843
* edge.span,
847844
)
848845
result[: youngest_par_index + 1] = self.lik.combine(
849-
self.lik.reduce(
846+
self.lik.ratio(
850847
ll_mut[: youngest_par_index + 1],
851848
np.max(ll_mut[: youngest_par_index + 1]),
852849
),

0 commit comments

Comments
 (0)