Skip to content

About

Buy-side decision tool for structured credit: loan-level prepayment & convexity modeling, credit stress testing with tranche waterfalls, and risk-adjusted relative value — driven by live FRED data.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

1 Commit

Folders and files

Repository files navigation

Fixed-Income Structured Credit Lab

A buy-side decision tool for structured fixed-income products. It takes real loan-level mortgage data and macro rates, models prepayment and credit behavior, and turns that into a relative-value view a fixed-income PM or credit analyst would actually use: which coupon / vintage / tranche would I buy, and why?

The whole system runs through one cash-flow engine, so the rates side (prepayment, duration, convexity) and the credit side (default, severity, loss) are not two disconnected models — they are two sets of assumptions feeding the same waterfall.

Architecture

  DATA LAYER        Fannie Mae loan-level performance  +  FRED macro / rates
       │
       ▼
  MODULE 1          Prepayment model: realized CPR/SMM -> S-curve on refi
  (rates spine)     incentive + seasoning + burnout
                    -> effective duration & NEGATIVE CONVEXITY under rate shocks
       │
       ▼
  MODULE 2          Credit stress: scenario (recession / rate spike) ->
  (credit)          default & severity assumptions
                    -> cash-flow waterfall -> tranche expected loss, subordination
       │
       ▼
  MODULE 3          Relative value: risk-adjusted spread across coupons /
  (RV)              vintages / tranches under each scenario
       │
       ▼
  MODULE 4          Decision dashboard: ranked buy list + the charts that justify it
  (output)

Sample output

The convexity smile — effective convexity is most negative near the par/cusp coupon, the classic MBS signature (generated by the model, not drawn by hand):

Convexity smile

Negative convexity made visual — the price–rate curve sits below its duration tangent on both sides; and tranche losses by scenario, where subordination keeps the senior untouched while equity absorbs first:

Price–rate (negative convexity) Tranche loss by scenario
Price-rate Tranche loss

Run python -m scripts.build_report for the full one-page decision report.

Why this design

  • Coherent interview story. One spine done well beats four done thinly. The arc is data -> model -> decision, and every layer is defensible in a drill-down.
  • Convexity is the differentiator. Negative convexity falls out of Module 1 numerically — as rates fall, prepayments accelerate, duration shortens, price compresses. That is the chart that sets this apart from a generic duration deck.
  • Real, free data. Fannie Mae loan-level is the cleanest free structured- finance dataset (millions of real loans: prepay, delinquency, FICO, LTV, note rate). FRED supplies the rate environment.

Data sources (all free)

Source What Use
FRED (primary, dynamic) Mortgage rate, Treasury curve, unemployment, home-price index Live rate environment + macro-anchored credit scenarios. Keyless, auto-fetched, cached — no manual step
Fannie Mae Loan-Level (optional) Real loan performance: prepay, DQ, FICO, LTV, note rate Loan-level prepayment fit (gated download; loader built + tested)
SEC EDGAR (later) ABS/CLO remittance reports Real tranches for Module 2

Data philosophy: the rate environment and macro scenarios are pulled live from FRED every run (cached to disk, refreshes when online, falls back to documented defaults offline). Loan-level GSE data is gated behind a login, so it's an optional upgrade for the prepayment fit rather than a required manual download.

Status

  • Repo + architecture
  • Synthetic loan-pool generator (real Fannie Mae schema) — lets the full pipeline run before real data is wired in
  • Module 1: realized CPR/SMM, S-curve prepayment fit, burnout, effective duration & convexity under rate shocks
  • Module 1 charts: S-curve fit, convexity smile, price–rate profile (outputs/)
  • FRED loader (keyless, cached) + real Fannie Mae loader (validated offline)
  • Unified dataset interface (synthetic / real behind one API)
  • Module 2: credit stress engine (default/severity/recovery) + sequential-pay tranche waterfall — conservation & subordination tested
  • Module 3: relative value — yield, expected loss across probability-weighted scenarios, risk-adjusted spread ranking + buy recommendation
  • OC/IC coverage tests + stress matrix (unemployment × home-price surface)
  • Module 4: Streamlit decision dashboard + self-contained HTML report
  • Live FRED data layer — rate environment + macro-anchored scenarios, auto-fetched & cached, graceful offline fallback (16 tests)
  • Optional: loan-level prepayment fit on real Fannie Mae data (loader ready)

Quickstart

pip install -r requirements.txt
python -m scripts.demo_prepay        # Module 1: prepayment & convexity
python -m scripts.demo_credit        # Modules 2-3: credit stress, waterfall, RV
python -m scripts.build_report       # one-file HTML decision report -> outputs/report.html
streamlit run app.py                 # Module 4: interactive decision dashboard
python -m pytest tests/ -q           # conservation + loader + RV tests

See NARRATIVE.md for the interview story and design rationale.

Data: live by default

Nothing to download. On every run the pipeline pulls the current rate environment and macro conditions from FRED (src/data/macro.py):

  • the 30y mortgage rate sets the prevailing rate / refi incentive (Module 1),
  • unemployment and the home-price index drive the credit scenarios (current / recession / severe, via scenarios.from_macro).

FRED is keyless; series are cached to data_files/cache/ and refresh when online. Offline, it falls back to documented defaults and flags live=False so nothing hard-fails. See it in any run's header line, e.g. FRED live as of 2024-06-01: 30y mortgage 7.10%, unemployment 4.1%, HPI YoY +3.0%.

Optional upgrade: loan-level prepayment fit

For a prepayment S-curve fit to individual loans (vs. the calibrated model anchored to live rates), drop in real Fannie Mae loan-level data:

  1. Free registration → download one quarter (e.g. 2019Q1) from Fannie's Single-Family Loan Performance portal.
  2. Confirm the column layout (positions drift between vintages):
    from src.data import fannie
    fannie.preview_raw("path/to/2019Q1.csv")   # prints indexed fields of row 1
  3. Adjust fannie.DEFAULT_LAYOUT if needed, then:
    python -m scripts.build_report --real path/to/2019Q1.csv

The scheduled/unscheduled principal split is derived from per-month UPB by amortization — the logic the offline round-trip test validates.

Layout

src/
  config.py          paths, constants
  data/
    schema.py        Fannie Mae loan-level field definitions + Pool
    synthetic.py     synthetic pool generator (mimics the real schema)
    fred.py          FRED macro/rate loader (keyless, cached)
    macro.py         live market context + macro-anchored credit scenarios
    fannie.py        real Fannie Mae loader + amortization-based prepay derivation
    dataset.py       unified entry point (synthetic / real behind one API)
  prepay/
    cpr.py           CPR/SMM math, refi incentive, S-curve model, duration/convexity
    plots.py         S-curve fit, convexity smile, price–rate charts
  credit/
    scenarios.py     macro scenarios -> CDR / severity / prepay multiplier
    engine.py        collateral cash flows with defaults, severity, lagged recovery
    waterfall.py     sequential-pay tranches + OC/IC coverage tests
    plots.py         cumulative loss, tranche loss, RV, stress-matrix charts
  rv/
    relative_value.py  yield solver, expected loss, risk-adjusted spread, recommend
    stress.py        unemployment × home-price stress matrix
app.py               Module 4: Streamlit decision dashboard
scripts/
  demo_prepay.py     Module 1 vertical slice (--real for live data)
  demo_credit.py     Modules 2-3 vertical slice
  build_report.py    self-contained HTML decision report (--real for live data)
tests/
  test_fannie_roundtrip.py  offline validation of the real-data loader
  test_waterfall.py         conservation, subordination, loss-ordering invariants
  test_rv.py                yield solver, coverage, stress-matrix tests
  test_fred.py              FRED parsing + live/fallback macro context
  test_report_smoke.py      full pipeline builds an HTML report end to end
NARRATIVE.md         interview story + design rationale
LICENSE · Makefile   MIT license · make {test,prepay,credit,report,dashboard}
outputs/             generated charts (PNG) + report.html

About

Buy-side decision tool for structured credit: loan-level prepayment & convexity modeling, credit stress testing with tranche waterfalls, and risk-adjusted relative value — driven by live FRED data.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages