A comprehensive simulation tool for managing and analyzing short-term loan portfolios with different tenors, default rates, and allocation strategies.
The DubPrime Loan Portfolio Simulator allows credit analysts and portfolio managers to model the performance of loan portfolios with varying parameters:
- Initial capital: The starting investment amount
- Interest rates: Monthly interest rates for the portfolio
- Tenor allocation: Distribution of capital across different loan terms (1, 2, and 3 months)
- Default rates: Expected default rates for each tenor
- Duration: Simulation length in months
The simulator generates detailed visualizations of portfolio performance, cash flows, defaults, and returns.
- Interactive UI: Adjust parameters and see results in real-time
- Cash Flow Modeling: View detailed breakdowns of principal, interest, and defaults
- Tenor-Based Analysis: Compare performance of different loan durations
- Default Visualization: See the impact of defaults on portfolio performance
- Advanced Financial Metrics: Calculate IRR, WAL, duration, and risk-adjusted returns
- Stress Testing: Model portfolio performance under various economic scenarios
- Robust IRR Calculations: Accurately handle extreme stress scenarios with novel estimation techniques
To ensure the financial soundness of the simulator, we've created a comprehensive validation suite:
See the complete Financial Validation Checklist for a detailed breakdown of validation requirements.
- test_finance_calculations.py: Unit tests for core financial calculations
- integration_test.py: End-to-end tests for the entire system
- financial_metrics.py: Advanced financial metrics beyond basic simulation
- stress_testing.py: Tools for scenario analysis and stress testing
The simulator now includes advanced financial metrics:
- Internal Rate of Return (IRR): Time-weighted returns by tenor and portfolio-wide
- Weighted Average Life (WAL): Average time to principal repayment
- Recovery Rates: Effective recovery on defaulted loans
- Sharpe Ratios: Risk-adjusted returns
- Duration and Convexity: Interest rate sensitivity measures
- Concentration Risk: Using Herfindahl-Hirschman Index (HHI)
- Cash Flow Volatility: Variability in periodic cash flows
Analyze portfolio performance under various scenarios:
- Default Stress: Increased default rates
- Interest Rate Changes: Rising or falling rate environments
- Allocation Shifts: Changes in tenor distribution
- Combined Scenarios: Multiple simultaneous stresses
- Sensitivity Analysis: Parameter-specific impact testing
The simulator features a sophisticated multi-tiered approach to IRR calculation in extreme stress scenarios:
- Standard IRR Calculation: First attempts the conventional IRR calculation using
numpy_financial.irr - Alternative Cash Flow Analysis: If the standard approach fails, tries a restructured cash flow analysis
- MOIC-Based Estimation: For severely distressed portfolios, estimates IRR using a modified MOIC approach
- Default-Rate Scaled Estimations: In catastrophic scenarios, uses default rate scaling to provide meaningful negative IRR values
This advanced approach ensures that even in extreme stress cases where conventional IRR calculation fails (due to all-negative cash flows), the simulator still provides financially meaningful metrics, avoiding "N/A" or "Not calculable" results.
Stress test reports now use descriptive financial terminology for extreme scenarios:
- Positive Returns: Shown as standard percentages (e.g., "16.17%")
- Minor Losses: Displayed as "Loss (-X.XX%)"
- Significant Losses: Shown as "Major Loss (-XX.X%)"
- Severe Losses: Displayed as "Severe Loss (-XX%)"
- Catastrophic Losses: Indicated as "Catastrophic (-XX%)"
- Total Losses: Clearly labeled as "Total Loss" rather than "-∞"
This improved terminology matches how credit analysts and portfolio managers would describe portfolio performance in real-world stressed environments.
-
Install dependencies:
pip install -r requirements.txt -
Run the simulator:
streamlit run streamlit_stress_test_app.py -
Adjust parameters in the sidebar and explore the visualizations
python -m unittest test_finance_calculations.py
python -m unittest integration_test.py
- Python 3.7+
- Streamlit
- Pandas
- NumPy
- NumPy-Financial
- Plotly
- Matplotlib
See the Financial Validation Checklist for current development priorities.
- Implement items from the checklist
- Add tests for new functionality
- Ensure financial soundness of all calculations
- Submit pull requests with detailed explanations
This file is part of Mythral AI™ which is owned by DubPrime, Inc. Copyright (C) 2025 Vincent Lucero vincent@dubprime.com https://www.linkedin.com/in/vincent-lucero/
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see https://www.gnu.org/licenses/.