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

Last change on this file since 1486 was 1486, checked in by Jari Häkkinen, 13 years ago

Addresses #436.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: KolmogorovSmirnov.cc 1486 2008-09-09 21:17:19Z jari $
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 this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA.
22*/
23
24#include "KolmogorovSmirnov.h"
25#include "yat/random/random.h"
26#include "yat/utility/stl_utility.h"
27
28#include <algorithm>
29#include <cassert>
30#include <cmath>
31#include <deque>
32#include <functional>
33
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      ++n1_;
54      sum_w1_+=w;
55    }
56    else {
57      ++n2_;
58      sum_w2_+=w;
59    }
60    cached_=false;
61  }
62
63
64  double KolmogorovSmirnov::p_value(size_t perm) const
65  {
66    size_t count=0;
67    std::deque<bool> targets;
68    typedef data_w::const_iterator const_iter;
69    for (const_iter i(data_.begin()); i!=data_.end(); ++i)
70      targets.push_back(i->second);
71
72    for (size_t i=0; i<perm; ++i){
73      random::random_shuffle(targets.begin(), targets.end()); 
74      KolmogorovSmirnov ks;
75      std::deque<bool>::const_iterator target_i(targets.begin());
76      const_iter end(data_.end());
77      for (const_iter i(data_.begin()); i!=end; ++i, ++target_i) 
78        ks.add(i->first.first, *target_i, i->first.second);
79     
80      if (ks.score()>=score())
81        ++count;
82    }
83    return static_cast<double>(count)/perm;
84  }
85
86
87  double KolmogorovSmirnov::score(void) const
88  {
89    if (cached_)
90      return score_;
91    if (!sum_w1_ || !sum_w2_)
92      return 0.0;
93    score_=0;
94    std::vector<double> v;
95    scores(v);
96    std::less<double> l;
97    utility::abs<double> a;
98    using utility::make_compose_f_gx_hy;
99    score_ = std::abs(*std::max_element(v.begin(), v.end(), 
100                                        make_compose_f_gx_hy(l, a, a)));
101    return score_;
102  }
103
104
105  void KolmogorovSmirnov::scores(std::vector<double>& res) const
106  {
107    res.clear();
108    res.reserve(data_.size());
109    data_w::const_iterator iter(data_.begin());
110    double f1=0;
111    double f2=0;
112    // Peter, we should take care of ties!
113    while(iter!=data_.end()){
114      if (iter->second)
115        f1 += iter->first.second;
116      else
117        f2 += iter->first.second;
118      res.push_back(f1/sum_w1_-f2/sum_w2_);
119      ++iter;
120    }
121  }
122
123
124  void KolmogorovSmirnov::reset(void)
125  {
126    cached_=false;
127    sum_w1_=0;
128    sum_w2_=0;
129  }
130
131
132  std::ostream& operator<<(std::ostream& os, const KolmogorovSmirnov& ks)
133  {
134    std::vector<double> s;
135    ks.scores(s);
136    for (size_t i=0; i<s.size(); ++i)
137      os << i << "\t" << s[i] << "\n";
138    return os;
139  }
140
141
142}}} // of namespace theplu yat statistics
Note: See TracBrowser for help on using the repository browser.