|
| 1 | +/*! |
| 2 | + * Copyright (c) 2024 stochtree authors. All rights reserved. |
| 3 | + * Licensed under the MIT License. See LICENSE file in the project root for license information. |
| 4 | + */ |
| 5 | +#ifndef STOCHTREE_TREE_KERNEL_H_ |
| 6 | +#define STOCHTREE_TREE_KERNEL_H_ |
| 7 | + |
| 8 | +#include <stochtree/data.h> |
| 9 | +#include <stochtree/ensemble.h> |
| 10 | +#include <Eigen/Dense> |
| 11 | +#include <Eigen/Sparse> |
| 12 | + |
| 13 | +#include <cmath> |
| 14 | +#include <map> |
| 15 | +#include <memory> |
| 16 | +#include <random> |
| 17 | +#include <set> |
| 18 | +#include <string> |
| 19 | +#include <type_traits> |
| 20 | +#include <vector> |
| 21 | + |
| 22 | +namespace StochTree { |
| 23 | + |
| 24 | +typedef Eigen::Map<Eigen::Matrix<double, Eigen::Dynamic, Eigen::Dynamic, Eigen::ColMajor>> KernelMatrixType; |
| 25 | + |
| 26 | +class ForestKernel { |
| 27 | + public: |
| 28 | + ForestKernel() {} |
| 29 | + ~ForestKernel() {} |
| 30 | + |
| 31 | + void ComputeLeafIndices(Eigen::MatrixXd& covariates, TreeEnsemble& forest) { |
| 32 | + num_train_observations_ = covariates.rows(); |
| 33 | + num_trees_ = forest.NumTrees(); |
| 34 | + train_leaf_index_vector_.resize(num_train_observations_*num_trees_); |
| 35 | + forest.PredictLeafIndicesInplace(covariates, train_leaf_index_vector_, num_trees_, num_train_observations_); |
| 36 | + int max_cols = *std::max_element(train_leaf_index_vector_.begin(), train_leaf_index_vector_.end()); |
| 37 | + train_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_train_observations_,max_cols+1); |
| 38 | + int col_num; |
| 39 | + for (data_size_t i = 0; i < num_train_observations_; i++) { |
| 40 | + for (int j = 0; j < num_trees_; j++) { |
| 41 | + col_num = train_leaf_index_vector_.at(j*num_train_observations_ + i); |
| 42 | + train_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 43 | + } |
| 44 | + } |
| 45 | + train_leaf_indices_stored_ = true; |
| 46 | + } |
| 47 | + |
| 48 | + void ComputeLeafIndices(KernelMatrixType& covariates, TreeEnsemble& forest) { |
| 49 | + num_train_observations_ = covariates.rows(); |
| 50 | + num_trees_ = forest.NumTrees(); |
| 51 | + train_leaf_index_vector_.resize(num_train_observations_*num_trees_); |
| 52 | + forest.PredictLeafIndicesInplace(covariates, train_leaf_index_vector_, num_trees_, num_train_observations_); |
| 53 | + int max_cols = *std::max_element(train_leaf_index_vector_.begin(), train_leaf_index_vector_.end()); |
| 54 | + train_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_train_observations_,max_cols+1); |
| 55 | + int col_num; |
| 56 | + for (data_size_t i = 0; i < num_train_observations_; i++) { |
| 57 | + for (int j = 0; j < num_trees_; j++) { |
| 58 | + col_num = train_leaf_index_vector_.at(j*num_train_observations_ + i); |
| 59 | + train_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 60 | + } |
| 61 | + } |
| 62 | + train_leaf_indices_stored_ = true; |
| 63 | + } |
| 64 | + |
| 65 | + void ComputeLeafIndices(Eigen::MatrixXd& covariates_train, Eigen::MatrixXd& covariates_test, TreeEnsemble& forest) { |
| 66 | + CHECK_EQ(covariates_train.cols(), covariates_test.cols()); |
| 67 | + num_train_observations_ = covariates_train.rows(); |
| 68 | + num_test_observations_ = covariates_test.rows(); |
| 69 | + num_trees_ = forest.NumTrees(); |
| 70 | + train_leaf_index_vector_.resize(num_train_observations_*num_trees_); |
| 71 | + test_leaf_index_vector_.resize(num_test_observations_*num_trees_); |
| 72 | + forest.PredictLeafIndicesInplace(covariates_train, train_leaf_index_vector_, num_trees_, num_train_observations_); |
| 73 | + forest.PredictLeafIndicesInplace(covariates_test, test_leaf_index_vector_, num_trees_, num_test_observations_); |
| 74 | + int max_cols_train = *std::max_element(train_leaf_index_vector_.begin(), train_leaf_index_vector_.end()); |
| 75 | + int max_cols_test = *std::max_element(test_leaf_index_vector_.begin(), test_leaf_index_vector_.end()); |
| 76 | + int max_cols = max_cols_train > max_cols_test ? max_cols_train : max_cols_test; |
| 77 | + train_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_train_observations_,max_cols+1); |
| 78 | + test_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_test_observations_,max_cols+1); |
| 79 | + int col_num; |
| 80 | + for (data_size_t i = 0; i < num_train_observations_; i++) { |
| 81 | + for (int j = 0; j < num_trees_; j++) { |
| 82 | + col_num = train_leaf_index_vector_.at(j*num_train_observations_ + i); |
| 83 | + train_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 84 | + } |
| 85 | + } |
| 86 | + train_leaf_indices_stored_ = true; |
| 87 | + for (data_size_t i = 0; i < num_test_observations_; i++) { |
| 88 | + for (int j = 0; j < num_trees_; j++) { |
| 89 | + col_num = test_leaf_index_vector_.at(j*num_test_observations_ + i); |
| 90 | + test_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 91 | + } |
| 92 | + } |
| 93 | + test_leaf_indices_stored_ = true; |
| 94 | + } |
| 95 | + |
| 96 | + void ComputeLeafIndices(KernelMatrixType& covariates_train, KernelMatrixType& covariates_test, TreeEnsemble& forest) { |
| 97 | + CHECK_EQ(covariates_train.cols(), covariates_test.cols()); |
| 98 | + num_train_observations_ = covariates_train.rows(); |
| 99 | + num_test_observations_ = covariates_test.rows(); |
| 100 | + num_trees_ = forest.NumTrees(); |
| 101 | + train_leaf_index_vector_.resize(num_train_observations_*num_trees_); |
| 102 | + test_leaf_index_vector_.resize(num_test_observations_*num_trees_); |
| 103 | + forest.PredictLeafIndicesInplace(covariates_train, train_leaf_index_vector_, num_trees_, num_train_observations_); |
| 104 | + forest.PredictLeafIndicesInplace(covariates_test, test_leaf_index_vector_, num_trees_, num_test_observations_); |
| 105 | + int max_cols_train = *std::max_element(train_leaf_index_vector_.begin(), train_leaf_index_vector_.end()); |
| 106 | + int max_cols_test = *std::max_element(test_leaf_index_vector_.begin(), test_leaf_index_vector_.end()); |
| 107 | + int max_cols = max_cols_train > max_cols_test ? max_cols_train : max_cols_test; |
| 108 | + train_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_train_observations_,max_cols+1); |
| 109 | + test_leaf_index_matrix_ = Eigen::SparseMatrix<double>(num_test_observations_,max_cols+1); |
| 110 | + int col_num; |
| 111 | + for (data_size_t i = 0; i < num_train_observations_; i++) { |
| 112 | + for (int j = 0; j < num_trees_; j++) { |
| 113 | + col_num = train_leaf_index_vector_.at(j*num_train_observations_ + i); |
| 114 | + train_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 115 | + } |
| 116 | + } |
| 117 | + train_leaf_indices_stored_ = true; |
| 118 | + for (data_size_t i = 0; i < num_test_observations_; i++) { |
| 119 | + for (int j = 0; j < num_trees_; j++) { |
| 120 | + col_num = test_leaf_index_vector_.at(j*num_test_observations_ + i); |
| 121 | + test_leaf_index_matrix_.insert(i,col_num) = 1.; |
| 122 | + } |
| 123 | + } |
| 124 | + test_leaf_indices_stored_ = true; |
| 125 | + } |
| 126 | + |
| 127 | + void ComputeKernel(Eigen::MatrixXd& covariates, TreeEnsemble& forest) { |
| 128 | + ComputeLeafIndices(covariates, forest); |
| 129 | + tree_kernel_train_ = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 130 | + train_kernel_stored_ = true; |
| 131 | + } |
| 132 | + |
| 133 | + void ComputeKernel(KernelMatrixType& covariates, TreeEnsemble& forest) { |
| 134 | + ComputeLeafIndices(covariates, forest); |
| 135 | + tree_kernel_train_ = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 136 | + train_kernel_stored_ = true; |
| 137 | + } |
| 138 | + |
| 139 | + void ComputeKernelExternal(Eigen::MatrixXd& covariates, TreeEnsemble& forest, KernelMatrixType& kernel_map) { |
| 140 | + ComputeLeafIndices(covariates, forest); |
| 141 | + kernel_map = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 142 | + } |
| 143 | + |
| 144 | + void ComputeKernelExternal(KernelMatrixType& covariates, TreeEnsemble& forest, KernelMatrixType& kernel_map) { |
| 145 | + ComputeLeafIndices(covariates, forest); |
| 146 | + kernel_map = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 147 | + } |
| 148 | + |
| 149 | + void ComputeKernel(Eigen::MatrixXd& covariates_train, Eigen::MatrixXd& covariates_test, TreeEnsemble& forest) { |
| 150 | + ComputeLeafIndices(covariates_train, covariates_test, forest); |
| 151 | + tree_kernel_train_ = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 152 | + train_kernel_stored_ = true; |
| 153 | + tree_kernel_test_train_ = test_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 154 | + tree_kernel_test_ = test_leaf_index_matrix_ * test_leaf_index_matrix_.transpose(); |
| 155 | + test_kernel_stored_ = true; |
| 156 | + } |
| 157 | + |
| 158 | + void ComputeKernel(KernelMatrixType& covariates_train, KernelMatrixType& covariates_test, TreeEnsemble& forest) { |
| 159 | + ComputeLeafIndices(covariates_train, covariates_test, forest); |
| 160 | + tree_kernel_train_ = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 161 | + train_kernel_stored_ = true; |
| 162 | + tree_kernel_test_train_ = test_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 163 | + tree_kernel_test_ = test_leaf_index_matrix_ * test_leaf_index_matrix_.transpose(); |
| 164 | + test_kernel_stored_ = true; |
| 165 | + } |
| 166 | + |
| 167 | + void ComputeKernelExternal(Eigen::MatrixXd& covariates_train, Eigen::MatrixXd& covariates_test, TreeEnsemble& forest, |
| 168 | + KernelMatrixType& kernel_map_train, KernelMatrixType& kernel_map_test_train, KernelMatrixType& kernel_map_test) { |
| 169 | + ComputeLeafIndices(covariates_train, covariates_test, forest); |
| 170 | + kernel_map_train = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 171 | + kernel_map_test_train = test_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 172 | + kernel_map_test = test_leaf_index_matrix_ * test_leaf_index_matrix_.transpose(); |
| 173 | + } |
| 174 | + |
| 175 | + void ComputeKernelExternal(KernelMatrixType& covariates_train, KernelMatrixType& covariates_test, TreeEnsemble& forest, |
| 176 | + KernelMatrixType& kernel_map_train, KernelMatrixType& kernel_map_test_train, KernelMatrixType& kernel_map_test) { |
| 177 | + ComputeLeafIndices(covariates_train, covariates_test, forest); |
| 178 | + kernel_map_train = train_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 179 | + kernel_map_test_train = test_leaf_index_matrix_ * train_leaf_index_matrix_.transpose(); |
| 180 | + kernel_map_test = test_leaf_index_matrix_ * test_leaf_index_matrix_.transpose(); |
| 181 | + } |
| 182 | + |
| 183 | + std::vector<int32_t>& GetTrainLeafIndices() { |
| 184 | + CHECK(train_leaf_indices_stored_); |
| 185 | + return train_leaf_index_vector_; |
| 186 | + } |
| 187 | + |
| 188 | + std::vector<int32_t>& GetTestLeafIndices() { |
| 189 | + CHECK(test_leaf_indices_stored_); |
| 190 | + return test_leaf_index_vector_; |
| 191 | + } |
| 192 | + |
| 193 | + Eigen::MatrixXd& GetTrainKernel() { |
| 194 | + CHECK(train_kernel_stored_); |
| 195 | + return tree_kernel_train_; |
| 196 | + } |
| 197 | + |
| 198 | + Eigen::MatrixXd& GetTestTrainKernel() { |
| 199 | + CHECK(test_kernel_stored_); |
| 200 | + return tree_kernel_test_train_; |
| 201 | + } |
| 202 | + |
| 203 | + Eigen::MatrixXd& GetTestKernel() { |
| 204 | + CHECK(test_kernel_stored_); |
| 205 | + return tree_kernel_test_; |
| 206 | + } |
| 207 | + |
| 208 | + data_size_t NumTrainObservations() { |
| 209 | + return num_train_observations_; |
| 210 | + } |
| 211 | + |
| 212 | + data_size_t NumTestObservations() { |
| 213 | + return num_test_observations_; |
| 214 | + } |
| 215 | + |
| 216 | + int NumTrees() { |
| 217 | + return num_trees_; |
| 218 | + } |
| 219 | + |
| 220 | + bool HasTrainLeafIndices() { |
| 221 | + return train_leaf_indices_stored_; |
| 222 | + } |
| 223 | + |
| 224 | + bool HasTestLeafIndices() { |
| 225 | + return test_leaf_indices_stored_; |
| 226 | + } |
| 227 | + |
| 228 | + bool HasTrainKernel() { |
| 229 | + return train_kernel_stored_; |
| 230 | + } |
| 231 | + |
| 232 | + bool HasTestKernel() { |
| 233 | + return test_kernel_stored_; |
| 234 | + } |
| 235 | + |
| 236 | + private: |
| 237 | + data_size_t num_train_observations_{0}; |
| 238 | + data_size_t num_test_observations_{0}; |
| 239 | + int num_trees_{0}; |
| 240 | + std::vector<int32_t> train_leaf_index_vector_; |
| 241 | + std::vector<int32_t> test_leaf_index_vector_; |
| 242 | + Eigen::SparseMatrix<double> train_leaf_index_matrix_; |
| 243 | + Eigen::SparseMatrix<double> test_leaf_index_matrix_; |
| 244 | + Eigen::MatrixXd tree_kernel_train_; |
| 245 | + Eigen::MatrixXd tree_kernel_test_train_; |
| 246 | + Eigen::MatrixXd tree_kernel_test_; |
| 247 | + bool train_leaf_indices_stored_{false}; |
| 248 | + bool test_leaf_indices_stored_{false}; |
| 249 | + bool train_kernel_stored_{false}; |
| 250 | + bool test_kernel_stored_{false}; |
| 251 | +}; |
| 252 | + |
| 253 | +} // namespace StochTree |
| 254 | + |
| 255 | +#endif // STOCHTREE_TREE_KERNEL_H_ |
0 commit comments