Skip to main content

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 Regression
  • lasso: Lasso Regression
  • rf: Random Forest Regressor
  • gbm: Gradient Boosting Regressor
  • svr: Support Vector Regressor

API Reference

Core Classes

  • SearchSpace: Defines the hyperparameter search space
  • Real: Continuous parameter (with optional log10 transform)
  • Integer: Integer parameter
  • Categorical: Categorical parameter with finite choices
  • CVRisk: Repeated K-fold cross-validation risk estimator
  • HBOptimize: Main optimization coordinator

Key Methods

  • HBOptimize.run(): Run full optimization loop
  • HBOptimize.suggest(n): Get next n configurations to evaluate
  • HBOptimize.observe(x, mean, std, cost): Provide evaluation results
  • HBOptimize.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:

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

hboptimize-0.1.0.tar.gz (28.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

hboptimize-0.1.0-py3-none-any.whl (21.3 kB view details)

Uploaded Python 3

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

Hashes for hboptimize-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c863ec64a346cf5b419eaa91f8d8f3f276cb81de5bc76a9aa34520076529bec7
MD5 bf828a407f2b86c75b47e4bd9b883e68
BLAKE2b-256 eafc93d7b9d38159def13c9682905949ce3791aff24e667a1f034adb3d3f23a9

See more details on using hashes here.

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

Hashes for hboptimize-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5545de884d23e4f908f4406d04259f5fe991772c50559a812d1b1764baace112
MD5 2c3698c2867c551da73f1dc6f90c4888
BLAKE2b-256 a223e24e3838f2eff6c40290f608c02e2091a195531abfc2404d9b5f4e97e72e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page