1#ifndef FSTRESULTWEIGHTERSAMPLED_H
2#define FSTRESULTWEIGHTERSAMPLED_H
127template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
144 {
notify(
"Result_Weighter_Sampled constructor.");}
150 void reset(
const DIMTYPE n);
159 bool add(
const RETURNTYPE value,
const PSubset sub);
170 RETURNTYPE
score(
const DIMTYPE f)
const;
180 std::ostream&
print(std::ostream& os)
const {os <<
"Result_Weighter_Sampled(n=" <<
_n <<
", horizon=" <<
_horizon <<
", stats=" << (
_mode==STATS_ZCONTRAST?
"zcontrast":
"rawmean") << (
_frozen?
", FROZEN":
"") <<
", evals=" <<
_total_evals <<
")";
return os;}
181 bool print_stats(std::ostream& os,
const DIMTYPE top=20)
const;
213 std::vector<FeatureStat>
_fs;
221template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
227 for(DIMTYPE f=0;f<
_n;f++) {
228 _fs[f].m_is=0.0;
_fs[f].m_isnot=0.0;
_fs[f].v_is=0.0;
229 _fs[f].raw_sum_is=0.0;
_fs[f].raw_cnt_is=0.0;
230 _fs[f].n_is=0;
_fs[f].n_isnot=0;
239template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
246 assert(
_n==sub->get_n());
247 if(
_n!=sub->get_n())
return false;
252 for(DIMTYPE f : sub->selected_features()) e.
selected.push_back(f);
253 _batch.push_back(std::move(e));
258template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
263 const IDXTYPE B=(IDXTYPE)
_batch.size();
267 for(IDXTYPE i=0;i<B;i++) mu+=(
double)
_batch[i].value;
270 for(IDXTYPE i=0;i<B;i++) {
const double dv=(double)
_batch[i].value-mu; var+=dv*dv;}
272 const double sd=std::sqrt(var);
273 const bool degenerate=!(sd>1e-12);
274 for(IDXTYPE i=0;i<B;i++) {
277 const RETURNTYPE z=degenerate?(RETURNTYPE)0.0:(RETURNTYPE)(((
double)e.
value-mu)/sd);
279 const RETURNTYPE dinv=(e.
d>0)?(RETURNTYPE)(1.0/(double)e.
d):(RETURNTYPE)0.0;
280 for(DIMTYPE j=0;j<
_n;j++) {
302template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
311template<
class RETURNTYPE,
typename IDXTYPE,
typename DIMTYPE,
class SUBSET>
314 std::ostringstream sos;
315 if(
_n<1) {sos <<
"Result_Weighter_Sampled: nothing to print." << std::endl;
syncout::print(os,sos);
return false;}
316 std::vector<DIMTYPE> idx(
_n);
317 for(DIMTYPE f=0;f<
_n;f++) idx[f]=f;
318 const DIMTYPE k=(top>0 && top<
_n)?top:
_n;
319 std::partial_sort(idx.begin(),idx.begin()+k,idx.end(),
320 [
this](
const DIMTYPE a,
const DIMTYPE b){const RETURNTYPE sa=score(a),sb=score(b); return sa>sb || (sa==sb && a<b);});
321 sos <<
"Sampled-step feature stats (top " << k <<
" of " <<
_n <<
", evals=" <<
_total_evals <<
", batches=" <<
_flushed_batches <<
"):" << std::endl;
322 for(DIMTYPE i=0;i<k;i++) {
323 const DIMTYPE f=idx[i];
324 sos <<
" F:" << f <<
" score=" <<
score(f)
325 <<
" m_is=" <<
_fs[f].m_is <<
" m_isnot=" <<
_fs[f].m_isnot
326 <<
" n_is=" <<
_fs[f].n_is <<
" n_isnot=" <<
_fs[f].n_isnot << std::endl;
Online per-feature statistics for budgeted (sampled) sequential steps — discounted batch-standardized...
Definition result_weighter_sampled.hpp:128
IDXTYPE _flushed_batches
count of non-empty flush_batch() calls since construction/reset() (see flushed_batches())
Definition result_weighter_sampled.hpp:217
std::vector< BatchEntry > _batch
buffered, not-yet-flushed evaluations added since the last flush_batch()
Definition result_weighter_sampled.hpp:214
IDXTYPE _horizon
adaptive-rate horizon supplied at construction (clamped to at least 1)
Definition result_weighter_sampled.hpp:210
IDXTYPE count_isnot(const DIMTYPE f) const
how many evaluated subsets lacked f ("what happens without f" is the scarce observation for backward-...
Definition result_weighter_sampled.hpp:172
std::vector< FeatureStat > _fs
per-feature accumulators, indexed 0.._n-1
Definition result_weighter_sampled.hpp:213
bool print_stats(std::ostream &os, const DIMTYPE top=20) const
diagnostic listing of the top-scored features
Definition result_weighter_sampled.hpp:312
RETURNTYPE score(const DIMTYPE f) const
current sampling score of feature f (see class doc)
Definition result_weighter_sampled.hpp:303
IDXTYPE flushed_batches() const
number of non-empty flush_batch() calls processed since construction/reset() (a count of batches,...
Definition result_weighter_sampled.hpp:174
DIMTYPE get_n() const
current feature-space dimensionality (0 until the first add() or reset())
Definition result_weighter_sampled.hpp:151
IDXTYPE flush_batch()
z-score the buffered batch and fold it into the accumulators; returns the number of evaluations flush...
Definition result_weighter_sampled.hpp:259
StatsMode
Selects which statistic score(f) reports: STATS_ZCONTRAST (default) is the batch-standardized DAF-sty...
Definition result_weighter_sampled.hpp:136
StatsMode _mode
configured StatsMode, selects which statistic score() reports
Definition result_weighter_sampled.hpp:211
std::shared_ptr< SUBSET > PSubset
shared-ownership handle to the evaluated Subset passed into add()
Definition result_weighter_sampled.hpp:130
bool is_frozen() const
true once freeze() has been called (see freeze())
Definition result_weighter_sampled.hpp:168
std::ostream & print(std::ostream &os) const
Write a compact one-line summary (n, horizon, mode, frozen flag, total evaluations) to os....
Definition result_weighter_sampled.hpp:180
std::vector< char > _mark
flush_batch() scratch bitmap (size _n); marks one entry's selected features while folding it in,...
Definition result_weighter_sampled.hpp:215
IDXTYPE _total_evals
cumulative count of evaluations passed to add() since construction/reset() (see total_evals())
Definition result_weighter_sampled.hpp:216
bool add(const RETURNTYPE value, const PSubset sub)
Buffer one evaluated subset for the current batch (thread-safe; may be called from threaded-evaluator...
Definition result_weighter_sampled.hpp:240
std::mutex _add_mutex
guards _batch/_total_evals/_frozen so add() is safe to call from threaded-evaluator worker callbacks
Definition result_weighter_sampled.hpp:218
IDXTYPE total_evals() const
cumulative count of evaluations passed to add() since construction/reset(), including any still buffe...
Definition result_weighter_sampled.hpp:173
DIMTYPE _n
current feature-space dimensionality, set by reset() (called explicitly, or lazily by the first add()...
Definition result_weighter_sampled.hpp:209
void freeze()
Freezes the ranking: flushes any pending batch, then turns all further add()/flush_batch() calls into...
Definition result_weighter_sampled.hpp:167
StatsMode get_mode() const
the configured StatsMode (see StatsMode)
Definition result_weighter_sampled.hpp:152
IDXTYPE count_is(const DIMTYPE f) const
how many evaluated subsets contained f (exploration bookkeeping)
Definition result_weighter_sampled.hpp:171
Result_Weighter_Sampled(const IDXTYPE horizon=100, const StatsMode mode=STATS_ZCONTRAST)
Construct with the given adaptive-rate horizon and scoring mode; all statistics start at zero and are...
Definition result_weighter_sampled.hpp:142
IDXTYPE get_horizon() const
the (clamped, >=1) adaptive-rate horizon supplied at construction
Definition result_weighter_sampled.hpp:153
bool _frozen
set by freeze(); once true, add()/flush_batch() are no-ops and statistics stay frozen at their warm-u...
Definition result_weighter_sampled.hpp:212
void reset(const DIMTYPE n)
(Re)dimension to n features and zero all statistics, the pending batch and the counters....
Definition result_weighter_sampled.hpp:222
static void print(std::ostream &os, std::ostringstream &sos)
Writes sos's buffered content to os, serializing concurrent writers under FST_THREADED via a shared m...
Definition global.hpp:86
FST4 specific error passing class.
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
One buffered, not-yet-flushed criterion evaluation: its raw value, the cardinality of the evaluated s...
Definition result_weighter_sampled.hpp:204
std::vector< DIMTYPE > selected
indices of the subset's selected features
Definition result_weighter_sampled.hpp:207
DIMTYPE d
cardinality of the evaluated subset (caller contract: identical for every entry in one batch)
Definition result_weighter_sampled.hpp:206
RETURNTYPE value
the raw criterion value returned for this evaluated subset
Definition result_weighter_sampled.hpp:205
Per-feature accumulators, one instance per feature index, updated by flush_batch()....
Definition result_weighter_sampled.hpp:191
RETURNTYPE raw_cnt_is
matching 1/|S|-weighted normalizer for raw_sum_is
Definition result_weighter_sampled.hpp:196
IDXTYPE n_isnot
number of evaluations in which the feature was not selected (exploration count, see count_isnot())
Definition result_weighter_sampled.hpp:198
RETURNTYPE m_isnot
adaptive-rate mean of batch-z over evaluations where the feature was not selected (STATS_ZCONTRAST sc...
Definition result_weighter_sampled.hpp:193
RETURNTYPE raw_sum_is
1/|S|-weighted sum of raw criterion values over 'is' evaluations (STATS_RAWMEAN score source)
Definition result_weighter_sampled.hpp:195
RETURNTYPE m_is
adaptive-rate mean of batch-z over evaluations where the feature was selected (STATS_ZCONTRAST score ...
Definition result_weighter_sampled.hpp:192
IDXTYPE n_is
number of evaluations in which the feature was selected (exploration count, see count_is())
Definition result_weighter_sampled.hpp:197
RETURNTYPE v_is
adaptive-rate mean of z^2 on the 'is' side; maintained as a dispersion estimate but not read by score...
Definition result_weighter_sampled.hpp:194