A comprehensive loan data validation and metrics engine using DuckDB.
Project description
Loan Analyzer
A lightweight, SQL-powered engine for validating loan data and computing analytical metrics using DuckDB. It is designed to transform raw CSV loan data into auditable, time-series financial states.
Project Structure
The project follows the src layout for better packaging and isolation:
loan_analyzer/
├── src/
│ └── loan_analyzer/
│ ├── loan_system/ # Core Python logic
│ ├── sql/ # SQL transformations (CTEs, etc.)
│ └── tests/ # SQL-based validation tests
├── docs/ # Documentation
├── pyproject.toml # Build configuration
└── README.md
Installation
You can install the package directly from GitHub:
pip install git+https://github.com/zakiahmed1234/loan_analyzer.git
Core API Reference
The API is modularized into specialized classes within the loan_analyzer package.
1. DataLoader
Handles data ingestion from CSVs into an in-memory DuckDB instance.
__init__(directory_path, connection=None)
- directory_path: Directory containing the required CSV files.
- connection: (Optional) An existing DuckDB connection. If omitted, a new in-memory connection is created.
- Behavior: Automatically loads all
.csvfiles in the directory as tables.
from loan_analyzer import DataLoader
loader = DataLoader("./data/my_loan_batch")
2. DataValidation
Runs a suite of SQL-based invariant tests to ensure data integrity.
__init__(loader: DataLoader)
Initializes with a populated DataLoader.
validate_all()
Runs all tests in src/loan_analyzer/tests/data_validation/.
- Returns: A pandas DataFrame containing the results (check name, pass/fail, details).
from loan_analyzer import DataValidation
validator = DataValidation(loader)
report = validator.validate_all()
print(report)
3. LoanCalculation
The execution engine for financial business logic.
__init__(validator: DataValidation)
Initializes the runner with a DataValidation instance. It ensures validation has been run before executing calculations.
run_loan_state()
Executes the Recursive Balance Engine. This calculates monthly snapshots for every loan, tracking interest accrual, payments, and principal amortization.
from loan_analyzer import LoanCalculation
calculator = LoanCalculation(validator)
calculator.run_loan_state()
run_delinquency()
Calculates the Delinquency Waterfall (DPD and status classification).
calculator.run_delinquency()
4. LoanAudit
The Forensic Audit Tool. Provides a line-item reconciliation of a loan's principal change.
audit_loan(loan_id, as_of_date)
Returns a detailed text report reconciling Opening - Closing == Amortization + Write-offs.
from loan_analyzer import LoanAudit
auditor = LoanAudit(calculator)
print(auditor.audit_loan("L-001", "2024-05-15"))
Advanced Usage Example
from loan_analyzer import DataLoader, DataValidation, LoanCalculation, LoanAudit
# 1. Setup Data Environment (Auto-loads CSVs)
loader = DataLoader("FakeData/1/")
# 2. Perform Health Check
validator = DataValidation(loader)
report = validator.validate_all()
if not report.passed.all():
print("Data issues found!")
print(report[report.passed == False])
# 3. Generate Financial Models
calculator = LoanCalculation(validator)
calculator.run_all() # Runs both state and delinquency
# 4. Forensic Investigation
auditor = LoanAudit(calculator)
print(auditor.audit_loan("3c60c443-a997-4a9c-8948-33fd5301208e", "2023-12-01"))
SQL Resources
The core logic is modularized in src/loan_analyzer/sql/. You can find the recursive CTEs for balance tracking and the cumulative window functions for delinquency there.
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file loan_analyzer-0.1.1.tar.gz.
File metadata
- Download URL: loan_analyzer-0.1.1.tar.gz
- Upload date:
- Size: 12.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da4506f4bd2253b658beaf5094a631823b7f8265c37520ce8fc0febbe5e76880
|
|
| MD5 |
d584fd3502eb7d6a35fb6f976eba8415
|
|
| BLAKE2b-256 |
dc55fc8552b6bc2339dc377dd64244a8e202d55e4c2723ef4855bb55b0865dcc
|
File details
Details for the file loan_analyzer-0.1.1-py3-none-any.whl.
File metadata
- Download URL: loan_analyzer-0.1.1-py3-none-any.whl
- Upload date:
- Size: 15.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.14.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9437c98d613df51308ae3abc7ddd9ed319038bca9315b17999e9959d938debf7
|
|
| MD5 |
0c94d1ba4cbe3fe2fe778305018ba42f
|
|
| BLAKE2b-256 |
80a2e37fffa1ce584efbbcfd019be750d41117cd4abdfad10ab301ba79abe830
|