1 | // $Id: tTest.cc 1451 2008-08-28 20:46:10Z 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 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 2 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 this program; if not, write to the Free Software |
---|
23 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
24 | 02111-1307, USA. |
---|
25 | */ |
---|
26 | |
---|
27 | #include "tTest.h" |
---|
28 | #include "AveragerWeighted.h" |
---|
29 | #include "tScore.h" |
---|
30 | |
---|
31 | #include <algorithm> |
---|
32 | #include <cassert> |
---|
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) |
---|
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_value(void) const |
---|
79 | { |
---|
80 | double p=2*p_value_one_sided(); |
---|
81 | return std::min(p,2-p); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | double tTest::p_value_one_sided(void) const |
---|
86 | { |
---|
87 | double t=t_; |
---|
88 | double dof=dof_; |
---|
89 | if (!updated_){ |
---|
90 | tScore score(false); |
---|
91 | t = score.score(pos_, neg_, &dof); |
---|
92 | } |
---|
93 | if (!dof) |
---|
94 | return 1.0; |
---|
95 | return gsl_cdf_tdist_Q(t, dof); |
---|
96 | } |
---|
97 | |
---|
98 | }}} // of namespace statistics, yat, and theplu |
---|