code: def test_threshold_optimization_demographic_parity(
      score_transform, y_transform, sensitive_features_transform
  ):
      y = y_transform(labels_ex)
      sensitive_features = sensitive_features_transform(
          sensitive_features_ex1
      )
      # PassThroughPredictor takes scores_ex as input in predict and
      # returns score_transform(scores_ex) as output
      estimator = ThresholdOptimizer(
          estimator=PassThroughPredictor(score_transform),
          constraints="demographic_parity",
          flip=True,
          predict_method="predict",
      )
      estimator.fit(
          pd.DataFrame(scores_ex),
          y,
          sensitive_features=sensitive_features,
      )
  
      def prob_pred(sensitive_features, scores):
          return estimator._pmf_predict(
              pd.DataFrame(scores),
              sensitive_features=sensitive_features,
          )[0, 1]
  
      # For Demographic Parity we can ignore p_ignore since it's always 0.
  
      # sensitive feature value A
      value_for_less_than_2_5 = 0.8008
      assert np.isclose(
          value_for_less_than_2_5,
          prob_pred([sensitive_feature_names_ex1[0]], [0]),
      )
      assert np.isclose(
          value_for_less_than_2_5,
          prob_pred([sensitive_feature_names_ex1[0]], [2.499]),
      )
      assert 0 == prob_pred([sensitive_feature_names_ex1[0]], [2.5])
      assert 0 == prob_pred([sensitive_feature_names_ex1[0]], [100])
  
      # sensitive feature value B
      value_for_less_than_0_5 = 0.00133333333333
      assert np.isclose(
          value_for_less_than_0_5,
          prob_pred([sensitive_feature_names_ex1[1]], [0]),
      )
      assert np.isclose(
          value_for_less_than_0_5,
          prob_pred([sensitive_feature_names_ex1[1]], [0.5]),
      )
      assert 1 == prob_pred([sensitive_feature_names_ex1[1]], [0.51])
      assert 1 == prob_pred([sensitive_feature_names_ex1[1]], [1])
      assert 1 == prob_pred([sensitive_feature_names_ex1[1]], [100])
  
      # sensitive feature value C
      value_between_0_5_and_1_5 = 0.608
      assert 0 == prob_pred([sensitive_feature_names_ex1[2]], [0])
      assert 0 == prob_pred([sensitive_feature_names_ex1[2]], [0.5])
      assert np.isclose(
          value_between_0_5_and_1_5,
          prob_pred([sensitive_feature_names_ex1[2]], [0.51]),
      )
      assert np.isclose(
          value_between_0_5_and_1_5,
          prob_pred([sensitive_feature_names_ex1[2]], [1]),
      )
      assert np.isclose(
          value_between_0_5_and_1_5,
          prob_pred([sensitive_feature_names_ex1[2]], [1.5]),
      )
      assert 1 == prob_pred([sensitive_feature_names_ex1[2]], [1.51])
      assert 1 == prob_pred([sensitive_feature_names_ex1[2]], [100])
  
      # Assert Demographic Parity actually holds
      predictions_by_sensitive_feature = (
          _get_predictions_by_sensitive_feature(
              prob_pred, sensitive_features_ex1, scores_ex, labels_ex
          )
      )
  
      def _average_prediction(
          sensitive_feature_value, predictions_by_sensitive_feature
      ):
          relevant_predictions = predictions_by_sensitive_feature[
              sensitive_feature_value
          ]
          predictions = [lp.prediction for lp in relevant_predictions]
          return np.sum(predictions) / len(relevant_predictions)
  
      average_probabilities_by_sensitive_feature = []
      for sensitive_feature_value in sorted(
          predictions_by_sensitive_feature
      ):
          average_probabilities_by_sensitive_feature.append(
              _average_prediction(
                  sensitive_feature_value,
                  predictions_by_sensitive_feature,
              )
          )
      assert np.isclose(
          average_probabilities_by_sensitive_feature, [0.572] * 3
      ).all()
  
imports: from copy import deepcopy
  from test.unit.input_convertors import _map_into_single_column
  import numpy
  import pandas
  import pytest
  from sklearn.base import BaseEstimator, ClassifierMixin
  from sklearn.datasets import make_classification
  from fairlearn.postprocessing import ThresholdOptimizer
  from fairlearn.postprocessing._threshold_optimizer import BASE_ESTIMATOR_NONE_ERROR_MESSAGE, NOT_SUPPORTED_CONSTRAINTS_ERROR_MESSAGE
  from fairlearn.postprocessing._tradeoff_curve_utilities import DEGENERATE_LABELS_ERROR_MESSAGE
  from fairlearn.utils._input_validation import _LABELS_NOT_0_1_ERROR_MESSAGE, _MESSAGE_SENSITIVE_FEATURES_NONE, _MESSAGE_Y_NONE
  from .conftest import ExamplePredictor, X_ex, _get_predictions_by_sensitive_feature, candidate_A_transforms, candidate_X_transforms, candidate_Y_transforms, degenerate_labels_ex, is_invalid_transformation, labels_ex, scores_ex, sensitive_feature_names_ex1, sensitive_features_ex1
label_desc: Use image node_id 8 for calling a global function with example usage: test_threshold_optimization_demographic_parity(score_transform, y_transform, sensitive_features_transform) and returns: estimator, unknown