Skip to content

Commit 77588c6

Browse files
committed
towards testing README.md
1 parent efe145e commit 77588c6

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

README.md

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -256,20 +256,24 @@ The covariance matrix encodes not just the volatility of an asset, but also how
256256
- Long/short: by default all of the mean-variance optimization methods in PyPortfolioOpt are long-only, but they can be initialised to allow for short positions by changing the weight bounds:
257257

258258
```python
259-
ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
259+
>>> ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
260+
260261
```
261262

262263
- Market neutrality: for the `efficient_risk` and `efficient_return` methods, PyPortfolioOpt provides an option to form a market-neutral portfolio (i.e weights sum to zero). This is not possible for the max Sharpe portfolio and the min volatility portfolio because in those cases because they are not invariant with respect to leverage. Market neutrality requires negative weights:
263264

264265
```python
265-
ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
266-
ef.efficient_return(target_return=0.2, market_neutral=True)
266+
>>> ef = EfficientFrontier(mu, S, weight_bounds=(-1, 1))
267+
>>> ef.efficient_return(target_return=0.2, market_neutral=True)
268+
OrderedDict({'GOOG': 0.0747287764570896, 'AAPL': 0.0532061998403115, 'FB': 0.0663647763595121, 'BABA': 0.0115771487708806, 'AMZN': 0.051794511454659, 'GE': -0.0594560621731438, 'AMD': -0.0678975317682523, 'WMT': -0.0817205719345985, 'BAC': -0.1413007724407138, 'GM': -0.1402101962690842, 'T': -0.13713261204016, 'UAA': 0.0002656163909862, 'SHLD': -0.0705951831340284, 'XOM': -0.0775452287164678, 'RRC': -0.0510171940919588, 'BBY': 0.0349455362769414, 'MA': 0.375760614087238, 'PFE': 0.1111984245745791, 'JPM': 0.0140774027288155, 'SBUX': 0.0329563456273947})
269+
267270
```
268271

269272
- Minimum/maximum position size: it may be the case that you want no security to form more than 10% of your portfolio. This is easy to encode:
270273

271274
```python
272-
ef = EfficientFrontier(mu, S, weight_bounds=(0, 0.1))
275+
>>> ef = EfficientFrontier(mu, S, weight_bounds=(0, 0.1))
276+
273277
```
274278

275279
One issue with mean-variance optimization is that it leads to many zero-weights. While these are
@@ -278,9 +282,12 @@ mean-variance portfolios to underperform out-of-sample. To that end, I have intr
278282
objective function that can reduce the number of negligible weights for any of the objective functions. Essentially, it adds a penalty (parameterised by `gamma`) on small weights, with a term that looks just like L2 regularisation in machine learning. It may be necessary to try several `gamma` values to achieve the desired number of non-negligible weights. For the test portfolio of 20 securities, `gamma ~ 1` is sufficient
279283

280284
```python
281-
ef = EfficientFrontier(mu, S)
282-
ef.add_objective(objective_functions.L2_reg, gamma=1)
283-
ef.max_sharpe()
285+
>>> from pypfopt import objective_functions
286+
>>> ef = EfficientFrontier(mu, S)
287+
>>> ef.add_objective(objective_functions.L2_reg, gamma=1)
288+
>>> ef.max_sharpe()
289+
OrderedDict({'GOOG': 0.0819942016928946, 'AAPL': 0.0918509031802692, 'FB': 0.1073667333688086, 'BABA': 0.0680482478876387, 'AMZN': 0.1010796289877925, 'GE': 0.0309429468523964, 'AMD': 0.0, 'WMT': 0.0353042020828323, 'BAC': 0.0001739443220274, 'GM': 0.0, 'T': 0.0274224141523135, 'UAA': 0.0182927430888646, 'SHLD': 0.0, 'XOM': 0.0465545659178931, 'RRC': 0.0023903728743853, 'BBY': 0.0644567269626333, 'MA': 0.1426239959760212, 'PFE': 0.0840602539751452, 'JPM': 0.0279123528004041, 'SBUX': 0.0695257658776802})
290+
284291
```
285292

286293
### Black-Litterman allocation

tests/test_docs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ def readme_path() -> Path:
4747

4848
def test_doc(readme_path):
4949
"""Test the README file with doctest."""
50-
result = doctest.testfile(str(readme_path), module_relative=False, verbose=True, optionflags=doctest.ELLIPSIS)
50+
result = doctest.testfile(str(readme_path), module_relative=False, verbose=False, optionflags=doctest.ELLIPSIS)
5151
assert result.failed == 0

0 commit comments

Comments
 (0)