orange / source / orange / distance.cpp

  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
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
/*
    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/>.
*/


#include <math.h>
#include "stladdon.hpp"

#include "vars.hpp"
#include "domain.hpp"
#include "examples.hpp"
#include "examplegen.hpp"
#include "distvars.hpp"
#include "contingency.hpp"
#include "basstat.hpp"
#include "orvector.hpp"

#include "distance.ppp"


TExamplesDistanceConstructor::TExamplesDistanceConstructor(const bool &ic)
: ignoreClass(ic)
{}


TExamplesDistanceConstructor_Hamming::TExamplesDistanceConstructor_Hamming()
: ignoreClass(true),
  ignoreUnknowns(false)
{}


PExamplesDistance TExamplesDistanceConstructor_Hamming::operator()(PExampleGenerator, const int &, PDomainDistributions, PDomainBasicAttrStat) const
{ return mlnew TExamplesDistance_Hamming(ignoreClass, ignoreUnknowns); }


TExamplesDistance_Hamming::TExamplesDistance_Hamming(const bool &ic, const bool &iu)
: ignoreClass(ic),
  ignoreUnknowns(iu)
{}


float TExamplesDistance_Hamming::operator()(const TExample &e1, const TExample &e2) const 
{ if (   (e1.domain != e2.domain)
      && (ignoreClass ? e1.domain->attributes != e2.domain->attributes
                     : e1.domain->variables != e2.domain->variables))
    raiseError("cannot compare examples from different domains");

  float dist = 0.0;
  int Na = e1.domain->attributes->size() + (!ignoreClass && e1.domain->classVar ? 1 : 0);
  for(TExample::const_iterator i1 = e1.begin(), i2 = e2.begin(); Na--; i1++, i2++)
    if (   (!ignoreUnknowns || !(*i1).isSpecial() && !(*i2).isSpecial())
        && (!(*i1).compatible(*i2)))
      dist += 1.0;
  return dist;
}


TExamplesDistanceConstructor_Normalized::TExamplesDistanceConstructor_Normalized()
: TExamplesDistanceConstructor(),
  normalize(true),
  ignoreUnknowns(false)
{}


TExamplesDistanceConstructor_Normalized::TExamplesDistanceConstructor_Normalized(const bool &ic, const bool &no, const bool &iu)
: TExamplesDistanceConstructor(ic),
  normalize(no),
  ignoreUnknowns(iu)
{}


TExamplesDistance_Normalized::TExamplesDistance_Normalized()
: normalizers(PFloatList()),
  domainVersion(-1),
  normalize(true),
  ignoreUnknowns(false)
{}


