Skip to content

Comments

[ENH] Add CVXCLA backend integration for faster performance and interactive Plotly support#700

Open
ayushraj09 wants to merge 4 commits intoPyPortfolio:mainfrom
ayushraj09:main
Open

[ENH] Add CVXCLA backend integration for faster performance and interactive Plotly support#700
ayushraj09 wants to merge 4 commits intoPyPortfolio:mainfrom
ayushraj09:main

Conversation

@ayushraj09
Copy link

@ayushraj09 ayushraj09 commented Feb 19, 2026

Summary

Fixes NumPy and Pandas compatibility issues and adds optional interactive Plotly support for efficient frontier plotting. Fully backward compatible.

Fixes

  • NumPy ≥1.26 compatibility: Resolved scalar conversion TypeError in CLA (_compute_w, _compute_lambda) using safe .item() extraction.
  • Pandas compatibility: Replaced deprecated squeeze=True in pd.read_csv() with .squeeze().

Enhancements

  • Added interactive=True option to plot_efficient_frontier() for Plotly-based interactive plots.
  • Matplotlib remains default (no breaking changes).

Dependencies

  • Added plotly
  • Added packaging
  • Ignored uv.lock

Testing

  • All tests pass (CLA, plotting, efficient frontier).

- Fix CLA algorithm TypeError with numpy arrays in _compute_w and _compute_lambda
- Fix deprecated pandas squeeze parameter in examples.py
- Add interactive Plotly plotting support to efficient frontier
- Add missing dependencies (packaging, plotly) to pyproject.toml
- Add uv.lock to .gitignore for cleaner repo management
- Fix remaining TypeError in _compute_w and _compute_lambda methods
- All CLA tests now pass (12/12) including short selling scenarios
- Tested with modern NumPy versions >= 1.26.0
@ayushraj09
Copy link
Author

@tschm @fkiraly
Can you review this PR ?

@ayushraj09 ayushraj09 changed the title Fix NumPy/Pandas compatibility issues and add interactive Plotly support [BUG] Fix NumPy/Pandas compatibility issues and add interactive Plotly support Feb 19, 2026
@tschm
Copy link
Contributor

tschm commented Feb 19, 2026

Honestly, the cla algorithm in this package is extremely slow. You better replace it with a modern implementation, as given in https://github.com/cvxgrp/cvxcla

@tschm
Copy link
Contributor

tschm commented Feb 19, 2026

For a benchmark comparison, see https://github.com/tschm/eqd/blob/main/PresentationEQD.pdf

@ayushraj09
Copy link
Author

Can you please check the url for benchmark comparison?

…ctions for showfig parameter. Fixed test files for depreciation issues.
@tschm
Copy link
Contributor

tschm commented Feb 19, 2026

It's a link into a pdf document. A presentation I gave 2 years ago. Slide 5 or so, contains the benchmark

@ayushraj09 ayushraj09 changed the title [BUG] Fix NumPy/Pandas compatibility issues and add interactive Plotly support [ENH] Add CVXCLA backend integration for faster performance and interactive Plotly support Feb 19, 2026
@ayushraj09
Copy link
Author

Summary

This PR integrates cvxcla library as an optional high-performance backend for the Critical Line Algorithm (CLA) for portfolio optimization while maintaining full backward compatibility. Additionally, it enhances plotting capabilities with consistent showfig parameter handling for both matplotlib and Plotly plots.

Performance Enhancement

CVXCLA Backend Integration

  • Added optional use_cvxcla=True parameter to CLA constructor
  • Faster execution for max_sharpe(), min_volatility(), and efficient_frontier() operations
  • Automatic fallback to original implementation when cvxcla is not available
  • Zero breaking changes - all existing code continues to work without modification

Usage

# Before (original implementation)
cla = CLA(expected_returns, cov_matrix)

# After (high-performance backend) 
cla = CLA(expected_returns, cov_matrix, use_cvxcla=True)

Technical Implementation

Backend Architecture

  • Implemented backend abstraction layer with seamless API compatibility
  • Added dynamic covariance matrix updates with automatic engine recreation
  • Preserved all existing method signatures and return formats
  • Comprehensive error handling with graceful degradation

Enhanced Plotting System

  • Fixed showfig parameter handling for interactive Plotly plots
  • Enhanced plot_efficient_frontier() with consistent behavior across matplotlib/Plotly
  • Interactive plots now properly display when showfig=True

Testing & Validation

Comprehensive Test Coverage

  • Added tests/test_cvxcla_backend.py with 5 focused backend parity tests
  • All existing tests pass (312+ tests) ensuring no regressions

Performance Verification

# Both backends produce identical results
Original:  Expected return: 29.9%, Volatility: 21.8%, Sharpe: 1.38
CVXCLA:    Expected return: 29.9%, Volatility: 21.8%, Sharpe: 1.38

Dependencies & Compatibility

New Dependencies

  • cvxcla>=1.5.1 - High-performance CLA implementation (optional)
  • plotly - Interactive plotting capabilities
  • packaging - Dependency management utilities

Compatibility Fixes

  • Updated pandas frequency notation: "M""ME" for modern pandas compatibility in test files
  • Ensured Python 3.12+ support across all components

Files Changed

Core Implementation

  • pypfopt/cla.py - Backend integration and dynamic engine management
  • pypfopt/plotting.py - Enhanced showfig parameter handling for interactive plots
  • pyproject.toml - Updated dependencies (cvxcla, plotly, packaging)

@ayushraj09
Copy link
Author

ayushraj09 commented Feb 19, 2026

@tschm Thanks for the benchmark comparison which I found in your github: https://github.com/tschm/eqd_markowitz/blob/main/PresentationEQDweb.pdf (corrected url i was asking for :-|)

Please review the current PR with changes that add cvxcla integration.

@tschm
Copy link
Contributor

tschm commented Feb 19, 2026

Interesting that you keep both implementations of the cla algorithm in the package. I would remove the previous implementation and point to the cvxvla repo where both implementations are present. Less code is better than more code. Glad you kept the original API so cvxcla is an easy replacement

@ayushraj09
Copy link
Author

@tschm Is any there anything else that needs to be done for merging this PR?

@tschm
Copy link
Contributor

tschm commented Feb 20, 2026

@ayushraj09 Many thanks for those contributions. I have no write access to the repo. I would recommend imho to replace the existing cla implementation rather than accumulating code

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.

2 participants