# How to maximize the Sharpe ratio given historical closing prices?

I have historical adjusted closing prices for $k$ stocks over $n$ days. I have a budget of $B$ dollars, and I'd like to choose allocations for each of the stocks, $a_{1:k}$, such that I maximize the Sharpe ratio for this time period.

More formally:

\begin{align*} \text{given } & c_{i, j} \text{ for } i=1...n, \; j=1...k && \text{adjusted close of stock } j \text{ on day } i \\ \text{and } & B && \text{total budget} \\ \text{find } & a_{1:k} && \text{allocations for each stock} \\ \text{that maximize } & s = \frac{\mu}{\sigma} && \text{Sharpe ratio} \\ \text{where } & \mu = \frac{1}{n} \sum_{i=1}^n r_i && \text{sample mean of the daily returns} \\ & \sigma = \sqrt{\frac{1}{n}\sum_{i=1}^n (r_i - \mu)^2} && \text{sample standard deviation of the daily returns} \\ & r_1 = 0 && \text{return on day one is zero} \\ & r_i = \frac{p_i}{p_{i-1}} - 1 \;\; \text{for } i=2...n && \text{percent change in portfolio value on day } i \\ & p_i = a^{\top} c_i && \text{portfolio value on day } i \\ \text{subject to } & a_j \geq 0 \text{ for } j = 1...k && \text{only non-negative allocations for each stock} \\ & \sum_{j=1}^k a_j = B && \text{must use total budget} \end{align*}

One way I tried solving this was simply setting $a_{1:k} = B \times \texttt{softmax}(w_{1:k})$ or $a_{1:k} = B \times \frac{\texttt{relu}(w_{1:k})}{\texttt{sum}(\texttt{relu}(w_{1:k}))}$, for some latent variables $w_{1:k}$, and then running gradient ascent using TensorFlow. This works well in practice, but I'm wondering if there is a better way (i.e., something with guaranteed convergence to the global maximum).

• Any assumptions on covariance of the assets? Is there a reason you can't use Markowitz' tangency portfolio with the constraints $w_i \geq 0$, $\forall i = 1, \ldots,k$ and $\sum_{i=1}^k w_i =1$? You will have perfect knowledge of the future so you can just pretend you are at day one and determine your portfolio weights. Sep 10, 2017 at 23:13
• You can easily do this in excel "solver" optimisation tool. Basically you would set it out in excel as a portfolio where the return of the portfolio is the weighted average return of all assets. Then calculate the sharpe ratio for the portfolio. Then using solver, you would maximise the sharpe ratio by changing asset weights constraining that the sum of weights equal 1. You can also do short sales in solver. Look up "optimal portfolio with excel solver" on YouTube you will find an example of what I am trying to explain to you. Sep 11, 2017 at 10:17

1. What you're looking for in known in finance and portfolio theory as the tangency portfolio.

2. Your formulation of the problem is imprecise/problematic.

• It's much cleaner to formulate this problem in terms of portfolio weights, returns, and covariance of returns, rather than with prices.
• Along those lines, where's the covariance matrix of returns? Portfolio variance with portfolio weights $w_1, \ldots, w_m$ can be written as $\sum_{ij} \operatorname{Cov}(r_i, r_j) w_i w_j$ or with covariance matrix $\Sigma$ and portfolio weight vector $\mathbf{w}$ as $\mathbf{w}' \Sigma \mathbf{w}$.
• Returns are $\frac{P_t + D_t}{P_{t-1}}$, that is, you must include distributions. (Use a data provider that calculates returns for you.)
3. There are excellent resources across the Internet on the basics of Markowitz portfolio theory.

4. The solution to this problem in its most basic form can be trivially found once you solve the linear system $\Sigma \mathbf{x} = \boldsymbol{\mu}$ for $\mathbf{x}$. Using gradient descent to solve this problem strikes me as a big bizarre.

5. In practice, a big problem is that your estimates of expected returns are garbage and estimates of covariance are poor. Garbage in, garbage out. Possible solutions? Explicit Bayesian methods or regularlization etc.... See Cochrane (2007) and his section on wacky weights.

### An intuitive, numerical approach with no short sale constraints

These these slides from Eric Zivot given an overview.

Financial portfolio theory defines the efficient frontier as the curve showing the lowest standard deviation that can achieve a given expected return.

Points along the efficient frontier can be found numerically by solving an optimization problem. To find a non-negative portfolio weight vector $\mathbf{w}\succeq \mathbf{0}$ that achieve expected return $\mu_p$ at minimum variance, solve:

$$$$\begin{array}{*2{>{\displaystyle}r}} \mbox{minimize (over \mathbf{w})} & \mathbf{w}'\Sigma\mathbf{w} \\ \mbox{subject to} & \mathbf{w}'\boldsymbol{\mu} = \mu_p \\ & \mathbf{w}'\mathbf{1} = 1 \\ & \mathbf{w} \succeq \mathbf{0} \end{array}$$$$ Then portfolio variance is $\sigma^2_p = \mathbf{w}' \Sigma \mathbf{w}$.

For reasonable number of assets, this quadratic programming problem can be solved close to instantaneously for a global solution. Starting with the minimum variance portfolio, you can solve then this problem multiple times with different $\mu_p$ values to trace out the efficient frontier, and then you can find the tangency portfolio.

### References:

Cochrane, John, "Portfolio Theory, 2007, "Portfolio Theory"

• But how do you handle the short sale constraint? In general, the tangency portfolio allows for negative weights. Sep 12, 2017 at 0:38
• @JohnM Yes, perhaps these slides help. Sep 13, 2017 at 14:52