Rename remaining gcm unit tests
Signed-off-by: Patrick Bloebaum <bloebp@amazon.com>
This commit is contained in:
Родитель
de5ddd54f6
Коммит
2a2b3f4f7d
|
@ -7,7 +7,7 @@ from scipy import stats
|
|||
from dowhy.gcm import BayesianGaussianMixtureDistribution, EmpiricalDistribution, ScipyDistribution
|
||||
|
||||
|
||||
def test_bayesian_gaussian_mixture_distribution():
|
||||
def test_when_fitting_bayesian_gaussian_mixture_distribution_then_samples_should_be_drawn_correctly():
|
||||
test_data = np.array([[0, 0], [0, 0], [1, 2], [1, 2]])
|
||||
|
||||
approximated_data_distribution_model = BayesianGaussianMixtureDistribution()
|
||||
|
@ -15,13 +15,13 @@ def test_bayesian_gaussian_mixture_distribution():
|
|||
assert approximated_data_distribution_model.draw_samples(5).shape == (5, 2)
|
||||
|
||||
|
||||
def test_bayesian_gaussian_mixture_distribution_runtime_error():
|
||||
def test_when_drawing_samples_from_unfitted_bayesian_gaussian_mixture_distribution_then_runtime_error_should_occur():
|
||||
approximated_data_distribution_model = BayesianGaussianMixtureDistribution()
|
||||
with pytest.raises(RuntimeError):
|
||||
approximated_data_distribution_model.draw_samples(5)
|
||||
|
||||
|
||||
def test_scipy_fixed_parametric_distribution():
|
||||
def test_when_creating_scipy_distribution_with_fixed_parameters_then_it_should_return_the_correct_parameter_values():
|
||||
distribution = ScipyDistribution(stats.norm, loc=0, scale=1)
|
||||
|
||||
assert distribution.parameters["loc"] == 0
|
||||
|
@ -29,7 +29,7 @@ def test_scipy_fixed_parametric_distribution():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_scipy_fittable_parametric_distribution():
|
||||
def test_when_fitting_normal_scipy_distribution_then_it_should_return_correctly_fitted_parameter_values():
|
||||
distribution = ScipyDistribution(stats.norm)
|
||||
|
||||
X = np.random.normal(0, 1, 1000)
|
||||
|
@ -40,7 +40,7 @@ def test_scipy_fittable_parametric_distribution():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_scipy_auto_select_continuous_parametric_distribution():
|
||||
def test_given_gaussian_data_when_fitting_scipy_distribution_automatically_then_it_should_return_correctly_fitted_parameter_values():
|
||||
distribution = ScipyDistribution()
|
||||
|
||||
X = np.random.normal(0, 1, 1000)
|
||||
|
@ -50,7 +50,7 @@ def test_scipy_auto_select_continuous_parametric_distribution():
|
|||
assert np.std(distribution.draw_samples(1000)) == approx(1, abs=0.1)
|
||||
|
||||
|
||||
def test_empirical_distribution():
|
||||
def test_when_drawing_samples_from_empirical_distribution_then_all_samples_should_be_present_in_the_data():
|
||||
X = np.random.normal(0, 1, 1000)
|
||||
|
||||
distribution = EmpiricalDistribution()
|
||||
|
@ -63,7 +63,7 @@ def test_empirical_distribution():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_fitted_parameters_assigned_correctly_using_normal_distribution():
|
||||
def test_when_fitting_scipy_distribution_with_normal_distribution_then_it_should_return_correctly_fitted_parameter_values():
|
||||
distribution = ScipyDistribution(stats.norm)
|
||||
distribution.fit(ScipyDistribution(stats.norm, loc=3, scale=2).draw_samples(10000))
|
||||
|
||||
|
@ -72,7 +72,7 @@ def test_fitted_parameters_assigned_correctly_using_normal_distribution():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_fitted_parameters_assigned_correctly_using_beta_distribution():
|
||||
def test_when_fitting_scipy_distribution_with_beta_distribution_then_it_should_return_correctly_fitted_parameter_values():
|
||||
distribution = ScipyDistribution(stats.beta)
|
||||
distribution.fit(ScipyDistribution(stats.beta, a=2, b=0.5).draw_samples(10000))
|
||||
|
||||
|
|
|
@ -13,7 +13,7 @@ from dowhy.gcm.uncertainty import (
|
|||
)
|
||||
|
||||
|
||||
def test_estimate_entropy_using_discretization():
|
||||
def test_given_gaussian_data_when_estimating_entropy_using_discretization_then_it_should_return_correct_entropy_values():
|
||||
X = np.random.normal(0, 1, 10000)
|
||||
Y = np.random.normal(0, 3, 10000)
|
||||
|
||||
|
@ -21,7 +21,7 @@ def test_estimate_entropy_using_discretization():
|
|||
assert estimate_entropy_using_discretization(Y) == approx(2.45, abs=0.2)
|
||||
|
||||
|
||||
def test_estimate_entropy_kmeans():
|
||||
def test_given_gaussian_data_when_estimating_entropy_using_kmeans_then_it_should_return_correct_entropy_values():
|
||||
X = np.random.normal(0, 1, 10000)
|
||||
Y = np.random.normal(0, 3, 10000)
|
||||
|
||||
|
@ -29,7 +29,7 @@ def test_estimate_entropy_kmeans():
|
|||
assert estimate_entropy_kmeans(Y) == approx(2.5, abs=0.2)
|
||||
|
||||
|
||||
def test_estimate_gaussian_entropy():
|
||||
def test_given_gaussian_data_when_estimating_gaussian_entropy_then_it_should_return_correct_entropy_values():
|
||||
X = np.random.normal(0, 1, 10000)
|
||||
Y = np.random.normal(0, 3, 10000)
|
||||
|
||||
|
@ -38,7 +38,7 @@ def test_estimate_gaussian_entropy():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_estimate_variance():
|
||||
def test_given_gaussian_data_when_estimating_variance_then_it_should_return_correct_variance_values():
|
||||
X = np.random.normal(0, 1, 10000)
|
||||
Y = np.random.normal(0, 3, 10000)
|
||||
|
||||
|
@ -52,7 +52,7 @@ def test_estimate_variance():
|
|||
assert estimate_variance(Y) == approx(729, abs=15)
|
||||
|
||||
|
||||
def test_estimate_entropy_of_probabilities():
|
||||
def test_given_probabilities_when_estimating_entropy_of_probabilities_then_it_should_return_correct_entropy_values():
|
||||
assert estimate_entropy_of_probabilities(np.array([[0.25, 0.25, 0.25, 0.25]])) == approx(1.38, abs=0.05)
|
||||
assert estimate_entropy_of_probabilities(np.array([[1, 0, 0, 0]])) == approx(0, abs=0.05)
|
||||
assert estimate_entropy_of_probabilities(np.array([[0.5, 0, 0, 0.5]])) == approx(0.69, abs=0.05)
|
||||
|
@ -60,7 +60,7 @@ def test_estimate_entropy_of_probabilities():
|
|||
assert estimate_entropy_of_probabilities(np.array([[1, 0, 0, 0], [1, 0, 0, 0]])) == approx(0, abs=0.05)
|
||||
|
||||
|
||||
def test_estimate_entropy_discrete():
|
||||
def test_given_discrete_data_when_estimating_entropy_discrete_then_it_should_return_correct_entropy_values():
|
||||
assert estimate_entropy_discrete(np.random.choice(2, (1000, 1), replace=True)) == approx(entropy([0.5, 0.5]), 0.05)
|
||||
assert estimate_entropy_discrete(np.random.choice(2, (1000, 2), replace=True)) == approx(
|
||||
entropy([0.25, 0.25, 0.25, 0.25]), 0.05
|
||||
|
|
|
@ -23,7 +23,7 @@ def _generate_simple_non_linear_data() -> pd.DataFrame:
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_refute_causal_structure_collider():
|
||||
def test_given_collider_when_refuting_causal_structure_then_not_rejected():
|
||||
# collider: X->Z<-Y
|
||||
collider_dag = nx.DiGraph([("X", "Z"), ("Y", "Z")])
|
||||
X = np.random.normal(size=500)
|
||||
|
@ -43,7 +43,7 @@ def test_refute_causal_structure_collider():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_refute_causal_structure_chain():
|
||||
def test_given_chain_when_refuting_causal_structure_then_not_rejected():
|
||||
# chain: X->Z->Y
|
||||
chain_dag = nx.DiGraph([("X", "Z"), ("Z", "Y")])
|
||||
X = np.random.normal(size=500)
|
||||
|
@ -62,7 +62,7 @@ def test_refute_causal_structure_chain():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_refute_causal_structure_fork():
|
||||
def test_given_fork_when_refuting_causal_structure_then_not_rejected():
|
||||
# fork: X<-Z->Y
|
||||
fork_dag = nx.DiGraph([("Z", "X"), ("Z", "Y")])
|
||||
Z = np.random.normal(size=500)
|
||||
|
@ -81,7 +81,7 @@ def test_refute_causal_structure_fork():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_refute_causal_structure_general():
|
||||
def test_given_general_dag_when_refuting_causal_structure_then_not_rejected():
|
||||
# general DAG: X<-Z->Y, X->Y
|
||||
general_dag = nx.DiGraph([("Z", "X"), ("Z", "Y"), ("X", "Y")])
|
||||
Z = np.random.normal(size=500)
|
||||
|
@ -101,7 +101,7 @@ def test_refute_causal_structure_general():
|
|||
|
||||
|
||||
@flaky(max_runs=5)
|
||||
def test_refute_causal_structure_adjusted_p_values():
|
||||
def test_given_fdr_bh_when_refuting_causal_structure_then_return_correct_adjusted_p_values():
|
||||
# fork: X<-Z->Y
|
||||
fork_dag = nx.DiGraph([("Z", "X"), ("Z", "Y")])
|
||||
Z = np.random.normal(size=500)
|
||||
|
|
|
@ -20,7 +20,7 @@ from dowhy.gcm import (
|
|||
from dowhy.gcm.ml import create_linear_regressor, create_logistic_regression_classifier
|
||||
|
||||
|
||||
def __create_and_fit_simple_probabilistic_causal_model():
|
||||
def _create_and_fit_simple_probabilistic_causal_model():
|
||||
X0 = np.random.uniform(-1, 1, 10000)
|
||||
X1 = 2 * X0 + np.random.normal(0, 0.1, 10000)
|
||||
X2 = 0.5 * X0 + np.random.normal(0, 0.1, 10000)
|
||||
|
@ -40,8 +40,8 @@ def __create_and_fit_simple_probabilistic_causal_model():
|
|||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_atomic():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_atomic_intervention_with_specific_input_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
observed_data = pd.DataFrame({"X0": [0], "X1": [1], "X2": [2], "X3": [3]})
|
||||
|
||||
|
@ -54,8 +54,8 @@ def test_interventional_samples_atomic():
|
|||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_conditional():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_conditional_intervention_with_specific_input_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
observed_data = pd.DataFrame({"X0": [0], "X1": [1], "X2": [2], "X3": [3]})
|
||||
|
||||
|
@ -68,23 +68,23 @@ def test_interventional_samples_conditional():
|
|||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_atomic_draw():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_atomic_intervention_without_specific_input_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
samples = interventional_samples(causal_model, dict(X2=lambda x: np.array(10)), num_samples_to_draw=10)
|
||||
assert samples["X2"].to_numpy() == approx(10, abs=0)
|
||||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_conditional_draw():
|
||||
causal_model, training_data = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_conditional_intervention_without_specific_input_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model, training_data = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
samples = interventional_samples(causal_model, dict(X2=lambda x: x + 10), num_samples_to_draw=10)
|
||||
assert samples["X2"].to_numpy() == approx(np.mean(training_data["X2"].to_numpy()) + 10, abs=1)
|
||||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_with_categorical_variables_draw():
|
||||
def test_given_categorical_variables_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model = ProbabilisticCausalModel(nx.DiGraph([("X0", "X2"), ("X1", "X2"), ("X2", "X3")]))
|
||||
causal_model.set_causal_mechanism("X0", EmpiricalDistribution())
|
||||
causal_model.set_causal_mechanism("X1", EmpiricalDistribution())
|
||||
|
@ -127,8 +127,8 @@ def test_interventional_samples_with_categorical_variables_draw():
|
|||
|
||||
|
||||
@flaky(max_runs=3)
|
||||
def test_interventional_samples_atomic_multiple_interventions():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_multiple_atomic_intervention_with_specific_input_when_draw_interventional_samples_then_return_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
observed_data = pd.DataFrame({"X0": [0], "X1": [1], "X2": [2], "X3": [3]})
|
||||
|
||||
|
@ -140,15 +140,15 @@ def test_interventional_samples_atomic_multiple_interventions():
|
|||
assert sample[3] == approx(5, abs=0.3)
|
||||
|
||||
|
||||
def test_interventional_samples_raise_error_all_parameter_none():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_when_draw_interventional_samples_without_observed_data_or_num_samples_parameter_then_raise_error():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
interventional_samples(causal_model, dict(X0=lambda x: 10))
|
||||
|
||||
|
||||
def test_interventional_samples_raise_error_both_parameter_given():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_when_draw_interventional_samples_with_observed_data_and_num_samples_parameter_then_raise_error():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
observed_data = pd.DataFrame({"X0": [0], "X1": [1], "X2": [2], "X3": [3]})
|
||||
|
||||
|
@ -158,8 +158,8 @@ def test_interventional_samples_raise_error_both_parameter_given():
|
|||
)
|
||||
|
||||
|
||||
def test_counterfactual_samples_with_observed_samples():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_observed_sample_when_estimate_counterfactual_then_returns_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
observed_samples = pd.DataFrame({"X0": [1], "X1": [3], "X2": [3], "X3": [4]})
|
||||
|
||||
|
@ -170,8 +170,8 @@ def test_counterfactual_samples_with_observed_samples():
|
|||
assert sample["X3"].to_numpy().squeeze() == approx(3.5, abs=0.05)
|
||||
|
||||
|
||||
def test_counterfactual_samples_with_noise_samples():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_given_noise_sample_when_estimate_counterfactual_then_returns_correct_sample_values():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
noise_samples = pd.DataFrame({"X0": [1], "X1": [2], "X2": [3], "X3": [4]})
|
||||
|
||||
|
@ -182,15 +182,15 @@ def test_counterfactual_samples_with_noise_samples():
|
|||
assert sample["X3"].to_numpy().squeeze() == approx(5, abs=0.05)
|
||||
|
||||
|
||||
def test_counterfactual_samples_raises_error_all_parameter_none():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_when_estimate_counterfactual_without_observed_or_noise_data_then_raise_error():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
counterfactual_samples(causal_model, dict(X0=lambda x: 10))
|
||||
|
||||
|
||||
def test_counterfactual_samples_raises_error_both_parameter_given():
|
||||
causal_model, _ = __create_and_fit_simple_probabilistic_causal_model()
|
||||
def test_when_estimate_counterfactual_with_observed_and_noise_data_then_raise_error():
|
||||
causal_model, _ = _create_and_fit_simple_probabilistic_causal_model()
|
||||
|
||||
with pytest.raises(ValueError):
|
||||
counterfactual_samples(
|
||||
|
|
Загрузка…
Ссылка в новой задаче