1 | // $Id: tTest.cc 2078 2009-10-07 16:33:56Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2004 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2005 Peter Johansson |
---|
6 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
7 | Copyright (C) 2007, 2008 Jari Häkkinen, 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 "tTest.h" |
---|
26 | #include "AveragerWeighted.h" |
---|
27 | #include "tScore.h" |
---|
28 | |
---|
29 | #include <algorithm> |
---|
30 | #include <cmath> |
---|
31 | |
---|
32 | namespace theplu { |
---|
33 | namespace yat { |
---|
34 | namespace statistics { |
---|
35 | |
---|
36 | tTest::tTest(void) |
---|
37 | : updated_(false) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | |
---|
42 | void tTest::add(double x, bool target, double w) |
---|
43 | { |
---|
44 | if (!w) |
---|
45 | return; |
---|
46 | if (target) |
---|
47 | pos_.add(x,w); |
---|
48 | else |
---|
49 | neg_.add(x,w); |
---|
50 | updated_=false; |
---|
51 | } |
---|
52 | |
---|
53 | |
---|
54 | void tTest::reset(void) |
---|
55 | { |
---|
56 | pos_.reset(); |
---|
57 | neg_.reset(); |
---|
58 | dof_=0; |
---|
59 | t_=0; |
---|
60 | updated_=false; |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | double tTest::score(void) |
---|
65 | { |
---|
66 | if (!updated_){ |
---|
67 | tScore score(false); |
---|
68 | t_ = score.score(pos_, neg_, &dof_); |
---|
69 | updated_=true; |
---|
70 | } |
---|
71 | return t_; |
---|
72 | } |
---|
73 | |
---|
74 | |
---|
75 | double tTest::p_value(void) const |
---|
76 | { |
---|
77 | double p=2*p_value_one_sided(); |
---|
78 | return std::min(p,2-p); |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | double tTest::p_value_one_sided(void) const |
---|
83 | { |
---|
84 | double t=t_; |
---|
85 | double dof=dof_; |
---|
86 | if (!updated_){ |
---|
87 | tScore score(false); |
---|
88 | t = score.score(pos_, neg_, &dof); |
---|
89 | } |
---|
90 | if (!dof) |
---|
91 | return 1.0; |
---|
92 | return gsl_cdf_tdist_Q(t, dof); |
---|
93 | } |
---|
94 | |
---|
95 | }}} // of namespace statistics, yat, and theplu |
---|