Skip to content

Commit

Permalink
Merge pull request robertmartin8#543 from AveryLevin/quad_form/assume…
Browse files Browse the repository at this point in the history
…_PSD

Add the assume_PSD flag in calls to cvxpy.quad_form
  • Loading branch information
robertmartin8 authored Dec 1, 2024
2 parents 5bb80a6 + 1b28947 commit 5fa9dae
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 4 deletions.
2 changes: 1 addition & 1 deletion pypfopt/efficient_frontier/efficient_frontier.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def max_sharpe(self, risk_free_rate=0.02):

# max_sharpe requires us to make a variable transformation.
# Here we treat w as the transformed variable.
self._objective = cp.quad_form(self._w, self.cov_matrix)
self._objective = cp.quad_form(self._w, self.cov_matrix, assume_PSD=True)
k = cp.Variable()

# Note: objectives are not scaled by k. Hence there are subtle differences
Expand Down
6 changes: 3 additions & 3 deletions pypfopt/objective_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def portfolio_variance(w, cov_matrix):
:return: value of the objective function OR objective function expression
:rtype: float OR cp.Expression
"""
variance = cp.quad_form(w, cov_matrix)
variance = cp.quad_form(w, cov_matrix, assume_PSD=True)
return _objective_value(w, variance)


Expand Down Expand Up @@ -109,7 +109,7 @@ def sharpe_ratio(w, expected_returns, cov_matrix, risk_free_rate=0.02, negative=
:rtype: float
"""
mu = w @ expected_returns
sigma = cp.sqrt(cp.quad_form(w, cov_matrix))
sigma = cp.sqrt(cp.quad_form(w, cov_matrix, assume_PSD=True))
sign = -1 if negative else 1
sharpe = (mu - risk_free_rate) / sigma
return _objective_value(w, sign * sharpe)
Expand Down Expand Up @@ -156,7 +156,7 @@ def quadratic_utility(w, expected_returns, cov_matrix, risk_aversion, negative=T
"""
sign = -1 if negative else 1
mu = w @ expected_returns
variance = cp.quad_form(w, cov_matrix)
variance = cp.quad_form(w, cov_matrix, assume_PSD=True)

risk_aversion_par = cp.Parameter(
value=risk_aversion, name="risk_aversion", nonneg=True
Expand Down
Loading

0 comments on commit 5fa9dae

Please sign in to comment.