コード例 #1
0
ファイル: test_hypotheses.py プロジェクト: gesta81/pandera
def test_two_sample_ttest_hypothesis_relationships():
    """Check allowable relationships in two-sample ttest."""
    for relationship in Hypothesis.RELATIONSHIPS:
        schema = DataFrameSchema({
            "height_in_feet": Column(Float, [
                Hypothesis.two_sample_ttest(
                    sample1="M",
                    sample2="F",
                    groupby="sex",
                    relationship=relationship,
                    alpha=0.5),
            ]),
            "sex": Column(String)
        })
        assert isinstance(schema, DataFrameSchema)

    for relationship in ["foo", "bar", 1, 2, 3, None]:
        with pytest.raises(errors.SchemaInitError):
            DataFrameSchema({
                "height_in_feet": Column(Float, [
                    Hypothesis.two_sample_ttest(
                        sample1="M",
                        sample2="F",
                        groupby="sex",
                        relationship=relationship,
                        alpha=0.5),
                ]),
                "sex": Column(String)
            })
コード例 #2
0
def hypothesis_accident_probability(feature, increases=True):
    relationship = "greater_than" if increases else "less_than"
    return {
        feature: Column(checks=Check.isin([1, 0])),
        f"{feature}_shap": Column(
            pa.Float,
            checks=Hypothesis.two_sample_ttest(
                sample1=1,
                sample2=0,
                groupby=feature,
                relationship=relationship,
                alpha=0.01,
            )
        ),
    }
コード例 #3
0
def test_hypothesis():
    """Tests the different API calls of Hypothesis."""
    # Example df for tests:
    df = pd.DataFrame({
        "height_in_feet": [6.5, 7, 6.1, 5.1, 4],
        "sex": ["M", "M", "F", "F", "F"],
    })

    # Initialise the different ways of calling a test:
    schema_pass_ttest_on_alpha_val_1 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis.two_sample_ttest(
                    sample1="M",
                    sample2="F",
                    groupby="sex",
                    relationship="greater_than",
                    alpha=0.5,
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    schema_pass_ttest_on_alpha_val_2 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis(
                    test=stats.ttest_ind,
                    samples=["M", "F"],
                    groupby="sex",
                    relationship="greater_than",
                    relationship_kwargs={"alpha": 0.5},
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    schema_pass_ttest_on_alpha_val_3 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis.two_sample_ttest(
                    sample1="M",
                    sample2="F",
                    groupby="sex",
                    relationship="greater_than",
                    alpha=0.5,
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    schema_pass_ttest_on_custom_relationship = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis(
                    test=stats.ttest_ind,
                    samples=["M", "F"],
                    groupby="sex",
                    relationship=lambda stat, pvalue, alpha=0.01:
                    (stat > 0 and pvalue / 2 < alpha),
                    relationship_kwargs={"alpha": 0.5},
                )
            ],
        ),
        "sex":
        Column(String),
    })

    # Check the 3 happy paths are successful:
    schema_pass_ttest_on_alpha_val_1.validate(df)
    schema_pass_ttest_on_alpha_val_2.validate(df)
    schema_pass_ttest_on_alpha_val_3.validate(df)
    schema_pass_ttest_on_custom_relationship.validate(df)

    schema_fail_ttest_on_alpha_val_1 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis.two_sample_ttest(
                    sample1="M",
                    sample2="F",
                    groupby="sex",
                    relationship="greater_than",
                    alpha=0.05,
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    schema_fail_ttest_on_alpha_val_2 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis(
                    test=stats.ttest_ind,
                    samples=["M", "F"],
                    groupby="sex",
                    relationship="greater_than",
                    relationship_kwargs={"alpha": 0.05},
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    schema_fail_ttest_on_alpha_val_3 = DataFrameSchema({
        "height_in_feet":
        Column(
            Float,
            [
                Hypothesis.two_sample_ttest(
                    sample1="M",
                    sample2="F",
                    groupby="sex",
                    relationship="greater_than",
                    alpha=0.05,
                ),
            ],
        ),
        "sex":
        Column(String),
    })

    with pytest.raises(errors.SchemaError):
        schema_fail_ttest_on_alpha_val_1.validate(df)
    with pytest.raises(errors.SchemaError):
        schema_fail_ttest_on_alpha_val_2.validate(df)
    with pytest.raises(errors.SchemaError):
        schema_fail_ttest_on_alpha_val_3.validate(df)