Exemple #1
0
    def test_get_distance_to_equal_risk_contrib(self):
        factors_covariance = self.factors_df.cov()
        weights = pd.Series([0.5, 0.5], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_distance_to_equal_risk_contrib(factors_covariance, weights)
        expected_result = 0.342309791791
        self.assertAlmostEqual(actual_result, expected_result, places=10)

        weights = pd.Series([0.25, 0.75], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_distance_to_equal_risk_contrib(factors_covariance, weights)
        expected_result = 0.72012126510882146
        self.assertAlmostEqual(actual_result, expected_result, places=10)
Exemple #2
0
    def test_get_risk_contribution_optimised(self):
        weights = pd.Series([0.5, 0.5], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_risk_contribution_optimised(assets_rets=self.factors_df,
                                                                                 weights_of_assets=weights)
        expected_result = pd.Series([0.328845104104390, 0.671154895895610], index=self.factors_df.columns)
        assert_series_equal(expected_result, actual_result)

        weights = pd.Series([0.25, 0.75], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_risk_contribution_optimised(assets_rets=self.factors_df,
                                                                                 weights_of_assets=weights)
        expected_result = pd.Series([0.139939367445589, 0.860060632554411], index=self.factors_df.columns)
        assert_series_equal(expected_result, actual_result)
Exemple #3
0
    def test_is_equal_risk_contribution(self):
        asset_a_tms = self.factors_df.loc[:, 'a']
        factors_df = pd.concat((asset_a_tms, asset_a_tms), axis=1)
        factors_df = cast_dataframe(factors_df, SimpleReturnsDataFrame)
        factors_df.columns = ['a', 'b']
        factors_covariance = factors_df.cov()

        weights = pd.Series([0.25, 0.75], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.is_equal_risk_contribution(factors_covariance, weights)
        self.assertFalse(actual_result)

        weights = pd.Series([0.5, 0.5], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.is_equal_risk_contribution(factors_covariance, weights)
        self.assertTrue(actual_result)
Exemple #4
0
    def test_get_risk_contribution(self):
        weights = pd.Series([0.5, 0.5], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_risk_contribution(factors_rets=self.factors_df,
                                                                       weigths_of_assets=weights,
                                                                       portfolio_rets=self.portfolio_tms)
        expected_result = pd.Series([0.32739478440485410, 0.672605215595146], index=self.factors_df.columns)
        assert_series_equal(expected_result, actual_result)

        weights = pd.Series([0.25, 0.75], index=self.factors_df.columns)
        actual_result = RiskContributionAnalysis.get_risk_contribution(factors_rets=self.factors_df,
                                                                       weigths_of_assets=weights,
                                                                       portfolio_rets=self.portfolio_tms)
        expected_result = pd.Series([0.139601453264340, 0.860398546735660], index=self.factors_df.columns)
        assert_series_equal(expected_result, actual_result)
Exemple #5
0
    def setup(self):
        self._calc_coefficients()
        self._setup_return_analysis_of_fund_and_fit(self.fitted_tms)
        self._setup_out_of_sample_fit()

        residuals = self.fit_model.resid
        self.durbin_watson_test = durbin_watson(residuals)

        regressors_df = self.input_data.regressors_df
        analysed_tms = self.input_data.analysed_tms
        self.risk_contribution = RiskContributionAnalysis.get_risk_contribution(
            regressors_df, self.coefficients, analysed_tms)

        factors_perf_attrib, unexplained_perf_attrib = ReturnAttributionAnalysis.get_factor_return_attribution(
            analysed_tms, self.fitted_tms, regressors_df, self.coefficients,
            self.intercept)
        self.factors_performance_attribution_ret = factors_perf_attrib
        self.unexplained_performance_attribution_ret = unexplained_perf_attrib

        self._setup_correlations(self.fitted_tms)
        self.condition_number = cond(regressors_df.values)
        self._setup_r_square_of_each_predictor()
        self._setup_autocorrelation(residuals)
        _, _, _, self.heteroskedasticity = het_breuschpagan(
            residuals, self.fit_model.model.exog)
        self._setup_cooks_distance(self.fit_model)
Exemple #6
0
    def get_weights(self) -> QFSeries:
        def minimised_func(weights_values: Sequence[float]):
            weights_series = QFSeries(data=weights_values,
                                      index=self.cov_matrix.columns)
            return RiskContributionAnalysis.get_distance_to_equal_risk_contrib(
                self.cov_matrix, weights_series)

        weights = NonlinearFunctionOptimizer.get_weights(
            minimised_func,
            max_iter=self.max_iter,
            upper_constraints=self.upper_constraint,
            num_of_assets=self.cov_matrix.shape[1])
        weights = QFSeries(data=weights, index=self.cov_matrix.columns.copy())

        if not RiskContributionAnalysis.is_equal_risk_contribution(
                self.cov_matrix, weights):
            self.logger.warning(
                "EqualRiskContributionPortfolio: calculated weights do not create an ERC Portfolio."
            )

        return weights
Exemple #7
0
 def minimised_func(weights_values: Sequence[float]):
     weights_series = QFSeries(data=weights_values,
                               index=self.cov_matrix.columns)
     return RiskContributionAnalysis.get_distance_to_equal_risk_contrib(
         self.cov_matrix, weights_series)