How to derive the weights of tangency portfolio?

I am well aware of this formula but I could not find how to derive this. Of course, I failed to derive (or prove) it by myself. I will appreciate if you guys provide me a good, detailed derivation.

• The answer in this post derive the weights for the tangency portfolio for $n$ assets.
– Pleb
Oct 6 '21 at 14:50

The derivation is simple but quite tedious. The tangency portfolio is found by maximizing the slope of the capital allocation line (CAL). The slope $$S_p$$ of the CAL is given by:

\begin{align}S_p=\frac{E[r_p]-r_f }{\sigma_p}\end{align}

In a 2-asset portfolio, the expected return $$E[r_p]$$ and variance $$\sigma_p^2$$ can be written as:

\begin{align} E[r_p] &= w_A E[r_A]+ (1-w_A)E[r_B] \\ \sigma_p^2 &=w_A^2 \sigma_A^2 +(1-w_A)^2 \sigma_B^2 +2w_A (1-w_A) \sigma_{A,B}\end{align}

Replacing these expressions in the slope formula, we get:

\begin{align} S_p =\frac{w_A E[r_A]+ (1-w_A)E[r_B] -r_f}{\sqrt{w_A^2 \sigma_A^2 +(1-w_A)^2 \sigma_B^2 +2w_A (1-w_A) \sigma_{A,B}}}\end{align}

Since we're looking for the portfolio for which $$S_p$$ is maximum, we need to solve:

\begin{align} w_A^*\equiv\arg \max \left\{ \frac{w_A E[r_A]+ (1-w_A)E[r_B] -r_f}{\sqrt{w_A^2 \sigma_A^2 +(1-w_A)^2 \sigma_B^2 +2w_A (1-w_A) \sigma_{A,B}}}\right\} \end{align}

Taking the derivative with respect to $$w_A$$ and setting it to zero will give you the solution to the optimal portfolio.