# jedizr/credit-risk-and-loan-default-analysis architecture

> Architecture diagram of the GitHub repository jedizr/credit-risk-and-loan-default-analysis, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-07-17). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/jedizr/credit-risk-and-loan-default-analysis
- Repository: https://github.com/jedizr/credit-risk-and-loan-default-analysis

## Overview

#### Purpose and Entry Points
This is a Python ML system for predicting loan approval from applicant data, with reproducible training, batch scoring, explanations, and report generation. The primary entry point is the `credit-risk` CLI in `src/credit_risk/cli.py`; orchestration is shared with notebooks through `src/credit_risk/workflow.py`. Container execution is defined by `Dockerfile`.

#### Data Lifecycle and Provenance
Immutable CSV input is read from `data/raw/loan_risk_prediction_dataset.csv`. The workflow produces typed, hashed parquet stages:
- repaired/typed data: `data/processed/applicants.parquet`
- stateless engineered model input: `data/preprocessed/applicants.parquet`
- stage hashes and provenance: `data/registry.json`

Data contracts, quality repair, and I/O sit behind `src/credit_risk/data/schema.py`, `src/credit_risk/data/quality.py`, and `src/credit_risk/data/io.py`. Stateful imputation, scaling, and encoding are intentionally deferred to training folds to prevent holdout leakage.

#### Workflow Orchestration
`src/credit_risk/workflow.py` coordinates `prepare → preprocess → train → evaluate`, callable as a full run or an individual stage. `src/credit_risk/config.py` centralizes paths, seed, domain thresholds, and training options, optionally overridden by root `config.toml`. CLI commands also expose prediction and explanation paths for new CSV inputs.

#### Feature and Anomaly Stage
Stateless domain feature creation and optional feature selection are implemented in `src/credit_risk/features/engineering.py` and `src/credit_risk/features/selection.py`. Training-row outliers are identified through interchangeable detectors in `src/credit_risk/anomaly/detectors.py` and removed/retained through `src/credit_risk/anomaly/handling.py`. The optional autoencoder feature transformer is adapted to sklearn in `src/credit_risk/features/autoencoder.py`.

#### Model Pipeline, Training, and Tuning
`src/credit_risk/pipeline.py` builds the sklearn composite that owns fold-fitted preprocessing plus classifier, ensuring training and inference share transformations. Model strategies and runtime selection are defined in `src/credit_risk/models/base.py`, `src/credit_risk/models/implementations.py`, and `src/credit_risk/models/registry.py`; supported implementations include LightGBM, logistic regression, random forest, and gradient boosting. `src/credit_risk/train.py` performs split, fit, calibration, scoring, and persistence; `src/credit_risk/tuning.py` runs Optuna cross-validation searches.

#### Evaluation, Decisioning, and Diagnostics
The headline quality metric is PR-AUC because approvals are imbalanced. `src/credit_risk/evaluation.py` performs validation and selects a decision threshold using configured false-approval versus missed-approval cost. Fairness, error slices, and model explanations are separate boundaries in `src/credit_risk/fairness.py`, `src/credit_risk/error_analysis.py`, and `src/credit_risk/explain.py`; SHAP supports global and applicant-level decision reasons.

#### Artefacts, Runtime, and Delivery
Versioned model artefacts and reproducibility manifests are managed by `src/credit_risk/versioning.py`; models are persisted as joblib plus content-addressed metadata, model cards, and a current-run registry under `models/`. Runtime requires Python managed by uv and ML dependencies including scikit-learn, LightGBM, PyArrow, SHAP, and Optuna. Docker mounts raw data read-only and derived data, models, reports, and output read-write; GitHub Actions in `.github/workflows/ci.yml` and `.github/workflows/release.yml` validate and publish the image to GHCR.

Main entry points: [`src/credit_risk/train.py`](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/train.py) (Training & calibration), [`src/credit_risk/workflow.py`](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/workflow.py) (Workflow stages), [`src/credit_risk/data/schema.py`](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/data/schema.py) (Data contracts & repair), [`src/credit_risk/features/engineering.py`](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/features/engineering.py) (Stateless features)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_entry["Entry &amp; orchestration"]
  node_cli["credit-risk CLI<br/>[cli.py]"]
  node_workflow["Workflow stages<br/>orchestrator<br/>[workflow.py]"]
  node_config["Configuration<br/>settings<br/>[config.py]"]
