Source

orange / source / orange / rulelearner.hpp

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
/*
    This file is part of Orange.
    
    Copyright 1996-2010 Faculty of Computer and Information Science, University of Ljubljana
    Contact: janez.demsar@fri.uni-lj.si

    Orange is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Orange is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Orange.  If not, see <http://www.gnu.org/licenses/>.
*/

#ifndef __RULES_HPP 
#define __RULES_HPP

#include "domain.hpp"
#include "classify.hpp"
#include "learn.hpp"

WRAPPER(ProgressCallback)
WRAPPER(Rule)
WRAPPER(Discretization)
WRAPPER(EVDist)

#define TRuleList TOrangeVector<PRule>
VWRAPPER(RuleList)
#define TEVDistList TOrangeVector<PEVDist>
VWRAPPER(EVDistList)


WRAPPER(ExampleGenerator)
WRAPPER(ExampleTable)
WRAPPER(Filter)

class ORANGE_API TRule : public TOrange {
public:
  __REGISTER_CLASS

  PFilter filter; //P stored filter for this rule
  PFilter valuesFilter; //P Filter_values representation of main filter (sometimes needed)
  PClassifier classifier; //P classifies an example
  PLearner learner; //P learns a classifier from data
  PRule parentRule; //P
  PDistribution baseDist; //P initial distribution

  PDistribution classDistribution; //P distribution of classes covered by the rule

  PExampleTable examples; //P covered examples
  int weightID; //P weight for the stored examples
  float quality; //P some measure of rule quality
  int complexity; //P
  float chi; //P 
  float estRF;
  float distP;
  int requiredConditions; //P conditions that are mandatory in rule - rule attribute significance avoids these

  int *coveredExamples;
  int coveredExamplesLength;
  
  TRule();
  TRule(PFilter filter, PClassifier classifier, PLearner lr, PDistribution dist, PExampleTable ce = PExampleTable(), const int &w = 0, const float &qu = -1);
  TRule(const TRule &other, bool copyData = true);
  ~TRule();

  bool operator()(const TExample &); //P Returns 1 for accept, 0 for reject
  PExampleTable operator()(PExampleTable, const bool ref = true, const bool negate = false); //P filter examples
    
  void filterAndStore(PExampleTable, const int &weightID = 0, const int &targetClass = -1, const int *prevCovered = NULL, const int anExamples = -1); //P Selects examples from given data
                                                                          // stores them in coveredExamples, computes distribution
                                                                          // and sets classValue (if -1 then take majority)
  bool operator >(const TRule &) const; 
  bool operator <(const TRule &) const;
  bool operator >=(const TRule &) const;
  bool operator <=(const TRule &) const;
  bool operator ==(const TRule &) const;

  bool operator >(const PRule &r) const
  { return operator >(r.getReference()); }

  bool operator <(const PRule &r) const
  { return operator <(r.getReference()); }

  bool operator >=(const PRule &r) const
  { return operator >=(r.getReference()); }

  bool operator <=(const PRule &r) const
  { return operator <=(r.getReference()); }

  bool operator ==(const PRule &r) const
  { return operator ==(r.getReference()); }

  // need string representation of a rule? 
};



WRAPPER(RuleValidator)
class ORANGE_API TRuleValidator : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual bool operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution apriori) const = 0;
};


class ORANGE_API TRuleValidator_LRS : public TRuleValidator {
public:
  __REGISTER_CLASS

  float alpha; //P
  float min_coverage; //P
  int max_rule_complexity; //P
  float min_quality; //P

  TRuleValidator_LRS(const float &alpha = 0.05, const float &min_coverage = 0.0, const int &max_rule_complexity = -1, const float &min_quality = -numeric_limits<float>::max());
  virtual bool operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution ) const;
};


WRAPPER(RuleEvaluator)
class ORANGE_API TRuleEvaluator : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual float operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution ) = 0;
};


class ORANGE_API TRuleEvaluator_Entropy : public TRuleEvaluator {
  __REGISTER_CLASS

  virtual float operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution );
};

class ORANGE_API TRuleEvaluator_Laplace : public TRuleEvaluator {
  __REGISTER_CLASS

  virtual float operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution );
};

class ORANGE_API TEVDist : public TOrange {
public:
  __REGISTER_CLASS

  float mu; //P mu of Fisher-Tippett distribution
  float beta; //P beta of Fisher-Tippett distribution
  PFloatList percentiles; //P usually 10 values - 0 = 5th percentile, 1 = 15th percentile, 9 = 95th percentile, change maxPercentile and step for other settings
  float maxPercentile; //P maxPercentile Value, default 0.95
  float step; //P step of percentiles, default 0.1

  TEVDist();
  TEVDist(const float &, const float &, PFloatList &);
  double getProb(const float & chi);
  float median();
};

WRAPPER(EVDistGetter)
class ORANGE_API TEVDistGetter: public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual PEVDist operator()(const PRule , const int & parentLength, const int & length) const = 0;
};

class ORANGE_API TEVDistGetter_Standard: public TEVDistGetter {
public:
  __REGISTER_CLASS

  PEVDistList dists; //P EVC distribution (sorted by rule length, 0 = for rules without conditions)
  TEVDistGetter_Standard();
  TEVDistGetter_Standard(PEVDistList);
  virtual PEVDist operator()(const PRule, const int & parentLength, const int & length) const;
};

class DiffFunc {
public:
  virtual double operator()(float) = 0;
};

class LNLNChiSq: public DiffFunc {
public:
  PEVDist evd;
  float chi, exponent, pp;
  double extremeAlpha;

  LNLNChiSq(PEVDist evd, const float & chi, const float & aprioriProb);
  double operator()(float chix);
};

class LRInv: public DiffFunc {
public:
  float n,P,N,chiCorrected;

  LRInv(float &, float &, float &, float chiCorrected);
  double operator()(float p);
};

class LRInvMean: public DiffFunc {
public:
  float p,n,P,N;

  LRInvMean(float, PRule, PRule, const int & targetClass);
  double operator()(float pc);
};


class LRInvE: public DiffFunc {
public:
  float n,p,N,chiCorrected;

  LRInvE(PRule, PRule, const int & targetClass, float chiCorrected);
  double operator()(float P);
};


class ORANGE_API TRuleEvaluator_mEVC: public TRuleEvaluator {
public:
  __REGISTER_CLASS

  float m; //P Parameter m for m-estimate after EVC correction
  PEVDistGetter evDistGetter; //P get EVC distribution for chi correction
  PVariable probVar;//P probability coverage variable (meta usually)
  PRuleValidator validator; //P rule validator for best rule
  int min_improved; //P minimal number of improved examples
  float min_improved_perc; //P minimal percentage of improved examples
  PRule bestRule; //P best rule found and evaluated given conditions (min_improved, validator)
  float ruleAlpha; //P minimal 'true' rule significance
  float attributeAlpha; //P minimal attribute significance
  bool returnExpectedProb; //P if true, evaluator returns expected class probability, if false, current class probability
  int optimismReduction; //P to select optimstic (0), pessimistic (1) or EVC (2) evaluation

  TRuleEvaluator_mEVC();
  TRuleEvaluator_mEVC(const int & m,  PEVDistGetter, PVariable, PRuleValidator, const int & min_improved, const float & min_improved_perc, const int & optimismReduction);
  void reset();
  bool ruleAttSignificant(PRule, PExampleTable, const int &, const int &targetClass, PDistribution, float &);
  float chiAsimetryCorrector(const float &);
  float evaluateRuleEVC(PRule rule, PExampleTable examples, const int & weightID, const int &targetClass, PDistribution apriori, const int & rLength, const float & aprioriProb);
  float evaluateRulePessimistic(PRule rule, PExampleTable examples, const int & weightID, const int &targetClass, PDistribution apriori, const int & rLength, const float & aprioriProb);
  float evaluateRuleM(PRule rule, PExampleTable examples, const int & weightID, const int &targetClass, PDistribution apriori, const int & rLength, const float & aprioriProb);
  float operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution );
};

class ORANGE_API TRuleEvaluator_LRS : public TRuleEvaluator {
public:
  __REGISTER_CLASS

  PRuleList rules; //P
  bool storeRules; //P

  TRuleEvaluator_LRS(const bool & = false);
  virtual float operator()(PRule, PExampleTable, const int &, const int &targetClass, PDistribution );
};

WRAPPER(RuleFinder)
class ORANGE_API TRuleFinder : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  PRuleValidator validator; //P
  PRuleEvaluator evaluator; //P

  virtual PRule operator()(PExampleTable, const int & =0, const int &targetClass = -1, PRuleList baseRules = PRuleList()) = 0;
};


WRAPPER(RuleBeamInitializer)
class ORANGE_API TRuleBeamInitializer : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual PRuleList operator()(PExampleTable, const int &weightID, const int &targetClass, PRuleList baseRules, PRuleEvaluator, PDistribution apriori, PRule &bestRule) = 0;
};


