code: def test_p_effective():
      """Tests the computation of `p_effective` in `fit_ml_model` for "linear" and "ridge" models."""
      np.random.seed(123)
      X = np.random.rand(5, 3)
      n, p = X.shape
      beta = np.array([1] * p).reshape((p, 1))
      y = X @ beta
  
      # Fits a linear regression.
      model = fit_model_via_design_matrix(
          x_train=X,
          y_train=y,
          fit_algorithm="linear",
          fit_algorithm_params=None,
          sample_weight=None,
      )
      alpha = 0
      XTX_alpha = X.T @ X + np.diag([alpha] * p)
      p_effective = round(
          np.trace(scipy.linalg.pinvh(XTX_alpha) @ X.T @ X), 3
      )
  
      assert p == 3
      # The `df_model` attribute from `statsmodels` is inconsistent.
      # The value is supposed to be rank minus 1, i.e. 2.
      # We add this check so that we're aware of the inconsistency until it is fixed.
      assert model.df_model == 3
      assert np.linalg.matrix_rank(X) == 3
      assert p_effective == 3
  
      # Duplicates the columns in `X`, result should not change.
      X = np.concatenate([X, X], axis=1)
      n, p = X.shape
      beta = np.array([1] * p).reshape((p, 1))
      y = X @ beta
  
      model = fit_model_via_design_matrix(
          x_train=X,
          y_train=y,
          fit_algorithm="linear",
          fit_algorithm_params=None,
          sample_weight=None,
      )
      alpha = 0
      XTX_alpha = X.T @ X + np.diag([alpha] * p)
      p_effective = round(
          np.trace(scipy.linalg.pinvh(XTX_alpha) @ X.T @ X), 3
      )
  
      assert p == 6
      # The `df_model` attribute from `statsmodels` is inconsistent.
      # The value is supposed to be rank minus 1, i.e. 2.
      # We add this check so that we're aware of the inconsistency until it is fixed.
      assert model.df_model == 3
      assert np.linalg.matrix_rank(X) == 3
      assert p_effective == 3
  
      # Adds an intercept column to `X`, `p_effective` and rank should increase by 1.
      X = np.concatenate([X, np.ones((5, 1))], axis=1)
      n, p = X.shape
      beta = np.array([1] * p).reshape((p, 1))
      y = X @ beta
  
      model = fit_model_via_design_matrix(
          x_train=X,
          y_train=y,
          fit_algorithm="linear",
          fit_algorithm_params=None,
          sample_weight=None,
      )
      alpha = 0
      XTX_alpha = X.T @ X + np.diag([alpha] * p)
      p_effective = round(
          np.trace(scipy.linalg.pinvh(XTX_alpha) @ X.T @ X), 3
      )
  
      assert p == 7
      assert model.df_model == 3  # This is the expected behavior.
      assert np.linalg.matrix_rank(X) == 4
      assert p_effective == 4
  
      # Fits a ridge regression.
      with warnings.catch_warnings():
          warnings.simplefilter("ignore")
          model = fit_model_via_design_matrix(
              x_train=X,
              y_train=y,
              fit_algorithm="ridge",
              fit_algorithm_params=None,
              sample_weight=None,
          )
      alpha = model.alpha_
      XTX_alpha = X.T @ X + np.diag([alpha] * p)
      log_cond = np.log10(np.linalg.cond(XTX_alpha))
      digits_to_lose = 8
      if log_cond < digits_to_lose:
          h_mat = scipy.linalg.solve(XTX_alpha, X.T, assume_a="pos")
      else:
          h_mat = scipy.linalg.pinvh(XTX_alpha) @ X.T
      p_effective = round(np.trace(h_mat @ X), 3)
  
      assert round(log_cond, 1) == 6.1
      assert p_effective == 4.0
  
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 29 for calling a global function with example usage: test_p_effective() without return types