Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_normal_gmahalanobis.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONNORMALGMAHALANOBIS_H
2#define FSTCRITERIONNORMALGMAHALANOBIS_H
3 /*
27=========================================================================
28Copyright and license:
29 * FST4 software (with the exception of any bundled or externally linked
30 third-party components) is Copyright (C) 2000-present Institute of
31 Information Theory and Automation of the Czech Academy of Sciences (UTIA).
32 * FST4 is free software: you can redistribute it and/or modify it under
33 the terms of the GNU Affero General Public License as published by the
34 Free Software Foundation, version 3. The full text of the license is in
35 the file LICENSE in the distribution root and at
36 https://www.gnu.org/licenses/agpl-3.0.html.
37 * FST4 is distributed in the hope that it will be useful, but WITHOUT ANY
38 WARRANTY; without even the implied warranty of MERCHANTABILITY or
39 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
40 License for more details.
41 * Closed-source use without the AGPL's copyleft obligations requires a
42 commercial license from UTIA; in case of interest write to
43 utia@utia.cas.cz. LICENSING.md in the distribution root explains both
44 options, the contributor license agreement that every contribution
45 requires, and the contacts (technical questions and pull requests:
46 somol@utia.cas.cz, with "FST" in the subject line).
47========================================================================== */
48
49#include <memory>
50#include <cassert>
51#include <iostream>
52#include <cmath>
53#include "error.hpp"
54#include "global.hpp"
55#include "criterion_normal.hpp"
56#include "indexed_vector.hpp"
57#include "indexed_matrix.hpp"
58
59/*============== Template parameter type naming conventions ==============
60--------- Numeric types: -------------------------------------------------
61DATATYPE - data sample values - usually real numbers (but may be integers
62 in text processing etc.)
63REALTYPE - must be real numbers - for representing intermediate results of
64 calculations like mean, covariance etc.
65IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
66 extent depends on numbers of samples in data
67DIMTYPE - index values for enumeration of features (dimensions), or classes (not
68 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
69 but be aware of expressions like _classes*_features*_features !
70 in linearized representations of feature matrices for all classes
71BINTYPE - feature selection marker type - represents ca. <10 different feature
72 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
73RETURNTYPE - criterion value: real value, but may be extended in future to support
74 multiple values
75--------- Class types: ---------------------------------------------------
76SUBSET - class of class type Subset
77CLASSIFIER - class implementing interface defined in abstract class Classifier
78EVALUATOR - class implementing interface defined in abstract class Sequential_Step
79DISTANCE - class implementing interface defined in abstract class Distance
80DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
81INTERVALCONTAINER - class of class type Data_Intervaller
82CONTAINER - STL container of class type Data_Interval
83========================================================================== */
84
85namespace FST {
86
106template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
107class Criterion_Normal_GMahalanobis : public Clonable_Impl<Criterion_Normal_GMahalanobis<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Criterion_Normal<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
108public:
111 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
112 typedef std::shared_ptr<SUBSET> PSubset;
113 Criterion_Normal_GMahalanobis() {notify("Criterion_Normal_GMahalanobis constructor.");}
114 virtual ~Criterion_Normal_GMahalanobis() {notify("Criterion_Normal_GMahalanobis destructor.");}
115
116
117protected:
118 // Template-method hooks: initialize()/evaluate() skeletons live in Criterion_Normal
119 virtual const char* criterion_name() const override {return "Criterion_Normal_GMahalanobis";}
125 virtual void update_derived_model() override {
126 _cov12->set_all_raw_to(0.0);
127 _cov12->add_constmul_raw(parent::_model->get_cov()[0],parent::_model->get_Pc(0));
128 _cov12->add_constmul_raw(parent::_model->get_cov()[1],parent::_model->get_Pc(1));
129 }
130 virtual bool evaluate_narrowed(RETURNTYPE &result, const PSubset sub) override;
131private:
132 friend parent_impl;
134 Criterion_Normal_GMahalanobis(const Criterion_Normal_GMahalanobis& cngm);
135private:
136 std::unique_ptr<Indexed_Matrix<DATATYPE,DIMTYPE,SUBSET> > _cov12;
137 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> > _invbuf;
138 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> > _LUtemp;
139};
140
141template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
142Criterion_Normal_GMahalanobis<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Criterion_Normal_GMahalanobis(const Criterion_Normal_GMahalanobis& cngm) :
143 parent_impl(cngm)
144{
145 notify("Criterion_Normal_GMahalanobis copy-constructor.");
149}
150
151
152
153template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
155{ // (model and _meandif have been narrowed to sub by Criterion_Normal::evaluate)
156 notify("Criterion_Normal_GMahalanobis::evaluate_narrowed().");
157 assert(_cov12);
158 assert(_invbuf);
159 assert(_LUtemp);
160 assert(parent::get_n()==_cov12->get_n());
161 assert(parent::get_n()<=_invbuf->get_n_max());
162 assert(parent::get_n()<=_LUtemp->get_n_max());
163
164 _cov12->narrow_to(sub);
165
166 _invbuf->redim(parent::get_d());
167 _LUtemp->redim(parent::get_d());
168
169 _cov12->LUdecompose(*_LUtemp);
170 _cov12->invert(*_invbuf,*_LUtemp);
171
172 RETURNTYPE left=0.0;
173 RETURNTYPE pom;
174
175 for(DIMTYPE j=0;j<parent::get_d();j++)
176 {
177 pom=0.0;
178 for(DIMTYPE i=0;i<parent::get_d();i++)
179 {
180 pom+=parent::_meandif->at(i)*_invbuf->at_raw(i,j);
181 }
182 left+=pom*parent::_meandif->at(j);
183 }
184 result=left;
185 return true;
186}
187
188
189} // namespace
190#endif // FSTCRITERIONNORMALGMAHALANOBIS_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements Generalized Mahalanobis distance based on normal (gaussian) model to serve as feature sele...
Definition criterion_normal_gmahalanobis.hpp:107
virtual void allocate_buffers() override
(re)allocate criterion-specific work buffers for get_n() features (use the alloc_* helpers)
Definition criterion_normal_gmahalanobis.hpp:120
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor the model is learned from
Definition criterion_normal_gmahalanobis.hpp:111
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _LUtemp
scratch LU-decomposition storage, reused for each matrix inversion
Definition criterion_normal_gmahalanobis.hpp:138
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _invbuf
inverse of the narrowed _cov12, used in the quadratic-form term
Definition criterion_normal_gmahalanobis.hpp:137
virtual bool evaluate_narrowed(RETURNTYPE &result, const PSubset sub) override
narrow remaining buffers to sub, redim work buffers to get_d(), compute the criterion value....
Definition criterion_normal_gmahalanobis.hpp:154
std::unique_ptr< Indexed_Matrix< DATATYPE, DIMTYPE, SUBSET > > _cov12
prior-weighted pooled covariance Pc(0)*cov[0]+Pc(1)*cov[1], computed by update_derived_model()
Definition criterion_normal_gmahalanobis.hpp:136
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_normal_gmahalanobis.hpp:112
virtual const char * criterion_name() const override
class name, used in output and error messages
Definition criterion_normal_gmahalanobis.hpp:119
Criterion_Normal< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > parent
base class: the normal-model template-method skeleton
Definition criterion_normal_gmahalanobis.hpp:109
Clonable_Impl< Criterion_Normal_GMahalanobis, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Criterion
Definition criterion_normal_gmahalanobis.hpp:110
virtual void update_derived_model() override
Definition criterion_normal_gmahalanobis.hpp:125
Abstract class, defines interface for feature selection criterion implementations based on normal mod...
Definition criterion_normal.hpp:100
std::unique_ptr< Model_Normal< DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > > _model
the learned per-class normal (mean/covariance) model, narrowed in evaluate()
Definition criterion_normal.hpp:149
void alloc_matrix(std::unique_ptr< Indexed_Matrix< MATRIXVALUETYPE, DIMTYPE, SUBSET > > &m, const bool LUstorage=false)
lazy (re)allocation of a work matrix for get_n() features; LUstorage as required by Indexed_Matrix::L...
Definition criterion_normal.hpp:144
DIMTYPE get_n() const
the full (raw, unnarrowed) feature count the model was learned for
Definition criterion_normal.hpp:119
std::unique_ptr< Indexed_Vector< DATATYPE, DIMTYPE, SUBSET > > _meandif
mean[0]-mean[1], shared by all two-class normal criteria
Definition criterion_normal.hpp:150
DIMTYPE get_d() const
the currently active (narrowed) feature count, see evaluate()
Definition criterion_normal.hpp:120
Matrix representation and operations, allows operation in selected subspace.
Definition indexed_matrix.hpp:102
Defines interface for feature selection criterion implementations based on normal model.
FST4 specific error passing class.
Global definitions.
Matrix representation and operations, allows operation in selected subspace.
Vector representation and operations, allows operation in selected subspace.
The Feature Selection Toolbox 4 library: every library component – data access, criteria and classifi...
Definition data_accessor.hpp:80
void notify(const std::string msg)
Prints msg followed by a newline via syncout::print(); a no-op unless the preprocessor macro DEBUG is...
Definition global.hpp:112
Copyright © 2000–present ÚTIA, Institute of Information Theory and Automation of the Czech Academy of Sciences · Contacts · This site sets no cookies and uses no analytics.