TExamplesDistance_Normalized::TExamplesDistance_Normalized(const bool &ignoreClass, const bool &no, const bool &iu, PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat)
: normalizers(mlnew TAttributedFloatList()),
  bases(mlnew TAttributedFloatList()),
  averages(mlnew TAttributedFloatList()),
  variances(mlnew TAttributedFloatList()),
  domainVersion(egen ? egen->domain->version : -1),
  normalize(no),
  ignoreUnknowns(iu)
{ TFloatList &unormalizers = normalizers.getReference();

  PVarList varlist;

  if (!bstat && !ddist && egen)
    bstat = mlnew TDomainBasicAttrStat(egen, weightID);

  if (bstat && egen) {
    varlist  = ignoreClass ? egen->domain->attributes : egen->domain->variables;

    TDomainBasicAttrStat::const_iterator si(bstat->begin()), ei(bstat->end());
    TVarList::const_iterator vi (egen->domain->variables->begin()), evi(egen->domain->variables->end());

    if (ignoreClass && egen->domain->classVar) {
      evi--;
      ei--;
    }

    for(; (vi!=evi) && (si!=ei); si++, vi++) {
      if ((*vi)->varType==TValue::FLOATVAR) {
        if (*si && ((*si)->n>0)) {
          normalizers->push_back((*si)->max!=(*si)->min ? 1.0/((*si)->max-(*si)->min) : 0.0);
          bases->push_back((*si)->min);
          averages->push_back((*si)->avg);
          variances->push_back((*si)->dev * (*si)->dev);
        }
        else {
          normalizers->push_back(0.0);
          bases->push_back(0.0);
          averages->push_back(0.0);
          variances->push_back(0.0);
        }
      }
      else if ((*vi)->varType==TValue::INTVAR) {
        if ((*vi)->ordered)
          if ((*vi)->noOfValues()>0)
            normalizers->push_back(1.0/(*vi)->noOfValues());
          else
            normalizers->push_back(0.0);
        else
          normalizers->push_back(-1.0);
        bases->push_back(0.0);
        averages->push_back(0.0);
        variances->push_back(0.0);
      }
      else {
        normalizers->push_back(0.0);
        bases->push_back(0.0);
        averages->push_back(0.0);
        variances->push_back(0.0);
      }
    }

    if ((vi!=evi) || (si!=ei))
      raiseError("lengths of domain and basic attribute statistics do not match");
  }

  else if (ddist) {
    varlist = mlnew TVarList;

    PITERATE(TDomainDistributions, ci, ddist) {
      if (*ci) {
        const PVariable &vi = (*ci)->variable;
        varlist->push_back(vi);

        if (vi->varType==TValue::FLOATVAR) {
          TContDistribution *dcont = (*ci).AS(TContDistribution);
          if (dcont && (dcont->begin() != dcont->end())) {
            const float min = (*dcont->distribution.begin()).first;
            const float dif = (*dcont->distribution.rbegin()).first - min;
            normalizers->push_back(dif > 0.0 ? 1.0/dif : 0.0);
            bases->push_back(min);
            averages->push_back(dcont->average());
            variances->push_back(dcont->var());
          }
          else {
            normalizers->push_back(0.0);
            averages->push_back(0.0);
            variances->push_back(0.0);
          }
        }
        else if (vi->varType==TValue::INTVAR) {
          if (vi->ordered) {
            const int nval = vi->noOfValues();
            normalizers->push_back(nval ? 1.0/float(nval) : 0.0);
          }
          else
            normalizers->push_back(-1.0);
            bases->push_back(0.0);
            averages->push_back(0.0);
            variances->push_back(0.0);
        }
        else {
          normalizers->push_back(0.0);
          bases->push_back(0.0);
          averages->push_back(0.0);
          variances->push_back(0.0);
        }
      }
      else {
        normalizers->push_back(0.0);
        bases->push_back(0.0);
        averages->push_back(0.0);
        variances->push_back(0.0);
      }
    }
  }

  else if (bstat) {
    varlist = mlnew TVarList;

    TDomainBasicAttrStat::const_iterator si(bstat->begin()), ei(bstat->end());

    if (ignoreClass && bstat->hasClassVar && (si != ei)) // can't check it, but suppose there is a class attribute
      ei--;

    for(; si!=ei; si++) {
      if (!*si)
        raiseError("cannot compute normalizers from BasicAttrStat in presence of non-continuous attributes");

      varlist->push_back((*si)->variable);
      if (((*si)->n>0) && ((*si)->max!=(*si)->min)) {
        normalizers->push_back(1.0/((*si)->max-(*si)->min));
        bases->push_back((*si)->min);
        averages->push_back((*si)->avg);
        variances->push_back((*si)->dev * (*si)->dev);
      }
      else {
        normalizers->push_back(0.0);
        bases->push_back(0.0);
        averages->push_back(0.0);
        variances->push_back(0.0);
      }
    }
  }

  else
   raiseError("no data");

  normalizers->attributes = bases->attributes = averages->attributes = variances->attributes = varlist;
}


