Skip to content

Commit

Permalink
Fix Sharpe ratio example code (#115)
Browse files Browse the repository at this point in the history
- Fix docs figure code and clarify 'risk' meaning
  • Loading branch information
etowle authored Jul 31, 2023
1 parent d88e9ab commit 6de867a
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Binary file modified docs/source/mods/figures/sharpe-ratio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 7 additions & 6 deletions docs/source/mods/sharpe-ratio.rst
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,9 @@ One can optionally pass in ``rf_rate``, the non-negative risk-free return rate :
The ``max_sharpe_ratio`` function returns a :class:`~gurobi_optimods.sharpe_ratio.SharpeRatioResult` instance. This object contains information about the computed portfolio. It features the following attributes:

* ``x``: The portfolio that maximizes the Sharpe ratio. The values in the portfolio represent the relative weights that should be allocated to each asset. These weights sum to 1. If ``cov_matrix`` and/or ``mu`` were given as a pandas object, the portfolio is a :class:`pandas.Series`. Otherwise, the portfolio is a :class:`numpy.ndarray`.
* ``sharpe_ratio``: The Sharpe ratio of the optimal portfolio.
* ``ret``: The estimated return of the optimal portfolio.
* ``risk``: The estimated risk of the optimal portfolio.
* ``sharpe_ratio``: The Sharpe ratio :math:`(\mu^\top x - r_f) / \sqrt{x^\top \Sigma x}` of the optimal portfolio.
* ``ret``: The estimated return :math:`\mu^\top x` of the optimal portfolio.
* ``risk``: The estimated risk :math:`x^\top \Sigma x` of the optimal portfolio.

Example code
------------
Expand Down Expand Up @@ -199,6 +199,7 @@ In the code below, we randomly generate 10,000 portfolios for the six assets fro

.. code-block:: Python
import math
import numpy as np
import matplotlib.pyplot as plt
Expand All @@ -209,7 +210,7 @@ In the code below, we randomly generate 10,000 portfolios for the six assets fro
wts = np.random.random((num_portfolios, num_assets))
wts = wts / wts.sum(axis=1, keepdims=1)
# Calculate return, risk, and Sharpe ratio of each random portfolio
# Calculate return, standard deviation, and Sharpe ratio of each random portfolio
risks = np.array([np.sqrt(wts[i] @ data.cov_matrix @ wts[i]) for i in range(num_portfolios)])
returns = wts @ data.mu
sharpe_ratios = returns / risks
Expand All @@ -218,9 +219,9 @@ In the code below, we randomly generate 10,000 portfolios for the six assets fro
plt.figure(figsize=(10, 8))
plt.scatter(risks, returns, c=sharpe_ratios, cmap="plasma")
plt.colorbar(label="Sharpe ratio")
plt.xlabel("Risk")
plt.xlabel("Risk (standard deviation)")
plt.ylabel("Return")
plt.scatter(portfolio.risk, portfolio.ret, marker="*", c="red", s=200)
plt.scatter(math.sqrt(portfolio.risk), portfolio.ret, marker="*", c="red", s=200)
plt.show()
Expand Down

0 comments on commit 6de867a

Please sign in to comment.