A professional-grade portfolio risk management and optimization platform that combines Python's ease of use with C++'s computational performance.
Built for quantitative analysts, portfolio managers, and financial engineers who need production-ready risk management tools with institutional-grade performance.
- Monte Carlo Value at Risk (VaR) - Industry-standard risk measurement with configurable confidence levels
- Expected Shortfall (ES/CVaR) - Coherent risk measure for tail risk assessment
- Portfolio Risk Decomposition - Asset-level risk contribution analysis
- Maximum Drawdown - Historical drawdown analysis with path simulation
- Volatility Metrics - Annualized portfolio volatility with time scaling
- Mean-Variance Optimization - Classic Markowitz portfolio theory implementation
- Maximum Sharpe Ratio - Risk-adjusted return optimization
- Minimum Variance Portfolio - Risk minimization with return constraints
- Efficient Frontier Generation - Complete risk-return frontier analysis
- Flexible Constraints - Min/max weight bounds and sector constraints
- C++ Computational Core - High-performance linear algebra using Eigen library
- Python Interface - Intuitive API with automatic fallback implementations
- pybind11 Integration - Seamless C++/Python interoperability with GIL release
- Memory Efficient - Optimized for large portfolios (50+ assets)
- Professional Web Interface - Built with Dash/Plotly for institutional use
- Real-time Calculations - Responsive UI with progress indicators
- Comprehensive Visualizations - Risk histograms, efficient frontiers, allocation charts
- Performance Benchmarking - C++ vs Python speed comparisons
- Export Capabilities - PDF reports and CSV data export
- Installation
- Quick Start
- Web Dashboard
- API Documentation
- Architecture
- Performance
- Development
- Testing
- Contributing
- License
The following system dependencies are required:
# Ubuntu/Debian
sudo apt-get install cmake libeigen3-dev g++
# CentOS/RHEL
sudo yum install cmake eigen3-devel gcc-c++
# macOS (with Homebrew)
brew install cmake eigen
# Windows (with vcpkg)
vcpkg install eigen3 cmakepip install quant-risk-optimiser# Clone the repository
git clone https://github.com/quantrisk/quant-risk-optimiser.git
cd quant-risk-optimiser
# Create virtual environment
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate
# Install in development mode
pip install -e .
# Build C++ extensions
python setup.py build_ext --inplaceTest the installation:
python -c "import quant_risk_core; print('C++ backend available:', quant_risk_core.version())"
python -m pytest tests/ -vimport numpy as np
from quant_risk_optimiser import create_risk_manager
# Portfolio data
returns = np.array([0.10, 0.08, 0.12, 0.04, 0.09]) # Expected annual returns
weights = np.array([0.30, 0.25, 0.20, 0.15, 0.10]) # Portfolio weights
# Covariance matrix (5x5 for 5 assets)
cov_matrix = np.array([
[0.0225, 0.0045, 0.0030, -0.0015, 0.0020],
[0.0045, 0.0324, 0.0054, -0.0009, 0.0015],
[0.0030, 0.0054, 0.0625, 0.0000, 0.0040],
[-0.0015, -0.0009, 0.0000, 0.0025, -0.0005],
[0.0020, 0.0015, 0.0040, -0.0005, 0.0400]
])
# Create risk manager
risk_manager = create_risk_manager(seed=42)
# Calculate 95% VaR with 10,000 Monte Carlo simulations
var_result = risk_manager.calculate_var(
returns=returns,
cov_matrix=cov_matrix,
weights=weights,
confidence_level=0.95,
num_simulations=10000
)
print(f"Portfolio VaR (95%, 1-day): {var_result['var']:.2%}")
print(f"Computation time: {var_result['computation_time']:.3f}s")
print(f"Backend used: {var_result['method']}")
# Calculate Expected Shortfall
es_result = risk_manager.calculate_es(
returns, cov_matrix, weights, confidence_level=0.95
)
print(f"Expected Shortfall (95%): {es_result['es']:.2%}")from quant_risk_optimiser import create_optimizer
# Create optimizer
optimizer = create_optimizer()
# Find maximum Sharpe ratio portfolio
optimal_result = optimizer.maximize_sharpe_ratio(
returns=returns,
cov_matrix=cov_matrix,
risk_free_rate=0.02 # 2% risk-free rate
)
if optimal_result.converged:
print(f"Optimal weights: {optimal_result.weights}")
print(f"Expected return: {optimal_result.expected_return:.2%}")
print(f"Volatility: {optimal_result.volatility:.2%}")
print(f"Sharpe ratio: {optimal_result.sharpe_ratio:.3f}")
# Generate efficient frontier
frontier_points = optimizer.generate_efficient_frontier(
returns, cov_matrix, num_points=50
)
print(f"Generated {len(frontier_points)} efficient frontier points")# Set investment constraints
min_weights = np.full(5, 0.05) # Minimum 5% in each asset
max_weights = np.full(5, 0.40) # Maximum 40% in each asset
# Optimize with constraints
constrained_result = optimizer.maximize_sharpe_ratio(
returns=returns,
cov_matrix=cov_matrix,
min_weights=min_weights,
max_weights=max_weights
)
# Validate the result
validation = optimizer.validate_weights(
constrained_result.weights, min_weights, max_weights
)
print(f"Weights valid: {validation['valid']}")Launch the interactive web dashboard for comprehensive portfolio analysis:
# Start the dashboard server
python -m quant_risk_optimiser.app
# Or run directly
cd frontend && python app.pyNavigate to http://localhost:8050 to access the dashboard.
- Portfolio Upload: CSV/Excel file upload or use sample data
- Risk Analysis: Interactive Monte Carlo simulations with real-time charts
- Optimization: Sharpe ratio maximization, minimum variance, target return
- Efficient Frontier: Visual risk-return analysis with 50 frontier points
- Performance Benchmark: C++ vs Python speed comparison
- Export Options: Download optimized portfolios and risk reports
- Upload your portfolio CSV with columns:
Asset,Weight,Expected_Return,Volatility - Configure risk parameters (confidence level, simulations, time horizon)
- Run risk calculation to see VaR, ES, and Monte Carlo histogram
- Optimize portfolio using your preferred objective function
- Generate efficient frontier to explore risk-return tradeoffs
- Export results for further analysis
Factory function to create a risk manager instance.
Parameters:
seed(int): Random seed for reproducible resultsuse_cpp(bool): Use C++ backend when available
Returns: RiskManager instance
Calculate Value at Risk using Monte Carlo simulation.
Parameters:
returns(np.ndarray): Expected returns vectorcov_matrix(np.ndarray): Covariance matrixweights(np.ndarray): Portfolio weights (must sum to 1)confidence_level(float): Confidence level (default: 0.95)num_simulations(int): Number of MC simulations (default: 10,000)time_horizon(int): Time horizon in days (default: 1)
Returns: Dictionary with VaR result and metadata
Calculate Expected Shortfall (Conditional VaR).
Returns: Dictionary with ES result and metadata
Factory function to create an optimizer instance.
Find portfolio with maximum Sharpe ratio.
Returns: PortfolioResult with optimal weights and metrics
Generate efficient frontier points.
Returns: List of FrontierPoint objects
Named tuple containing optimization results:
weights: Optimal portfolio weightsexpected_return: Portfolio expected returnvolatility: Portfolio volatilitysharpe_ratio: Sharpe ratioconverged: Convergence flagcomputation_time: Execution timemethod: Backend used ('cpp' or 'python')
βββββββββββββββββββββββ βββββββββββββββββββββββ βββββββββββββββββββββββ
β Web Dashboard β β Python API β β C++ Core β
β (Dash/Plotly) βββββΊβ (Frontend) βββββΊβ (Backend) β
β β β β β β
β β’ Interactive UI β β β’ Risk Management β β β’ Monte Carlo β
β β’ File Upload β β β’ Optimization β β β’ Linear Algebra β
β β’ Visualizations β β β’ Data Validation β β β’ Matrix Ops β
β β’ Export Tools β β β’ Error Handling β β β’ Eigen Library β
βββββββββββββββββββββββ βββββββββββββββββββββββ βββββββββββββββββββββββ
β β β
βββββββββββββββββββββββββββββΌββββββββββββββββββββββββββββ
β
βββββββββββββββββββββββ
β pybind11 β
β Bindings β
β β
β β’ C++/Python Bridge β
β β’ GIL Release β
β β’ Type Conversion β
β β’ Error Propagation β
βββββββββββββββββββββββ
Backend (C++):
- Eigen 3.4+: High-performance linear algebra
- Modern C++17: Memory safety and performance optimizations
- CMake: Cross-platform build system
- OpenMP: Parallel computing (optional)
Frontend (Python):
- NumPy/SciPy: Numerical computing and fallback implementations
- Dash 2.14+: Web application framework
- Plotly 5.15+: Interactive visualizations
- Pandas: Data manipulation and analysis
Integration:
- pybind11 2.10+: Seamless C++/Python bindings
- pytest: Comprehensive testing framework
- setuptools: Package building and distribution
The system uses a hybrid approach:
- C++ Core: Computationally intensive operations (Monte Carlo, optimization)
- Python Orchestration: High-level logic, data validation, UI
- Automatic Fallback: Pure Python implementations when C++ unavailable
- Memory Efficiency: Eigen's expression templates minimize memory allocation
- Parallel Processing: OpenMP support for multi-core systems
Performance comparison on a representative portfolio optimization problem:
| Operation | Problem Size | C++ Time | Python Time | Speedup |
|---|---|---|---|---|
| Monte Carlo VaR (10K sims) | 10 assets | 15ms | 180ms | 12x |
| Mean-Variance Optimization | 20 assets | 8ms | 95ms | 11.9x |
| Efficient Frontier (50 pts) | 15 assets | 120ms | 1.2s | 10x |
| Risk Decomposition | 25 assets | 5ms | 45ms | 9x |
Benchmarks run on Intel i7-10700K, 32GB RAM, compiled with -O3 optimization
The system efficiently handles portfolios of varying sizes:
- Small portfolios (2-10 assets): Sub-millisecond optimization
- Medium portfolios (10-30 assets): Millisecond-scale calculations
- Large portfolios (30-50 assets): Still under 100ms for most operations
- Memory usage: Linear scaling, ~1MB per 100 assets
- GIL Release: C++ computations don't block Python threads
- SIMD Instructions: Vectorized operations on supported hardware
- Memory Pooling: Reduced allocation overhead for repeated calculations
- Expression Templates: Eigen optimizes away temporary objects
- Compiler Optimizations: Profile-guided optimization for hot paths
# Clone and setup
git clone https://github.com/quantrisk/quant-risk-optimiser.git
cd quant-risk-optimiser
# Install development dependencies
pip install -e ".[dev]"
# Build C++ extensions with debug symbols
CMAKE_BUILD_TYPE=Debug python setup.py build_ext --inplace
# Run development server
cd frontend && python app.pyquant-risk-optimiser/
βββ backend/ # C++ computational core
β βββ risk.h/.cpp # Risk calculation implementations
β βββ optimize.h/.cpp # Portfolio optimization algorithms
β βββ bindings.cpp # pybind11 Python bindings
β βββ CMakeLists.txt # Build configuration
βββ frontend/ # Python API and web interface
β βββ risk.py # Risk management interface
β βββ optimize.py # Optimization interface
β βββ app.py # Dash web application
β βββ config.py # Configuration and constants
βββ tests/ # Comprehensive test suite
β βββ test_risk.py # Risk calculation tests
β βββ test_optimize.py # Optimization tests
βββ data/ # Sample data and uploads
β βββ sample_portfolio.csv
βββ setup.py # Package configuration
βββ requirements.txt # Python dependencies
βββ README.md # This documentation
- Fork the repository and create a feature branch
- Write tests for new functionality with >90% coverage
- Follow PEP 8 style guidelines and C++ Core Guidelines
- Add documentation for public API functions
- Benchmark performance-critical changes
- Submit a pull request with clear description
# Run linting
flake8 frontend/ tests/
black frontend/ tests/ --check
# Type checking
mypy frontend/
# Security scan
bandit -r frontend/
# Run full test suite with coverage
pytest tests/ --cov=frontend --cov-report=htmlThe project includes a comprehensive test suite covering both Python and C++ functionality.
# Run all tests
python -m pytest tests/ -v
# Run with coverage
python -m pytest tests/ --cov=frontend --cov-report=term-missing
# Run specific test modules
python -m pytest tests/test_risk.py -v
python -m pytest tests/test_optimize.py -v
# Run C++ backend tests (if available)
python -c "import tests.test_risk; tests.test_risk.TestRiskCalculations().test_cpp_availability()"- Unit Tests: Individual function testing with edge cases
- Integration Tests: Full workflow testing with realistic data
- Performance Tests: Benchmark validation and regression detection
- Validation Tests: Mathematical correctness of financial calculations
- Error Handling: Input validation and graceful error recovery
The project uses automated testing on multiple platforms:
- Linux: Ubuntu 20.04+ with GCC 9+
- macOS: macOS 11+ with Clang 12+
- Windows: Windows 10+ with MSVC 2019+
- Python versions: 3.8, 3.9, 3.10, 3.11
This project is licensed under the MIT License - see the LICENSE file for details.
We welcome contributions from the quantitative finance community! Please see our Contributing Guidelines for details.
- New risk metrics (CVaR variants, coherent risk measures)
- Advanced optimization (robust optimization, transaction costs)
- Performance optimizations (GPU computing, distributed processing)
- Additional constraints (sector limits, ESG constraints)
- Enhanced visualizations (3D frontier plots, correlation heatmaps)
- Documentation: Project Wiki
- Issues: GitHub Issues
- Discussions: GitHub Discussions
- Email: quant@example.com
- Eigen Team for the exceptional linear algebra library
- pybind11 Contributors for seamless C++/Python integration
- Plotly Team for outstanding visualization capabilities
- Quantitative Finance Community for feedback and testing
Built with β€οΈ for the quantitative finance community
Quant Risk Optimizer - Where Performance Meets Precision