Feature Selection Toolbox FST4
UTIA · Czech Academy of Sciences
Loading...
Searching...
No Matches
criterion_normal.hpp
Go to the documentation of this file.
1#ifndef FSTCRITERIONNORMAL_H
2#define FSTCRITERIONNORMAL_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 <string>
54#include "error.hpp"
55#include "global.hpp"
56#include "criterion.hpp"
57#include "model_normal.hpp"
58#include "indexed_vector.hpp"
59#include "indexed_matrix.hpp"
60
61/*============== Template parameter type naming conventions ==============
62--------- Numeric types: -------------------------------------------------
63DATATYPE - data sample values - usually real numbers (but may be integers
64 in text processing etc.)
65REALTYPE - must be real numbers - for representing intermediate results of
66 calculations like mean, covariance etc.
67IDXTYPE - index values for enumeration of data samples - (nonnegative) integers,
68 extent depends on numbers of samples in data
69DIMTYPE - index values for enumeration of features (dimensions), or classes (not
70 class sizes) - (nonnegative) integers, usually lower extent than IDXTYPE,
71 but be aware of expressions like _classes*_features*_features !
72 in linearized representations of feature matrices for all classes
73BINTYPE - feature selection marker type - represents ca. <10 different feature
74 states (selected, deselected, sel./desel. temporarily 1st nested loop, 2nd...)
75RETURNTYPE - criterion value: real value, but may be extended in future to support
76 multiple values
77--------- Class types: ---------------------------------------------------
78SUBSET - class of class type Subset
79CLASSIFIER - class implementing interface defined in abstract class Classifier
80EVALUATOR - class implementing interface defined in abstract class Sequential_Step
81DISTANCE - class implementing interface defined in abstract class Distance
82DATAACCESSOR - class implementing interface defined in abstract class Data_Accessor
83INTERVALCONTAINER - class of class type Data_Intervaller
84CONTAINER - STL container of class type Data_Interval
85========================================================================== */
86
87namespace FST {
88
99template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
100class Criterion_Normal : public Criterion_Filter<RETURNTYPE,SUBSET> { // abstract class
101public:
102 typedef std::shared_ptr<DATAACCESSOR> PDataAccessor;
103 typedef std::shared_ptr<SUBSET> PSubset;
104 Criterion_Normal();
105 virtual ~Criterion_Normal() {notify("Criterion_Normal destructor.");}
109 virtual bool initialize(PDataAccessor da);
110
111 virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override;
112
117 virtual bool is_monotonic() const override {return true;}
118
119 DIMTYPE get_n() const {assert(_model); return _model->get_n();}
120 DIMTYPE get_d() const {assert(_model); return _model->get_d();}
121
122 virtual std::ostream& print(std::ostream& os) const override {os << criterion_name() << "()"; return os;}
123protected:
126protected:
127 // hooks for the concrete two-class normal criteria, called by the template methods above
128 virtual const char* criterion_name() const =0;
129 virtual void allocate_buffers() =0;
130 virtual void update_derived_model() =0;
135 virtual bool evaluate_narrowed(RETURNTYPE &result, const PSubset sub) =0;
136
139 if(!v) v.reset(new Indexed_Vector<DATATYPE,DIMTYPE,SUBSET>);
140 if(get_n()>v->get_n()) v->reset(get_n());
141 }
142
143 template<typename MATRIXVALUETYPE>
144 void alloc_matrix(std::unique_ptr<Indexed_Matrix<MATRIXVALUETYPE,DIMTYPE,SUBSET> > &m, const bool LUstorage=false) {
145 if(!m) m.reset(new Indexed_Matrix<MATRIXVALUETYPE,DIMTYPE,SUBSET>(get_n(),LUstorage));
146 if(get_n()>m->get_n()) m->reset(get_n(),LUstorage);
147 }
148protected:
149 std::unique_ptr<Model_Normal<DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR> > _model;
150 std::unique_ptr<Indexed_Vector<DATATYPE,DIMTYPE,SUBSET> > _meandif;
151};
152
153template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
154Criterion_Normal<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Criterion_Normal(const Criterion_Normal& cn)
155{
156 notify("Criterion_Normal copy-constructor.");
159}
160
161template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
162Criterion_Normal<RETURNTYPE,DATATYPE,REALTYPE,IDXTYPE,DIMTYPE,SUBSET,DATAACCESSOR>::Criterion_Normal()
163{
164 notify("Criterion_Normal constructor.");
166}
167
168template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
170{
171 if(da->getNoOfClasses()!=2) throw fst_error(std::string(criterion_name())+"() defined for two-class problems only.");
172 notify("Criterion_Normal::initialize().");
173 assert(_model);
174 _model->learn(da);
175
177 _meandif->copy_raw(_model->get_mean()[0]);
178 _meandif->subtract_raw(_model->get_mean()[1]);
179
182 return true;
183}
184
185template<class RETURNTYPE, typename DATATYPE, typename REALTYPE, typename IDXTYPE, typename DIMTYPE, class SUBSET, class DATAACCESSOR>
187{
188 notify("Criterion_Normal::evaluate().");
189 assert(_model);
190 assert(get_n()>0);
191 assert(_meandif);
192 assert(get_n()==_meandif->get_n());
193 assert(sub);
194
195 if(sub->get_d_raw()==0) return false;
196
197 _model->narrow_to(sub);
198 _meandif->narrow_to(sub);
199 if(!evaluate_narrowed(result,sub)) return false;
200 // A non-finite value (NaN or an infinity) means the normal-model arithmetic broke down
201 // on this subset -- an inverse or determinant of a numerically singular covariance. It
202 // must never reach a search: NaN compares false against everything, so a search would
203 // silently accept or reject candidates on garbage and still report success.
204 if(!std::isfinite(result))
205 throw FST::fst_error(std::string(criterion_name())+"::evaluate: non-finite criterion value on the evaluated subset (numerically singular class covariance; reduce the subset size, drop constant or collinear features, or use more training data)");
206 return true;
207}
208
209
210} // namespace
211#endif // FSTCRITERIONNORMAL_H ///:~
Abstract class, defines interface for feature selection criterion implementations based on normal mod...
Definition criterion_normal.hpp:100
virtual void update_derived_model()=0
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
virtual const char * criterion_name() const =0
class name, used in output and error messages
DIMTYPE get_n() const
the full (raw, unnarrowed) feature count the model was learned for
Definition criterion_normal.hpp:119
virtual bool evaluate_narrowed(RETURNTYPE &result, const PSubset sub)=0
narrow remaining buffers to sub, redim work buffers to get_d(), compute the criterion value....
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::shared_ptr< DATAACCESSOR > PDataAccessor
shared pointer to the data accessor the model is learned from
Definition criterion_normal.hpp:102
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
virtual bool initialize(PDataAccessor da)
template method: learn model, compute _meandif, then allocate_buffers() + update_derived_model().
Definition criterion_normal.hpp:169
std::shared_ptr< SUBSET > PSubset
shared pointer to the feature subset being evaluated
Definition criterion_normal.hpp:103
virtual std::ostream & print(std::ostream &os) const override
writes a human-readable identification of the object; base default writes nothing
Definition criterion_normal.hpp:122
virtual bool is_monotonic() const override
Definition criterion_normal.hpp:117
virtual bool evaluate(RETURNTYPE &result, const PSubset sub) override
template method: narrow model and _meandif to sub, then evaluate_narrowed()
Definition criterion_normal.hpp:186
virtual void allocate_buffers()=0
(re)allocate criterion-specific work buffers for get_n() features (use the alloc_* helpers)
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
Vector representation and operations, allows operation in selected subspace. Also serves as the helpe...
Definition indexed_vector.hpp:100
Implements normal (gaussian) model.
Definition model_normal.hpp:112
FST4 specific error passing class.
Definition error.hpp:61
Defines interface for feature selection criterion classes.
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.
Implements normal (gaussian) model.
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.