def assert_monotized_correctly(self, input_seq, expected_seq, increasing, weights=None): mono_seq = isotonic.isotonic_regression(input_seq, weights, increasing) np.testing.assert_array_equal(mono_seq, expected_seq) if increasing: self.assert_increasing(mono_seq) else: self.assert_decreasing(mono_seq)
def _is_increasing(y: Sequence[float], w: Sequence[float]) -> bool: """Returns whether a mono-up or a mono-down sequence better approximates y. Tries to monotonize values in both the increasing and decreasing directions with respect to the given norm. Then returns whether the increasing or the decreasing sequence gives the better fit, as measured by mean squared error. Args: y: (sequence of floats) sequence to monotonize. w: (sequence of floats) weights. Returns: True if a monotonically increasing sequence is an equal or better fit to the data than a monotonically decreasing sequence. False otherwise. """ increasing = isotonic.isotonic_regression(y, w, increasing=True) decreasing = isotonic.isotonic_regression(y, w, increasing=False) increasing_norm = np.average((increasing - y)**2, weights=w) decreasing_norm = np.average((decreasing - y)**2, weights=w) return increasing_norm <= decreasing_norm