Bias-Variance Aware Bayesian Optimization for Hyperparameter Tuning
Project description
HBOptimize: Bias-Variance Aware Bayesian Optimization
A lightweight Bayesian Optimization package designed for hyperparameter tuning with bias-variance decomposition via repeated cross-validation.
Features
- ๐ฏ Bias-Variance Aware: Uses repeated K-fold CV to estimate both mean and variance of model performance
- ๐ฌ Heteroskedastic GP Surrogate: Gaussian Process that models observation noise
- ๐ Noisy Expected Improvement: Acquisition function designed for noisy objectives
- โก Multi-Fidelity Ready: Architecture supports variable-cost evaluations
- ๐ ๏ธ Sklearn Integration: Built-in adapters for Ridge, Lasso, Random Forest, GBM, SVR
Installation
# From source
git clone https://github.com/DashDecker/HBOptimize.git
cd HBOptimize
pip install -e .
# With visualization support
pip install -e ".[viz]"
# For development
pip install -e ".[dev]"
Quick Start
import numpy as np
from hboptimize import HBOptimize, SearchSpace, Real, Integer, Categorical, CVRisk
from hboptimize.runners.sklearn_adapter import set_data
# Prepare your dataset
X = np.random.randn(200, 10)
y = X[:, 0] + 2*X[:, 1] + np.random.randn(200) * 0.1
set_data(X, y)
# Define search space
space = SearchSpace({
'model': Categorical(('ridge', 'lasso')),
'alpha': Real(1e-4, 1e2, log10=True),
'max_iter': Integer(100, 2000)
})
# Create CV risk estimator
risk = CVRisk(k=5, repeats=3, metric='mse')
# Run Bayesian Optimization
from hboptimize.api import Config
config = Config(budget_evals=50, batch_size=1, seed=42)
bo = HBOptimize(space, risk, config=config)
best_params, best_score = bo.run()
print(f"Best parameters: {best_params}")
print(f"Best CV MSE: {best_score:.6f}")
Usage
1. Define Search Space
from hboptimize import SearchSpace, Real, Integer, Categorical
space = SearchSpace({
# Continuous parameters (with optional log transform)
'learning_rate': Real(1e-4, 1e-1, log10=True),
# Integer parameters
'n_estimators': Integer(10, 500),
# Categorical parameters
'model': Categorical(('ridge', 'lasso', 'rf')),
'kernel': Categorical(('linear', 'rbf', 'poly'))
})
2. Set Up Risk Estimator
from hboptimize.risk.cv import CVRisk
# Repeated K-fold cross-validation
risk = CVRisk(
k=5, # 5-fold CV
repeats=3, # Repeat 3 times for variance estimation
metric='mse', # or 'mae'
fixed_splits=True # Use same splits across all configs
)
3. Run Optimization
from hboptimize.api import HBOptimize, Config
config = Config(
budget_evals=100, # Total number of evaluations
batch_size=1, # Sequential (batch_size > 1 for parallel)
seed=42 # For reproducibility
)
bo = HBOptimize(space, risk, config=config)
best_params, best_score = bo.run()
4. Interactive Optimization
For more control, use the suggest-observe pattern:
bo = HBOptimize(space, risk)
for i in range(100):
# Get next configuration to try
configs = bo.suggest(n=1)
for cfg in configs:
# Evaluate configuration
mean, std, meta = risk.evaluate(cfg)
# Provide feedback
bo.observe(cfg, mean, std, cost=meta.get('time'))
# Check current best
best_params, best_score = bo.best()
print(f"Iteration {i+1}: Best score = {best_score:.6f}")
Supported Models
The built-in sklearn adapter supports:
ridge: Ridge Regressionlasso: Lasso Regressionrf: Random Forest Regressorgbm: Gradient Boosting Regressorsvr: Support Vector Regressor
API Reference
Core Classes
SearchSpace: Defines the hyperparameter search spaceReal: Continuous parameter (with optional log10 transform)Integer: Integer parameterCategorical: Categorical parameter with finite choicesCVRisk: Repeated K-fold cross-validation risk estimatorHBOptimize: Main optimization coordinator
Key Methods
HBOptimize.run(): Run full optimization loopHBOptimize.suggest(n): Get next n configurations to evaluateHBOptimize.observe(x, mean, std, cost): Provide evaluation resultsHBOptimize.best(): Get current best configuration and score
Architecture
HBOptimize/
โโโ src/hboptimize/
โโโ core/
โ โ โโโ search_space.py # Parameter space transforms
โ โ โโโ surrogate.py # Heteroscedastic GP
โ โ โโโ acquisition.py # Noisy EI
โ โ โโโ scheduler.py # Batch proposal
โ โโโ risk/
โ โ โโโ base.py # RiskEstimator interface
โ โ โโโ cv.py # CV implementation
โ โโโ runners/
โ โ โโโ sklearn_adapter.py # Model builders
โ โโโ utils/
โ โโโ seeding.py # Reproducibility
โ โโโ storage.py # Result tracking
Roadmap
- v0.1.0: Core Bayesian Optimization loop with CV risk
- v0.2.0: Visualization tools and progress tracking
- v0.3.0: Multi-fidelity support (early stopping, subsampling)
- v0.4.0: Parallel batch evaluation
- v0.5.0: Custom surrogate models (GPyTorch integration)
Contributing
Contributions are welcome! Please open an issue or PR.
License
MIT License - see LICENSE file for details.
Citation
If you use this package in your research, please cite:
@software{hboptimize2025,
title={HBOptimize: Bias-Variance Aware Bayesian Optimization},
author={Dashi},
year={2025},
url={https://github.com/DashDecker/HBOptimize}
}
Acknowledgments
Built with:
- scikit-learn for GP and CV
- scipy for optimization
- pydantic for configuration validation
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 hboptimize-0.1.0.tar.gz.
File metadata
- Download URL: hboptimize-0.1.0.tar.gz
- Upload date:
- Size: 28.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c863ec64a346cf5b419eaa91f8d8f3f276cb81de5bc76a9aa34520076529bec7
|
|
| MD5 |
bf828a407f2b86c75b47e4bd9b883e68
|
|
| BLAKE2b-256 |
eafc93d7b9d38159def13c9682905949ce3791aff24e667a1f034adb3d3f23a9
|
File details
Details for the file hboptimize-0.1.0-py3-none-any.whl.
File metadata
- Download URL: hboptimize-0.1.0-py3-none-any.whl
- Upload date:
- Size: 21.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5545de884d23e4f908f4406d04259f5fe991772c50559a812d1b1764baace112
|
|
| MD5 |
2c3698c2867c551da73f1dc6f90c4888
|
|
| BLAKE2b-256 |
a223e24e3838f2eff6c40290f608c02e2091a195531abfc2404d9b5f4e97e72e
|