Skip to content

How It Fits Together: One Portfolio, Eight Lenses

cranalytics keeps all eight credit-risk workflows in one package because real portfolio work rarely stays inside one silo. The same consumer lending book needs segmentation, early-performance diagnostics, vintage emergence curves, lifetime loss reserves, cashflow stress runs, survival timing, loan-level scoring, and monthly operating rollforwards.

Rather than splitting those lenses across separate installs, the package enforces one simple user pattern on the outside and strict one-way seams on the inside.


The User Pattern (Learn Once, Use Everywhere)

Every workflow follows the same three-step loop:

  1. Validate inputs with check(df) — auto-detects the workflow from your columns (or checks an explicit workflow=... name), reports issues in one DataFrame, and prints the next runnable command.
  2. Run one entry point (cranalytics.<workflow>.run(df, ...)) — returns a WorkflowResult (and feature_analytics exposes focused toolkit functions directly).
  3. Consume results uniformly — every workflow result exposes the same five methods:
  4. .summary() — primary headline DataFrame
  5. .tables() — named dictionary of in-memory DataFrames
  6. .plot() — default diagnostic figure
  7. .assumptions() — workflow name, version, and effective parameters
  8. .to_dict() — JSON-safe export (summary in full, large tables capped)

See ADR 0001: Every Workflow Returns a Workflow Result for the full contract.


Package Architecture & One-Way Boundaries

+-----------------------------------------------------------------------+
|  Unified UX & Interfaces                                              |
|  check()  |  WorkflowResult  |  viz (theme_context)  |  CLI / Demo    |
+-----------------------------------------------------------------------+
          |                                           |
          v                                           v
+----------------------------------+   +--------------------------------+
|  Core Portfolio Lenses           |   |  Advanced Lenses               |
|  - vintage                       |   |  - predictive (ML backtests)   |
|  - loss_forecasting + reserve    |   |  - survival + survival_flows   |
|  - portfolio (FICO / LGD)        |   |  - simulation (cashflows)      |
|  - feature_analytics (WoE / FPF) |   |  - rollforward (monthly flows) |
+----------------------------------+   +--------------------------------+
                         \                   /
                          v                 v
+-----------------------------------------------------------------------+
|  Foundation & Contracts (strictly bottom-layer, zero workflow imports)|
|  validation  |  loan_tape  |  data_shapes  |  metrics  |  finance     |
+-----------------------------------------------------------------------+

Three enforced boundary rules keep the codebase clean as it grows (pinned by tests/test_architecture_seams.py):

  1. validation is a pure bottom layer — it never imports any workflow package or loan_tape.
  2. loan_tape only normalizes source tapes — workflow-specific adapters (loan_snapshot_to_survival_data in cranalytics.survival, build_targets_from_loan_history in cranalytics.predictive) live inside the workflow that owns the target shape.
  3. Workflow subpackages stay laterally isolated — vintage, survival, predictive, feature_analytics, and rollforward do not import one another; shared primitives live strictly in the foundation layer.

See Validation Ownership for how schema invariants and workflow-local checks divide responsibility.


One Lending Book, End to End

All eight workflows operate on just three named data shapes (see Your Data and Input Data Contracts):

Stage of your work Question you are answering Data shape Entry point
1. Ingest & sanity-check Are dates, rates, balances, and statuses clean? Loan snapshot or loan history cranalytics.loan_tape.validate_loan_tape(df, kind=...) or cranalytics.check(df)
2. Portfolio mix & LGD Where is risk concentrated across FICO bands? Loan snapshot cranalytics.portfolio.run(df)
3. Early performance & signal Which originations or features are separating bad loans early? Loan snapshot / flags cranalytics.feature_analytics (calculate_early_performance_rates, compute_woe_iv, run_fpf_workflow)
4. Cohort emergence Where will each vintage's cumulative loss land at maturity? Vintage long panel / wide triangle cranalytics.vintage.run(df)
5. Lifetime loss & reserve walk What reserve does the current book imply, and why did it move since last quarter? Loan snapshot + transition matrix or loan history cranalytics.loss_forecasting.run(df, ...) and cranalytics.reserve.reserve_walk(...)
6. Event timing When do defaults vs. prepayments happen (accounting for censoring)? Loan snapshot cranalytics.survival.run(df, ...)
7. Monthly cashflow stress How do balances, payments, and defaults evolve month by month under stress? Loan snapshot + transition model cranalytics.simulation.run(df, ...)
8. Loan-level PD modeling Which individual loans will hit a performance flag out-of-time? Historical modeling panel cranalytics.predictive.run(df, ...)
9. Operating rollforward What are next quarter's payments and charge-offs from monthly aggregated flows? Monthly aggregated segment flows cranalytics.rollforward.Rollforward(df).readiness() then .run(...)

Notice how the foundation helpers bridge shapes cleanly when moving between stages:

  • cranalytics.snapshot_from_history(history_df, as_of=...) turns a loan history ledger into a point-in-time snapshot.
  • cranalytics.history_to_triangle(history_df) aggregates loan history into a vintage loss triangle.
  • cranalytics.survival.loan_snapshot_to_survival_data(snapshot_df) adapts a normalized snapshot into survival durations and competing-risk event codes.
  • cranalytics.predictive.build_targets_from_loan_history(history_df, ...) builds FPD/FPF/charge-off targets from a normalized history panel.

Chart Styling in One Line

cranalytics.viz ships two built-in palettes ("josan" default and "apollo") and pairs semantic color roles (primary, positive, negative, neutral, muted) with Matplotlib styling.

Use theme_context() to scope both Matplotlib rcParams and the active cranalytics.viz palette for a block (both restore cleanly on exit):

from cranalytics.viz import add_source, format_percent_axis, theme_context

with theme_context("apollo"):
    fig = result.plot()
    add_source(fig, "Source: Internal portfolio tape")

To set a palette globally for a notebook session, call apply_theme("apollo") (updates Matplotlib rcParams and activates "apollo" for all subsequent cranalytics.viz charts). For Plotly charts, pass plotly_layout() to fig.update_layout(...).


Next Steps