1 | // $Id: tTest.cc 3114 2013-11-10 23:51:47Z 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 | Copyright (C) 2012, 2013 Peter Johansson |
---|
9 | |
---|
10 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
11 | |
---|
12 | The yat library is free software; you can redistribute it and/or |
---|
13 | modify it under the terms of the GNU General Public License as |
---|
14 | published by the Free Software Foundation; either version 3 of the |
---|
15 | License, or (at your option) any later version. |
---|
16 | |
---|
17 | The yat library is distributed in the hope that it will be useful, |
---|
18 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
19 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
20 | General Public License for more details. |
---|
21 | |
---|
22 | You should have received a copy of the GNU General Public License |
---|
23 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
24 | */ |
---|
25 | |
---|
26 | #include <config.h> |
---|
27 | |
---|
28 | #include "tTest.h" |
---|
29 | #include "AveragerWeighted.h" |
---|
30 | #include "tScore.h" |
---|
31 | |
---|
32 | #include <algorithm> |
---|
33 | #include <cmath> |
---|
34 | |
---|
35 | namespace theplu { |
---|
36 | namespace yat { |
---|
37 | namespace statistics { |
---|
38 | |
---|
39 | tTest::tTest(void) |
---|
40 | : updated_(false) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | void tTest::add(double x, bool target, double w) |
---|
46 | { |
---|
47 | if (!w) |
---|
48 | return; |
---|
49 | if (target) |
---|
50 | pos_.add(x,w); |
---|
51 | else |
---|
52 | neg_.add(x,w); |
---|
53 | updated_=false; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | void tTest::reset(void) |
---|
58 | { |
---|
59 | pos_.reset(); |
---|
60 | neg_.reset(); |
---|
61 | dof_=0; |
---|
62 | t_=0; |
---|
63 | updated_=false; |
---|
64 | } |
---|
65 | |
---|
66 | |
---|
67 | double tTest::score(void) const |
---|
68 | { |
---|
69 | if (!updated_){ |
---|
70 | tScore score(false); |
---|
71 | t_ = score.score(pos_, neg_, &dof_); |
---|
72 | updated_=true; |
---|
73 | } |
---|
74 | return t_; |
---|
75 | } |
---|
76 | |
---|
77 | |
---|
78 | double tTest::p_left(void) const |
---|
79 | { |
---|
80 | double t = score(); |
---|
81 | if (!dof_) |
---|
82 | return 1.0; |
---|
83 | return gsl_cdf_tdist_P(t, dof_); |
---|
84 | } |
---|
85 | |
---|
86 | |
---|
87 | double tTest::p_right(void) const |
---|
88 | { |
---|
89 | double t = score(); |
---|
90 | if (!dof_) |
---|
91 | return 1.0; |
---|
92 | return gsl_cdf_tdist_Q(t, dof_); |
---|
93 | } |
---|
94 | |
---|
95 | |
---|
96 | double tTest::p_value(void) const |
---|
97 | { |
---|
98 | double p=2*p_right(); |
---|
99 | return std::min(p,2-p); |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | double tTest::p_value_one_sided(void) const |
---|
104 | { |
---|
105 | return p_right(); |
---|
106 | } |
---|
107 | |
---|
108 | }}} // of namespace statistics, yat, and theplu |
---|