skfolio icon indicating copy to clipboard operation
skfolio copied to clipboard

[BUG] risk_free_rate argument does not seem to work

Open avrenli2 opened this issue 1 year ago • 2 comments

It seems the MeanRisk class ignores the input value of risk_free_rate and keeps it fixed at 0 no matter what the user inputs. You can run the example below and notice that the risk free rate is printed as 0 even if it is input as 0.02/252.

You can run the code below.


from yfinance import download
DATA = download(["TSLA","SBUX","CAKE"], start="2019-5-1", end="2024-5-1")
ADJUSTED = DATA["Adj Close"]
from skfolio.preprocessing import prices_to_returns
RETURNS = prices_to_returns(ADJUSTED)
from skfolio.optimization import MeanRisk, ObjectiveFunction
MODEL = MeanRisk(objective_function=ObjectiveFunction.MAXIMIZE_RATIO , risk_free_rate=0.02/252)
P = MODEL.fit_predict(RETURNS)
print( P.risk_free_rate)

Expected behavior

The printed risk free rate should not equal 0.

avrenli2 avatar May 05 '24 18:05 avrenli2

Hello, for a quick fix you can explicitly pass the risk free rate to the predicted portfolio using portfolio_params:

model = MeanRisk(
    objective_function=ObjectiveFunction.MAXIMIZE_RATIO,
    risk_free_rate=0.02 / 252,
    portfolio_params=dict(risk_free_rate=0.02 / 252)
)

The risk_free_rate of estimators like MeanRisk is only used for the optimization that involves risk measures that are a function of risk free rates like the Sharpe Ratio. For improved usability, optimization parameters that are also found in Portfolio parameters are automatically passed to the predicted Portfolio unless already provided by portfolio_params (this includes transaction_costs, management_fees and previous_weights). I'll add the risk free rate to that list.

HugoDelatte avatar May 05 '24 21:05 HugoDelatte

Thank you. skfolio seems much more efficient than both PyPortfolioOpt and Riskfolio-Lib. I decided to stop teaching Riskfolio-Lib and switch to skfolio even though it is still in development phase.

avrenli2 avatar May 05 '24 21:05 avrenli2