예제 #1
0
    def test_risk_analysis(self):
        n_samples = 36000
        n_dates = 20
        n_risk_factors = 35

        dates = np.sort(np.random.randint(n_dates, size=n_samples))
        weights_series = pd.Series(data=np.random.randn(n_samples),
                                   index=dates)
        bm_series = pd.Series(data=np.random.randn(n_samples), index=dates)
        next_bar_return_series = pd.Series(data=np.random.randn(n_samples),
                                           index=dates)
        risk_table = pd.DataFrame(data=np.random.randn(n_samples,
                                                       n_risk_factors),
                                  columns=list(range(n_risk_factors)),
                                  index=dates)

        explained_table, _ = risk_analysis(weights_series - bm_series,
                                           next_bar_return_series, risk_table)

        to_explain = (weights_series - bm_series).multiply(
            next_bar_return_series, axis=0)
        aggregated = explained_table.sum(axis=1)

        np.testing.assert_array_almost_equal(to_explain.values,
                                             aggregated.values)
예제 #2
0
def perf_attribution_by_pos(net_weight_series: pd.Series,
                            next_bar_return_series: pd.Series,
                            benchmark_table: pd.DataFrame) -> pd.DataFrame:
    explained_table, _ = risk_analysis(net_weight_series,
                                       next_bar_return_series, benchmark_table)
    return explained_table.groupby(level=0).sum()