Professional Documents
Culture Documents
Portfolio Optimization For Minimum Risk With Scipy - Efficient Frontier Explained
Portfolio Optimization For Minimum Risk With Scipy - Efficient Frontier Explained
Portfolio Optimization For Minimum Risk With Scipy - Efficient Frontier Explained
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 1/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
Environment setup
Follow step 1 in this post
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 2/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
Code
scatter plotting the individual asset risk-return
def get_risk(prices):
return (prices / prices.shift(1) -
1).dropna().std().values
def get_return(prices):
return ((prices / prices.shift(1) - 1).dropna().mean() *
np.sqrt(250)).values
prices = prices.dropna()
risk_v = get_risk(prices)
return_v = get_return(prices)
fig, ax = plt.subplots()
ax.scatter(x=risk_v, y=return_v, alpha=0.5)
ax.set(title='Return and Risk', xlabel='Risk',
ylabel='Return')
plt.show()
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 3/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
Return and risk for BA (Boeing), C (Citigroup), AAL (American Airlines Group), NFLX (Net ix)
def random_weights(n):
weights = np.random.rand(n)
return weights / sum(weights)
risk_all = np.array([])
return_all = np.array([])
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 4/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
fig, ax = plt.subplots()
ax.scatter(x=risk_all, y=return_all, alpha=0.5)
ax.set(title='Return and Risk', xlabel='Risk',
ylabel='Return')
Risk comes from not knowing what you are doing — Warren Bu ett
Let’s look at the risk and return. we do see that there are blue dots
outside of the area. So diversi cation does help reduce the risk. But
how do we nd the best or e cient weight?
Return and risk of a portfolio of random weighted BA (Boeing), C (Citigroup), AAL (American Airlines
Group), NFLX (Net ix) (3000 samples)
E cient Frontier
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 5/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
Here we will use scipy’s optimizer to get optimal weights for di erent
targeted return. Note that, we have bounds that make sure weight are
in range [0, 1] and constraints to ensure sum of weights is 1, also
portfolio return meets our target return. With all this condition, scipy
optimizer is able to nd the best allocation.
# optimizer
def optimize(prices, symbols, target_return=0.1):
normalized_prices = prices / prices.ix[0, :]
init_guess = np.ones(len(symbols)) * (1.0 /
len(symbols))
bounds = ((0.0, 1.0),) * len(symbols)
weights = minimize(get_portfolio_risk, init_guess,
args=(normalized_prices,),
method='SLSQP',
options={'disp': False},
constraints=({'type': 'eq', 'fun':
lambda inputs: 1.0 - np.sum(inputs)},
{'type': 'eq', 'args':
(normalized_prices,),
'fun': lambda inputs,
normalized_prices:
target_return -
get_portfolio_return(weights=inputs,
normalized_prices=normalized_prices)}),
bounds=bounds)
return weights.x
optimal_risk_all = np.array([])
optimal_return_all = np.array([])
for target_return in np.arange(0.005, .0402, .0005):
opt_w = optimize(prices=prices, symbols=symbols,
target_return=target_return)
optimal_risk_all = np.append(optimal_risk_all,
get_portfolio_risk(opt_w, normalized_prices))
optimal_return_all = np.append(optimal_return_all,
get_portfolio_return(opt_w, normalized_prices))
# plotting
fig, ax = plt.subplots()
# random portfolio risk return
ax.scatter(x=risk_all, y=return_all, alpha=0.5)
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 6/9
3/4/2019 Portfolio Optimization for Minimum Risk with Scipy — Efficient Frontier Explained
The green line indicate the e cient frontier. Now we know the best
allocation with a given target return. Next question, what is the best
allocation overall? Portfolio performance can be evaluated with
return/risk ratio (known as Sharpe Ratio). High Sharpe Ratio indicates
good balance of return and risk. This allocation can be found by
drawing a Capital Allocation line that tangent to the e cient frontier.
The tangent point is the allocation yields highest Sharpe ratio. To learn
more, you can take a look at this article on how to nd the highest
sharpe ratio from the e cient frontier with Capital Allocation Line
(CAL).
E cient frontier — Return and risk of optimal asset-weights at di erent targeted returns
That’s it. Now we know the theory of diversi cation. Happy investment
and coding!
https://towardsdatascience.com/efficient-frontier-optimize-portfolio-with-scipy-57456428323e 7/9