text
stringlengths
0
828
""""""The objective function that minimizes variance.""""""
return np.matmul(np.matmul(weights.transpose(), cov_matrix), weights)
def func_deriv(weights):
""""""The derivative of the objective function.""""""
return (
np.matmul(weights.transpose(), cov_matrix.transpose()) +
np.matmul(weights.transpose(), cov_matrix)
)
constraints = ({'type': 'eq', 'fun': lambda weights: (weights.sum() - 1)})
solution = self.solve_minimize(func, weights, constraints, func_deriv=func_deriv)
# NOTE: `min_risk` is unused, but may be helpful later.
# min_risk = solution.fun
allocation = solution.x
return allocation"
726,"def get_max_return(self, weights, returns):
""""""
Maximizes the returns of a portfolio.
""""""
def func(weights):
""""""The objective function that maximizes returns.""""""
return np.dot(weights, returns.values) * -1
constraints = ({'type': 'eq', 'fun': lambda weights: (weights.sum() - 1)})
solution = self.solve_minimize(func, weights, constraints)
max_return = solution.fun * -1
# NOTE: `max_risk` is not used anywhere, but may be helpful in the future.
# allocation = solution.x
# max_risk = np.matmul(
# np.matmul(allocation.transpose(), cov_matrix), allocation
# )
return max_return"
727,"def efficient_frontier(
self,
returns,
cov_matrix,
min_return,
max_return,
count
):
""""""
Returns a DataFrame of efficient portfolio allocations for `count` risk
indices.
""""""
columns = [coin for coin in self.SUPPORTED_COINS]
# columns.append('Return')
# columns.append('Risk')
values = pd.DataFrame(columns=columns)
weights = [1/len(self.SUPPORTED_COINS)] * len(self.SUPPORTED_COINS)
def func(weights):
""""""The objective function that minimizes variance.""""""
return np.matmul(np.matmul(weights.transpose(), cov_matrix), weights)
def func_deriv(weights):
""""""The derivative of the objective function.""""""
return (
np.matmul(weights.transpose(), cov_matrix.transpose()) +
np.matmul(weights.transpose(), cov_matrix)
)
for point in np.linspace(min_return, max_return, count):
constraints = (
{'type': 'eq', 'fun': lambda weights: (weights.sum() - 1)},
{'type': 'ineq', 'fun': lambda weights, i=point: (
np.dot(weights, returns.values) - i
)}
)
solution = self.solve_minimize(func, weights, constraints, func_deriv=func_deriv)
columns = {}
for index, coin in enumerate(self.SUPPORTED_COINS):
columns[coin] = math.floor(solution.x[index] * 100 * 100) / 100
# NOTE: These lines could be helpful, but are commented out right now.
# columns['Return'] = round(np.dot(solution.x, returns), 6)
# columns['Risk'] = round(solution.fun, 6)
values = values.append(columns, ignore_index=True)
return values"
728,"def solve_minimize(
self,
func,
weights,
constraints,
lower_bound=0.0,
upper_bound=1.0,
func_deriv=False
):
""""""
Returns the solution to a minimization problem.
""""""