Skip to content

Commit 25def59

Browse files
Copilottschm
andcommitted
Add numpy-style docstrings to black_litterman, cla, and plotting modules
Co-authored-by: tschm <[email protected]>
1 parent a115163 commit 25def59

File tree

3 files changed

+442
-179
lines changed

3 files changed

+442
-179
lines changed

pypfopt/black_litterman.py

Lines changed: 64 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
"""
2-
The ``black_litterman`` module houses the BlackLittermanModel class, which
3-
generates posterior estimates of expected returns given a prior estimate and user-supplied
4-
views. In addition, two utility functions are defined, which calculate:
2+
The ``black_litterman`` module houses the BlackLittermanModel class.
3+
4+
This module generates posterior estimates of expected returns given a prior
5+
estimate and user-supplied views. In addition, two utility functions are
6+
defined, which calculate:
57
68
- market-implied prior estimate of returns
79
- market-implied risk-aversion parameter
@@ -21,25 +23,38 @@ def market_implied_prior_returns(
2123
):
2224
r"""
2325
Compute the prior estimate of returns implied by the market weights.
24-
In other words, given each asset's contribution to the risk of the market
25-
portfolio, how much are we expecting to be compensated?
26+
27+
Given each asset's contribution to the risk of the market portfolio,
28+
how much are we expecting to be compensated?
2629
2730
.. math::
2831
2932
\Pi = \delta \Sigma w_{mkt}
3033
31-
:param market_caps: market capitalisations of all assets
32-
:type market_caps: {ticker: cap} dict or pd.Series
33-
:param risk_aversion: risk aversion parameter
34-
:type risk_aversion: positive float
35-
:param cov_matrix: covariance matrix of asset returns
36-
:type cov_matrix: pd.DataFrame
37-
:param risk_free_rate: risk-free rate of borrowing/lending, defaults to 0.0.
38-
You should use the appropriate time period, corresponding
39-
to the covariance matrix.
40-
:type risk_free_rate: float, optional
41-
:return: prior estimate of returns as implied by the market caps
42-
:rtype: pd.Series
34+
Parameters
35+
----------
36+
market_caps : dict or pd.Series
37+
Market capitalisations of all assets, e.g., ``{ticker: cap}``.
38+
risk_aversion : float
39+
Risk aversion parameter (must be positive).
40+
cov_matrix : pd.DataFrame
41+
Covariance matrix of asset returns.
42+
risk_free_rate : float, optional
43+
Risk-free rate of borrowing/lending, defaults to 0.0.
44+
You should use the appropriate time period corresponding
45+
to the covariance matrix.
46+
47+
Returns
48+
-------
49+
pd.Series
50+
Prior estimate of returns as implied by the market caps.
51+
52+
Examples
53+
--------
54+
>>> from pypfopt import risk_models, black_litterman
55+
>>> # S = risk_models.sample_cov(prices)
56+
>>> # market_caps = {"AAPL": 1e12, "GOOG": 800e9, ...}
57+
>>> # pi = black_litterman.market_implied_prior_returns(market_caps, 1.0, S)
4358
"""
4459
if not isinstance(cov_matrix, pd.DataFrame):
4560
warnings.warn(
@@ -54,25 +69,43 @@ def market_implied_prior_returns(
5469

5570
def market_implied_risk_aversion(market_prices, frequency=252, risk_free_rate=0.0):
5671
r"""
57-
Calculate the market-implied risk-aversion parameter (i.e market price of risk)
58-
based on market prices. For example, if the market has excess returns of 10% a year
59-
with 5% variance, the risk-aversion parameter is 2, i.e you have to be compensated 2x
60-
the variance.
72+
Calculate the market-implied risk-aversion parameter.
73+
74+
Also known as the market price of risk. For example, if the market has
75+
excess returns of 10% a year with 5% variance, the risk-aversion parameter
76+
is 2, i.e., you have to be compensated 2x the variance.
6177
6278
.. math::
6379
6480
\delta = \frac{R - R_f}{\sigma^2}
6581
66-
:param market_prices: the (daily) prices of the market portfolio, e.g SPY.
67-
:type market_prices: pd.Series with DatetimeIndex.
68-
:param frequency: number of time periods in a year, defaults to 252 (the number
69-
of trading days in a year)
70-
:type frequency: int, optional
71-
:param risk_free_rate: annualised risk-free rate of borrowing/lending, defaults to 0.0.
72-
:type risk_free_rate: float, optional
73-
:raises TypeError: if market_prices cannot be parsed
74-
:return: market-implied risk aversion
75-
:rtype: float
82+
Parameters
83+
----------
84+
market_prices : pd.Series
85+
The (daily) prices of the market portfolio, e.g., SPY.
86+
Should have a DatetimeIndex.
87+
frequency : int, optional
88+
Number of time periods in a year, defaults to 252
89+
(the number of trading days in a year).
90+
risk_free_rate : float, optional
91+
Annualised risk-free rate of borrowing/lending, defaults to 0.0.
92+
93+
Returns
94+
-------
95+
float
96+
Market-implied risk aversion.
97+
98+
Raises
99+
------
100+
TypeError
101+
If market_prices cannot be parsed.
102+
103+
Examples
104+
--------
105+
>>> import pandas as pd
106+
>>> from pypfopt import black_litterman
107+
>>> # market_prices = pd.read_csv("spy_prices.csv", index_col="date", parse_dates=True)
108+
>>> # delta = black_litterman.market_implied_risk_aversion(market_prices)
76109
"""
77110
if not isinstance(market_prices, (pd.Series, pd.DataFrame)):
78111
raise TypeError("Please format market_prices as a pd.Series")

0 commit comments

Comments
 (0)