RiskReg: imbalanced regression toolkit (φ relevance, SERT & SERA metrics, NestedCV, oversampling).
Project description
RiskReg: Rare-Event Regression Toolkit for Safety-Critical AI
| PyPI | https://pypi.org/project/riskreg/ |
| Install | pip install riskreg · optional DL extras: pip install riskreg[dl] |
RiskReg is a comprehensive experimental framework for imbalanced regression that addresses the critical challenge of rare but important target values in regression datasets. Unlike classification, regression problems often suffer from extreme value bias where rare high-value or low-value targets are underrepresented, leading to poor model performance on critical predictions.
What is Imbalanced Regression?
Traditional regression assumes uniform importance across all target values. However, in real-world scenarios:
- Housing prices: Rare luxury properties are more important than common mid-range homes
- Medical costs: Extreme treatment costs are more critical than routine expenses
- Risk assessment: High-risk events are more significant than normal occurrences
- Financial modeling: Market crashes and booms are more important than stable periods
RiskReg solves this by introducing relevance-weighted evaluation and intelligent oversampling techniques.
Key Features
Relevance Function (φ) System
Identify rare and important regions in your target distribution using multiple methods:
- Control Points: Manual or auto-generated relevance thresholds
- Density Estimation: GMM, KDE, Histogram-based relevance
- Spectral Analysis: Frequency-domain relevance detection
- Visualization: Interactive plots showing relevance curves
Regression oversampling
Synthetic minority oversampling for regression with Gaussian noise (Branco et al., 2017):
- Smart Sampling: Over-sample rare regions, under-sample common regions
- Gaussian Noise: Add realistic variation to synthetic samples
- Feature Handling: Support for both numerical and categorical features
- Quality Control: Maintains data distribution characteristics
Advanced Evaluation Metrics
Beyond traditional MAE/RMSE, RiskReg provides relevance-aware metrics:
- SERA: Squared Error-Relevance Area (φ-weighted error)
- SERT: Squared Error-Relevance at Threshold (cumulative error analysis)
- Normalization: Multiple strategies for cross-dataset comparison
- Bias Analysis: Quantify prediction bias across target regions
Robust Model Evaluation
- Nested Cross-Validation: 5x5 CV with hyperparameter tuning
- Multiple Models: Linear Regression, Decision Trees, Random Forest, XGBoost, Neural Networks
- Fair Comparison: Relevance-weighted scoring prevents scale bias
- Statistical Analysis: Bootstrap confidence intervals and significance testing
Multi-Agent Pipeline (NEW)
- CrewAI Orchestration: 5 specialized agents handle the full pipeline end-to-end
- LangChain Tools: Each pipeline stage exposed as a callable tool
- Local LLM: Runs on-device via llama-cpp-python (GGUF models) — no API keys needed
- CLI Interface: Single command to analyze, oversample, train, and evaluate
Quick Start
Installation
Published on PyPI: pypi.org/project/riskreg
# Install from PyPI
pip install riskreg
# Or with deep learning support
pip install riskreg[dl]
Basic Usage
import pandas as pd
from riskreg import phi, smoter
from riskreg.nestedCV import computeNestedCV
# Load your dataset
df = pd.read_csv('your_data.csv')
target = 'price' # Your target column
# 1. Compute relevance function
y_phi = phi(df[target], method="default")
# 2. Apply oversampling (smoter)
df_balanced = smoter(
data=df,
y=target,
k=5, # Number of neighbors
pert=0.02, # Perturbation level
samp_method="balance", # Sampling strategy
rel_thres=0.5 # Relevance threshold
)
# 3. Run nested cross-validation
results = computeNestedCV('your_data.csv')
Advanced Usage
# Custom relevance function with manual control points
ctrl_pts = [[100000, 1, 0], [200000, 0, 0], [400000, 1, 0]]
y_phi = phi(df[target], ctrl_pts=ctrl_pts, method="manual")
# Density-based relevance using GMM
y_phi = phi(df[target], method="gmm")
# Evaluate with SERT/SERA metrics (module filename contains &)
import importlib
_sert = importlib.import_module("riskreg.SERT&SERA")
sert_sera_results = _sert.compute_sert_sera("housing", dt=0.05, t0=0.5)
Multi-Agent Pipeline (LangChain + CrewAI)
RiskReg includes an AI-powered multi-agent pipeline that orchestrates the entire imbalanced regression workflow using a local LLM. Five specialized agents collaborate sequentially — each backed by LangChain tools that wrap the core library (riskreg/ in this repo; PyPI package riskreg).
Architecture
| Agent | Role | Tools |
|---|---|---|
| Data Analyst | Profile the dataset, detect imbalance | list_datasets, analyze_dataset |
| Phi Configurator | Compute relevance function | list_phi_methods, compute_phi_values |
| Oversampling Specialist | Balance rare target regions | run_oversampling |
| Model Trainer | Nested CV with RF, XGB, SVR, DNN | run_nested_cv |
| Evaluation Analyst | SERT/SERA metrics + bias analysis | compute_sert_sera, run_bias_analysis |
Setup
# Create a virtual environment (Python 3.13 recommended)
python3.13 -m venv .venv
source .venv/bin/activate
# Install core + agent dependencies
pip install -r requirements.txt
pip install "llama-cpp-python[server]" litellm
Running the Pipeline
Step 1 — Start the local LLM server (keep this running):
python -m llama_cpp.server \
--model /path/to/your-model.gguf \
--n_ctx 4096 --n_gpu_layers -1 \
--host 127.0.0.1 --port 8787
Step 2 — Launch the agent crew:
python -m agents.run --dataset housing --target SalePrice --phi-method default
CLI Options
| Flag | Description | Default |
|---|---|---|
--dataset |
Dataset name (file stem in data/) |
housing |
--target |
Target column name | last column |
--phi-method |
default, gmm, kde, hist, spectral |
default |
--rel-coef |
Box-plot coefficient for phi control points | 1.5 |
--list-datasets |
Print available datasets and exit | — |
--quiet |
Suppress verbose agent reasoning | — |
Using Tools Standalone
Each tool can be used independently without the full crew:
from agents.tools.data_tools import list_datasets, analyze_dataset
from agents.tools.phi_tools import compute_phi_values
from agents.tools.oversampling_tools import run_oversampling
# List all benchmark datasets
print(list_datasets.invoke({}))
# Analyze a dataset
print(analyze_dataset.invoke({"dataset_name": "housing"}))
# Compute phi relevance
print(compute_phi_values.invoke({"dataset_name": "housing", "method": "default"}))
# Run oversampling
print(run_oversampling.invoke({"dataset_name": "housing", "target_col": "SalePrice"}))
Research Results
Our comprehensive evaluation across 34 diverse datasets reveals:
Model Performance Insights
- XGBoost & Random Forest: Best on scale-normalized metrics (NRMSE/Target normalization)
- Decision Trees: Excel on relevance-focused metrics (SERT normalization)
- Linear Regression: Most consistent across different normalizations
- Neural Networks: Competitive but require careful hyperparameter tuning
Normalization Impact
- Adjusted SERA: 30% average improvement over raw metrics
- NRMSE/Target normalization: Best for cross-dataset comparison
- SERT normalization: Better for relevance-sensitive applications
- Combined approaches: Optimal for comprehensive evaluation
Relevance Method Sensitivity
- Default (boxplot): Most robust across datasets
- GMM/KDE: Better for multi-modal distributions
- Histogram: Good for discrete target spaces
- Spectral: Effective for time-series-like patterns
Project Structure
RiskReg/
├── riskreg/ # Core package (also published on PyPI as `riskreg`)
│ ├── phi.py # Relevance function computation
│ ├── phi_ctrl_pts.py # Control point generation
│ ├── phi_density_methods.py # Density-based methods
│ ├── smoter.py # Regression oversampling (smoter)
│ ├── nestedCV.py # Nested cross-validation
│ ├── SERT&SERA.py # Evaluation metrics
│ ├── imbreg_region_bias_analysis.py # Region bias analysis
│ └── ...
├── agents/ # LangChain + CrewAI multi-agent pipeline
│ ├── config.py # LLM config (local GGUF via llama-cpp-python)
│ ├── crew.py # CrewAI crew: 5 agents, 5 tasks
│ ├── run.py # CLI entry point
│ └── tools/ # LangChain @tool wrappers
│ ├── data_tools.py # list_datasets, analyze_dataset
│ ├── phi_tools.py # list_phi_methods, compute_phi_values
│ ├── oversampling_tools.py # run_oversampling
│ ├── training_tools.py # run_nested_cv
│ └── eval_tools.py # compute_sert_sera, run_bias_analysis
├── data/ # 34 benchmark datasets
├── pypi/ # PyPI packaging (`pip install riskreg`; imports `riskreg`)
├── results/ # Experimental results
│ ├── predictions/ # Model predictions
│ ├── tables/ # Summary tables
│ └── plots/ # Visualizations
└── notebooks/ # Analysis notebooks
Documentation
PyPI Package Documentation
For detailed API documentation, installation guides, and examples, see the pypi/ directory.
Tutorial Notebooks
- RiskReg_TUTORIAL.ipynb: Complete walkthrough of the framework
- SERA_SERT_Analysis.ipynb: Advanced analysis techniques
- Examples/: Quick-start examples and use cases
Experimental Framework
Datasets (34 total)
- Housing: Boston, California, House prices
- Healthcare: Medical costs, treatment outcomes
- Finance: Mortgage rates, insurance claims
- Manufacturing: Engine performance, quality metrics
- Energy: Power consumption, efficiency data
- And more...
Evaluation Protocol
- Data Preprocessing: Dataset-specific feature engineering
- Relevance Computation: Multiple φ methods
- Model Training: 5×5 nested cross-validation
- Metric Calculation: Standard + relevance-weighted metrics
- Statistical Analysis: Bootstrap confidence intervals
- Visualization: Comprehensive plots and tables
🛠️ Advanced Features
Bias Analysis
Use agents.tools.eval_tools.run_bias_analysis or follow the script in riskreg/imbreg_region_bias_analysis.py (configure INPUT_CSV and run as a script).
Cross-Dataset Comparison
from pathlib import Path
from riskreg.combineSertSera import combine
combine(Path("sert&sera"), Path("combined_results.csv"), {"Dataset", "Model", "Method", "Raw SERA", "Adj SERA"})
Dataset Summarization
from riskreg.summarize_datasets import summarize
# Configure paths inside summarize() for your phi CSV directory — see riskreg/summarize_datasets.py
Visualization Examples
The framework generates comprehensive visualizations:
- Relevance Curves: Show φ function across target range
- SERT Plots: Cumulative error at different relevance thresholds
- Bias Analysis: Prediction bias across target regions
- Model Comparison: Performance across different normalizations
- Distribution Plots: Before/after oversampling
Contributing
We welcome contributions! Please see our Contributing Guidelines for details.
Development Setup
git clone https://github.com/Bhavneet345/RiskReg.git
cd RiskReg
pip install -e .
pip install -e .[dl] # For deep learning features
pip install -e .[agents] # For multi-agent pipeline (LangChain + CrewAI)
License
This project is licensed under the MIT License - see the LICENSE file for details.
License Summary
- Commercial use allowed
- Modification allowed
- Distribution allowed
- Private use allowed
- Liability not provided
- Warranty not provided
Full License Text: LICENSE
Acknowledgments
- Regression oversampling: Based on Branco et al. (2017)
- Relevance Functions: Inspired by Ribeiro (2011)
- Evaluation Metrics: SERT/SERA methodology
- Datasets: Various sources including UCI ML Repository
Contact
- Author: Bhavneet Singh
- Email: bsing048@uottawa.ca
- GitHub: @Bhavneet345
- PyPI: riskreg
Links
- GitHub Repository: https://github.com/Bhavneet345/RiskReg
- PyPI Package: https://pypi.org/project/riskreg/
- Documentation: pypi/
- Issues: https://github.com/Bhavneet345/RiskReg/issues
- Releases: https://github.com/Bhavneet345/RiskReg/releases
RiskReg - Making regression fair for rare but important predictions.
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 riskreg-0.2.0.tar.gz.
File metadata
- Download URL: riskreg-0.2.0.tar.gz
- Upload date:
- Size: 612.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
db5759956d1d26cb095287cfb1cb9adae8bb8b7c5ba949f8d81db709599b257a
|
|
| MD5 |
c43176d92558fcc12edcec2cb931d740
|
|
| BLAKE2b-256 |
df8bb44027326fc34d9365287b1c322cb4fd2d77231bb5e780a928cae9bbf3fe
|
Provenance
The following attestation bundles were made for riskreg-0.2.0.tar.gz:
Publisher:
publish-pypi.yml on Bhavneet345/RiskReg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
riskreg-0.2.0.tar.gz -
Subject digest:
db5759956d1d26cb095287cfb1cb9adae8bb8b7c5ba949f8d81db709599b257a - Sigstore transparency entry: 1136837967
- Sigstore integration time:
-
Permalink:
Bhavneet345/RiskReg@d81781c22c103b1b0a57463a4f6069f6c87a3a08 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Bhavneet345
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@d81781c22c103b1b0a57463a4f6069f6c87a3a08 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file riskreg-0.2.0-py3-none-any.whl.
File metadata
- Download URL: riskreg-0.2.0-py3-none-any.whl
- Upload date:
- Size: 730.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
01c58927966d73b93279d36084125b82a5265389d9d76084116f410927aa800f
|
|
| MD5 |
23c2ac7f4862458de5afcb9acd3fef34
|
|
| BLAKE2b-256 |
a7f9a3d4f9d180a8a0888896520b788e4aead30735668b07b91df9a55c049433
|
Provenance
The following attestation bundles were made for riskreg-0.2.0-py3-none-any.whl:
Publisher:
publish-pypi.yml on Bhavneet345/RiskReg
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
riskreg-0.2.0-py3-none-any.whl -
Subject digest:
01c58927966d73b93279d36084125b82a5265389d9d76084116f410927aa800f - Sigstore transparency entry: 1136838017
- Sigstore integration time:
-
Permalink:
Bhavneet345/RiskReg@d81781c22c103b1b0a57463a4f6069f6c87a3a08 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/Bhavneet345
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-pypi.yml@d81781c22c103b1b0a57463a4f6069f6c87a3a08 -
Trigger Event:
workflow_dispatch
-
Statement type: