Metadata-Version: 2.5
Name: cwgboost
Version: 0.1.0
Summary: Component-wise gradient boosting for additive models with P-spline and tree-stump base learners
Project-URL: Homepage, https://github.com/dballinari/ComponentWiseGradientBoosting
Project-URL: Source, https://github.com/dballinari/ComponentWiseGradientBoosting
Project-URL: Issues, https://github.com/dballinari/ComponentWiseGradientBoosting/issues
Project-URL: Changelog, https://github.com/dballinari/ComponentWiseGradientBoosting/blob/main/CHANGELOG.md
Author: Daniele Ballinari
License-Expression: BSD-3-Clause
License-File: LICENSE
Keywords: GAM,P-splines,additive models,boosting,gradient boosting,machine learning
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Science/Research
Classifier: Operating System :: OS Independent
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Scientific/Engineering :: Mathematics
Requires-Python: >=3.10
Requires-Dist: numpy>=1.22
Requires-Dist: scipy>=1.8
Provides-Extra: examples
Requires-Dist: matplotlib; extra == 'examples'
Requires-Dist: scikit-learn>=1.0; extra == 'examples'
Provides-Extra: test
Requires-Dist: pytest>=7; extra == 'test'
Requires-Dist: scikit-learn>=1.0; extra == 'test'
Description-Content-Type: text/markdown

# cwgboost

[![tests](https://github.com/dballinari/ComponentWiseGradientBoosting/actions/workflows/tests.yml/badge.svg)](https://github.com/dballinari/ComponentWiseGradientBoosting/actions/workflows/tests.yml)

Component-wise gradient boosting for additive regression models, with P-splines and
regression stumps as base learners.

The model it fits is an additive model

    f(x) = intercept + f_1(x_1) + f_2(x_2) + ... + f_p(x_p),

built up step by step. In every boosting step a base learner is fitted to the current
residuals on *each feature separately*, and only the one that reduces the loss most is
added (scaled by the learning rate). This gives:

- **Interpretability:** each feature's estimated effect `f_j` can be plotted directly.
- **Built-in feature selection:** features that never improve the fit are never used.
- **Regularisation by early stopping:** the number of steps controls model complexity.

P-spline base learners use a cubic B-spline basis with a second-order difference penalty.
The penalty of each feature is chosen so that every base learner has the same degrees of
freedom (`df`, default 4), so smooth and wiggly features compete on equal terms. The
approach follows Bühlmann & Hothorn (2007) and Schmid & Hothorn (2008).

## Installation

```bash
pip install cwgboost
```

Requires Python 3.10+, NumPy and SciPy.

## Quick start

```python
import numpy as np
from cwgboost import CWGBoost

rng = np.random.default_rng(0)
X = rng.uniform(-3, 3, (1000, 5))
y = np.sin(X[:, 0]) + X[:, 1] ** 2 / 4 + rng.normal(0, 0.3, 1000)
X_train, X_val, y_train, y_val = X[:800], X[800:], y[:800], y[800:]

model = CWGBoost(n_estimators=2000, learning_rate=0.1, early_stopping_rounds=50)
model.fit(X_train, y_train, eval_set=(X_val, y_val))

model.predict(X_val)                       # predictions
model.score(X_val, y_val)                  # R^2
model.feature_importance(X_val, y_val)     # relative MSE increase without each feature
effects = model.predict_components(X_val)  # (n_samples, n_features) effect of each feature
```

`CWGBoost` follows the scikit-learn estimator interface, so it works with `clone`,
pipelines, `cross_val_score` and `GridSearchCV` (scikit-learn itself is not required).

## Parameters

| Parameter | Default | Description |
|---|---|---|
| `n_estimators` | `100` | Maximum number of boosting steps. |
| `learning_rate` | `0.1` | Shrinkage applied to each base learner. |
| `base_learner` | `"pspline"` | `"pspline"` (smooth effects), `"stump"` (step functions) or `"both"` (compete in every step). |
| `df` | `4` | Degrees of freedom of each P-spline base learner. |
| `n_knots` | `20` | Number of equally spaced knots of the B-spline basis. |
| `degree` | `3` | Degree of the B-splines. |
| `penalty_order` | `2` | Order of the difference penalty. |
| `early_stopping_rounds` | `None` | Stop when the `eval_set` loss has not improved for this many steps and keep the best ensemble. |
| `verbose` | `0` | Print progress every `verbose` steps. |

After fitting, `estimators_` lists the selected base learners, `validation_loss_` holds the
validation loss after each step, and `intercept_` is the starting value (the mean of `y`).

Outside the range of the training data, P-spline effects are held constant at their
boundary value rather than extrapolated.

Only the squared-error loss is implemented.

## Examples

The [`examples/`](examples) folder contains:

- `additive_regression.py`: fit an additive model and plot the estimated effects.
- `nonlinear_timeseries.py`: forecast a regime-switching AR(1) process from its lags.
- `pspline_smoothing.py`: smooth a noisy curve with a single P-spline.

## Development

```bash
pip install -e ".[test]"
pytest
python benchmarks/benchmark.py
```

## References

- Bühlmann, P. and Hothorn, T. (2007). Boosting algorithms: regularization, prediction and
  model fitting. *Statistical Science*, 22(4), 477–505.
- Schmid, M. and Hothorn, T. (2008). Boosting additive models using component-wise
  P-splines. *Computational Statistics & Data Analysis*, 53(2), 298–311.
- Eilers, P. H. C. and Marx, B. D. (1996). Flexible smoothing with B-splines and penalties.
  *Statistical Science*, 11(2), 89–121.

## License

BSD 3-Clause, see [LICENSE](LICENSE).
