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

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

fixes #452

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.5 KB
Line 
1// $Id: KolmogorovSmirnov.cc 1593 2008-10-21 16:48:24Z 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
32
33#include <iostream>
34
35namespace theplu {
36namespace yat {
37namespace statistics {
38
39
40  KolmogorovSmirnov::KolmogorovSmirnov(void)
41    : cached_(true), score_(0.0), sum_w1_(0.0), sum_w2_(0.0)
42  {
43  }
44
45
46  void KolmogorovSmirnov::add(double x, bool target, double w)
47  {
48    if (w==0) // ignoring zero weight data
49      return;
50
51    data_.insert(std::make_pair(std::make_pair(x,w),target));
52    if (target){
53      sum_w1_+=w;
54    }
55    else {
56      sum_w2_+=w;
57    }
58    cached_=false;
59  }
60
61
62  double KolmogorovSmirnov::p_value(void) const
63  {
64    double res=0;
65    double res2=0;
66    double s2 = score() * sqrt(sum_w1_*sum_w2_/(sum_w1_+sum_w2_));
67    s2 *= s2;
68    int sign = 1;
69    for (size_t k = 1; k<100; ++k) {
70      res += sign * exp(-2.0 * k * k * s2);
71      sign *= -1;
72      if (res==res2)
73        return res;
74      res2 = res;
75    }
76
77    return res;
78  }
79
80
81  double KolmogorovSmirnov::p_value(size_t perm) const
82  {
83    size_t count=0;
84    std::deque<bool> targets;
85    typedef data_w::const_iterator const_iter;
86    for (const_iter i(data_.begin()); i!=data_.end(); ++i)
87      targets.push_back(i->second);
88
89    for (size_t i=0; i<perm; ++i){
90      random::random_shuffle(targets.begin(), targets.end()); 
91      KolmogorovSmirnov ks;
92      std::deque<bool>::const_iterator target_i(targets.begin());
93      const_iter end(data_.end());
94      for (const_iter i(data_.begin()); i!=end; ++i, ++target_i) 
95        ks.add(i->first.first, *target_i, i->first.second);
96     
97      if (ks.score()>=score())
98        ++count;
99    }
100    return static_cast<double>(count)/perm;
101  }
102
103
104  double KolmogorovSmirnov::score(void) const
105  {
106    if (cached_)
107      return score_;
108    if (!sum_w1_ || !sum_w2_)
109      return 0.0;
110    score_=0;
111    std::vector<double> v;
112    scores(v);
113    std::less<double> l;
114    utility::abs<double> a;
115    using utility::make_compose_f_gx_hy;
116    score_ = std::abs(*std::max_element(v.begin(), v.end(), 
117                                        make_compose_f_gx_hy(l, a, a)));
118    return score_;
119  }
120
121
122  void KolmogorovSmirnov::scores(std::vector<double>& res) const
123  {
124    res.clear();
125    res.reserve(data_.size());
126    data_w::const_iterator iter(data_.begin());
127    double f1=0;
128    double f2=0;
129    // Peter, we should take care of ties!
130    while(iter!=data_.end()){
131      if (iter->second)
132        f1 += iter->first.second;
133      else
134        f2 += iter->first.second;
135      res.push_back(f1/sum_w1_-f2/sum_w2_);
136      ++iter;
137    }
138  }
139
140
141  void KolmogorovSmirnov::reset(void)
142  {
143    cached_=false;
144    sum_w1_=0;
145    sum_w2_=0;
146  }
147
148
149  std::ostream& operator<<(std::ostream& os, const KolmogorovSmirnov& ks)
150  {
151    std::vector<double> s;
152    ks.scores(s);
153    for (size_t i=0; i<s.size(); ++i)
154      os << i << "\t" << s[i] << "\n";
155    return os;
156  }
157
158
159}}} // of namespace theplu yat statistics
Note: See TracBrowser for help on using the repository browser.