/* Returns a vector of normalized differences between the two examples.
   Quick checks do not guarantee that domains are really same to the training domain.
   To be really on the safe side, we should know the domain and convert both examples. Too slow...
*/
void TExamplesDistance_Normalized::getDifs(const TExample &e1, const TExample &e2, vector<float> &difs) const
{ checkProperty(normalizers);

  if (   (e1.domain != e2.domain)
      && (e1.domain->variables != e2.domain->variables))
    raiseError("examples are from different domains");
  
/*  if (domainVersion>=0
        ? (domainVersion != e1.domain->version)
        : ((normalizers->size() > e1.domain->variables->size()) || (normalizers->size()< e1.domain->attributes->size())))
    raiseError("examples are from a wrong domain");*/

  difs = vector<float>(normalizers->size(), 0.0);
  vector<float>::iterator di(difs.begin());

  TExample::const_iterator i1(e1.begin()), i2(e2.begin());
  for(TFloatList::const_iterator si(normalizers->begin()), se(normalizers->end()); si!=se; si++, i1++, i2++, di++)
    if ((*i1).isSpecial() || (*i2).isSpecial())
      *di = ((*si!=0) && !ignoreUnknowns) ? 0.5 : 0.0;
    else 
      if (normalize) {
        if (*si>0) {
          if ((*i1).varType == TValue::FLOATVAR)
            *di = *si * fabs((*i1).floatV - (*i2).floatV);
          else if ((*i1).varType == TValue::INTVAR)
            *di = *si * fabs(float((*i1).intV - (*i2).intV));
        }
        else if (*si<0)
          *di = (*i1).compatible(*i2) ? 0.0 : 1.0;
      }
      else {
        if ((*i1).varType == TValue::FLOATVAR) {
          *di = fabs((*i1).floatV - (*i2).floatV);
        }
        else 
          if (*si>0) {
            if ((*i1).varType == TValue::INTVAR)
              *di = fabs(float((*i1).intV - (*i2).intV));
            else
              *di = (*i1).compatible(*i2) ? 0.0 : 1.0;
          }
    }
}



void TExamplesDistance_Normalized::getNormalized(const TExample &e1, vector<float> &normalized) const
{
  checkProperty(normalizers);
  checkProperty(bases);

  if (  domainVersion>=0
        ? (domainVersion != e1.domain->version)
        : ((normalizers->size() > e1.domain->variables->size()) || (normalizers->size()< e1.domain->attributes->size())))
    raiseError("example is from a wrong domain");

  normalized.clear();
  TExample::const_iterator ei(e1.begin());
  for(TFloatList::const_iterator normi(normalizers->begin()), norme(normalizers->end()), basi(bases->begin()); normi!=norme; ei++, normi++, basi++) {
// changed by PJ
/*
    if ((*ei).isSpecial())
      normalized.push_back(numeric_limits<float>::quiet_NaN());
    else
      if ((*normi>0) && ((*ei).varType == TValue::FLOATVAR))
        normalized.push_back(normalize ? ((*ei).floatV - *basi) / *normi : (*ei).floatV);
      else
        normalized.push_back(-1.0);
*/
	if ((*ei).isSpecial() || ei->varType != TValue::FLOATVAR)
		normalized.push_back(numeric_limits<float>::signaling_NaN());
    else
      if (*normi>0 && normalize)
        normalized.push_back(((*ei).floatV - *basi) * (*normi));
      else
        normalized.push_back((*ei).floatV);
  }
}


/*TExamplesDistanceConstructor_Maximal::TExamplesDistanceConstructor_Maximal()
{}
*/

TExamplesDistanceConstructor_Euclidean::TExamplesDistanceConstructor_Euclidean()
{}


TExamplesDistanceConstructor_Manhattan::TExamplesDistanceConstructor_Manhattan()
{}

TExamplesDistanceConstructor_Lp::TExamplesDistanceConstructor_Lp(): p(1.0)
{}

PExamplesDistance TExamplesDistanceConstructor_Maximal::operator()(PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat) const
{ return mlnew TExamplesDistance_Maximal(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, bstat); }


PExamplesDistance TExamplesDistanceConstructor_Manhattan::operator()(PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat) const
{ return mlnew TExamplesDistance_Manhattan(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, bstat); }


PExamplesDistance TExamplesDistanceConstructor_Euclidean::operator()(PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat) const
{ return mlnew TExamplesDistance_Euclidean(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, bstat); }

PExamplesDistance TExamplesDistanceConstructor_Lp::operator()(PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat) const
{ return mlnew TExamplesDistance_Lp(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, bstat, p); }

TExamplesDistance_Maximal::TExamplesDistance_Maximal()
{}


TExamplesDistance_Manhattan::TExamplesDistance_Manhattan()
{}


TExamplesDistance_Euclidean::TExamplesDistance_Euclidean()
{}



TExamplesDistance_Maximal::TExamplesDistance_Maximal(const bool &ignoreClass, const bool &normalize, const bool &ignoreUnknowns, PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat dstat)
: TExamplesDistance_Normalized(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, dstat)
{}


TExamplesDistance_Manhattan::TExamplesDistance_Manhattan(const bool &ignoreClass, const bool &normalize, const bool &ignoreUnknowns, PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat dstat)
: TExamplesDistance_Normalized(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, dstat)
{}


TExamplesDistance_Euclidean::TExamplesDistance_Euclidean(const bool &ignoreClass, const bool &normalize, const bool &ignoreUnknowns, PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat dstat)
: TExamplesDistance_Normalized(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, dstat),
  distributions(mlnew TDomainDistributions(egen, weightID, false, true)),
  bothSpecialDist(mlnew TAttributedFloatList())
{
  bothSpecialDist->attributes = averages->attributes;

  PITERATE(TDomainDistributions, di, distributions) {
    if (*di) {
      float sum2 = 0;
      TDiscDistribution *distr = (*di).AS(TDiscDistribution);
      ITERATE(vector<float>, pi, distr->distribution)
        sum2 += (*pi) * (*pi);
      if (distr->abs > 1e-6) {
        sum2 /= distr->abs * distr->abs;
        bothSpecialDist->push_back(1-sum2);
      }
      else {
        bothSpecialDist->push_back(0.0);
      }
    }
    else
      bothSpecialDist->push_back(0.0);
  }
}

TExamplesDistance_Lp::TExamplesDistance_Lp()
{
	this->p = 1.0;
}
TExamplesDistance_Lp::TExamplesDistance_Lp(float p)
{
	this->p = p;
}

TExamplesDistance_Lp::TExamplesDistance_Lp(const bool &ignoreClass, const bool &normalize, const bool &ignoreUnknowns, PExampleGenerator egen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat dstat, float _p)
: TExamplesDistance_Normalized(ignoreClass, normalize, ignoreUnknowns, egen, weightID, ddist, dstat), p(_p)
{}

float TExamplesDistance_Maximal::operator ()(const TExample &e1, const TExample &e2) const 
{ 
  vector<float> difs;
  getDifs(e1, e2, difs);
  return difs.size() ? *max_element(difs.begin(), difs.end()) : 0.0;
}


float TExamplesDistance_Manhattan::operator ()(const TExample &e1, const TExample &e2) const 
{ 
  vector<float> difs;
  getDifs(e1, e2, difs);
  float dist = 0.0;
  const_ITERATE(vector<float>, di, difs)
    dist += *di;
  return dist;
}


float TExamplesDistance_Euclidean::operator ()(const TExample &e1, const TExample &e2) const 
{ 
  vector<float> difs;
  getDifs(e1, e2, difs);
  float dist = 0.0;
  TExample::const_iterator e1i(e1.begin()), e2i(e2.begin());
  TFloatList::const_iterator avgi(averages->begin()), vari(variances->begin());
  vector<float>::const_iterator di(difs.begin()), de(difs.end());
  TDomainDistributions::const_iterator disti(distributions->begin());
  TFloatList::const_iterator bsi(bothSpecialDist->begin());
  TFloatList::const_iterator si(normalizers->begin());

  for(; di!=de; di++, e1i++, e2i++, avgi++, vari++, disti++, si++)
    if ((*e1i).varType == TValue::FLOATVAR) {
      if ((*e1i).isSpecial())
        if ((*e2i).isSpecial())
          dist += 2 * *vari;
        else {
          const float e2a = (*e2i).floatV - *avgi;
          if (normalize)
            dist += e2a*e2a + *vari * *si * *si;
          else
            dist += e2a*e2a + *vari;
        }
      else // e1i is not special
        if ((*e2i).isSpecial()) {
          const float e2a = (*e1i).floatV - *avgi;
          if (normalize)
            dist += e2a*e2a + *vari * *si * *si;
          else
            dist += e2a*e2a + *vari;
        }
      else // none is special
        dist += (*di) * (*di);
    }

    else if ((*e1i).varType == TValue::INTVAR) {
      if ((*e1i).isSpecial())
        if ((*e2i).isSpecial())
          dist += *bsi;
        else
          dist += 1 - (*disti)->p((*e2i).intV);
      else // e1i is not special
        if ((*e2i).isSpecial())
          dist += 1 - (*disti)->p((*e1i).intV);
        else
          if ((*e1i).intV != (*e2i).intV)
            dist += 1;
    }
    else
      dist += (*di)*(*di);

  return sqrt(dist);
}


float TExamplesDistance_Lp::operator ()(const TExample &e1, const TExample &e2) const
{
  vector<float> difs;
  getDifs(e1, e2, difs);
  float dist = 0.0;
  const_ITERATE(vector<float>, di, difs)
    dist += pow(fabs(*di), p);
  return pow(dist, 1.0f / p);
}


TExamplesDistanceConstructor_Relief::TExamplesDistanceConstructor_Relief()
{}


PExamplesDistance TExamplesDistanceConstructor_Relief::operator()(PExampleGenerator gen, const int &weightID, PDomainDistributions ddist, PDomainBasicAttrStat bstat) const
{ 
  const TDomain &domain = gen->domain.getReference();

  PVariable otherAttribute = domain.hasOtherAttributes();
  if (otherAttribute)
    raiseError("domain has attributes whose type is not supported by ReliefF (e.g. '%s')", otherAttribute->get_name().c_str());

  // for continuous attributes BasicAttrStat suffices; for discrete it does not
  const bool hasDiscrete = domain.hasDiscreteAttributes() || domain.classVar && (domain.classVar->varType == TValue::INTVAR);
  if (!bstat || (hasDiscrete && !ddist))
    if (!gen)
      raiseError("examples or domain distributions expected");
    else
      if (hasDiscrete)
        ddist = mlnew TDomainDistributions(gen, weightID);
      else
        bstat = mlnew TDomainBasicAttrStat(gen, weightID);

  TExamplesDistance_Relief *edr = mlnew TExamplesDistance_Relief();
  PExamplesDistance res = edr;

  if (!ignoreClass)
    raiseError("'ignoreClass' not supported");

  edr->averages       = mlnew TAttributedFloatList(gen->domain->attributes);
  edr->normalizations = mlnew TAttributedFloatList(gen->domain->attributes);
  edr->bothSpecial    = mlnew TAttributedFloatList(gen->domain->attributes);

  edr->distributions = CLONE(TDomainDistributions, ddist);
  if (ddist)
    edr->distributions->normalize();
  
  for(int attrIndex = 0, nAttrs = gen->domain->variables->size(); attrIndex != nAttrs; attrIndex++)
    if (domain.variables->at(attrIndex)->varType == TValue::FLOATVAR) {
      if (bstat) {
        const TBasicAttrStat &bas = bstat->at(attrIndex).getReference();
        edr->averages->push_back(bas.avg);
        edr->normalizations->push_back(bas.max - bas.min);
      }
      else {
        const TContDistribution *contd = ddist->at(attrIndex).AS(TContDistribution);
        if (contd->size()) {
          edr->averages->push_back(contd->average());
          edr->normalizations->push_back((*contd->distribution.rbegin()).first - (*contd->distribution.begin()).first);
        }
        else {
          edr->averages->push_back(0.0);
          edr->normalizations->push_back(1.0);
        }
      }
      edr->bothSpecial->push_back(0.5);
    }
  else {
    edr->averages->push_back(0.0);
    edr->normalizations->push_back(0.0);
    float dist = 1.0;
    const_PITERATE(TDiscDistribution, di, ddist->at(attrIndex).AS(TDiscDistribution))
      dist -= *di * *di;
    edr->bothSpecial->push_back(dist);
  }

  return res;
}


