code: def test_breakdown_regression_based_prediction():
      "Tests ``breakdown_regression_based_prediction``."
      data = generate_test_data_for_fitting()
      df = data["df"]
  
      # Adds a few more columns
      df["var1"] = df["x1"] ** 2
      df["var2"] = df["x2"] ** 2
      df["y_lag1"] = df["x1"] ** 3
      df["y_lag2"] = df["x2"] ** 3
      df["u1"] = (df["x1"] + 3) ** 2
      df["u2"] = (df["x2"] - 3) ** 2
  
      df_train = df[:800].reset_index(drop=True)
      df_test = df[800:].reset_index(drop=True)
  
      trained_model = fit_ml_model(
          df=df_train,
          model_formula_str="y~x1+x2+x3+x4+var1+var2+x1_categ+y_lag1+y_lag2+u1+u2",
          fit_algorithm="sgd",
          fit_algorithm_params={"alpha": 0.1},
      )
  
      pred_res = predict_ml(fut_df=df_test, trained_model=trained_model)
  
      pred_df = pred_res["fut_df"]
      x_mat = pred_res["x_mat"]
  
      grouping_regex_patterns_dict = {
          "A": ".*categ",
          "B": "var",
          "C": "x",
          "D": ".*_lag.*",
      }
  
      # Example 1: ``center_components=False``
      result = breakdown_regression_based_prediction(
          trained_model=trained_model,
          x_mat=x_mat,
          grouping_regex_patterns_dict=grouping_regex_patterns_dict,
          remainder_group_name="OTHER",
          center_components=False,
      )
  
      column_grouping_result = result["column_grouping_result"]
      breakdown_df = result["breakdown_df"]
      breakdown_fig = result["breakdown_fig"]
      assert breakdown_fig.layout.title.text == "prediction breakdown"
      assert len(breakdown_fig.data) == 6
      assert list(breakdown_df.columns) == [
          "Intercept",
          "A",
          "B",
          "C",
          "D",
          "OTHER",
      ]
  
      # Note that if a variable/column is already picked in a step,
      # it will be taken out from the columns list and will not appear
      # in next groups.
      assert column_grouping_result == {
          "str_groups": [
              [
                  "x1_categ[T.C1]",
                  "x1_categ[T.C2]",
                  "x1_categ[T.C3]",
                  "x1_categ[T.C4]",
                  "x1_categ[T.C5]",
                  "x1_categ[T.C6]",
                  "x1_categ[T.C7]",
              ],
              ["var1", "var2"],
              ["x1", "x2", "x3", "x4"],
              ["y_lag1", "y_lag2"],
          ],
          "remainder": ["u1", "u2"],
      }
  
      ml_model = trained_model["ml_model"]
      ml_model_coef = ml_model.coef_
      intercept = ml_model.intercept_
      x_mat_weighted = x_mat * ml_model_coef
      y_mean = trained_model["y_mean"]
  
      pred_raw = round(pred_df["y"], 5)
      pred_raw_sum = round(x_mat_weighted.sum(axis=1) + intercept, 5)
      pred_from_breakdown = round(breakdown_df.sum(axis=1), 5)
  
      assert pred_raw_sum.equals(pred_from_breakdown)
      assert pred_raw.equals(pred_from_breakdown)
  
      # Example 2: ``remainder_group_name="REMAINDER"``
      result = breakdown_regression_based_prediction(
          trained_model=trained_model,
          x_mat=x_mat,
          grouping_regex_patterns_dict=grouping_regex_patterns_dict,
          remainder_group_name="REMAINDER",
      )
  
      column_grouping_result = result["column_grouping_result"]
      breakdown_df = result["breakdown_df"]
      assert list(breakdown_df.columns) == [
          "Intercept",
          "A",
          "B",
          "C",
          "D",
          "REMAINDER",
      ]
  
      assert column_grouping_result == {
          "str_groups": [
              [
                  "x1_categ[T.C1]",
                  "x1_categ[T.C2]",
                  "x1_categ[T.C3]",
                  "x1_categ[T.C4]",
                  "x1_categ[T.C5]",
                  "x1_categ[T.C6]",
                  "x1_categ[T.C7]",
              ],
              ["var1", "var2"],
              ["x1", "x2", "x3", "x4"],
              ["y_lag1", "y_lag2"],
          ],
          "remainder": ["u1", "u2"],
      }
  
      # Example 3: ``center_components=True``
      result = breakdown_regression_based_prediction(
          trained_model=trained_model,
          x_mat=x_mat,
          grouping_regex_patterns_dict=grouping_regex_patterns_dict,
          remainder_group_name="OTHER",
          center_components=True,
      )
  
      column_grouping_result = result["column_grouping_result"]
      breakdown_df = result["breakdown_df"]
      assert list(breakdown_df.columns) == [
          "Intercept",
          "A",
          "B",
          "C",
          "D",
          "OTHER",
      ]
  
      assert column_grouping_result == {
          "str_groups": [
              [
                  "x1_categ[T.C1]",
                  "x1_categ[T.C2]",
                  "x1_categ[T.C3]",
                  "x1_categ[T.C4]",
                  "x1_categ[T.C5]",
                  "x1_categ[T.C6]",
                  "x1_categ[T.C7]",
              ],
              ["var1", "var2"],
              ["x1", "x2", "x3", "x4"],
              ["y_lag1", "y_lag2"],
          ],
          "remainder": ["u1", "u2"],
      }
  
      pred_from_breakdown = round(breakdown_df.sum(axis=1), 5)
  
      assert pred_raw_sum.equals(pred_from_breakdown)
      assert pred_raw.equals(pred_from_breakdown)
  
      # Checks to see if components are centered
      for col in ["A", "B", "C", "D", "OTHER"]:
          assert round(breakdown_df[col].mean(), 5) == 0
  
      # Example 4: ``center_components=True``
      result_denom = breakdown_regression_based_prediction(
          trained_model=trained_model,
          x_mat=x_mat,
          grouping_regex_patterns_dict=grouping_regex_patterns_dict,
          remainder_group_name="OTHER",
          center_components=True,
          denominator="abs_y_mean",
      )
  
      column_grouping_result = result_denom["column_grouping_result"]
      breakdown_df_denom = result_denom["breakdown_df"]
      assert list(breakdown_df_denom.columns) == [
          "Intercept",
          "A",
          "B",
          "C",
          "D",
          "OTHER",
      ]
  
      assert column_grouping_result == {
          "str_groups": [
              [
                  "x1_categ[T.C1]",
                  "x1_categ[T.C2]",
                  "x1_categ[T.C3]",
                  "x1_categ[T.C4]",
                  "x1_categ[T.C5]",
                  "x1_categ[T.C6]",
                  "x1_categ[T.C7]",
              ],
              ["var1", "var2"],
              ["x1", "x2", "x3", "x4"],
              ["y_lag1", "y_lag2"],
          ],
          "remainder": ["u1", "u2"],
      }
  
      pred_from_breakdown = round(
          breakdown_df_denom.sum(axis=1) * abs(y_mean), 5
      )
      assert pred_raw_sum.equals(pred_from_breakdown)
      assert pred_raw.equals(pred_from_breakdown)
  
      # Checks to see if components are centered
      for col in ["A", "B", "C", "D", "OTHER"]:
          assert round(breakdown_df_denom[col].mean(), 5) == 0
  
      # Checks to see if components are divided by absolute mean
      for col in ["A", "B", "C", "D", "OTHER"]:
          assert (
              max(
                  abs(
                      breakdown_df_denom[col] * abs(y_mean)
                      - breakdown_df[col]
                  )
              )
              < 0.0001
          )
  
      with pytest.raises(
          NotImplementedError,
          match=f"quantile is not an admissable denominator",
      ):
          breakdown_regression_based_prediction(
              trained_model=trained_model,
              x_mat=x_mat,
              grouping_regex_patterns_dict=grouping_regex_patterns_dict,
              remainder_group_name="OTHER",
              center_components=True,
              denominator="quantile",
          )
  
