# thiagofsdata-collab/credit-risk-analysis architecture

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

- Interactive diagram: https://gitdiagram.com/thiagofsdata-collab/credit-risk-analysis
- Repository: https://github.com/thiagofsdata-collab/credit-risk-analysis

## Overview

1. **Purpose and shape of the repo**  
   This is an end-to-end **credit risk analytics pipeline**, not a general application. It takes a Kaggle loan default dataset, loads it into **PostgreSQL**, transforms it into a cleaned analytical layer, runs segment/risk analyses in **SQL + Python**, and publishes results in **Power BI**.

2. **Primary data flow**  
   The core flow is: external CSV dataset → raw database table → cleaned database table → analytical SQL queries / pandas metrics → exported outputs and dashboard. The repository is organized around this pipeline rather than around a service/API boundary.

3. **Ingestion boundary**  
   `src/ingestion/load_data.py` is the entry point for bringing the raw Kaggle file into the database. This is the boundary between file-based input and the relational model. The README indicates the raw file is not versioned and must be supplied locally, so ingestion depends on external user setup.

4. **Database and transformation layer**  
   `sql/schema/01_create_tables.sql` and `sql/schema/02_create_clean_table.sql` define the persistent warehouse-like layers:
   - `loans_raw` for imported source data
   - `loans_clean` for standardized, imputed, and feature-engineered data  
   This is the main architectural separation: raw data is preserved, while downstream work is done from a curated table.

5. **Analytical SQL layer**  
   `sql/queries/` contains the business logic for the core metrics:
   - default rate by segment
   - delinquency risk profiling
   - portfolio risk concentration  
   These queries are where segmentation, ranking, and concentration analysis live. The README shows use of **CTEs** and **window functions**, so the database is doing much of the heavy analytical work.

6. **Python analysis layer**  
   `src/analysis/credit_risk_analysis.py` likely extracts results from PostgreSQL and computes portfolio metrics or summary tables with **pandas** and **SQLAlchemy**. This layer sits between the database and the reporting artifacts, handling analysis that is easier in Python than in pure SQL.

7. **Visualization and reporting boundary**  
   `src/visualization/dashboard.py` supports dashboard-oriented data preparation, while the primary reporting artifact is the **Power BI** dashboard in `outputs/credit_risk_dashboard.pbix`. The dashboard connects directly to PostgreSQL, so reporting is database-backed rather than file-backed.

8. **Exploratory analysis vs. production analysis**  
   `notebooks/exploratory/01_eda_credit_risk.ipynb` is separate from the scripted pipeline and serves as exploratory work. That makes the repo split fairly clear: notebooks for discovery, SQL/Python scripts for repeatable analysis, Power BI for presentation.

9. **Artifacts and deliverables**  
   The `outputs/` folder contains exported analysis results, figures, and the Power BI file. This indicates the repo is designed to produce reusable deliverables rather than only interactive runtime output.

10. **Supporting documentation and semantic layer**  
   `docs/data_dictionary.md` suggests the project defines dataset fields and business meanings explicitly, which is important because the main transformations depend on domain-specific interpretations like delinquency, utilization, and income bands.

11. **Runtime and infrastructure assumptions**  
   The architecture depends on:
   - **PostgreSQL 15** running in **Docker**
   - **Python 3.11**
   - local credentials/config for database access
   - a manually supplied Kaggle CSV  
   Power BI is a separate desktop consumer of the curated database, not part of the Python runtime.

12. **How to think about the boundaries in a diagram**  
   A good architecture diagram should show three major zones:
   - **Source/ingestion zone**: Kaggle CSV + loader script
   - **Data/analytics zone**: PostgreSQL raw/clean tables + SQL queries + Python analysis
   - **Presentation zone**: Power BI dashboard and exported figures/reports  
   The most important boundary is between **raw source data** and **clean analytical data**, because that is where the project’s business logic is concentrated.

Main entry points: [`docs/data_dictionary.md`](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/docs/data_dictionary.md) (Data Dictionary), [`src/ingestion/load_data.py`](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/ingestion/load_data.py) (Load Data), [`sql/schema/01_create_tables.sql`](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/01_create_tables.sql) (Raw Schema), [`sql/schema/02_create_clean_table.sql`](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/02_create_clean_table.sql) (Clean Schema)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_source["Source & Ingestion"]
  node_kaggle_csv["Kaggle CSV<br/>external dataset"]
  node_load_data["Load Data<br/>ingestion script<br/>[load_data.py]"]
end

subgraph group_warehouse["Warehouse Layer"]
  node_postgresql[("PostgreSQL<br/>database")]
  node_raw_schema["Raw Schema<br/>ddl"]
  node_clean_schema["Clean Schema<br/>ddl"]
  node_loans_raw[("Loans Raw<br/>raw table")]
  node_loans_clean[("Loans Clean<br/>analytical table")]
end

subgraph group_analytics["Analytics Layer"]
  node_segment_query["Segment Risk<br/>sql query"]
  node_delinq_query["Delinquency Profile<br/>sql query"]
  node_concentration_query["Risk Concentration<br/>sql query"]
  node_analysis_py["Credit Analysis<br/>python analysis"]
end

subgraph group_reporting["Reporting Layer"]
  node_dashboard_py["Dashboard Prep<br/>python prep<br/>[dashboard.py]"]
  node_powerbi["Power BI Dashboard<br/>reporting artifact"]
  node_html_export["HTML Export<br/>published output"]
end

subgraph group_support["Supporting Assets"]
  node_eda_notebook["EDA Notebook"]
  node_data_dict["Data Dictionary<br/>documentation<br/>[data_dictionary.md]"]
end

node_kaggle_csv -->|"input"| node_load_data
node_load_data -->|"write"| node_postgresql
node_raw_schema -->|"creates"| node_loans_raw
node_clean_schema -->|"creates"| node_loans_clean
node_loans_raw -->|"transform"| node_loans_clean
node_loans_clean -->|"analyze"| node_segment_query
node_loans_clean -->|"analyze"| node_delinq_query
node_loans_clean -->|"analyze"| node_concentration_query
node_postgresql -->|"read"| node_analysis_py
node_analysis_py -->|"prepare"| node_dashboard_py
node_dashboard_py -->|"publish"| node_powerbi
node_postgresql -->|"connects to"| node_powerbi
node_postgresql -->|"source"| node_html_export
node_eda_notebook -.->|"explore"| node_loans_clean
node_data_dict -.->|"defines"| node_raw_schema
node_data_dict -.->|"defines"| node_clean_schema

click node_load_data "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/ingestion/load_data.py"
click node_raw_schema "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/01_create_tables.sql"
click node_clean_schema "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/02_create_clean_table.sql"
click node_segment_query "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/01_default_rate_by_segment.sql"
click node_delinq_query "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/02_delinquency_risk_profile.sql"
click node_concentration_query "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/03_portfolio_risk_concentration.sql"
click node_analysis_py "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/analysis/credit_risk_analysis.py"
click node_dashboard_py "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/visualization/dashboard.py"
click node_powerbi "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/outputs/credit_risk_dashboard.pbix"
click node_html_export "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/outputs/figures/credit_risk_dashboard.html"
click node_eda_notebook "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/notebooks/exploratory/01_eda_credit_risk.ipynb"
click node_data_dict "https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/docs/data_dictionary.md"

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_kaggle_csv,node_load_data toneBlue
class node_postgresql,node_raw_schema,node_clean_schema,node_loans_raw,node_loans_clean toneAmber
class node_segment_query,node_delinq_query,node_concentration_query,node_analysis_py toneMint
class node_dashboard_py,node_powerbi,node_html_export toneRose
class node_eda_notebook,node_data_dict toneIndigo
```

## Components

### Source & Ingestion

External dataset and entry point into PostgreSQL.

- **Kaggle CSV**: external dataset · Manually supplied loan default source file.
- [Load Data](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/ingestion/load_data.py): ingestion script · Imports the raw Kaggle file into PostgreSQL. · `src/ingestion/load_data.py`

### Warehouse Layer

Raw and cleaned database tables plus schema definitions.

- **PostgreSQL**: database · Operational analytical database backing the pipeline.
- [Raw Schema](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/01_create_tables.sql): ddl · Creates the preserved raw loans table. · `sql/schema/01_create_tables.sql`
- [Clean Schema](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/schema/02_create_clean_table.sql): ddl · Creates the standardized analytical loans table. · `sql/schema/02_create_clean_table.sql`
- **Loans Raw**: raw table · Imported source data stored without transformation.
- **Loans Clean**: analytical table · Cleaned and feature-engineered dataset for analysis.

### Analytics Layer

SQL business logic and Python-based analysis.

- [Segment Risk](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/01_default_rate_by_segment.sql): sql query · Default rate analysis by borrower segment. · `sql/queries/01_default_rate_by_segment.sql`
- [Delinquency Profile](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/02_delinquency_risk_profile.sql): sql query · Profiles delinquency and risk behavior. · `sql/queries/02_delinquency_risk_profile.sql`
- [Risk Concentration](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/sql/queries/03_portfolio_risk_concentration.sql): sql query · Measures portfolio concentration and ranking. · `sql/queries/03_portfolio_risk_concentration.sql`
- [Credit Analysis](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/analysis/credit_risk_analysis.py): python analysis · Runs pandas/SQLAlchemy analysis against PostgreSQL. · `src/analysis/credit_risk_analysis.py`

### Reporting Layer

Dashboard preparation and published deliverables.

- [Dashboard Prep](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/src/visualization/dashboard.py): python prep · Prepares data structures for dashboard consumption. · `src/visualization/dashboard.py`
- [Power BI Dashboard](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/outputs/credit_risk_dashboard.pbix): reporting artifact · Published interactive dashboard connected to PostgreSQL. · `outputs/credit_risk_dashboard.pbix`
- [HTML Export](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/outputs/figures/credit_risk_dashboard.html): published output · Static dashboard export for sharing and review. · `outputs/figures/credit_risk_dashboard.html`

### Supporting Assets

Documentation and exploratory analysis.

- [EDA Notebook](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/notebooks/exploratory/01_eda_credit_risk.ipynb): notebook · Exploratory analysis used for discovery and validation. · `notebooks/exploratory/01_eda_credit_risk.ipynb`
- [Data Dictionary](https://github.com/thiagofsdata-collab/credit-risk-analysis/blob/main/docs/data_dictionary.md): documentation · Defines dataset fields and business meanings. · `docs/data_dictionary.md`

## Connections

- Kaggle CSV → Load Data: input · Local source file is loaded by the ingestion script.
- Load Data → PostgreSQL: write · Loads source data into the database.
- Raw Schema → Loans Raw: creates · Defines the raw landing table.
- Clean Schema → Loans Clean: creates · Defines the curated analytical table.
- Loans Raw → Loans Clean: transform · Raw data is standardized and feature-engineered into the clean layer.
- Loans Clean → Segment Risk: analyze · Clean data feeds default-rate segmentation.
- Loans Clean → Delinquency Profile: analyze · Clean data feeds delinquency profiling.
- Loans Clean → Risk Concentration: analyze · Clean data feeds concentration analysis.
- PostgreSQL → Credit Analysis: read · Python analysis extracts data from the database.
- Credit Analysis → Dashboard Prep: prepare · Analysis outputs are shaped for dashboard use.
- Dashboard Prep → Power BI Dashboard: publish · Prepared outputs support the Power BI report.
- PostgreSQL → Power BI Dashboard: connects to · Dashboard reads curated data directly from PostgreSQL.
- PostgreSQL → HTML Export: source · Exported figures are derived from database-backed analysis.
- EDA Notebook → Loans Clean: explore · Notebook work uses the curated dataset for investigation.
- Data Dictionary → Raw Schema: defines · Semantic definitions inform the database schema.
- Data Dictionary → Clean Schema: defines · Business meanings guide cleaning and feature engineering.