float TExamplesDistance_Relief::operator()(const TExample &e1, const TExample &e2) const
{ 
  checkProperty(averages);
  checkProperty(normalizations);
  checkProperty(bothSpecial);

  const bool hasDistributions = bool(distributions);

  TExample::const_iterator e1i(e1.begin()), e1end((e1.domain->classVar)? e1.end() - 1: e1.end());
  TExample::const_iterator e2i(e2.begin());
  TFloatList::const_iterator avgi(averages->begin()), avge(averages->end()),
                             nori(normalizations->begin()),
                             btsi(bothSpecial->begin());

  TDomainDistributions::const_iterator di;
  if (hasDistributions)
    di = distributions->begin();

  float dist = 0.0;
  for(; e1i!=e1end; e1i++, e2i++, avgi++, nori++, btsi++) {
    float dd = 0.0;
    const TValue &v1 = *e1i, &v2 = *e2i;
    if (v1.varType==TValue::INTVAR) {             // discrete
      if (v1.isSpecial())
        if (v2.isSpecial()) 
          dd = *btsi;                               // both special
        else {
          if (!hasDistributions)
            raiseError("'distributions' not set; cannot deal with unknown values");
          dd = 1-(*di)->atint(v2.intV);        // v1 special
        }
      else
        if (v2.isSpecial()) {
          if (!hasDistributions)
            raiseError("'distributions' not set; cannot deal with unknown values");
          dd = 1-(*di)->atint(v1.intV);        // v2 special
        }
        else
          if (v1.intV != v2.intV)
            dd = 1.0;                               // both known, different
    }
    else if (*nori>0) {                           // continuous, and can normalize
      if (v1.isSpecial())
        if (v2.isSpecial()) 
          dd = float(0.5);                          // both special
        else
          dd = fabs(*avgi - v2.floatV) / *nori;      // v1 special
      else
        if (v2.isSpecial())
          dd = fabs(*avgi - v1.floatV) / *nori;      // v2 special
        else
          dd = fabs(v1.floatV - v2.floatV) / *nori;  // both known
    }

    dist += dd>1.0 ? 1.0 : dd;

    if (hasDistributions)
      di++;
  }

  return dist;
}


float TExamplesDistance_Relief::operator()(const int &attrNo, const TValue &v1, const TValue &v2) const
{
  float dd = -1.0;
  if (v1.varType==TValue::INTVAR) {                              // discrete
    if (v1.isSpecial())
      if (v2.isSpecial()) 
        dd = bothSpecial->at(attrNo);                                // both special
        else 
          dd = 1 - distributions->at(attrNo)->atint(v2.intV);   // v1 special
      else
        if (v2.isSpecial())  
          dd = 1 - distributions->at(attrNo)->atint(v1.intV);   // v2 special
        else
          dd = (v1.intV != v2.intV) ? 1.0 : 0.0;                     // both known
    }
    else if (normalizations->at(attrNo)>0) {                     // continuous, and can normalize
      if (v1.isSpecial())
        if (v2.isSpecial()) 
          dd = 0.5;                                                                  // both special
        else
          dd = fabs(averages->at(attrNo) - v2.floatV) / normalizations->at(attrNo);   // v1 special
      else
        if (v2.isSpecial())
          dd = fabs(averages->at(attrNo) - v1.floatV) / normalizations->at(attrNo);   // v2 special
        else
          dd = fabs(v1.floatV - v2.floatV) / normalizations->at(attrNo);              // both known
    }

    return dd>1.0 ? 1.0 : dd;
}
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.