class ORANGE_API TRuleBeamInitializer_Default : public TRuleBeamInitializer {
public:
  __REGISTER_CLASS

  virtual PRuleList operator()(PExampleTable, const int &weightID, const int &targetClass, PRuleList baseRules, PRuleEvaluator, PDistribution apriori, PRule &bestRule);
};


WRAPPER(RuleBeamRefiner)
class ORANGE_API TRuleBeamRefiner : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual PRuleList operator()(PRule rule, PExampleTable, const int &weightID, const int &targetClass = -1) = 0;
};


class ORANGE_API TRuleBeamRefiner_Selector : public TRuleBeamRefiner {
public:
  __REGISTER_CLASS

  PDiscretization discretization; //P discretization for continuous attributes
  
  virtual PRuleList operator()(PRule rule, PExampleTable, const int &weightID, const int &targetClass = -1);
};


WRAPPER(RuleBeamCandidateSelector)
class ORANGE_API TRuleBeamCandidateSelector : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual PRuleList operator()(PRuleList &existingRules, PExampleTable, const int &weightID) = 0;
};


class ORANGE_API TRuleBeamCandidateSelector_TakeAll : public TRuleBeamCandidateSelector {
public:
  __REGISTER_CLASS

  virtual PRuleList operator()(PRuleList &existingRules, PExampleTable, const int &weightID);
};


WRAPPER(RuleBeamFilter)
class ORANGE_API TRuleBeamFilter : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual void operator()(PRuleList &existingRules, PExampleTable, const int &weightID) = 0;
};


class ORANGE_API TRuleBeamFilter_Width : public TRuleBeamFilter {
public:
  __REGISTER_CLASS

  int width; //P

  TRuleBeamFilter_Width(const int &w = 5);

  void operator()(PRuleList &rules, PExampleTable, const int &weightID);
};



class ORANGE_API TRuleBeamFinder : public TRuleFinder {
public:
  __REGISTER_CLASS

  PRuleBeamInitializer initializer; //P
  PRuleBeamRefiner refiner; //P
  PRuleBeamCandidateSelector candidateSelector; //P
  PRuleBeamFilter ruleFilter; //P
  PRuleValidator ruleStoppingValidator; //P
  
  PRule operator()(PExampleTable, const int & =0, const int &targetClass = -1, PRuleList baseRules = PRuleList());
};



WRAPPER(RuleDataStoppingCriteria)
class ORANGE_API TRuleDataStoppingCriteria : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual bool operator()(PExampleTable, const int &weightID, const int &targetClass) const = 0;
};


class ORANGE_API TRuleDataStoppingCriteria_NoPositives : public TRuleDataStoppingCriteria {
public:
  __REGISTER_CLASS

  virtual bool operator()(PExampleTable, const int &weightID, const int &targetClass) const;
};


WRAPPER(RuleStoppingCriteria)
class ORANGE_API TRuleStoppingCriteria : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual bool operator()(PRuleList, PRule, PExampleTable, const int &weightID) const = 0;
};

class ORANGE_API TRuleStoppingCriteria_NegativeDistribution : public TRuleStoppingCriteria {
public:
  __REGISTER_CLASS

  virtual bool operator()(PRuleList, PRule, PExampleTable, const int &weightID) const;
};

WRAPPER(RuleCovererAndRemover)
class ORANGE_API TRuleCovererAndRemover : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

   virtual PExampleTable operator()(PRule, PExampleTable, const int &weightID, int &newWeight, const int &targetClass) const = 0;
};


class ORANGE_API TRuleCovererAndRemover_Default : public TRuleCovererAndRemover {
public:
  __REGISTER_CLASS

  virtual PExampleTable operator()(PRule, PExampleTable, const int &weightID, int &newWeight, const int &targetClass) const;
};

WRAPPER(RuleClassifierConstructor)
WRAPPER(RuleClassifier)
class ORANGE_API TRuleClassifierConstructor : public TOrange {
public:
  __REGISTER_ABSTRACT_CLASS

  virtual PRuleClassifier operator()(PRuleList, PExampleTable, const int &weightID = 0) = 0;
};


class ORANGE_API TRuleClassifierConstructor_firstRule: public TRuleClassifierConstructor {
  __REGISTER_CLASS
  virtual PRuleClassifier operator()(PRuleList, PExampleTable, const int &weightID = 0);
};


WRAPPER(RuleLearner)
class ORANGE_API TRuleLearner : public TLearner {
public:
  __REGISTER_CLASS
  
  PRuleDataStoppingCriteria dataStopping; //P
  PRuleStoppingCriteria ruleStopping; //P
  PRuleCovererAndRemover coverAndRemove; //P
  PRuleFinder ruleFinder; //P
  PRuleClassifierConstructor classifierConstructor; //P classifier

  PProgressCallback progressCallback; //P progress callback function

  bool storeExamples; //P
  int targetClass; //P
  PRuleList baseRules; //P

  TRuleLearner(bool storeExamples = true, int targetClass = -1, PRuleList baseRules = PRuleList());

  PClassifier operator()(PExampleGenerator, const int & =0);
  PClassifier operator()(PExampleGenerator, const int &, const int &targetClass = -1, PRuleList baseRules = PRuleList());
};



class ORANGE_API TRuleClassifier : public TClassifier {
public:
  __REGISTER_ABSTRACT_CLASS

  PRuleList rules; //P
  PExampleTable examples; //P
  int weightID; //P

  TRuleClassifier();
  TRuleClassifier(PRuleList rules, PExampleTable examples, const int &weightID = 0);

  virtual PDistribution classDistribution(const TExample &ex) = 0;
};

// Zakaj moram se enkrat definirati konstruktor;
class ORANGE_API TRuleClassifier_firstRule : public TRuleClassifier {
public:
  __REGISTER_CLASS

  PDistribution prior; //P prior distribution

  TRuleClassifier_firstRule();
  TRuleClassifier_firstRule(PRuleList rules, PExampleTable examples, const int &weightID = 0);
  virtual PDistribution classDistribution(const TExample &ex);
};

WRAPPER(LogitClassifierState)
class ORANGE_API TLogitClassifierState : public TOrange {
public:
  __REGISTER_CLASS

  PRuleList rules; //P
  PExampleTable examples; //P
  int weightID;

  float eval, **f, **p, *betas, *priorBetas;
  bool *isExampleFixed;
  PFloatList avgProb; //P
  PFloatList avgPriorProb; //P
  PIntList *ruleIndices; //P
  PIntList prefixRules; //P

  TLogitClassifierState(PRuleList, PExampleTable, const int &);
  TLogitClassifierState(PRuleList,const PDistributionList &,PExampleTable,const int &);
  ~TLogitClassifierState();
  void updateExampleP(int);
  void computePs(int);
  void setFixed(int);
  void updateFixedPs(int);
  void setPrefixRule(int);
  void computeAvgProbs();
  void computePriorProbs();
  void copyTo(PLogitClassifierState &);
  void newBeta(int, float);
  void newPriorBeta(int, float);
  float getAUC();
  float getBrierScore();
};

class ORANGE_API TRuleClassifier_logit : public TRuleClassifier {
public:
  __REGISTER_CLASS

  PDistribution prior; //P prior distribution
  PDomain domain; //P Domain
  PFloatList ruleBetas; //P Rule betas
  float minSignificance; //P minimum requested significance for betas. 
  float minBeta; //P minimum beta by rule to be included in the model. 
  bool setPrefixRules; //P should we order prefix rules ? 
  bool optimizeBetasFlag; //P should we assign betas to rules ? 
  float penalty; //P

  PClassifier priorClassifier; //P prior classifier used if provided
  PLogitClassifierState currentState; //P
  bool *skipRule;
  // standard deviations of rule quality
  PFloatList wsd; //P
  PFloatList wavgCov; //P
  PFloatList wSatQ; //P
  PFloatList wsig; //P
  PRuleList prefixRules; //P rules that trigger before logit sum.

  TRuleClassifier_logit();
  TRuleClassifier_logit(PRuleList rules, const float &minSignificance, const float &minBeta, const float &penalty, PExampleTable examples, const int &weightID = 0, const PClassifier &classifer = NULL, const PDistributionList &probList = NULL, bool setPrefixRules = false, bool optimizeBetasFlag = true);

  void initialize(const PDistributionList &);
  void updateRuleBetas(float step);
  void optimizeBetas();
  void updateRuleBetas_old(float step_);
  bool setBestPrefixRule();
  void correctPriorBetas(float & step);
  void stabilizeAndEvaluate(float & step, int rule_index);
  float getRuleLoss(int &);
  
  void addPriorClassifier(const TExample &, double *);
  virtual PDistribution classDistribution(const TExample &ex);
};

#endif