Skip to content

[BUG] CLA crashes with TypeError and produces non-deterministic results when expected returns are equal #738

@ayushraj09

Description

@ayushraj09

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions