45 virtual size_t predict(
const Eigen::Ref<const Eigen::VectorXd> &x)
const = 0;
73 void compute(
const std::vector<Eigen::VectorXd> &points,
size_t k);
81 const std::vector<size_t> &
operator[](
size_t i)
const;
122 Eigen::MatrixXd
xObs_ = Eigen::MatrixXd(0, 0);
131 std::shared_ptr<kernel::Kernel>
kernel_{
nullptr};
143 void set(std::shared_ptr<kernel::Kernel> kernel, std::shared_ptr<cmp::kernel::Bandwidth> bandwidth) {
157 void condition(
const Eigen::Ref<const Eigen::MatrixXd> &xObs,
const Eigen::Ref<const Eigen::VectorXs> &labels) {
166 throw std::runtime_error(
"KDE::set: Number of observations and labels size do not match.");
170 for(
size_t i = 0; i <
nObs_; i++) {
181 double density(
const Eigen::Ref<const Eigen::VectorXd> &x,
const size_t &classLabel)
const;
188 std::vector<double>
predictProbabilities(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override;
195 size_t predict(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override {
197 return std::max_element(probs.begin(), probs.end()) - probs.begin();
230 void fit(
const Eigen::Ref<const Eigen::MatrixXd>& xObs,
const Eigen::Ref<const Eigen::VectorXs>& labels,
cmp::statistics::KFold kf,
const double& minBw,
const double& maxBw,
const method method =
CV_PROB_SCORE, nlopt::algorithm algo = nlopt::LN_SBPLX,
double ftol_rel = 1e-4, std::vector<bool> logScaleFlags = {});
244 void fitLOO(
const Eigen::Ref<const Eigen::MatrixXd>& xObs,
const Eigen::Ref<const Eigen::VectorXs>& labels,
const double& minBw,
const double& maxBw, nlopt::algorithm algo = nlopt::LN_SBPLX,
double ftol_rel = 1e-4, std::vector<bool> logScaleFlags = {});
284 std::shared_ptr<cmp::covariance::Covariance>
covariance_{
nullptr};
328 for(
int i = 0; i < prob->l; i++) {
355 svm_free_and_destroy_model(&
model_);
367 void set(std::shared_ptr<cmp::covariance::Covariance> covariance,
const Eigen::Ref<const Eigen::VectorXd> hpar,
const double &C,
const double &eps = 1e-3) {
381 void condition(
const Eigen::Ref<const Eigen::MatrixXd> &xObs,
const Eigen::Ref<const Eigen::VectorXs> &labels);
389 size_t predict(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override;
396 std::vector<double>
predictProbabilities(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override;
420 void fit(
const method&
method,
const cmp::statistics::KFold& kf,
const Eigen::Ref<const Eigen::MatrixXd>& xObs,
const Eigen::Ref<const Eigen::VectorXs>& membershipTable, Eigen::VectorXd lb, Eigen::VectorXd ub, nlopt::algorithm algo = nlopt::LN_SBPLX,
double ftol_rel = 1e-4, std::vector<bool> logScaleFlags = {});
433 void fit(Eigen::Ref<const Eigen::MatrixXd> xObs, Eigen::Ref<const Eigen::VectorXs> membershipTable, Eigen::Ref<const Eigen::VectorXd> lb, Eigen::Ref<const Eigen::VectorXd> ub, nlopt::algorithm algo = nlopt::LN_SBPLX,
double ftol_rel = 1e-4, std::vector<bool> logScaleFlags = {});
478 void condition(
const Eigen::Ref<const Eigen::MatrixXd> &xObs,
const Eigen::Ref<const Eigen::VectorXs> &labels);
486 size_t predict(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override;
494 std::vector<double>
predictProbabilities(
const Eigen::Ref<const Eigen::VectorXd> &x)
const override;
Abstract base class for all classifiers.
Definition classifier.h:41
virtual size_t predict(const Eigen::Ref< const Eigen::VectorXd > &x) const =0
virtual ~Classifier()=default
virtual std::vector< double > predictProbabilities(const Eigen::Ref< const Eigen::VectorXd > &x) const =0
Baseline classifier predicting class labels based on training frequency distributions.
Definition classifier.h:467
size_t nClasses_
Total number of class labels.
Definition classifier.h:469
void condition(const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &labels)
Computes the class frequencies from training labels.
Definition classifier.cpp:662
size_t predict(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Predicts the class label with the highest prior frequency.
Definition classifier.cpp:669
std::vector< double > predictProbabilities(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Estimates the class prior probabilities based on training frequencies.
Definition classifier.cpp:673
Classifier that uses Kernel Density Estimation (KDE) for classification.
Definition classifier.h:120
double objectiveFunctionCV(const method &method, const cmp::statistics::KFold &kf) const
Objective function for cross-validation. This function computes the objective value for cross-validat...
Definition classifier.cpp:123
size_t dimX_
Input features dimensionality.
Definition classifier.h:127
size_t nObs_
Count of observations per class.
Definition classifier.h:126
size_t nClasses_
Total number of distinct class labels.
Definition classifier.h:128
std::shared_ptr< kernel::Kernel > kernel_
Bandwidth object for KDE.
Definition classifier.h:131
KDE()
Kernel object for KDE.
Definition classifier.h:134
void fitLOO(const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &labels, const double &minBw, const double &maxBw, nlopt::algorithm algo=nlopt::LN_SBPLX, double ftol_rel=1e-4, std::vector< bool > logScaleFlags={})
Fit the KDE by maximizing an efficient leave-one-out objective. This method optimizes bandwidth using...
Definition classifier.cpp:243
Eigen::VectorXs classCounts_
Labels for the observations.
Definition classifier.h:124
Eigen::VectorXs labels_
Observations.
Definition classifier.h:123
Eigen::MatrixXd xObs_
Definition classifier.h:122
std::vector< double > predictProbabilities(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Predict the class probabilities at a given point.
Definition classifier.cpp:104
void set(std::shared_ptr< kernel::Kernel > kernel, std::shared_ptr< cmp::kernel::Bandwidth > bandwidth)
Set the kernel and bandwidth for the KDE. This method allows you to set the kernel and bandwidth obje...
Definition classifier.h:143
std::shared_ptr< cmp::kernel::Bandwidth > bandwidth_
Definition classifier.h:130
void condition(const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &labels)
Condition the KDE with observations and labels. This method sets the observations and labels for the ...
Definition classifier.h:157
double objectiveFunctionLOO() const
Objective function for efficient leave-one-out optimization. This function computes mean LOO log-prob...
Definition classifier.cpp:180
void fit(const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &labels, cmp::statistics::KFold kf, const double &minBw, const double &maxBw, const method method=CV_PROB_SCORE, nlopt::algorithm algo=nlopt::LN_SBPLX, double ftol_rel=1e-4, std::vector< bool > logScaleFlags={})
Fit the KDE using cross-validation to optimize the bandwidth. This method uses the specified optimiza...
Definition classifier.cpp:217
size_t predict(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Predict the class label at a given point.
Definition classifier.h:195
K-Nearest Neighbors classifier helper class.
Definition classifier.h:60
size_t nPoints() const
Returns the total number of points in the dataset.
Definition classifier.cpp:76
std::vector< std::vector< size_t > > neighbours_
Cache containing nearest neighbor indices for each data point.
Definition classifier.h:62
const std::vector< size_t > & operator[](size_t i) const
Accesses the list of nearest neighbor indices for the i-th point.
Definition classifier.cpp:72
void compute(const std::vector< Eigen::VectorXd > &points, size_t k)
Computes the nearest neighbor index map for the given points.
Definition classifier.cpp:3
size_t kNearestValue_
The number of nearest neighbors (k).
Definition classifier.h:63
size_t k() const
Returns the value of k (number of neighbors).
Definition classifier.cpp:80
size_t nPoints_
Total number of data points.
Definition classifier.h:64
Classifier that uses Support Vector Machine (SVM) for classification.
Definition classifier.h:277
struct svm_problem prob_
Pointer to the SVM model.
Definition classifier.h:281
double objectiveFunctionSpan()
Objective function for span-based optimization. This function returns the negative mean support-vecto...
Definition classifier.cpp:531
Eigen::VectorXd hyperparameters_
Covariance object for the kernel.
Definition classifier.h:285
void initDefaultParameters(double C, double eps)
Initializes default parameters for the SVM model.
Definition classifier.h:304
double objectiveFunctionCV(const method &method, const cmp::statistics::KFold &kf)
Objective function for cross-validation. This function computes the objective value for cross-validat...
Definition classifier.cpp:427
void condition(const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &labels)
Condition the SVM with observations and labels. This method sets the observations and labels for the ...
Definition classifier.cpp:274
struct svm_model * model_
Definition classifier.h:280
Eigen::MatrixXd xObs_
Hyperparameters for the covariance function.
Definition classifier.h:288
static void silent_print(const char *s)
Custom print redirect function to suppress standard console output from LIBSVM.
Definition classifier.h:296
~SVM()
Definition classifier.h:354
void fit(const method &method, const cmp::statistics::KFold &kf, const Eigen::Ref< const Eigen::MatrixXd > &xObs, const Eigen::Ref< const Eigen::VectorXs > &membershipTable, Eigen::VectorXd lb, Eigen::VectorXd ub, nlopt::algorithm algo=nlopt::LN_SBPLX, double ftol_rel=1e-4, std::vector< bool > logScaleFlags={})
Fit the SVM using cross-validation to optimize hyperparameters and C. This method uses the specified ...
Definition classifier.cpp:360
size_t predict(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Predict the class label at a given point.
Definition classifier.cpp:315
SVM()
Definition classifier.h:344
size_t nObs_
Total number of training observations.
Definition classifier.h:289
std::shared_ptr< cmp::covariance::Covariance > covariance_
Structure to hold the SVM parameters.
Definition classifier.h:284
std::vector< double > predictProbabilities(const Eigen::Ref< const Eigen::VectorXd > &x) const override
Predict the class probabilities at a given point.
Definition classifier.cpp:332
std::pair< Eigen::VectorXd, double > getHyperparameters() const
Get the current hyperparameters and SVM regularization parameter C.
Definition classifier.h:402
void set(std::shared_ptr< cmp::covariance::Covariance > covariance, const Eigen::Ref< const Eigen::VectorXd > hpar, const double &C, const double &eps=1e-3)
Set the covariance function, hyperparameters, and SVM parameters. This method allows you to set the c...
Definition classifier.h:367
struct svm_parameter modelParameters_
Structure to hold the SVM problem.
Definition classifier.h:283
static void freeProblem(struct svm_problem *prob)
Definition classifier.h:326
K-Fold cross-validation partition generator.
Definition statistics.h:44
Matrix< size_t, Eigen::Dynamic, 1 > VectorXs
Definition cmp_defines.h:20
Definition classifier.h:17
method
Bandwidth selection objective criterion.
Definition classifier.h:22
@ CV_SCORE
Cross-validation score optimization based on classification accuracy.
Definition classifier.h:23
@ CV_PROB_SCORE
Cross-validation score optimization based on probability likelihood.
Definition classifier.h:24
Functor wrapper for NLopt with automatic, transparent log-scaling.
Header file for statistical functions and classes.