Esempio n. 1
0
def test_compare_branches():
    df = pd.DataFrame(columns=['branch', 'val'],
                      index=range(1000),
                      dtype='float')
    df.iloc[::2, 0] = 'control'
    df.iloc[1::2, 0] = 'test'
    df.iloc[:300, 1] = range(300)

    with pytest.raises(ValueError):
        mabssf.compare_branches(df, 'val', thresholds=[0., 15.9])

    df.iloc[300:, 1] = 0
    res = mabssf.compare_branches(df, 'val', thresholds=[0., 15.9])

    assert res['individual']['control'].loc[0.,
                                            '0.025'] == pytest.approx(st.beta(
                                                149 + 1, 351 + 1).ppf(0.025),
                                                                      abs=1e-6)
    assert res['individual']['control'].loc[0.,
                                            '0.5'] == pytest.approx(0.298537,
                                                                    abs=1e-6)

    assert res['individual']['test'].loc[15.9,
                                         '0.025'] == pytest.approx(st.beta(
                                             142 + 1, 358 + 1).ppf(0.025),
                                                                   abs=1e-6)
    assert res['individual']['test'].loc[15.9,
                                         '0.5'] == pytest.approx(0.284575,
                                                                 abs=1e-6)
Esempio n. 2
0
def test_compare_branches():
    df = pd.DataFrame(columns=["branch", "val"],
                      index=range(1000),
                      dtype="float")
    df.iloc[::2, 0] = "control"
    df.iloc[1::2, 0] = "test"
    df.iloc[:300, 1] = range(300)

    with pytest.raises(ValueError):
        mabssf.compare_branches(df, "val", thresholds=[0.0, 15.9])

    df.iloc[300:, 1] = 0
    res = mabssf.compare_branches(df, "val", thresholds=[0.0, 15.9])

    assert res["individual"]["control"].loc[0.0,
                                            "0.025"] == pytest.approx(st.beta(
                                                149 + 1, 351 + 1).ppf(0.025),
                                                                      abs=1e-6)
    assert res["individual"]["control"].loc[0.0,
                                            "0.5"] == pytest.approx(0.298537,
                                                                    abs=1e-6)

    assert res["individual"]["test"].loc[15.9,
                                         "0.025"] == pytest.approx(st.beta(
                                             142 + 1, 358 + 1).ppf(0.025),
                                                                   abs=1e-6)
    assert res["individual"]["test"].loc[15.9,
                                         "0.5"] == pytest.approx(0.284575,
                                                                 abs=1e-6)
Esempio n. 3
0
def test_few_auto_thresholds():
    df = pd.DataFrame(columns=['branch', 'val'], index=range(1000))
    df.iloc[::2, 0] = 'control'
    df.iloc[1::2, 0] = 'test'
    df.fillna(0, inplace=True)
    df.iloc[20:30] = 1
    df.iloc[405] = 100

    res = mabssf.compare_branches(df, 'val')
    assert set(res['individual']['test'].index) == {0, 1}
    assert set(res['individual']['control'].index) == {0, 1}
Esempio n. 4
0
def test_few_auto_thresholds():
    df = pd.DataFrame(columns=["branch", "val"], index=range(1000))
    df.iloc[::2, 0] = "control"
    df.iloc[1::2, 0] = "test"
    df.fillna(0, inplace=True)
    df.iloc[20:30] = 1
    df.iloc[405] = 100

    res = mabssf.compare_branches(df, "val")
    assert set(res["individual"]["test"].index) == {0, 1}
    assert set(res["individual"]["control"].index) == {0, 1}
Esempio n. 5
0
def plot_survival(df, col_label, ref_branch_label='control', thresholds=None):
    data = masf.compare_branches(df, col_label, ref_branch_label, thresholds)

    fig, (ax1, ax2) = plt.subplots(nrows=2, sharex=True, figsize=(6, 10))

    for ax in (ax1, ax2):
        ax.set_prop_cycle('color', plt.cm.tab20b(np.linspace(0, 1, 5)))

    plot_means_line(ax1, data['individual'], ref_branch_label)
    plot_uplifts_line(ax2, data['comparative'])

    ax1.set_title(
        'Survival fn: Fraction of users with {} > x'.format(col_label))
    ax1.set_ylim((0, 1))
    ax1.set_ylabel('Fraction of users')
    ax1.legend()
    ax2.set_xlabel(col_label)
    ax2.set_ylabel('Uplift relative to {}'.format(ref_branch_label))

    fig.tight_layout()
    return fig