Description of bug
CLA.max_sharpe() raises TypeError: '>' not supported between instances of 'NoneType' and 'float' when all expected returns are equal.
The root cause is that _compute_lambda explicitly returns (None, None) when its denominator c is exactly zero:
if c == 0:
return None, None
When all expected returns are equal, c = 0 is mathematically guaranteed — the term that distinguishes assets by return cancels out exactly. The calling loop in _solve does not guard against this None before using lam in a comparison:
) and lam > CLA._infnone(l_out):
^^^^^^^^^^^^^^^^^^^^^^^^^
_infnone is applied to the accumulated l_out but not to the local lam returned from each _compute_lambda call, causing the TypeError.
Expected behavior
Either a deterministic valid result — falling through to the minimum variance portfolio, which remains well-defined when returns are equal — or an informative ValueError explaining that max_sharpe is undefined when all expected returns are identical.
Code sample for reproducing error
import numpy as np
from pypfopt.cla import CLA
mu = np.array([0.1, 0.1])
S = np.array([[0.01, 0.01],
[0.01, 0.02]])
cla = CLA(mu, S)
cla.max_sharpe()
Operating system, python version, PyPortfolioOpt version
macOS, Python 3.12, PyPortfolioOpt 1.6.0
Description of bug
CLA.max_sharpe()raisesTypeError: '>' not supported between instances of 'NoneType' and 'float'when all expected returns are equal.The root cause is that
_compute_lambdaexplicitly returns(None, None)when its denominatorcis exactly zero:When all expected returns are equal,
c = 0is mathematically guaranteed — the term that distinguishes assets by return cancels out exactly. The calling loop in_solvedoes not guard against thisNonebefore usinglamin a comparison:_infnoneis applied to the accumulatedl_outbut not to the locallamreturned from each_compute_lambdacall, causing theTypeError.Expected behavior
Either a deterministic valid result — falling through to the minimum variance portfolio, which remains well-defined when returns are equal — or an informative
ValueErrorexplaining thatmax_sharpeis undefined when all expected returns are identical.Code sample for reproducing error
Operating system, python version, PyPortfolioOpt version
macOS, Python 3.12, PyPortfolioOpt 1.6.0