Skip to main content

Monotonic composite quantile gradient boost regressor

Project description

release

MQBoost introduces an advanced model for estimating multiple quantiles while ensuring the non-crossing condition (monotone quantile condition). This model harnesses the capabilities of both LightGBM and XGBoost, two leading gradient boosting frameworks.

By implementing the hyperparameter optimization prowess of Optuna, the model achieves great performance. Optuna's optimization algorithms fine-tune the hyperparameters, ensuring the model operates efficiently.

Installation

Install using pip:

pip install mqboost

Usage

Features

  • MQRegressor: A model for quantile regression

Parameters

#--------------------------------------------------------------------------------------------#
>> MQBoost.__init__
x                   # Explanatory data (e.g., pd.DataFrame).
                    # Column named '_tau' must not be included.
y                   # Response data (e.g., np.ndarray).
alphas              # Target quantiles.
                    # Must be in ascending order and contain no duplicates.
objective           # [Optional] Objective to minimize, "check" (default) or "huber".
model               # [Optional] Boosting algorithm to use, "lightgbm" (default) or "xgboost".
delta               # [Optional] Parameter for "huber" objective.
                    # Used only when objective == "huber".
                    # Must be smaller than 0.1.
#--------------------------------------------------------------------------------------------#
>> MQBoost.train
params              # [Optional] Model parameters; defaults to None.
                    # Any params related to model can be used except "objective".
                    # If None, hyperparameter optimization is executed.
n_trials            # [Optional] Number of hyperparameter optimization trials.
                    # Defaults to 20.
#--------------------------------------------------------------------------------------------#
>> MQBoost.predict
x                   # Explanatory data (e.g., pd.DataFrame).
alphas              # [Optional] Target quantiles for prediction.
                    # Defaults to alphas used in train.
#--------------------------------------------------------------------------------------------#
>> MQBoost.optimize_params
n_trials            # Number of hyperparameter optimization trials
get_params_func     # [Optional] Manual hyperparameter function
valid_dict          # [Optional] Manually selected validation sets
                    # Keys must contain "data" and "label"
#--------------------------------------------------------------------------------------------#

Example

import numpy as np
from optuna import Trial
from mqboost import MQRegressor

# Generate sample data
sample_size = 500
x = np.linspace(-10, 10, sample_size)
y = np.sin(x) + np.random.uniform(-0.4, 0.4, sample_size)
x_test = np.linspace(-10, 10, sample_size)
y_test = np.sin(x_test) + np.random.uniform(-0.4, 0.4, sample_size)

# Define target quantiles
alphas = [0.3, 0.4, 0.5, 0.6, 0.7]

# Specify model type
model = "lightgbm"  # Options: "lightgbm" or "xgboost"

# Set objective function
objective = "huber"  # Options: "huber" or "check"
delta = 0.01  # Set when objective is "huber", default is 0.05

# Initialize the LightGBM-based quantile regressor
mq_lgb = MQRegressor(
    x=x,
    y=y,
    alphas=alphas,
    objective=objective,
    model=model,
    delta=delta,
)

# Train the model with fixed parameters
lgb_params = {
    "max_depth": 4,
    "num_leaves": 15,
    "learning_rate": 0.1,
    "boosting_type": "gbdt",
}
mq_lgb.train(params=lgb_params)

# Train the model with Optuna hyperparameter optimization
mq_lgb.train(n_trials=10)

# Alternatively, optimize parameters first and then train
best_params = mq_lgb.optimize_params(n_trials=10)
mq_lgb.train(params=best_params)

# Moreover, you can optimize parameters by implementing functions manually
# Also, you can manually set the validation set
def get_params(trial: Trial, model: str):
    return {
        "verbose": -1,
        "learning_rate": trial.suggest_float("learning_rate", 1e-2, 1.0, log=True),
        "max_depth": trial.suggest_int("max_depth", 1, 10),
        "lambda_l1": trial.suggest_float("lambda_l1", 1e-8, 10.0, log=True),
        "lambda_l2": trial.suggest_float("lambda_l2", 1e-8, 10.0, log=True),
        "num_leaves": trial.suggest_int("num_leaves", 2, 256),
        "feature_fraction": trial.suggest_float("feature_fraction", 0.4, 1.0),
        "bagging_fraction": trial.suggest_float("bagging_fraction", 0.4, 1.0),
        "bagging_freq": trial.suggest_int("bagging_freq", 1, 7),
    }

valid_dict = {
    "data": x_test,
    "label": y_test,
}

best_params = mq_lgb.optimize_params(
    n_trials=10, get_params_func=get_params, valid_dict=valid_dict
)
mq_lgb.train(params=best_params)

# Predict using the trained model
preds_lgb = mq_lgb.predict(x=x_test, alphas=alphas)

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

mqboost-0.1.5.tar.gz (9.3 kB view details)

Uploaded Source

Built Distribution

mqboost-0.1.5-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

Details for the file mqboost-0.1.5.tar.gz.

File metadata

  • Download URL: mqboost-0.1.5.tar.gz
  • Upload date:
  • Size: 9.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1023-azure

File hashes

Hashes for mqboost-0.1.5.tar.gz
Algorithm Hash digest
SHA256 3c0865ea772215ccf2ea56ea549231c2dd6ef1562a1abf482b269dbb18233c10
MD5 c5dd7c8c6e9332a0d5d3217aca5d4486
BLAKE2b-256 dba8f7ffa5221fbcb11e0e06d30721c1c242148766076f32014d3e506c81f222

See more details on using hashes here.

File details

Details for the file mqboost-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: mqboost-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.3 CPython/3.12.1 Linux/6.5.0-1023-azure

File hashes

Hashes for mqboost-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 cee1068db123d7b3f02fa11634e9e090f94f3df316566d11d1fb1d03a4ac9d72
MD5 9d9a69da7321f0eacd93279b83b3e567
BLAKE2b-256 7a6c83b8e900a31732d2aff19603e8cb955b92289f81e8a299d7dbd3aa3d4538

See more details on using hashes here.

Supported by

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