end

subgraph group_data["Data lifecycle"]
  node_raw_data[("Immutable loan CSV<br/>raw dataset")]
  node_data_contracts["Data contracts &amp; repair<br/>data quality<br/>[schema.py]"]
  node_processed_data[("Typed applicants<br/>processed parquet<br/>[applicants.parquet]")]
  node_preprocessed_data[("Model input<br/>preprocessed parquet<br/>[applicants.parquet]")]
  node_provenance["Stage provenance<br/>hash registry<br/>[registry.json]"]
end

subgraph group_ml["ML pipeline"]
  node_feature_stage["Stateless features<br/>feature engineering<br/>[engineering.py]"]
  node_anomaly_handling["Training outliers<br/>anomaly handling<br/>[handling.py]"]
  node_model_pipeline["Fold-fitted ML pipeline<br/>sklearn composite<br/>[pipeline.py]"]
  node_model_strategies["Model strategies<br/>model registry<br/>[registry.py]"]
  node_tuning["Optuna tuning<br/>cross-validation search<br/>[tuning.py]"]
  node_training["Training &amp; calibration<br/>trainer<br/>[train.py]"]
end

subgraph group_assessment["Assessment &amp; decisioning"]
  node_evaluation["PR-AUC &amp; thresholding<br/>evaluation<br/>[evaluation.py]"]
  node_diagnostics["Fairness, slices &amp; SHAP<br/>diagnostics<br/>[explain.py]"]
end

subgraph group_delivery["Artefacts &amp; delivery"]
  node_model_artefacts[("Versioned model artefacts<br/>model store<br/>[versioning.py]")]
  node_container_delivery["Container CI/CD<br/>delivery pipeline"]
end

node_cli -->|"invokes"| node_workflow
node_workflow -->|"loads"| node_config
node_workflow -->|"prepare"| node_data_contracts
node_raw_data -->|"read"| node_data_contracts
node_data_contracts -->|"typed output"| node_processed_data
node_data_contracts -->|"records stage hash"| node_provenance
node_workflow -->|"preprocess"| node_feature_stage
node_processed_data -->|"input"| node_feature_stage
node_feature_stage -->|"engineered output"| node_preprocessed_data
node_feature_stage -->|"records stage hash"| node_provenance
node_workflow -->|"train"| node_training
node_preprocessed_data -->|"training rows"| node_anomaly_handling
node_anomaly_handling -->|"filtered rows"| node_training
node_model_strategies -->|"selects classifier"| node_model_pipeline
node_tuning -.->|"best parameters"| node_training
node_model_pipeline -->|"shared fit/inference pipeline"| node_training
node_training -->|"validation scores"| node_evaluation
node_evaluation -->|"predictions and threshold"| node_diagnostics
node_training -->|"persist model"| node_model_artefacts
node_evaluation -->|"metrics and model card"| node_model_artefacts
node_cli -->|"batch scoring"| node_model_artefacts
node_model_artefacts -->|"applicant explanations"| node_diagnostics
node_container_delivery -->|"runs"| node_cli

