ppforest2 v0.1.0
Projection Pursuit Decision Trees and Random Forests
Loading...
Searching...
No Matches
Evaluation.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "stats/Metrics.hpp"
4#include "utils/Types.hpp"
5
6#include <memory>
7#include <optional>
8
23namespace ppforest2 {
24 class Model;
25 class Tree;
26 class Forest;
28 class RegressionForest;
29
49
50 // ---------------------------------------------------------------------------
51 // Variable importance
52 // ---------------------------------------------------------------------------
53
59 types::FeatureVector vi_projections(Tree const& tree, int n_vars, types::FeatureVector const* scale = nullptr);
60
62 types::FeatureVector vi_projections(Forest const& forest, int n_vars, types::FeatureVector const* scale = nullptr);
63
71 vi_permuted(Forest const& forest, types::FeatureMatrix const& x, types::OutcomeVector const& y, int seed);
72
80 Forest const& forest,
81 types::FeatureMatrix const& x,
82 types::OutcomeVector const& y,
83 types::FeatureVector const* scale = nullptr
84 );
85
88
91 variable_importance(Forest const& forest, types::FeatureMatrix const& x, types::OutcomeVector const& y, int seed);
92
93 // ---------------------------------------------------------------------------
94 // Out-of-bag diagnostics
95 // ---------------------------------------------------------------------------
96
110
123
126
134 std::optional<double> oob_error(Forest const& forest, types::FeatureMatrix const& x, types::OutcomeVector const& y);
135
137 std::optional<double> oob_error(Forest const& forest, types::FeatureMatrix const& x, types::GroupIdVector const& y);
138
139 // ---------------------------------------------------------------------------
140 // Prediction error
141 // ---------------------------------------------------------------------------
142
151 double error(Model const& model, types::FeatureMatrix const& x, types::OutcomeVector const& y);
152
153 // ---------------------------------------------------------------------------
154 // Pointer-friendly overloads
155 //
156 // Each forwards to the reference overload via `*ptr`, so callers holding
157 // a `Tree::Ptr` / `Forest::Ptr` (or any concrete subclass smart pointer
158 // like `ClassificationForest::Ptr`) skip the dereference at the call site.
159 //
160 // Templated on `std::unique_ptr<T>` rather than overloaded per concrete
161 // type so they cover unique_ptr<Tree>, unique_ptr<Forest>,
162 // unique_ptr<ClassificationForest>, etc., uniformly. The `*` dereference
163 // gives a `T const&` that resolves the right reference overload above.
164 // ---------------------------------------------------------------------------
165
166 template<typename T>
168 vi_projections(std::unique_ptr<T> const& m, int n_vars, types::FeatureVector const* scale = nullptr) {
169 return vi_projections(*m, n_vars, scale);
170 }
171
172 template<typename T>
174 vi_permuted(std::unique_ptr<T> const& m, types::FeatureMatrix const& x, types::OutcomeVector const& y, int seed) {
175 return vi_permuted(*m, x, y, seed);
176 }
177
178 template<typename T>
180 std::unique_ptr<T> const& m,
181 types::FeatureMatrix const& x,
182 types::OutcomeVector const& y,
183 types::FeatureVector const* scale = nullptr
184 ) {
185 return vi_weighted_projections(*m, x, y, scale);
186 }
187
188 template<typename T>
189 VariableImportance variable_importance(std::unique_ptr<T> const& m, types::FeatureMatrix const& x) {
190 return variable_importance(*m, x);
191 }
192
193 template<typename T>
195 std::unique_ptr<T> const& m, types::FeatureMatrix const& x, types::OutcomeVector const& y, int seed
196 ) {
197 return variable_importance(*m, x, y, seed);
198 }
199
200 template<typename T> types::OutcomeVector oob_predict(std::unique_ptr<T> const& m, types::FeatureMatrix const& x) {
201 return oob_predict(*m, x);
202 }
203
204 template<typename T>
205 std::optional<double>
206 oob_error(std::unique_ptr<T> const& m, types::FeatureMatrix const& x, types::OutcomeVector const& y) {
207 return oob_error(*m, x, y);
208 }
209
210 template<typename T>
211 std::optional<double>
212 oob_error(std::unique_ptr<T> const& m, types::FeatureMatrix const& x, types::GroupIdVector const& y) {
213 return oob_error(*m, x, y);
214 }
215
216 template<typename T>
217 double error(std::unique_ptr<T> const& m, types::FeatureMatrix const& x, types::OutcomeVector const& y) {
218 return error(*m, x, y);
219 }
220}
Mode-specific evaluation metric blocks.
Random forest of classification trees.
Definition ClassificationForest.hpp:18
Abstract base class for projection pursuit random forests.
Definition Forest.hpp:31
Abstract base class for predictive models (trees and forests).
Definition Model.hpp:29
Random forest of regression trees.
Definition RegressionForest.hpp:13
Abstract base class for projection pursuit decision trees.
Definition Tree.hpp:29
Eigen::Matrix< Feature, Eigen::Dynamic, Eigen::Dynamic > FeatureMatrix
Dynamic-size matrix of feature values.
Definition Types.hpp:33
Eigen::Matrix< Outcome, Eigen::Dynamic, 1 > OutcomeVector
Dynamic-size column vector of predictions.
Definition Types.hpp:42
Eigen::Matrix< GroupId, Eigen::Dynamic, 1 > GroupIdVector
Dynamic-size column vector of internal group labels.
Definition Types.hpp:39
Eigen::Matrix< Feature, Eigen::Dynamic, 1 > FeatureVector
Dynamic-size column vector of feature values.
Definition Types.hpp:36
Binarization strategies for multiclass-to-binary reduction.
Definition Benchmark.hpp:25
stats::ClassificationMetrics::Maybe oob_metrics(ClassificationForest const &forest, types::FeatureMatrix const &x, types::OutcomeVector const &y)
Out-of-bag metrics — sentinel-free summary of OOB performance.
types::FeatureVector vi_weighted_projections(Forest const &forest, types::FeatureMatrix const &x, types::OutcomeVector const &y, types::FeatureVector const *scale=nullptr)
VI3 — weighted projection-coefficient importance.
VariableImportance variable_importance(Tree const &tree, types::FeatureMatrix const &x)
Bundle the available VI measures for a single tree (VI2 only).
std::optional< double > oob_error(Forest const &forest, types::FeatureMatrix const &x, types::OutcomeVector const &y)
Out-of-bag error.
double error(Model const &model, types::FeatureMatrix const &x, types::OutcomeVector const &y)
Prediction error of model on data (x, y).
types::FeatureVector vi_permuted(Forest const &forest, types::FeatureMatrix const &x, types::OutcomeVector const &y, int seed)
VI1 — per-variable permuted importance.
types::FeatureVector vi_projections(Tree const &tree, int n_vars, types::FeatureVector const *scale=nullptr)
VI2 for a single tree — projection-coefficient importance.
types::OutcomeVector oob_predict(Forest const &forest, types::FeatureMatrix const &x)
Out-of-bag predictions.
Grouped result of the variable importance measures.
Definition Evaluation.hpp:39
types::FeatureVector permuted
VI1 — per-variable permuted importance (forest only).
Definition Evaluation.hpp:41
types::FeatureVector projections
VI2 — per-variable projection-coefficient importance.
Definition Evaluation.hpp:43
types::FeatureVector weighted_projections
VI3 — per-variable weighted-projection importance (forest only).
Definition Evaluation.hpp:45
types::FeatureVector scale
Per-variable σ used to rescale coefficients (columnwise sd).
Definition Evaluation.hpp:47
std::optional< ClassificationMetrics > Maybe
Definition Metrics.hpp:26
std::optional< RegressionMetrics > Maybe
Definition Metrics.hpp:53