Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_normal_divergence.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONNORMALDIVERGENCE_H
2#define FSTCRITERIONNORMALDIVERGENCE_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
109template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
110class Criterion_Normal_Divergence : public Clonable_Impl<Criterion_Normal_Divergence<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>,Criterion_Normal<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > {
111public:
114 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
115 typedef std::shared_ptr<SUBSET> PSubset;
116 Criterion_Normal_Divergence() {notify("Criterion_Normal_Divergence constructor.");}
117 virtual ~Criterion_Normal_Divergence() {notify("Criterion_Normal_Divergence destructor.");}
118
119
120protected:
121 // Template-method hooks: initialize()/evaluate() skeletons live in Criterion_Normal
122 virtual const char* criterion_name() const override {return "Criterion_Normal_Divergence";}
129 virtual void update_derived_model() override {} // no criterion-specific model derivations beyond the shared _meandif
130 virtual bool evaluate_narrowed(RETURNTYPE &result, const PSubset sub) override;
131private:
132 friend parent_impl;
134 Criterion_Normal_Divergence(const Criterion_Normal_Divergence& cnd);
135private:
136 std::unique_ptr<Indexed_Vector<DATATYPE,DIMTYPE,SUBSET> > _diag;
137 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> > _invbuf1;
138 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> > _invbuf2;
139 std::unique_ptr<Indexed_Matrix<REALTYPE,DIMTYPE,SUBSET> > _LUtemp;
140};
141
142template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
143Criterion_Normal_Divergence<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Criterion_Normal_Divergence(const Criterion_Normal_Divergence& cnd) :
144 parent_impl(cnd)
145{
146 notify("Criterion_Normal_Divergence copy-constructor.");
151}
152
153
154
155template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
157{ // (model and _meandif have been narrowed to sub by Criterion_Normal::evaluate)
158 notify("Criterion_Normal_Divergence::evaluate_narrowed().");
159 assert(_diag);
160 assert(_invbuf1);
161 assert(_invbuf2);
162 assert(_LUtemp);
163 assert(parent::get_n()==_diag->get_n());
164 assert(parent::get_n()<=_invbuf1->get_n_max());
165 assert(parent::get_n()<=_invbuf2->get_n_max());
166 assert(parent::get_n()<=_LUtemp->get_n_max());
167
168 _invbuf1->redim(parent::get_d());
169 _invbuf2->redim(parent::get_d());
170 _LUtemp->redim(parent::get_d());
171
174
175 _cov1.LUdecompose(*_LUtemp);
176 _cov1.invert(*_invbuf1,*_LUtemp);
177 _cov2.LUdecompose(*_LUtemp);
178 _cov2.invert(*_invbuf2,*_LUtemp);
179
180 RETURNTYPE left=0.0;
181 RETURNTYPE pom;
182 for(DIMTYPE j=0;j<parent::get_d();j++)
183 {
184 pom=0.0;
185 for(DIMTYPE i=0;i<parent::get_d();i++)
186 {
187 pom+=parent::_meandif->at(i)*(_invbuf1->at_raw(i,j)+_invbuf2->at_raw(i,j));
188 }
189 left+=pom*parent::_meandif->at(j);
190 }
191
192 RETURNTYPE right=0.0;
193 for(DIMTYPE i=0;i<parent::get_d();i++)
194 {
195 _diag->at_raw(i)=0.0;
196 for(DIMTYPE k=0;k<parent::get_d();k++)
197 {
198 _diag->at_raw(i)+=_invbuf1->at_raw(i,k)*_cov2.at(k,i);
199 }
200 }
201 for(DIMTYPE i=0;i<parent::get_d();i++)
202 {
203 for(DIMTYPE k=0;k<parent::get_d();k++)
204 {
205 _diag->at_raw(i)+=_invbuf2->at_raw(i,k)*_cov1.at(k,i);
206 }
207 }
208 // trace of (cov1^-1*cov2 + cov2^-1*cov1) minus 2d - the offset that makes the term
209 // vanish when the two covariances coincide (see the class documentation)
210 for(DIMTYPE i=0;i<parent::get_d();i++) right+=_diag->at_raw(i)-2.0;
211
212 result=0.5*left+0.5*right;
213 return true;
214}
215
216} // namespace
217#endif // FSTCRITERIONNORMALDIVERGENCE_H ///:~
CRTP mixin implementing the Clonable protocol for a concrete class.
Definition clonable.hpp:184
Implements Divergence (distance) based on normal (gaussian) model to serve as feature selection crite...
Definition criterion_normal_divergence.hpp:110
virtual void update_derived_model() override
Definition criterion_normal_divergence.hpp:129
Criterion_Normal< RETURNTYPE, DATATYPE, REALTYPE, IDXTYPE, DIMTYPE, SUBSET, DATAACCESSOR > parent
base class: the normal-model template-method skeleton
Definition criterion_normal_divergence.hpp:112
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_divergence.hpp:156
virtual const char * criterion_name() const override
class name, used in output and error messages
Definition criterion_normal_divergence.hpp:122
std::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor the model is learned from
Definition criterion_normal_divergence.hpp:114
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _invbuf2
inverse of the narrowed class-2 covariance
Definition criterion_normal_divergence.hpp:138
std::unique_ptr< Indexed_Vector< DATATYPE, DIMTYPE, SUBSET > > _diag
per-feature diagonal of cov1^-1*cov2 + cov2^-1*cov1, summed into the trace term of the divergence
Definition criterion_normal_divergence.hpp:136
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_normal_divergence.hpp:115
virtual void allocate_buffers() override
(re)allocate criterion-specific work buffers for get_n() features (use the alloc_* helpers)
Definition criterion_normal_divergence.hpp:123
Clonable_Impl< Criterion_Normal_Divergence, parent > parent_impl
implements clone(); sharing/stateless_clone throw in Criterion
Definition criterion_normal_divergence.hpp:113
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _invbuf1
inverse of the narrowed class-1 covariance
Definition criterion_normal_divergence.hpp:137
std::unique_ptr< Indexed_Matrix< REALTYPE, DIMTYPE, SUBSET > > _LUtemp
scratch LU-decomposition storage, reused for each matrix inversion
Definition criterion_normal_divergence.hpp:139
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
void alloc_vector(std::unique_ptr< Indexed_Vector< DATATYPE, DIMTYPE, SUBSET > > &v)
lazy (re)allocation of a work vector for get_n() features, for use from allocate_buffers()
Definition criterion_normal.hpp:138
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
void invert(Indexed_Matrix &result, const Indexed_Matrix &LU_matrix)
Definition indexed_matrix.hpp:632
void LUdecompose(Indexed_Matrix &result)
Definition indexed_matrix.hpp:380
DATATYPE & at(const DIMTYPE row, const DIMTYPE col)
indexed access through the narrow_to() subset map – both row and col are mapped, row/col in [0,...
Definition indexed_matrix.hpp:114
Vector representation and operations, allows operation in selected subspace. Also serves as the helpe...
Definition indexed_vector.hpp:100
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.