code: def test_perf_attrib_regression(self):
      positions = pd.read_csv(
          "pyfolio/tests/test_data/positions.csv",
          index_col=0,
          parse_dates=True,
      )
  
      positions.columns = [
          int(col) if col != "cash" else col
          for col in positions.columns
      ]
  
      returns = pd.read_csv(
          "pyfolio/tests/test_data/returns.csv",
          index_col=0,
          parse_dates=True,
          header=None,
          squeeze=True,
      )
  
      factor_loadings = pd.read_csv(
          "pyfolio/tests/test_data/factor_loadings.csv",
          index_col=[0, 1],
          parse_dates=True,
      )
  
      factor_returns = pd.read_csv(
          "pyfolio/tests/test_data/factor_returns.csv",
          index_col=0,
          parse_dates=True,
      )
  
      residuals = pd.read_csv(
          "pyfolio/tests/test_data/residuals.csv",
          index_col=0,
          parse_dates=True,
      )
  
      residuals.columns = [int(col) for col in residuals.columns]
  
      intercepts = pd.read_csv(
          "pyfolio/tests/test_data/intercepts.csv",
          index_col=0,
          header=None,
          squeeze=True,
      )
  
      risk_exposures_portfolio, perf_attrib_output = perf_attrib(
          returns,
          positions,
          factor_returns,
          factor_loadings,
      )
  
      specific_returns = perf_attrib_output["specific_returns"]
      common_returns = perf_attrib_output["common_returns"]
      combined_returns = specific_returns + common_returns
  
      # since all returns are factor returns, common returns should be
      # equivalent to total returns, and specific returns should be 0
      pd.util.testing.assert_series_equal(
          returns, common_returns, check_names=False
      )
  
      self.assertTrue(np.isclose(specific_returns, 0).all())
  
      # specific and common returns combined should equal total returns
      pd.util.testing.assert_series_equal(
          returns, combined_returns, check_names=False
      )
  
      # check that residuals + intercepts = specific returns
      self.assertTrue(np.isclose((residuals + intercepts), 0).all())
  
      # check that exposure * factor returns = common returns
      expected_common_returns = risk_exposures_portfolio.multiply(
          factor_returns, axis="rows"
      ).sum(axis="columns")
  
      pd.util.testing.assert_series_equal(
          expected_common_returns, common_returns, check_names=False
      )
  
      # since factor loadings are ones, portfolio risk exposures
      # should be ones
      pd.util.testing.assert_frame_equal(
          risk_exposures_portfolio,
          pd.DataFrame(
              np.ones_like(risk_exposures_portfolio),
              index=risk_exposures_portfolio.index,
              columns=risk_exposures_portfolio.columns,
          ),
      )
  
      perf_attrib_summary, exposures_summary = create_perf_attrib_stats(
          perf_attrib_output, risk_exposures_portfolio
      )
  
      self.assertEqual(
          ep.annual_return(specific_returns),
          perf_attrib_summary["Annualized Specific Return"],
      )
  
      self.assertEqual(
          ep.annual_return(common_returns),
          perf_attrib_summary["Annualized Common Return"],
      )
  
      self.assertEqual(
          ep.annual_return(combined_returns),
          perf_attrib_summary["Annualized Total Return"],
      )
  
      self.assertEqual(
          ep.sharpe_ratio(specific_returns),
          perf_attrib_summary["Specific Sharpe Ratio"],
      )
  
      self.assertEqual(
          ep.cum_returns_final(specific_returns),
          perf_attrib_summary["Cumulative Specific Return"],
      )
  
      self.assertEqual(
          ep.cum_returns_final(common_returns),
          perf_attrib_summary["Cumulative Common Return"],
      )
  
      self.assertEqual(
          ep.cum_returns_final(combined_returns),
          perf_attrib_summary["Total Returns"],
      )
  
      avg_factor_exposure = risk_exposures_portfolio.mean().rename(
          "Average Risk Factor Exposure"
      )
      pd.util.testing.assert_series_equal(
          avg_factor_exposure,
          exposures_summary["Average Risk Factor Exposure"],
      )
  
      cumulative_returns_by_factor = pd.Series(
          [
              ep.cum_returns_final(perf_attrib_output[c])
              for c in risk_exposures_portfolio.columns
          ],
          name="Cumulative Return",
          index=risk_exposures_portfolio.columns,
      )
  
      pd.util.testing.assert_series_equal(
          cumulative_returns_by_factor,
          exposures_summary["Cumulative Return"],
      )
  
      annualized_returns_by_factor = pd.Series(
          [
              ep.annual_return(perf_attrib_output[c])
              for c in risk_exposures_portfolio.columns
          ],
          name="Annualized Return",
          index=risk_exposures_portfolio.columns,
      )
  
      pd.util.testing.assert_series_equal(
          annualized_returns_by_factor,
          exposures_summary["Annualized Return"],
      )
  
file_path: pyfolio/pyfolio/tests/test_perf_attrib.py
imports: import numpy
  import pandas
  import unittest
  import warnings
  import empyrical
  from pyfolio.perf_attrib import perf_attrib, create_perf_attrib_stats, _cumulative_returns_less_costs
label_desc: Use image node_id 2 for calling the PerfAttribTestCase obj's underlying member method code with example usage: obj.test_perf_attrib_regression() without return types