예제 #1
0
    def min_CVaR(self):
        prices = self.prices
        returns = self.returns
        cov = self.covariance

        ef = EfficientFrontier(None, cov)
        optimal_weights = ef.custom_objective(negative_cvar, returns)
        return optimal_weights, None
예제 #2
0
ef.portfolio_performance(verbose=True)

"""
Expected annual return: 20.0%
Annual volatility: 16.5%
Sharpe Ratio: 1.09
"""


# Custom objective
def utility_obj(weights, mu, cov_matrix, k=1):
    return -weights.dot(mu) + k * np.dot(weights.T, np.dot(cov_matrix, weights))


ef = EfficientFrontier(mu, S)
ef.custom_objective(utility_obj, ef.expected_returns, ef.cov_matrix, 1)
ef.portfolio_performance(verbose=True)

"""
Expected annual return: 40.1%
Annual volatility: 29.2%
Sharpe Ratio: 1.30
"""

ef.custom_objective(utility_obj, ef.expected_returns, ef.cov_matrix, 2)
ef.portfolio_performance(verbose=True)

"""
Expected annual return: 36.6%
Annual volatility: 24.7%
Sharpe Ratio: 1.39
예제 #3
0
import pandas as pd
import numpy as np
from pypfopt.efficient_frontier import EfficientFrontier
from pypfopt.objective_functions import negative_cvar
from pypfopt.risk_models import CovarianceShrinkage

##Importar série de dados
df = pd.read_excel(r'C:\Users\Jhona\OneDrive\Área de Trabalho\PRBR11.xlsx', index_col='Data')

##Obter os retornos dos ativos
returns = pd.DataFrame()
for i in df:
    returns[i] = df[i].pct_change().dropna()

##Criar a matrix de covariância eficiente
covMatrix = CovarianceShrinkage(df)
e_cov = covMatrix.ledoit_wolf()

##Fronteira
ef = EfficientFrontier(None, e_cov)

##Encontrando o CVaR 95% minimizando 
optimal_weights = ef.custom_objective(negative_cvar, returns)
print(optimal_weights)