# Optimal Portfolio from Efficient Frontier

I found this code on plotly site, using CVXOPT to find the efficient frontier, and then, the optimal Portfolio. The optimal function is

def optimal_portfolio(returns):
n = len(returns)
returns = np.asmatrix(returns)

N = 100
mus = [10**(5.0 * t/N - 1.0) for t in range(N)]

# Convert to cvxopt matrices
S = opt.matrix(np.cov(returns))
pbar = opt.matrix(np.mean(returns, axis=1))

# Create constraint matrices
G = -opt.matrix(np.eye(n))   # negative n x n identity matrix
h = opt.matrix(0.0, (n ,1))
A = opt.matrix(1.0, (1, n))
b = opt.matrix(1.0)

# Calculate efficient frontier weights using quadratic programming
portfolios = [solvers.qp(mu*S, -pbar, G, h, A, b)['x']
for mu in mus]
## CALCULATE RISKS AND RETURNS FOR FRONTIER
returns = [blas.dot(pbar, x) for x in portfolios]
risks = [np.sqrt(blas.dot(x, S*x)) for x in portfolios]
## CALCULATE THE 2ND DEGREE POLYNOMIAL OF THE FRONTIER CURVE
m1 = np.polyfit(returns, risks, 2)
x1 = np.sqrt(m1[2] / m1[0])
# CALCULATE THE OPTIMAL PORTFOLIO
wt = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']
return np.asarray(wt), returns, risks

weights, returns, risks = optimal_portfolio(return_vec)


My question refers to the lines where the code fits a parabola to the efficient frontier

m1 = np.polyfit(returns, risks, 2)


takes the square root of the division the intercept by the coefficient of the x-squared

x1 = np.sqrt(m1[2] / m1[0])


and puts it in the optimization

t = solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x']


Can anyone please shed light on why this is done?

Thanks!

• You should go through the mathematics of deriving the mean-variance frontier (There are versions all over the place, but here's an example.) Then see if what they're solving is correct/makes sense. For portfolios on the frontier, the variance of the portfolio return is quadratic in the expected return (which I assume is why they're fitting a 2nd degree polynomial). Feb 26, 2019 at 19:47

In quadratic equation of the form $$y= ax^2+bx+c = 0$$, while $$b=0$$ then $$+/-\sqrt{(c/a)}$$ is the values of cutting with the $$x$$-axis. Also, this is the solution of the equation. Using Vieta's formulas one can see that: $$x1*x2 = c/a$$ Also, using Trigonometric solution: $$x= \sqrt{(c/a)}*tan(\theta)$$ So maybe there is a need to rotate the axis in 90 degrees right to better understand it and change the axis of symmetry. And I guess there is a connection to focus of the parabola
Note that mus is not a series of expected return values; it is a series of 'weights' representing the risk aversion parameter, i.e., the relative importance of variance in the return-variance trade-off, also the Lagrange multiplier in a bi-criterion optimization problem. (See page 187, Figure 4.12 of the book Convex Optimization)
Compare solvers.qp(mu*S, -pbar, G, h, A, b)['x'] with solvers.qp(opt.matrix(x1 * S), -pbar, G, h, A, b)['x'], note that $$(x_1, x_2)=(\sqrt{c/a}, \sqrt{c/a})$$ is the vertex of the parabola, thus $$x_1$$ represents the risk aversion parameter that leads to the optimal portfolio with least std.