def test_both_concordance_index_with_only_censoring_fails_gracefully(): actual_times = np.array([1, 2, 3]) predicted_times = np.array([1, 2, 3]) obs = np.zeros(3) with pytest.raises(ZeroDivisionError, match="admissable pairs"): fast_cindex(actual_times, predicted_times, obs) with pytest.raises(ZeroDivisionError, match="admissable pairs"): slow_cindex(actual_times, predicted_times, obs)
def test_concordance_index_fast_is_same_as_slow(): size = 100 T = np.random.normal(size=size) P = np.random.normal(size=size) C = np.random.choice([0, 1], size=size) Z = np.zeros_like(T) # Hard to imagine these failing assert slow_cindex(T, Z, C) == fast_cindex(T, Z, C) assert slow_cindex(T, T, C) == fast_cindex(T, T, C) # This is the real test though assert slow_cindex(T, P, C) == fast_cindex(T, P, C) cp = CoxPHFitter() df = load_rossi() cp.fit(df, duration_col="week", event_col="arrest") T = cp.durations.values.ravel() P = -cp.predict_partial_hazard(df[df.columns.difference(["week", "arrest"])]).values.ravel() E = cp.event_observed.values.ravel() assert slow_cindex(T, P, E) == fast_cindex(T, P, E)
def test_both_concordance_index_function_deal_with_ties_the_same_way(): actual_times = np.array([1, 1, 2]) predicted_times = np.array([1, 2, 3]) obs = np.ones(3) assert fast_cindex(actual_times, predicted_times, obs) == slow_cindex( actual_times, predicted_times, obs) == 1.0