click node_cli "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/cli.py"
click node_workflow "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/workflow.py"
click node_config "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/config.py"
click node_raw_data "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/raw/loan_risk_prediction_dataset.csv"
click node_data_contracts "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/data/schema.py"
click node_processed_data "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/processed/applicants.parquet"
click node_feature_stage "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/features/engineering.py"
click node_preprocessed_data "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/preprocessed/applicants.parquet"
click node_provenance "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/registry.json"
click node_anomaly_handling "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/anomaly/handling.py"
click node_model_pipeline "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/pipeline.py"
click node_model_strategies "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/models/registry.py"
click node_tuning "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/tuning.py"
click node_training "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/train.py"
click node_evaluation "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/evaluation.py"
click node_diagnostics "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/explain.py"
click node_model_artefacts "https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/versioning.py"
click node_container_delivery "https://github.com/jedizr/credit-risk-and-loan-default-analysis/tree/main/Dockerfile"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_workflow,node_config toneBlue
class node_raw_data,node_data_contracts,node_processed_data,node_preprocessed_data,node_provenance toneAmber
class node_feature_stage,node_anomaly_handling,node_model_pipeline,node_model_strategies,node_tuning,node_training toneMint
class node_evaluation,node_diagnostics toneRose
class node_model_artefacts,node_container_delivery toneIndigo
```

## Components

### Entry & orchestration

- [credit-risk CLI](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/cli.py): CLI · Runs workflow, scoring, and explanation commands. · `src/credit_risk/cli.py`
- [Workflow stages](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/workflow.py): orchestrator · Coordinates prepare, preprocess, train, and evaluate. · `src/credit_risk/workflow.py`
- [Configuration](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/config.py): settings · Central paths, seed, thresholds, and training options. · `src/credit_risk/config.py`

### Data lifecycle

- [Immutable loan CSV](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/raw/loan_risk_prediction_dataset.csv): raw dataset · `data/raw/loan_risk_prediction_dataset.csv`
- [Data contracts & repair](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/data/schema.py): data quality · Validates, repairs, types, and reads data. · `src/credit_risk/data/schema.py`
- [Typed applicants](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/processed/applicants.parquet): processed parquet · `data/processed/applicants.parquet`
- [Model input](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/preprocessed/applicants.parquet): preprocessed parquet · `data/preprocessed/applicants.parquet`
- [Stage provenance](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/data/registry.json): hash registry · Stores stage hashes and provenance. · `data/registry.json`

### ML pipeline

- [Stateless features](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/features/engineering.py): feature engineering · Creates domain features and optionally selects them. · `src/credit_risk/features/engineering.py`
- [Training outliers](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/anomaly/handling.py): anomaly handling · Interchangeable detectors remove or retain training rows. · `src/credit_risk/anomaly/handling.py`
- [Fold-fitted ML pipeline](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/pipeline.py): sklearn composite · Owns imputation, scaling, encoding, optional autoencoder, and classifier. · `src/credit_risk/pipeline.py`
- [Model strategies](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/models/registry.py): model registry · Selects LightGBM, logistic regression, random forest, or gradient boosting. · `src/credit_risk/models/registry.py`
- [Optuna tuning](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/tuning.py): cross-validation search · `src/credit_risk/tuning.py`
- [Training & calibration](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/train.py): trainer · Splits, fits, calibrates, scores, and persists a model. · `src/credit_risk/train.py`

### Assessment & decisioning

- [PR-AUC & thresholding](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/evaluation.py): evaluation · Selects the cost-sensitive approval threshold. · `src/credit_risk/evaluation.py`
- [Fairness, slices & SHAP](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/explain.py): diagnostics · Produces fairness analysis, error slices, and decision reasons. · `src/credit_risk/explain.py`

### Artefacts & delivery

- [Versioned model artefacts](https://github.com/jedizr/credit-risk-and-loan-default-analysis/blob/main/src/credit_risk/versioning.py): model store · Manages joblib models, manifests, model cards, and current-run metadata. · `src/credit_risk/versioning.py`
- [Container CI/CD](https://github.com/jedizr/credit-risk-and-loan-default-analysis/tree/main/Dockerfile): delivery pipeline · Docker runtime with CI validation and GHCR image publishing. · `Dockerfile`

## Connections

- credit-risk CLI → Workflow stages: invokes
- Workflow stages → Configuration: loads
- Workflow stages → Data contracts & repair: prepare
- Immutable loan CSV → Data contracts & repair: read
- Data contracts & repair → Typed applicants: typed output
- Data contracts & repair → Stage provenance: records stage hash
- Workflow stages → Stateless features: preprocess
- Typed applicants → Stateless features: input
- Stateless features → Model input: engineered output
- Stateless features → Stage provenance: records stage hash
- Workflow stages → Training & calibration: train
- Model input → Training outliers: training rows
- Training outliers → Training & calibration: filtered rows
- Model strategies → Fold-fitted ML pipeline: selects classifier
- Optuna tuning → Training & calibration: best parameters
- Fold-fitted ML pipeline → Training & calibration: shared fit/inference pipeline
- Training & calibration → PR-AUC & thresholding: validation scores
- PR-AUC & thresholding → Fairness, slices & SHAP: predictions and threshold
- Training & calibration → Versioned model artefacts: persist model
- PR-AUC & thresholding → Versioned model artefacts: metrics and model card
- credit-risk CLI → Versioned model artefacts: batch scoring
- Versioned model artefacts → Fairness, slices & SHAP: applicant explanations
- Container CI/CD → credit-risk CLI: runs
