source: trunk/yat/statistics/KolmogorovSmirnov.cc @ 1594

Last change on this file since 1594 was 1594, checked in by Peter, 13 years ago

removing debug code

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.4 KB
Line 
1// $Id: KolmogorovSmirnov.cc 1594 2008-10-21 16:49:42Z peter $
2
3/*
4  Copyright (C) 2008 Peter Johansson
5
6  This file is part of the yat library, http://dev.thep.lu.se/yat
7
8  The yat library is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License as
10  published by the Free Software Foundation; either version 3 of the
11  License, or (at your option) any later version.
12
13  The yat library is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with yat. If not, see <http://www.gnu.org/licenses/>.
20*/
21
22#include "KolmogorovSmirnov.h"
23#include "yat/random/random.h"
24#include "yat/utility/stl_utility.h"
25
26#include <algorithm>
27#include <cassert>
28#include <cmath>
29#include <deque>
30#include <functional>
31
32namespace theplu {
33namespace yat {
34namespace statistics {
35
36
37  KolmogorovSmirnov::KolmogorovSmirnov(void)
38    : cached_(true), score_(0.0), sum_w1_(0.0), sum_w2_(0.0)
39  {
40  }
41
42
43  void KolmogorovSmirnov::add(double x, bool target, double w)
44  {
45    if (w==0) // ignoring zero weight data
46      return;
47
48    data_.insert(std::make_pair(std::make_pair(x,w),target));
49    if (target){
50      sum_w1_+=w;
51    }
52    else {
53      sum_w2_+=w;
54    }
55    cached_=false;
56  }
57
58
59  double KolmogorovSmirnov::p_value(void) const
60  {
61    double res=0;
62    double res2=0;
63    double s2 = score() * sqrt(sum_w1_*sum_w2_/(sum_w1_+sum_w2_));
64    s2 *= s2;
65    int sign = 1;
66    for (size_t k = 1; k<100; ++k) {
67      res += sign * exp(-2.0 * k * k * s2);
68      sign *= -1;
69      if (res==res2)
70        return res;
71      res2 = res;
72    }
73
74    return res;
75  }
76
77
78  double KolmogorovSmirnov::p_value(size_t perm) const
79  {
80    size_t count=0;
81    std::deque<bool> targets;
82    typedef data_w::const_iterator const_iter;
83    for (const_iter i(data_.begin()); i!=data_.end(); ++i)
84      targets.push_back(i->second);
85
86    for (size_t i=0; i<perm; ++i){
87      random::random_shuffle(targets.begin(), targets.end()); 
88      KolmogorovSmirnov ks;
89      std::deque<bool>::const_iterator target_i(targets.begin());
90      const_iter end(data_.end());
91      for (const_iter i(data_.begin()); i!=end; ++i, ++target_i) 
92        ks.add(i->first.first, *target_i, i->first.second);
93     
94      if (ks.score()>=score())
95        ++count;
96    }
97    return static_cast<double>(count)/perm;
98  }
99
100
101  double KolmogorovSmirnov::score(void) const
102  {
103    if (cached_)
104      return score_;
105    if (!sum_w1_ || !sum_w2_)
106      return 0.0;
107    score_=0;
108    std::vector<double> v;
109    scores(v);
110    std::less<double> l;
111    utility::abs<double> a;
112    using utility::make_compose_f_gx_hy;
113    score_ = std::abs(*std::max_element(v.begin(), v.end(), 
114                                        make_compose_f_gx_hy(l, a, a)));
115    return score_;
116  }
117
118
119  void KolmogorovSmirnov::scores(std::vector<double>& res) const
120  {
121    res.clear();
122    res.reserve(data_.size());
123    data_w::const_iterator iter(data_.begin());
124    double f1=0;
125    double f2=0;
126    // Peter, we should take care of ties!
127    while(iter!=data_.end()){
128      if (iter->second)
129        f1 += iter->first.second;
130      else
131        f2 += iter->first.second;
132      res.push_back(f1/sum_w1_-f2/sum_w2_);
133      ++iter;
134    }
135  }
136
137
138  void KolmogorovSmirnov::reset(void)
139  {
140    cached_=false;
141    sum_w1_=0;
142    sum_w2_=0;
143  }
144
145
146  std::ostream& operator<<(std::ostream& os, const KolmogorovSmirnov& ks)
147  {
148    std::vector<double> s;
149    ks.scores(s);
150    for (size_t i=0; i<s.size(); ++i)
151      os << i << "\t" << s[i] << "\n";
152    return os;
153  }
154
155
156}}} // of namespace theplu yat statistics
Note: See TracBrowser for help on using the repository browser.