This project demonstrates a simplified Residential Mortgage-Backed Security (RMBS) transaction workflow:
-
Loan Ingestion
Loads mortgage loans from a CSV file (sample_loans.csv) or, if unavailable, generates a synthetic pool. -
Risk Modeling
- Default: Uses a logistic-based default probability that factors in FICO scores and loan-to-value ratios.
- Prepayment: Applies a constant annual CPR converted to monthly probabilities.
-
Cash Flow Simulation
- Implements standard mortgage amortization (principal + interest).
- Assesses whether a loan defaults (with partial recovery) or prepays.
-
Structuring
- Aggregates monthly interest and principal from the pool.
- Distributes proceeds to three tranches (Senior, Mezzanine, Equity) via a simple top-down waterfall.
-
Bond Analytics
- Computes each tranche’s Yield (IRR) and Weighted Average Life (WAL).
-
Visualization
- Optionally produces a stacked area plot of monthly tranche cash flows.
-
Testing
- Contains unit tests that verify core functionalities, ensuring stability and correctness.
- Python 3.8+
- Refer to
requirements.txtfor required libraries: numpy>=1.21.0 pandas>=1.3.0 matplotlib>=3.4.0 pytest>=7.0.0 black>=23.1.0 flake8>=5.0.0
Install all dependencies:
pip install -r requirements.txt-
Loan Data
- If you have a loan dataset, place a CSV file in the project folder or specify its path.
- A sample file (
sample_loans.csv) is provided.
-
Run the Demo
From the project directory, use:python main.py \ --csv sample_loans.csv \ --months 360 \ --scenario base \ --show_plot--csvindicates the path to your loan CSV. If omitted, a synthetic set of loans is generated.--monthsis the number of months to simulate (e.g., 360 for 30 years).--scenariocan bebase,stress, oroptimistic, defining default/prepayment assumptions.--show_plotwill display a stacked area chart of monthly cash flows allocated to each tranche.
-
Results
- The script prints monthly cash flows for each tranche, as well as Yield (IRR) and WAL.
- If
--show_plotis used, a graphical output of monthly cash flows is displayed.
A suite of pytest-based tests is included. Run them with:
pytestThese tests confirm that each component (loan data, default/prepayment models, waterfall allocations, etc.) behaves as intended.
rmbs_demo/
├── .setup.cfg
├── README.md
├── requirements.txt
├── main.py # A small script that imports and runs src.main
├── src/
│ ├── __init__.py
│ ├── main.py # Contains the bulk of the application logic
│ ├── loan_pool.py
│ ├── risk_models.py
│ ├── structuring.py
│ ├── simulation.py
│ ├── metrics.py
│ ├── visualization.py
│ └── data/
│ └── sample_loans.csv
└── tests/
├── test_loan_pool.py
├── test_risk_models.py
├── test_structuring.py
├── test_simulation.py
└── test_metrics.py
- main.py
Command-line entry point. Parses arguments, loads loans, runs the simulation, and summarizes results. - loan_pool.py
ContainsLoandata class and methods to load or generate loan data. - risk_models.py
Implements default and prepayment probability calculations. - simulation.py
Coordinates monthly activity, including amortization, default/prepayment, and aggregation of payments. - structuring.py
Defines tranche objects and a waterfall function to allocate monthly cash flows. - metrics.py
Provides functions to compute bond-level metrics (Yield/IRR, WAL). - visualization.py
Creates a stacked area chart of monthly tranche cash flows. - tests/
Contains automated tests to verify correctness.