Documentation
¶
Overview ¶
Package treeboost provides a general-purpose, high-performance Gradient Boosted Decision Trees (GBDT) and multi-algorithm ensemble regression library in pure Go with AVX2 SIMD acceleration.
Theoretical Background & Inspiration: The GBDT algorithms and ensemble architectures in this package are inspired by and follow the mathematical formulations detailed in:
- Gradient Boosting: https://en.wikipedia.org/wiki/Gradient_boosting
- Symmetric Decision Trees (CatBoost): https://en.wikipedia.org/wiki/CatBoost
- Leaf-wise Decision Trees (LightGBM): https://en.wikipedia.org/wiki/LightGBM
- Extremely Randomized Trees (ExtraTrees): https://en.wikipedia.org/wiki/Random_forest#ExtraTrees
Index ¶
- func BuildHistogram(bins []uint8, fOffset int, weights []float64, targets []float64, indices []int, ...)
- func BuildHistogramSlice(bins []uint8, fOffset int, weights []float64, targets []float64, ...)
- func ComputeSampleWeights(Y []float64) []float64
- func EvaluateTree(nodes []Node, x FeatureVector) float64
- func EvaluateTreeColumnar(nodes []Node, data ColumnarDataset, sampleIdx int) float64
- func FindBestHistogramSplit(hist *FeatureHistogram, boundaries []float64, totalW, totalWY float64) (float64, float64, bool)
- func FindBestHistogramSplitWithCounts(hist *FeatureHistogram, boundaries []float64, totalW, totalWY float64, ...) (splitVal float64, score float64, leftW float64, leftWY float64, rightW float64, ...)
- func ScaleTreeLeavesInPlace(tree *Tree, factor float64)
- type BinnedDataset
- type CatBoostParams
- type ColumnarDataset
- type Dataset
- type EnsembleConfig
- type ExtraTreesParams
- type FeatureHistogram
- type FeatureVector
- type LightGBMParams
- type Model
- type Node
- type Regressor
- type TrainWorkspace
- type Tree
- func ScaleTreeLeaves(tree Tree, factor float64) Tree
- func TrainExtraTrees(X []FeatureVector, Y []float64, W []float64, params ExtraTreesParams) []Tree
- func TrainExtraTreesColumnar(data ColumnarDataset, params ExtraTreesParams) []Tree
- func TrainLeafwiseGBDT(X []FeatureVector, Y []float64, W []float64, params LightGBMParams) ([]Tree, float64)
- func TrainLeafwiseGBDTColumnar(data ColumnarDataset, params LightGBMParams) ([]Tree, float64)
- func TrainSymmetricGBDT(X []FeatureVector, Y []float64, W []float64, params CatBoostParams) ([]Tree, float64)
- func TrainSymmetricGBDTColumnar(data ColumnarDataset, params CatBoostParams) ([]Tree, float64)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildHistogram ¶
func BuildHistogram(bins []uint8, fOffset int, weights []float64, targets []float64, indices []int, hist *FeatureHistogram)
BuildHistogram accumulates sample weights and targets into a FeatureHistogram.
func BuildHistogramSlice ¶
func BuildHistogramSlice(bins []uint8, fOffset int, weights []float64, targets []float64, sampleIdxs []int, start, end int, hist *FeatureHistogram)
BuildHistogramSlice accumulates sample weights and targets over sampleIdxs[start:end] without slice allocations.
func ComputeSampleWeights ¶
ComputeSampleWeights calculates weights for observations using a log-distance from the target mean, emphasizing and penalizing extreme spikes/troughs.
func EvaluateTree ¶
func EvaluateTree(nodes []Node, x FeatureVector) float64
EvaluateTree traverses a slice of decision tree nodes from root (node index 0) down to a leaf node for the given feature vector x, returning the leaf's prediction value.
func EvaluateTreeColumnar ¶
func EvaluateTreeColumnar(nodes []Node, data ColumnarDataset, sampleIdx int) float64
EvaluateTreeColumnar evaluates a tree on sampleIdx within ColumnarDataset without allocations.
func FindBestHistogramSplit ¶
func FindBestHistogramSplit(hist *FeatureHistogram, boundaries []float64, totalW, totalWY float64) (float64, float64, bool)
FindBestHistogramSplit scans accumulated feature histogram bins to find the split threshold minimizing variance (maximizing gain).
func FindBestHistogramSplitWithCounts ¶
func FindBestHistogramSplitWithCounts(hist *FeatureHistogram, boundaries []float64, totalW, totalWY float64, totalCount int, minChildSamples int) (splitVal float64, score float64, leftW float64, leftWY float64, rightW float64, rightWY float64, found bool)
FindBestHistogramSplitWithCounts evaluates the optimal split threshold and partition metrics directly from histogram bins.
func ScaleTreeLeavesInPlace ¶
ScaleTreeLeavesInPlace scales tree leaf values in place without allocating a new slice.
Types ¶
type BinnedDataset ¶
type BinnedDataset struct {
NumSamples int
NumFeatures int
MaxBins int
Bins []uint8 // Size (NumFeatures * NumSamples)
BinBoundaries [][]float64 // Bin upper bound thresholds per feature [NumFeatures][]float64
BinCounts []int // Actual number of bins created per feature [NumFeatures]int
}
BinnedDataset represents a tabular dataset whose continuous features are quantized into discrete 8-bit bin IDs (0..maxBins-1) for fast histogram-based GBDT tree building.
func QuantizeDataset ¶
func QuantizeDataset(data ColumnarDataset, maxBins int) BinnedDataset
QuantizeDataset builds a BinnedDataset from a ColumnarDataset using equal-frequency quantile binning.
type CatBoostParams ¶
type CatBoostParams struct {
Depth int `json:"depth"`
Iterations int `json:"iterations"`
LearningRate float64 `json:"learning_rate"`
L2LeafReg float64 `json:"l2_leaf_reg"`
}
CatBoostParams holds hyperparameters for Symmetric GBDT (obliviated trees). Reference: https://en.wikipedia.org/wiki/CatBoost
type ColumnarDataset ¶
type ColumnarDataset struct {
NumSamples int
NumFeatures int
Features []float64 // Size (NumFeatures * NumSamples)
Targets []float64 // Size NumSamples
Weights []float64 // Size NumSamples
Binned BinnedDataset
}
ColumnarDataset holds dataset features in a flattened Structure of Arrays (SoA) layout. Feature f for sample i is accessed at index f * NumSamples + i.
func ToColumnar ¶
func ToColumnar(X []FeatureVector, Y []float64, W []float64) ColumnarDataset
ToColumnar converts legacy AoS feature vectors ([]FeatureVector) into flattened SoA ColumnarDataset.
type Dataset ¶
type Dataset struct {
X []FeatureVector
Y []float64
W []float64
}
Dataset represents a tabular dataset of feature vectors, target regression values, and optional sample weights.
type EnsembleConfig ¶
type EnsembleConfig struct {
CatBoostWeight float64 `json:"catboost_weight"`
LightGBMWeight float64 `json:"lightgbm_weight"`
ExtraTreesWeight float64 `json:"extratrees_weight"`
CatBoost CatBoostParams `json:"catboost"`
LightGBM LightGBMParams `json:"lightgbm"`
ExtraTrees ExtraTreesParams `json:"extratrees"`
}
EnsembleConfig specifies individual algorithm parameters and weighted contribution ratios.
func DefaultEnsembleConfig ¶
func DefaultEnsembleConfig() EnsembleConfig
DefaultEnsembleConfig returns a balanced, standard default configuration combining Symmetric GBDT, Leaf-wise GBDT, and ExtraTrees into an equal-weight ensemble.
type ExtraTreesParams ¶
type ExtraTreesParams struct {
Estimators int `json:"estimators"`
MinSamplesLeaf int `json:"min_samples_leaf"`
MaxFeatures float64 `json:"max_features"`
}
ExtraTreesParams holds hyperparameters for Extremely Randomized Trees. Reference: https://en.wikipedia.org/wiki/Random_forest#ExtraTrees
type FeatureHistogram ¶
FeatureHistogram holds accumulated count, weight, and weighted target sums across 256 discrete bins.
func (*FeatureHistogram) Reset ¶
func (h *FeatureHistogram) Reset()
Reset zeroes out histogram accumulators.
type FeatureVector ¶
type FeatureVector struct {
Values []float64
}
FeatureVector represents an n-dimensional sample observation vector of floating-point features.
type LightGBMParams ¶
type LightGBMParams struct {
MaxDepth int `json:"max_depth"`
MaxLeaves int `json:"max_leaves"`
MinChildSamples int `json:"min_child_samples"`
Estimators int `json:"estimators"`
LearningRate float64 `json:"learning_rate"`
}
LightGBMParams holds hyperparameters for Leaf-wise GBDT (best-first tree growth). Reference: https://en.wikipedia.org/wiki/LightGBM
type Model ¶
type Model struct {
ID int `json:"-"`
TrainedAt time.Time `json:"trained_at"`
TrainingStart time.Time `json:"training_start"`
TrainingEnd time.Time `json:"training_end"`
BaseValue float64 `json:"base_value"`
Trees []Tree `json:"trees"`
LearningRate float64 `json:"learning_rate"`
MAE float64 `json:"mae"`
RMSE float64 `json:"rmse"`
RSquared float64 `json:"r_squared"`
HorizonBins []string `json:"horizon_bins,omitempty"`
P10Offsets []float64 `json:"p10_offsets,omitempty"`
P90Offsets []float64 `json:"p90_offsets,omitempty"`
DiagnosticsJSON string `json:"diagnostics_json,omitempty"`
}
Model represents a trained ensemble model comprising decision trees, a base initial prediction value, learning rate, and evaluation / diagnostic metadata.
func TrainEnsemble ¶
func TrainEnsemble(X []FeatureVector, Y []float64, config EnsembleConfig) *Model
TrainEnsemble trains a multi-algorithm tree ensemble combining CatBoost, LightGBM, and ExtraTrees models.
func (*Model) ComputeMetrics ¶
func (m *Model) ComputeMetrics(X []FeatureVector, Y []float64)
ComputeMetrics evaluates MAE, RMSE, and R^2 of the Model on feature vectors X and targets Y, updating the model's MAE, RMSE, and RSquared fields.
func (*Model) Predict ¶
Predict evaluates the GBDT model on a slice of feature values and returns the predicted scalar result. It initializes with the BaseValue and adds the weighted leaf prediction from each decision tree in the ensemble.
func (*Model) PredictBatch ¶
PredictBatch evaluates the GBDT model on a batch of nSamples feature vectors. features is a flattened slice of length (nSamples * nFeatures) in row-major layout (where sample i feature f is at index i*nFeatures + f). Returns a slice of predicted values of length nSamples.
type Node ¶
type Node struct {
NodeID int `json:"node_id"`
SplitFeature int `json:"split_feature"` // -1 if leaf node; feature index 0..N-1 otherwise
SplitValue float64 `json:"split_value"`
LeafValue float64 `json:"leaf_value"`
LeftChild int `json:"left_child"`
RightChild int `json:"right_child"`
}
Node represents a single decision node or leaf node within a decision tree.
type Regressor ¶
Regressor defines the common interface for models capable of generating continuous scalar predictions.
type TrainWorkspace ¶
type TrainWorkspace struct {
SampleIdxs []int
PermBuf []int
SplitBuf []float64
Ranges []nodeRange
Hist FeatureHistogram
}
TrainWorkspace holds reusable slice buffers to eliminate heap allocations during tree construction.
type Tree ¶
type Tree struct {
Nodes []Node `json:"nodes"`
}
Tree represents a single decision tree composed of ordered nodes.
func ScaleTreeLeaves ¶
ScaleTreeLeaves returns a deep copy of tree with all leaf node values multiplied by factor.
func TrainExtraTrees ¶
func TrainExtraTrees(X []FeatureVector, Y []float64, W []float64, params ExtraTreesParams) []Tree
TrainExtraTrees fits an Extremely Randomized Trees (ExtraTrees) regression ensemble.
func TrainExtraTreesColumnar ¶
func TrainExtraTreesColumnar(data ColumnarDataset, params ExtraTreesParams) []Tree
TrainExtraTreesColumnar fits an ExtraTrees regression ensemble on ColumnarDataset without per-iteration conversions.
func TrainLeafwiseGBDT ¶
func TrainLeafwiseGBDT(X []FeatureVector, Y []float64, W []float64, params LightGBMParams) ([]Tree, float64)
TrainLeafwiseGBDT fits a leaf-wise (LightGBM-style) gradient boosted decision tree ensemble.
func TrainLeafwiseGBDTColumnar ¶
func TrainLeafwiseGBDTColumnar(data ColumnarDataset, params LightGBMParams) ([]Tree, float64)
TrainLeafwiseGBDTColumnar fits a leaf-wise GBDT on ColumnarDataset without per-iteration conversions.
func TrainSymmetricGBDT ¶
func TrainSymmetricGBDT(X []FeatureVector, Y []float64, W []float64, params CatBoostParams) ([]Tree, float64)
TrainSymmetricGBDT fits a symmetric (CatBoost-style) gradient boosted decision tree ensemble.
func TrainSymmetricGBDTColumnar ¶
func TrainSymmetricGBDTColumnar(data ColumnarDataset, params CatBoostParams) ([]Tree, float64)
TrainSymmetricGBDTColumnar fits a CatBoost-style GBDT on ColumnarDataset without per-iteration conversions.