Skip to content

Commit 19b59ca

Browse files
Translate tabs to spaces in efficient frontier algo
1 parent ba351c0 commit 19b59ca

File tree

1 file changed

+15
-15
lines changed

1 file changed

+15
-15
lines changed

lib/efficient_frontier.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -135,22 +135,22 @@ def _solve_frontier(R, C, rf):
135135
:param rf: risk-free rate
136136
"""
137137
def fitness(W, R, C, r):
138-
# For given level of return r, find weights which minimizes portfolio variance.
139-
mean, var = _port_mean_var(W, R, C)
140-
# Big penalty for not meeting stated portfolio return effectively serves as optimization constraint
141-
penalty = 50*abs(mean-r)
142-
return var + penalty
138+
# For given level of return r, find weights which minimizes portfolio variance.
139+
mean, var = _port_mean_var(W, R, C)
140+
# Big penalty for not meeting stated portfolio return effectively serves as optimization constraint
141+
penalty = 50*abs(mean-r)
142+
return var + penalty
143143
frontier_mean, frontier_var, frontier_weights = [], [], []
144144
n = len(R) # Number of assets in the portfolio
145145
for r in linspace(min(R), max(R), num=NUMBER_PORTFOLIOS_TO_GENERATE): # Iterate through the range of returns on Y axis
146-
W = ones([n])/n # Start optimization with equal weights
147-
b_ = [(0,1) for i in range(n)]
148-
c_ = ({'type':'eq', 'fun': lambda W: sum(W)-1. })
149-
optimized = scipy.optimize.minimize(fitness, W, (R, C, r), method='SLSQP', constraints=c_, bounds=b_)
150-
if not optimized.success:
151-
raise BaseException(optimized.message)
152-
# Add point to the min-var frontier [x,y] = [optimized.x, r]
153-
frontier_mean.append(r) # return
154-
frontier_var.append(_port_var(optimized.x, C)) # min-variance based on optimized weights
155-
frontier_weights.append(optimized.x)
146+
W = ones([n])/n # Start optimization with equal weights
147+
b_ = [(0,1) for i in range(n)]
148+
c_ = ({'type':'eq', 'fun': lambda W: sum(W)-1. })
149+
optimized = scipy.optimize.minimize(fitness, W, (R, C, r), method='SLSQP', constraints=c_, bounds=b_)
150+
if not optimized.success:
151+
raise BaseException(optimized.message)
152+
# Add point to the min-var frontier [x,y] = [optimized.x, r]
153+
frontier_mean.append(r) # return
154+
frontier_var.append(_port_var(optimized.x, C)) # min-variance based on optimized weights
155+
frontier_weights.append(optimized.x)
156156
return array(frontier_mean), array(frontier_var), frontier_weights

0 commit comments

Comments
 (0)