causal_testing.testing.causal_test_case

This module contains the CausalTestCase class, a class that holds the information required for a causal test

Module Contents

Classes

CausalTestCase

A CausalTestCase extends the information held in a BaseTestCase. As well as storing the treatment and outcome

Attributes

logger

causal_testing.testing.causal_test_case.logger
class causal_testing.testing.causal_test_case.CausalTestCase(expected_causal_effect: causal_testing.testing.causal_effect.CausalEffect, effect_measure: str, estimator: type(Estimator) = None, name: str = None, query: str = None, skip: bool = False, result: causal_testing.testing.causal_test_result.CausalTestResult = None)

A CausalTestCase extends the information held in a BaseTestCase. As well as storing the treatment and outcome variables, a CausalTestCase stores the values of these variables. Also the outcome variable and value are specified. The goal of a CausalTestCase is to test whether the intervention made to the control via the treatment causes the model-under-test to produce the expected change.

Parameters:
  • base_test_case – A BaseTestCase object consisting of a treatment variable, outcome variable and effect

  • expected_causal_effect – The expected causal effect (Positive, Negative, No Effect).

  • effect_measure – A string which denotes the type of estimate to return.

  • estimator – An Estimator class object

property treatment_variable
Returns:

The treatment variable of the test case.

property outcome_variable
Returns:

The outcome variable of the test case.

measure_adequacy(df: pandas.DataFrame, bootstrap_size: int = 100, group_by: str = None) → causal_testing.testing.data_adequacy.DataAdequacy

Calculate the adequacy measurement, and populate the data_adequacy field.

Parameters:
  • df – The original dataset to use.

  • bootstrap_size – The number of bootstrap samples to use. (Defaults to 100)

  • group_by – For IPCWEstimator - the “id” column to ensure that entire individuals are sampled rather than random rows.

execute_test(df: pandas.DataFrame, adequacy: bool = False, suppress_estimation_errors: bool = False, bootstrap_size: int = 100, group_by: str = None)

Execute a causal test case.

Parameters:
  • df – The data to use.

  • adequacy – Set to True to calculate the causal test adequacy associated with the effect estimate.

  • suppress_estimation_errors – Set to True to suppress estimation errors. (Defaults to False)

  • bootstrap_size – The number of bootstrap samples to use. (Defaults to 100)

  • group_by – For IPCWEstimator - the “id” column to ensure that entire individuals are sampled rather than random rows.

estimate_effect(df: pandas.DataFrame) → causal_testing.testing.causal_test_result.CausalTestResult

Execute a causal test case and return the causal test result.

Parameters:

df – The data to use.

Returns:

A CausalTestResult for the executed causal test case.

to_dict(include_adequacy_results: bool = False) → dict

Convert the test case to a python dictionary for easy serialisation as JSON.

Bool include_adequacy_results:

Whether to include the effect estimate and test outcome for adequacy bootstraps.

Returns:

A JSON serialisable dict representing the test case.