source: trunk/yat/statistics/KolmogorovSmirnov.h @ 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: 2.7 KB
Line 
1#ifndef _theplu_yat_statistics_kolmogorov_smirnov_
2#define _theplu_yat_statistics_kolmogorov_smirnov_
3
4// $Id: KolmogorovSmirnov.h 1593 2008-10-21 16:48:24Z peter $
5
6/*
7  Copyright (C) 2008 Peter Johansson
8
9  This file is part of the yat library, http://dev.thep.lu.se/yat
10
11  The yat library is free software; you can redistribute it and/or
12  modify it under the terms of the GNU General Public License as
13  published by the Free Software Foundation; either version 3 of the
14  License, or (at your option) any later version.
15
16  The yat library is distributed in the hope that it will be useful,
17  but WITHOUT ANY WARRANTY; without even the implied warranty of
18  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  General Public License for more details.
20
21  You should have received a copy of the GNU General Public License
22  along with yat. If not, see <http://www.gnu.org/licenses/>.
23*/
24
25#include <set>
26#include <vector>
27
28namespace theplu {
29namespace yat {
30namespace statistics { 
31
32  /**
33     \brief Kolmogow Smirnov Test
34   */
35  class KolmogorovSmirnov
36  {
37  public:
38    /**
39       \brief Constructor
40     */ 
41    KolmogorovSmirnov(void);
42
43    /**
44       \brief add a value
45     */
46    void add(double value, bool class_label, double weight=1.0);
47
48    /**
49       \brief Large-Sample Approximation
50
51       This analytical approximation of p-value can be used when all
52       weight equal unity and sample sizes \a n and \a m are
53       large. The p-value is calcuated as \f$ P = \displaystyle - 2
54       \sum_{k=1}^{\infty} (-1)^ke^{-2k^2s^2}\f$, where s is the
55       scaled score:
56
57       \f$ s = \sqrt\frac{nm}{n+m} \f$ score().
58
59       \since New in yat 0.5
60
61       Following Hollander and Wolfe
62    */
63    double p_value(void) const;
64
65    /**
66       \brief p-value
67
68       Performs a permutation test using \a perm label randomizations
69       and calculate how often a score equal or larger than score() is
70       obtained.
71    */
72    double p_value(size_t perm) const;
73
74    /**
75       \brief resets everything to zero
76    */
77    void reset(void);
78
79    /**
80       \brief Kolmogorov Smirnov statistic
81
82       \f$ sup_x | F_1(x) - F_2(x) | \f$ where
83       \f$ F(x) = \frac {\sum_{i:x_i\le x}w_i }{ \sum w_i }\f$
84    */
85    double score(void) const;
86
87  private:
88    void scores(std::vector<double>&) const;
89
90    mutable bool cached_;
91    mutable double score_;
92    typedef std::pair<std::pair<double,double>, bool> trip;
93    typedef std::set<trip, std::greater<trip> > data_w;
94    data_w data_;
95    double sum_w1_;
96    double sum_w2_;
97
98    friend std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
99
100    // Copy not allowed
101    KolmogorovSmirnov(const KolmogorovSmirnov&);
102    KolmogorovSmirnov& operator=(const KolmogorovSmirnov&);
103  };
104
105  /**
106     \brief output operator
107   */
108  std::ostream& operator<<(std::ostream&, const KolmogorovSmirnov&);
109
110
111}}} // of namespace theplu yat statistics
112
113#endif
Note: See TracBrowser for help on using the repository browser.