-
Notifications
You must be signed in to change notification settings - Fork 83
/
cla_mlf.py
630 lines (525 loc) · 25.5 KB
/
cla_mlf.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# from https://github.com/hudson-and-thames/mlfinlab
# license: https://github.com/hudson-and-thames/mlfinlab/blob/master/LICENSE.txt
'''
This module implements the famous Critical Line Algorithm for mean-variance portfolio
optimisation. It is reproduced with modification from the following paper:
`D.H. Bailey and M.L. Prado “An Open-Source Implementation of the Critical- Line Algorithm for
Portfolio Optimization”,Algorithms, 6 (2013), 169-196. <http://dx.doi.org/10.3390/a6010169>`_
'''
import numbers
from math import log, ceil
import numpy as np
import pandas as pd
class CLA:
# pylint: disable=too-many-instance-attributes
'''
CLA is a famous portfolio optimisation algorithm used for calculating the optimal allocation weights for a given
portfolio. It solves the optimisation problem with constraints on each weight - lower and upper bounds on the weight
value. This class can compute multiple types of solutions - the normal cla solution, minimum variance solution,
maximum sharpe solution and finally the solution to the efficient frontier.
'''
def __init__(self, weight_bounds=(0, 1), calculate_returns="mean"):
'''
Initialise the storage arrays and some preprocessing.
:param weight_bounds: (tuple) a tuple specifying the lower and upper bound ranges for the portfolio weights
:param calculate_returns: (str) the method to use for calculation of expected returns.
Currently supports "mean" and "exponential"
'''
self.weight_bounds = weight_bounds
self.calculate_returns = calculate_returns
self.weights = list()
self.lambdas = list()
self.gammas = list()
self.free_weights = list()
self.expected_returns = None
self.cov_matrix = None
self.lower_bounds = None
self.upper_bounds = None
self.max_sharpe = None
self.min_var = None
self.efficient_frontier_means = None
self.efficient_frontier_sigma = None
@staticmethod
def _infnone(number):
'''
Converts a Nonetype object to inf
:param number: (int/float/None) a number
:return: (float) -inf or number
'''
return float("-inf") if number is None else number
def _init_algo(self):
'''
Initial setting up of the algorithm. Calculates the first free weight of the first turning point.
:return: (list, list) asset index and the corresponding free weight value
'''
# Form structured array
structured_array = np.zeros((self.expected_returns.shape[0]), dtype=[("id", int), ("mu", float)])
expected_returns = [self.expected_returns[i][0] for i in range(self.expected_returns.shape[0])] # dump array into list
# Fill structured array
structured_array[:] = list(zip(list(range(self.expected_returns.shape[0])), expected_returns))
# Sort structured array based on increasing return value
expected_returns = np.sort(structured_array, order="mu")
# First free weight
index, weights = expected_returns.shape[0], np.copy(self.lower_bounds)
while np.sum(weights) < 1:
index -= 1
# Set weights one by one to the upper bounds
weights[expected_returns[index][0]] = self.upper_bounds[expected_returns[index][0]]
weights[expected_returns[index][0]] += 1 - np.sum(weights)
return [expected_returns[index][0]], weights
@staticmethod
def _compute_bi(c_final, asset_bounds_i):
'''
Calculates which bound value to assign to a bounded asset - lower bound or upper bound.
:param c_final: (float) a value calculated using the covariance matrices of free weights.
Refer to https://pdfs.semanticscholar.org/4fb1/2c1129ba5389bafe47b03e595d098d0252b9.pdf for
more information.
:param asset_bounds_i: (list) a list containing the lower and upper bound values for the ith weight
:return: bounded weight value
'''
if c_final > 0:
return asset_bounds_i[1][0]
return asset_bounds_i[0][0]
def _compute_w(self, covar_f_inv, covar_fb, mean_f, w_b):
'''
Compute the turning point associated with the current set of free weights F
:param covar_f_inv: (np.array) inverse of covariance matrix of free assets
:param covar_fb: (np.array) covariance matrix between free assets and bounded assets
:param mean_f: (np.array) expected returns of free assets
:param w_b: (np.array) bounded asset weight values
:return: (array, float) list of turning point weights and gamma value from the langrange equation
'''
# Compute gamma
ones_f = np.ones(mean_f.shape)
g_1 = np.dot(np.dot(ones_f.T, covar_f_inv), mean_f)
g_2 = np.dot(np.dot(ones_f.T, covar_f_inv), ones_f)
if w_b is None:
g_final, w_1 = float(-self.lambdas[-1] * g_1 / g_2 + 1 / g_2), 0
else:
ones_b = np.ones(w_b.shape)
g_3 = np.dot(ones_b.T, w_b)
g_4 = np.dot(covar_f_inv, covar_fb)
w_1 = np.dot(g_4, w_b)
g_4 = np.dot(ones_f.T, w_1)
g_final = float(-self.lambdas[-1] * g_1 / g_2 + (1 - g_3 + g_4) / g_2)
# Compute weights
w_2 = np.dot(covar_f_inv, ones_f)
w_3 = np.dot(covar_f_inv, mean_f)
free_asset_weights = -1*w_1 + g_final * w_2 + self.lambdas[-1] * w_3
return free_asset_weights, g_final
def _compute_lambda(self, covar_f_inv, covar_fb, mean_f, w_b, asset_index, b_i):
'''
Calculate the lambda value in the langrange optimsation equation
:param covar_f_inv: (np.array) inverse of covariance matrix of free assets
:param covar_fb: (np.array) covariance matrix between free assets and bounded assets
:param mean_f: (np.array) expected returns of free assets
:param w_b: (np.array) bounded asset weight values
:param asset_index: (int) index of the asset in the portfolio
:param b_i: (list) list of upper and lower bounded weight values
:return: (float) lambda value
'''
# Compute C
ones_f = np.ones(mean_f.shape)
c_1 = np.dot(np.dot(ones_f.T, covar_f_inv), ones_f)
c_2 = np.dot(covar_f_inv, mean_f)
c_3 = np.dot(np.dot(ones_f.T, covar_f_inv), mean_f)
c_4 = np.dot(covar_f_inv, ones_f)
c_final = -1*c_1 * c_2[asset_index] + c_3 * c_4[asset_index]
if c_final == 0:
return None, None
# Compute bi
if isinstance(b_i, list):
b_i = self._compute_bi(c_final, b_i)
# Compute Lambda
if w_b is None:
# All free assets
return float((c_4[asset_index] - c_1 * b_i) / c_final), b_i
ones_b = np.ones(w_b.shape)
l_1 = np.dot(ones_b.T, w_b)
l_2 = np.dot(covar_f_inv, covar_fb)
l_3 = np.dot(l_2, w_b)
l_2 = np.dot(ones_f.T, l_3)
lambda_value = float(((1 - l_1 + l_2) * c_4[asset_index] - c_1 * (b_i + l_3[asset_index])) / c_final)
return lambda_value, b_i
def _get_matrices(self, free_weights):
'''
Calculate the required matrices between free and bounded assets
:param free_weights: (list) list of free assets/weights
:return: (tuple of np.array matrices) the corresponding matrices
'''
covar_f = self._reduce_matrix(self.cov_matrix, free_weights, free_weights)
mean_f = self._reduce_matrix(self.expected_returns, free_weights, [0])
bounded_weights = self._get_bounded_weights(free_weights)
covar_fb = self._reduce_matrix(self.cov_matrix, free_weights, bounded_weights)
w_b = self._reduce_matrix(self.weights[-1], bounded_weights, [0])
return covar_f, covar_fb, mean_f, w_b
def _get_bounded_weights(self, free_weights):
'''
Compute the list of bounded assets
:param free_weights: (np.array) list of free weights/assets
:return: (np.array) list of bounded assets/weights
'''
return self._diff_lists(list(range(self.expected_returns.shape[0])), free_weights)
@staticmethod
def _diff_lists(list_1, list_2):
'''
Calculate the set difference between two lists
:param list_1: (list) a list of asset indices
:param list_2: (list) another list of asset indices
:return: (list) set difference between the two input lists
'''
return list(set(list_1) - set(list_2))
@staticmethod
def _reduce_matrix(matrix, row_indices, col_indices):
'''
Reduce a matrix to the provided set of rows and columns
:param matrix: (np.array) a matrix whose subset of rows and columns we need
:param row_indices: (list) list of row indices for the matrix
:param col_indices: (list) list of column indices for the matrix
:return: (np.array) subset of input matrix
'''
return matrix[np.ix_(row_indices, col_indices)]
def _purge_num_err(self, tol):
'''
Purge violations of inequality constraints (associated with ill-conditioned cov matrix)
:param tol: (float) tolerance level for purging
'''
index_1 = 0
while True:
flag = False
if index_1 == len(self.weights):
break
if abs(sum(self.weights[index_1]) - 1) > tol:
flag = True
else:
for index_2 in range(self.weights[index_1].shape[0]):
if (
self.weights[index_1][index_2] - self.lower_bounds[index_2] < -tol
or self.weights[index_1][index_2] - self.upper_bounds[index_2] > tol
):
flag = True
break
if flag is True:
del self.weights[index_1]
del self.lambdas[index_1]
del self.gammas[index_1]
del self.free_weights[index_1]
else:
index_1 += 1
def _purge_excess(self):
'''
Remove violations of the convex hull
'''
index_1, repeat = 0, False
while True:
if repeat is False:
index_1 += 1
if index_1 >= len(self.weights) - 1:
break
weights = self.weights[index_1]
mean = np.dot(weights.T, self.expected_returns)[0, 0]
index_2, repeat = index_1 + 1, False
while True:
if index_2 == len(self.weights):
break
weights = self.weights[index_2]
mean_ = np.dot(weights.T, self.expected_returns)[0, 0]
if mean < mean_:
del self.weights[index_1]
del self.lambdas[index_1]
del self.gammas[index_1]
del self.free_weights[index_1]
repeat = True
break
index_2 += 1
@staticmethod
def _golden_section(obj, left, right, **kwargs):
'''
Golden section method. Maximum if kargs['minimum']==False is passed
:param obj: (function) The objective function on which the extreme will be found.
:param left: (float) The leftmost extreme of search
:param right: (float) The rightmost extreme of search
'''
tol, sign, args = 1.0e-9, -1, None
args = kwargs.get("args", None)
num_iterations = int(ceil(-2.078087 * log(tol / abs(right - left))))
gs_ratio = 0.618033989
complementary_gs_ratio = 1.0 - gs_ratio
# Initialize
x_1 = gs_ratio * left + complementary_gs_ratio * right
x_2 = complementary_gs_ratio * left + gs_ratio * right
f_1 = sign * obj(x_1, *args)
f_2 = sign * obj(x_2, *args)
# Loop
for _ in range(num_iterations):
if f_1 > f_2:
left = x_1
x_1 = x_2
f_1 = f_2
x_2 = complementary_gs_ratio * left + gs_ratio * right
f_2 = sign * obj(x_2, *args)
else:
right = x_2
x_2 = x_1
f_2 = f_1
x_1 = gs_ratio * left + complementary_gs_ratio * right
f_1 = sign * obj(x_1, *args)
if f_1 < f_2:
return x_1, sign * f_1
return x_2, sign * f_2
def _eval_sr(self, alpha, w_0, w_1):
'''
Evaluate the sharpe ratio of the portfolio within the convex combination
:param alpha: (float) convex combination value
:param w_0: (list) first endpoint of convex combination of weights
:param w_1: (list) second endpoint of convex combination of weights
:return:
'''
weights = alpha * w_0 + (1 - alpha) * w_1
returns = np.dot(weights.T, self.expected_returns)[0, 0]
volatility = np.dot(np.dot(weights.T, self.cov_matrix), weights)[0, 0] ** 0.5
return returns / volatility
def _bound_free_weight(self, free_weights):
'''
Add a free weight to list of bounded weights
:param free_weights: (list) list of free-weight indices
:return: (float, int, int) lambda value, index of free weight to be bounded, bound weight value
'''
lambda_in = None
i_in = None
bi_in = None
if len(free_weights) > 1:
covar_f, covar_fb, mean_f, w_b = self._get_matrices(free_weights)
covar_f_inv = np.linalg.inv(covar_f)
j = 0
for i in free_weights:
lambda_i, b_i = self._compute_lambda(
covar_f_inv, covar_fb, mean_f, w_b, j, [self.lower_bounds[i], self.upper_bounds[i]]
)
if self._infnone(lambda_i) > self._infnone(lambda_in):
lambda_in, i_in, bi_in = lambda_i, i, b_i
j += 1
return lambda_in, i_in, bi_in
def _free_bound_weight(self, free_weights):
'''
Add a bounded weight to list of free weights
:param free_weights: (list) list of free-weight indices
:return: (float, int) lambda value, index of the bounded weight to be made free
'''
lambda_out = None
i_out = None
if len(free_weights) < self.expected_returns.shape[0]:
bounded_weight_indices = self._get_bounded_weights(free_weights)
for i in bounded_weight_indices:
covar_f, covar_fb, mean_f, w_b = self._get_matrices(free_weights + [i])
covar_f_inv = np.linalg.inv(covar_f)
lambda_i, _ = self._compute_lambda(
covar_f_inv,
covar_fb,
mean_f,
w_b,
mean_f.shape[0] - 1,
self.weights[-1][i],
)
if (self.lambdas[-1] is None or lambda_i < self.lambdas[-1]) and lambda_i > self._infnone(lambda_out):
lambda_out, i_out = lambda_i, i
return lambda_out, i_out
def _initialise(self, asset_prices, resample_by):
'''
Initialise covariances, upper-counds, lower-bounds and storage buffers
:param asset_prices: (pd.Dataframe) dataframe of asset prices
:param resample_by: (str) specifies how to resample the prices - weekly, daily, monthly etc.. Defaults to
'B' meaning daily business days which is equivalent to no resampling
'''
# Initial checks
if not isinstance(asset_prices, pd.DataFrame):
raise ValueError("Asset prices matrix must be a dataframe")
if not isinstance(asset_prices.index, pd.DatetimeIndex):
raise ValueError("Asset prices dataframe must be indexed by date.")
# Resample the asset prices
asset_prices = asset_prices.resample(resample_by).last()
# Calculate the expected returns
if self.calculate_returns == "mean":
self.expected_returns = self._calculate_mean_historical_returns(asset_prices=asset_prices)
elif self.calculate_returns == "exponential":
self.expected_returns = self._calculate_exponential_historical_returns(asset_prices=asset_prices)
else:
raise ValueError("Unknown returns specified. Supported returns - mean, exponential")
self.expected_returns = np.array(self.expected_returns).reshape((len(self.expected_returns), 1))
if (self.expected_returns == np.ones(self.expected_returns.shape) * self.expected_returns.mean()).all():
self.expected_returns[-1, 0] += 1e-5
# Calculate the covariance matrix
self.cov_matrix = np.asarray(asset_prices.cov())
# Intialise lower bounds
if isinstance(self.weight_bounds[0], numbers.Real):
self.lower_bounds = np.ones(self.expected_returns.shape) * self.weight_bounds[0]
else:
self.lower_bounds = np.array(self.weight_bounds[0]).reshape(self.expected_returns.shape)
# Intialise upper bounds
if isinstance(self.weight_bounds[0], numbers.Real):
self.upper_bounds = np.ones(self.expected_returns.shape) * self.weight_bounds[1]
else:
self.upper_bounds = np.array(self.weight_bounds[1]).reshape(self.expected_returns.shape)
# Initialise storage buffers
self.weights = []
self.lambdas = []
self.gammas = []
self.free_weights = []
@staticmethod
def _calculate_mean_historical_returns(asset_prices, frequency=252):
'''
Calculate the annualised mean historical returns from asset price data
:param asset_prices: (pd.DataFrame) asset price data
:return: (np.array) returns per asset
'''
returns = asset_prices.pct_change().dropna(how="all")
returns = returns.mean() * frequency
return returns
@staticmethod
def _calculate_exponential_historical_returns(asset_prices, frequency=252, span=500):
'''
Calculate the exponentially-weighted mean of (daily) historical returns, giving
higher weight to more recent data.
:param asset_prices: (pd.DataFrame) asset price data
:return: (np.array) returns per asset
'''
returns = asset_prices.pct_change().dropna(how="all")
returns = returns.ewm(span=span).mean().iloc[-1] * frequency
return returns
def allocate(self, asset_prices, solution="cla_turning_points", resample_by="B"):
# pylint: disable=consider-using-enumerate,too-many-locals,too-many-branches,too-many-statements
'''
Calculate the portfolio asset allocations using the method specified.
:param asset_prices: (pd.Dataframe) a dataframe of historical asset prices (adj closed)
:param solution: (str) specify the type of solution to compute. Options are: cla_turning_points, max_sharpe,
min_volatility, efficient_frontier
:param resample_by: (str) specifies how to resample the prices - weekly, daily, monthly etc.. Defaults to
'B' meaning daily business days which is equivalent to no resampling
'''
# Some initial steps before the algorithm runs
self._initialise(asset_prices=asset_prices, resample_by=resample_by)
assets = asset_prices.columns
# Compute the turning points, free sets and weights
free_weights, weights = self._init_algo()
self.weights.append(np.copy(weights)) # store solution
self.lambdas.append(None)
self.gammas.append(None)
self.free_weights.append(free_weights[:])
while True:
# 1) Bound one free weight
lambda_in, i_in, bi_in = self._bound_free_weight(free_weights)
# 2) Free one bounded weight
lambda_out, i_out = self._free_bound_weight(free_weights)
# 3) Compute minimum variance solution
if (lambda_in is None or lambda_in < 0) and (lambda_out is None or lambda_out < 0):
self.lambdas.append(0)
covar_f, covar_fb, mean_f, w_b = self._get_matrices(free_weights)
covar_f_inv = np.linalg.inv(covar_f)
mean_f = np.zeros(mean_f.shape)
# 4) Decide whether to free a bounded weight or bound a free weight
else:
if self._infnone(lambda_in) > self._infnone(lambda_out):
self.lambdas.append(lambda_in)
free_weights.remove(i_in)
weights[i_in] = bi_in # set value at the correct boundary
else:
self.lambdas.append(lambda_out)
free_weights.append(i_out)
covar_f, covar_fb, mean_f, w_b = self._get_matrices(free_weights)
covar_f_inv = np.linalg.inv(covar_f)
# 5) Compute solution vector
w_f, gamma = self._compute_w(covar_f_inv, covar_fb, mean_f, w_b)
for i in range(len(free_weights)):
weights[free_weights[i]] = w_f[i]
self.weights.append(np.copy(weights)) # store solution
self.gammas.append(gamma)
self.free_weights.append(free_weights[:])
if self.lambdas[-1] == 0:
break
# 6) Purge turning points
self._purge_num_err(10e-10)
self._purge_excess()
# Compute the specified solution
self._compute_solution(assets=assets, solution=solution)
def _compute_solution(self, assets, solution):
'''
Compute the desired solution to the portfolio optimisation problem
:param assets: (list) a list of asset names
:param solution: (str) specify the type of solution to compute. Options are: cla_turning_points, max_sharpe,
min_volatility, efficient_frontier
'''
if solution == "max_sharpe":
self.max_sharpe, self.weights = self._max_sharpe()
self.weights = pd.DataFrame(self.weights)
self.weights.index = assets
self.weights = self.weights.T
elif solution == "min_volatility":
self.min_var, self.weights = self._min_volatility()
self.weights = pd.DataFrame(self.weights)
self.weights.index = assets
self.weights = self.weights.T
elif solution == "efficient_frontier":
self.efficient_frontier_means, self.efficient_frontier_sigma, self.weights = self._efficient_frontier()
weights_copy = self.weights.copy()
for i, turning_point in enumerate(weights_copy):
self.weights[i] = turning_point.reshape(1, -1)[0]
self.weights = pd.DataFrame(self.weights, columns=assets)
elif solution == "cla_turning_points":
# Reshape the weight matrix
weights_copy = self.weights.copy()
for i, turning_point in enumerate(weights_copy):
self.weights[i] = turning_point.reshape(1, -1)[0]
self.weights = pd.DataFrame(self.weights, columns=assets)
else:
raise ValueError("Unknown solution string specified. Supported solutions - cla_turning_points, "
"efficient_frontier, min_volatility, max_sharpe")
def _max_sharpe(self):
'''
Compute the maximum sharpe portfolio allocation
:return: (float, np.array) tuple of max. sharpe value and the set of weight allocations
'''
# 1) Compute the local max SR portfolio between any two neighbor turning points
w_sr, sharpe_ratios = [], []
for i in range(len(self.weights) - 1):
w_0 = np.copy(self.weights[i])
w_1 = np.copy(self.weights[i + 1])
kwargs = {"minimum": False, "args": (w_0, w_1)}
alpha, sharpe_ratio = self._golden_section(self._eval_sr, 0, 1, **kwargs)
w_sr.append(alpha * w_0 + (1 - alpha) * w_1)
sharpe_ratios.append(sharpe_ratio)
maximum_sharp_ratio = max(sharpe_ratios)
weights_with_max_sharpe_ratio = w_sr[sharpe_ratios.index(maximum_sharp_ratio)]
return maximum_sharp_ratio, weights_with_max_sharpe_ratio
def _min_volatility(self):
'''
Compute minimum volatility portfolio allocation
:return: (float, np.array) tuple of minimum variance value and the set of weight allocations
'''
var = []
for weights in self.weights:
volatility = np.dot(np.dot(weights.T, self.cov_matrix), weights)
var.append(volatility)
min_var = min(var)
return min_var ** .5, self.weights[var.index(min_var)]
def _efficient_frontier(self, points=100):
# pylint: disable=invalid-name
'''
Compute the entire efficient frontier solution
:param points: (int) number of efficient frontier points to be calculated
:return: tuple of mean, variance amd weights of the frontier solutions
'''
means, sigma, weights = [], [], []
# remove the 1, to avoid duplications
partitions = np.linspace(0, 1, points // len(self.weights))[:-1]
b = list(range(len(self.weights) - 1))
for i in b:
w_0, w_1 = self.weights[i], self.weights[i + 1]
if i == b[-1]:
# include the 1 in the last iteration
partitions = np.linspace(0, 1, points // len(self.weights))
for j in partitions:
w = w_1 * j + (1 - j) * w_0
weights.append(np.copy(w))
means.append(np.dot(w.T, self.expected_returns)[0, 0])
sigma.append(np.dot(np.dot(w.T, self.cov_matrix), w)[0, 0] ** 0.5)
return means, sigma, weights