imports: import warnings
  import numpy
  import pandas
  import pytest
  import scipy
  from pandas.testing import assert_frame_equal
  from patsy import dmatrices
  from greykite.algo.common.ml_models import breakdown_regression_based_prediction
  from greykite.algo.common.ml_models import design_mat_from_formula
  from greykite.algo.common.ml_models import fit_ml_model
  from greykite.algo.common.ml_models import fit_ml_model_with_evaluation
  from greykite.algo.common.ml_models import fit_model_via_design_matrix
  from greykite.algo.common.ml_models import get_h_mat
  from greykite.algo.common.ml_models import get_intercept_col_from_design_mat
  from greykite.algo.common.ml_models import predict_ml
  from greykite.algo.common.ml_models import predict_ml_with_uncertainty
  from greykite.algo.uncertainty.conditional.conf_interval import predict_ci
  from greykite.common.constants import ERR_STD_COL
  from greykite.common.constants import QUANTILE_SUMMARY_COL
  from greykite.common.constants import TimeFeaturesEnum
  from greykite.common.evaluation import EvaluationMetricEnum
  from greykite.common.evaluation import calc_pred_err
  from greykite.common.features.timeseries_features import build_time_features_df
  from greykite.common.features.timeseries_features import fourier_series_multi_fcn
  from greykite.common.features.timeseries_features import get_fourier_col_name
  from greykite.common.python_utils import assert_equal
  from greykite.common.testing_utils import gen_sliced_df
label_desc: Use image node_id 34 for calling a global function with example usage: test_breakdown_regression_based_prediction() without return types