code: def test_perf_attrib_simple(self):
      start_date = "2017-01-01"
      periods = 2
      dts = pd.date_range(start_date, periods=periods)
      dts.name = "dt"
  
      tickers = ["stock1", "stock2"]
      styles = ["risk_factor1", "risk_factor2"]
  
      returns = pd.Series(data=[0.1, 0.1], index=dts)
  
      factor_returns = pd.DataFrame(
          columns=styles,
          index=dts,
          data={"risk_factor1": [0.1, 0.1], "risk_factor2": [0.1, 0.1]},
      )
  
      positions = pd.DataFrame(
          index=dts,
          data={"stock1": [20, 20], "stock2": [50, 50], "cash": [0, 0]},
      )
  
      index = pd.MultiIndex.from_product(
          [dts, tickers], names=["dt", "ticker"]
      )
  
      factor_loadings = pd.DataFrame(
          columns=styles,
          index=index,
          data={
              "risk_factor1": [0.25, 0.25, 0.25, 0.25],
              "risk_factor2": [0.25, 0.25, 0.25, 0.25],
          },
      )
  
      expected_perf_attrib_output = (
          _empyrical_compat_perf_attrib_result(
              index=dts,
              columns=[
                  "risk_factor1",
                  "risk_factor2",
                  "total_returns",
                  "common_returns",
                  "specific_returns",
                  "tilt_returns",
                  "timing_returns",
              ],
              data={
                  "risk_factor1": [0.025, 0.025],
                  "risk_factor2": [0.025, 0.025],
                  "common_returns": [0.05, 0.05],
                  "specific_returns": [0.05, 0.05],
                  "tilt_returns": [0.05, 0.05],
                  "timing_returns": [0.0, 0.0],
                  "total_returns": returns,
              },
          )
      )
  
      expected_exposures_portfolio = pd.DataFrame(
          index=dts,
          columns=["risk_factor1", "risk_factor2"],
          data={
              "risk_factor1": [0.25, 0.25],
              "risk_factor2": [0.25, 0.25],
          },
      )
  
      exposures_portfolio, perf_attrib_output = perf_attrib(
          returns, positions, factor_returns, factor_loadings
      )
  
      pd.util.testing.assert_frame_equal(
          expected_perf_attrib_output, perf_attrib_output
      )
  
      pd.util.testing.assert_frame_equal(
          expected_exposures_portfolio, exposures_portfolio
      )
  
      # test long and short positions
      positions = pd.DataFrame(
          index=dts,
          data={
              "stock1": [20, 20],
              "stock2": [-20, -20],
              "cash": [20, 20],
          },
      )
  
      exposures_portfolio, perf_attrib_output = perf_attrib(
          returns, positions, factor_returns, factor_loadings
      )
  
      expected_perf_attrib_output = (
          _empyrical_compat_perf_attrib_result(
              index=dts,
              columns=[
                  "risk_factor1",
                  "risk_factor2",
                  "total_returns",
                  "common_returns",
                  "specific_returns",
                  "tilt_returns",
                  "timing_returns",
              ],
              data={
                  "risk_factor1": [0.0, 0.0],
                  "risk_factor2": [0.0, 0.0],
                  "common_returns": [0.0, 0.0],
                  "specific_returns": [0.1, 0.1],
                  "tilt_returns": [0.0, 0.0],
                  "timing_returns": [0.0, 0.0],
                  "total_returns": returns,
              },
          )
      )
  
      expected_exposures_portfolio = pd.DataFrame(
          index=dts,
          columns=["risk_factor1", "risk_factor2"],
          data={"risk_factor1": [0.0, 0.0], "risk_factor2": [0.0, 0.0]},
      )
  
      pd.util.testing.assert_frame_equal(
          expected_perf_attrib_output, perf_attrib_output
      )
  
      pd.util.testing.assert_frame_equal(
          expected_exposures_portfolio, exposures_portfolio
      )
  
      perf_attrib_summary, exposures_summary = create_perf_attrib_stats(
          perf_attrib_output, exposures_portfolio
      )
  
      self.assertEqual(
          perf_attrib_summary["Annualized Specific Return"],
          perf_attrib_summary["Annualized Total Return"],
      )
  
      self.assertEqual(
          perf_attrib_summary["Cumulative Specific Return"],
          perf_attrib_summary["Total Returns"],
      )
  
      pd.util.testing.assert_frame_equal(
          exposures_summary,
          pd.DataFrame(
              0.0,
              index=["risk_factor1", "risk_factor2"],
              columns=[
                  "Average Risk Factor Exposure",
                  "Annualized Return",
                  "Cumulative 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 1 for calling the PerfAttribTestCase obj's underlying member method code with example usage: obj.test_perf_attrib_simple() without return types