Skip to content

Fix/efficient frontier label alignment - #761

Open
losterbr wants to merge 7 commits into
PyPortfolio:mainfrom
losterbr:fix/efficient-frontier-label-alignment
Open

losterbr wants to merge 7 commits into
PyPortfolio:mainfrom
losterbr:fix/efficient-frontier-label-alignment

Conversation

@losterbr

@losterbr losterbr commented Sep 13, 2026

Copy link
Copy Markdown

Summary

Features

  • Aligns labeled covariance matrices with expected returns by asset label, preventing silent positional mismatches.
  • Normalizes covariance DataFrame rows to column order.
  • Validates covariance matrices are square and have matching, unique axis labels.
  • Validates covariance dimensions against expected returns.
  • Preserves existing positional behavior for mixed pandas/array inputs and documents the required asset ordering.
  • Adds constructor type annotations and separates input normalization into focused helpers.
  • Uses None as the cache sentinel, correctly preserving a cached maximum return of 0.0. Negative cached returns were already handled correctly.
  • Includes minor documentation and lint cleanups.

Tests

  • Added focused tests for every input-normalization branch.
  • Covers Series, DataFrame, list, NumPy array, and None inputs.
  • Covers mismatched dimensions, invalid types, non-square matrices, duplicate labels, and differing axis labels.
  • Covers covariance row/column order differences.
  • Covers expected-return order differing from covariance order.
  • Covers labeled, unlabeled, and mixed-input ticker selection.
  • Adds regression coverage for the cached zero maximum return.

Validation:

  • 99 passed, 4 skipped in test_efficient_frontier.py
  • Ruff passes.
  • Pylint reports only existing API-shape warnings.

Potential Breaking Changes

No public API signatures were removed or changed incompatibly.

The validation is intentionally stricter:

  • Covariance DataFrames with duplicate, mismatched, or non-square axes now raise ValueError.
  • Fully labeled pandas inputs are aligned by asset label rather than assumed to share positional order.
  • Previously accepted but incorrectly labeled inputs may now fail.
  • Correctly labeled inputs that were previously silently misaligned may produce different, corrected portfolio results.

Mixed pandas/array inputs remain positional for backward compatibility.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant