robertmartin8 / PyPortfolioOpt

Financial portfolio optimisation in python, including classical efficient frontier, Black-Litterman, Hierarchical Risk Parity
https://pyportfolioopt.readthedocs.io/
MIT License
4.24k stars 927 forks source link

The function could black_litterman.market_implied_prior_return should be revised #574

Open siliconMagic opened 5 months ago

siliconMagic commented 5 months ago

Describe the bug The parameter cov_matrix required to be pd.DataFrame, but matrix multiplier may be error due to the issue of matrix align. But if we pass the values of matrix to cov_matrix, the warnings show.

Expected behavior No warnings, no errors.

Code sample Just a little change could be useful

 if not isinstance(cov_matrix, pd.DataFrame):
        warnings.warn(
            "If cov_matrix is not a dataframe, market cap index must be aligned to cov_matrix",
            RuntimeWarning,
        )
    mcaps = pd.Series(market_caps)
    mkt_weights = mcaps / mcaps.sum()
    # Pi is excess returns so must add risk_free_rate to get return.
    return risk_aversion * cov_matrix.values.dot(mkt_weights) + risk_free_rate # change here!

Also, the parameter risk_free_rate should be used with frequency. For example, if we use weekly data, then the extra parameter such as freq=52 should be provided by users, and the code should be added:

risk_free_rate = risk_free_rate / freq

Operating system, python version, PyPortfolioOpt version Win 11, python 3.8.15, PyPortfolioOpt 1.2.6