-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsharpe_ratio.py
104 lines (104 loc) · 5.49 KB
/
sharpe_ratio.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import numpy as np
import pandas as pd
import plotly.graph_objs as go
from scipy.optimize import minimize
# Function to calculate portfolio returns and standard deviation
def portfolioPerformance(weights, meanReturns, covMatrix):
returns = np.sum(meanReturns * weights) * len(meanReturns) # Assuming meanReturns is a pandas Series
std = np.sqrt(np.dot(weights.T, np.dot(covMatrix, weights))) * np.sqrt(len(meanReturns))
return returns, std
# Function to calculate the negative Sharpe ratio
def negativeSR(weights, meanReturns, covMatrix, riskFreeRate=0):
pReturns, pStd = portfolioPerformance(weights, meanReturns, covMatrix)
return -(pReturns - riskFreeRate) / pStd
# Function to find the portfolio with maximum Sharpe Ratio
def maxSR(meanReturns, covMatrix, riskFreeRate=0, constraintSet=(0, 0.1)):
numAssets = len(meanReturns)
args = (meanReturns, covMatrix, riskFreeRate)
constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bound = constraintSet
bounds = tuple(bound for asset in range(numAssets))
result = minimize(negativeSR, numAssets * [1. / numAssets], args=args, method='SLSQP', bounds=bounds, constraints=constraints)
return result
# Function to calculate the portfolio variance
def portfolioVariance(weights, meanReturns, covMatrix):
return portfolioPerformance(weights, meanReturns, covMatrix)[1]
# Function to minimize the portfolio variance (for Min Vol portfolio)
def minimizeVariance(meanReturns, covMatrix, constraintSet=(0, 1)):
numAssets = len(meanReturns)
args = (meanReturns, covMatrix)
constraints = ({'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bound = constraintSet
bounds = tuple(bound for asset in range(numAssets))
result = minimize(portfolioVariance, numAssets * [1. / numAssets], args=args, method='SLSQP', bounds=bounds, constraints=constraints)
return result
# Function to calculate the portfolio return
def portfolioReturn(weights, meanReturns, covMatrix):
return portfolioPerformance(weights, meanReturns, covMatrix)[0]
# Function to calculate the Efficient Frontier
def efficientOpt(meanReturns, covMatrix, returnTarget, constraintSet=(0, 1)):
numAssets = len(meanReturns)
args = (meanReturns, covMatrix)
constraints = ({'type': 'eq', 'fun': lambda x: portfolioReturn(x, meanReturns, covMatrix) - returnTarget},
{'type': 'eq', 'fun': lambda x: np.sum(x) - 1})
bound = constraintSet
bounds = tuple(bound for asset in range(numAssets))
effOpt = minimize(portfolioVariance, numAssets * [1. / numAssets], args=args, method='SLSQP', bounds=bounds, constraints=constraints)
return effOpt
# Function to calculate all necessary results
def calculatedResults(meanReturns, covMatrix, riskFreeRate=0, constraintSet=(0, 1)):
maxSR_Portfolio = maxSR(meanReturns, covMatrix)
maxSR_returns, maxSR_std = portfolioPerformance(maxSR_Portfolio['x'], meanReturns, covMatrix)
maxSR_returns, maxSR_std = round(maxSR_returns * 100, 2), round(maxSR_std * 100, 2)
maxSR_allocation = pd.DataFrame(maxSR_Portfolio['x'], index=meanReturns.index, columns=['allocation'])
maxSR_allocation.allocation = [round(i * 100, 0) for i in maxSR_allocation.allocation]
minVol_Portfolio = minimizeVariance(meanReturns, covMatrix)
minVol_returns, minVol_std = portfolioPerformance(minVol_Portfolio['x'], meanReturns, covMatrix)
minVol_returns, minVol_std = round(minVol_returns * 100, 2), round(minVol_std * 100, 2)
minVol_allocation = pd.DataFrame(minVol_Portfolio['x'], index=meanReturns.index, columns=['allocation'])
minVol_allocation.allocation = [round(i * 100, 0) for i in minVol_allocation.allocation]
efficientList = []
targetReturns = np.linspace(minVol_returns, maxSR_returns, 20)
for target in targetReturns:
efficientList.append(efficientOpt(meanReturns, covMatrix, target)['fun'])
return maxSR_returns, maxSR_std, maxSR_allocation, minVol_returns, minVol_std, minVol_allocation, efficientList, targetReturns
# Function to plot the Efficient Frontier, Min Vol, and Max SR
def EF_graph(meanReturns, covMatrix, riskFreeRate=0, constraintSet=(0, 1)):
maxSR_returns, maxSR_std, maxSR_allocation, minVol_returns, minVol_std, minVol_allocation, efficientList, targetReturns = calculatedResults(meanReturns, covMatrix, riskFreeRate, constraintSet)
MaxSharpeRatio = go.Scatter(
name='Maximum Sharpe Ratio',
mode='markers',
x=[maxSR_std],
y=[maxSR_returns],
marker=dict(color='red', size=14, line=dict(width=3, color='black'))
)
MinVol = go.Scatter(
name='Minimum Volatility',
mode='markers',
x=[minVol_std],
y=[minVol_returns],
marker=dict(color='green', size=14, line=dict(width=3, color='black'))
)
EF_curve = go.Scatter(
name='Efficient Frontier',
mode='lines',
x=[round(ef_std * 100, 2) for ef_std in efficientList],
y=[round(target * 100, 2) for target in targetReturns],
line=dict(color='black', width=4, dash='dashdot')
)
data = [MaxSharpeRatio, MinVol, EF_curve]
layout = go.Layout(
title='Portfolio Optimisation with the Efficient Frontier',
yaxis=dict(title='Annualised Return (%)'),
xaxis=dict(title='Annualised Volatility (%)'),
showlegend=True,
legend=dict(
x=0.75, y=0, traceorder='normal',
bgcolor='#E2E2E2',
bordercolor='black',
borderwidth=2),
width=800,
height=600
)
fig = go.Figure(data=data, layout=layout)
fig.show()