code: def test_given_simple_gaussian_data_when_attribute_anomaly_scores_with_feature_relevance_then_returns_qualitatively_correct_results():
      X0 = np.random.normal(0, 1, 5000)
      X1 = np.random.normal(0, 1, 5000)
      X2 = np.random.normal(0, 1, 5000)
      X3 = np.random.normal(0, 1, 5000)
  
      original_observations = np.column_stack([X0, X1, X2, X3])
  
      # Defining an anomaly scorer that handles multidimensional inputs.
      density_estimator = GaussianMixtureDensityEstimator(
          num_components=5
      )
      anomaly_scorer = InverseDensityScorer(density_estimator)
      anomaly_scorer.fit(original_observations)
      expectation_of_score = np.mean(
          anomaly_scorer.score(original_observations)
      )
  
      # Seeing that the expectation of the noise in all nodes is 0, we introduce anomalies by setting some of them to 3.
      anomaly_samples = np.array(
          [[3, 0, 0, 0], [0, 3, 3, 0], [3, 0, 3, 3]]
      )
  
      contributions = attribute_anomaly_scores(
          anomaly_samples,
          original_observations,
          anomaly_scorer.score,
          True,
      )
  
      assert (
          np.argmax(contributions[0]) == 0
      )  # The biggest contribution to the score should be for the first variable.
      assert (
          contributions[0][0] > 0
      )  # The contribution should be positive (anomalous variable).
      assert np.all(
          contributions[0][1:] < 0
      )  # Contributions should be negative (non-anomalous variables), since they
      # reduce the score.
      # The contributions should add up to g(x) - E[g(X)]
      assert np.sum(contributions[0]) == approx(
          anomaly_scorer.score(anomaly_samples[0].reshape(1, -1))
          - expectation_of_score
      )
  
      assert (
          contributions[1][1] > 0
      )  # The contribution should be positive (anomalous variable).
      assert (
          contributions[1][2] > 0
      )  # The contribution should be positive (anomalous variable).
      assert (
          contributions[1][0] < 0
      )  # The contribution should be negative (non-anomalous variable).
      assert (
          contributions[1][3] < 0
      )  # The contribution should be negative (non-anomalous variable).
      # The contributions should add up to g(x) - E[g(X)]
      assert np.sum(contributions[1]) == approx(
          anomaly_scorer.score(anomaly_samples[1].reshape(1, -1))
          - expectation_of_score
      )
  
      assert (
          contributions[2][0] > 0
      )  # The contribution should be positive (anomalous variable).
      assert (
          contributions[2][2] > 0
      )  # The contribution should be positive (anomalous variable).
      assert (
          contributions[2][3] > 0
      )  # The contribution should be positive (anomalous variable).
      assert (
          contributions[2][1] < 0
      )  # The contribution should be negative (non-anomalous variable).
      # The contributions should add up to g(x) - E[g(X)]
      assert np.sum(contributions[2]) == approx(
          anomaly_scorer.score(anomaly_samples[2].reshape(1, -1))
          - expectation_of_score
      )
  
file_path: dowhy/tests/gcm/test_anomaly_attribution.py
imports: import networkx
  import numpy
  import pandas
  from flaky import flaky
  from pytest import approx
  from dowhy.gcm import AdditiveNoiseModel, InverseDensityScorer, InvertibleStructuralCausalModel, MedianCDFQuantileScorer, attribute_anomalies, auto, fit
  from dowhy.gcm.anomaly import _relative_frequency, attribute_anomaly_scores
  from dowhy.gcm.density_estimators import GaussianMixtureDensityEstimator
  from dowhy.gcm.ml import PredictionModel
label_desc: Use image node_id 2 for calling a global function with example usage: test_given_simple_gaussian_data_when_attribute_anomaly_scores_with_feature_relevance_then_returns_qualitatively_correct_results() without return types