Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions docs/RiskModels.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,24 @@ covariance.

.. autofunction:: corr_to_cov

Pass standard deviations in the same asset order as both axes of the
correlation matrix. The function uses their values by position, even
when the standard deviations are a labeled pandas Series.

Align the Series before calling the function when the orders differ:

.. code-block:: python

import pandas as pd
from pypfopt.risk_models import corr_to_cov

assets = ["Asset A", "Asset B"]
corr = pd.DataFrame([[1.0, 0.5], [0.5, 1.0]], index=assets, columns=assets)
stdevs = pd.Series({"Asset B": 0.30, "Asset A": 0.10})

cov = corr_to_cov(corr, stdevs.loc[corr.columns])
# Diagonal variances in matrix order: 0.01, 0.09.



Shrinkage estimators
Expand Down
3 changes: 2 additions & 1 deletion tests/test_risk_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,8 @@ def test_corr_to_cov():
df = get_data()
rets = risk_models.returns_from_prices(df).dropna()
test_corr = risk_models.cov_to_corr(rets.cov())
new_cov = risk_models.corr_to_cov(test_corr, rets.std())
stdevs = rets.std().iloc[::-1]
new_cov = risk_models.corr_to_cov(test_corr, stdevs.loc[test_corr.columns])
pd.testing.assert_frame_equal(new_cov, rets.cov())

with pytest.warns(RuntimeWarning) as w:
Expand Down