1 | #ifndef _theplu_yat_statistics_ttest_ |
---|
2 | #define _theplu_yat_statistics_ttest_ |
---|
3 | |
---|
4 | // $Id: tTest.h 1275 2008-04-11 06:10:12Z jari $ |
---|
5 | |
---|
6 | /* |
---|
7 | Copyright (C) 2004, 2005 Peter Johansson |
---|
8 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson, Markus Ringnér |
---|
9 | Copyright (C) 2007 Jari Häkkinen, Peter Johansson |
---|
10 | Copyright (C) 2008 Peter Johansson |
---|
11 | |
---|
12 | This file is part of the yat library, http://trac.thep.lu.se/yat |
---|
13 | |
---|
14 | The yat library is free software; you can redistribute it and/or |
---|
15 | modify it under the terms of the GNU General Public License as |
---|
16 | published by the Free Software Foundation; either version 2 of the |
---|
17 | License, or (at your option) any later version. |
---|
18 | |
---|
19 | The yat library is distributed in the hope that it will be useful, |
---|
20 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
22 | General Public License for more details. |
---|
23 | |
---|
24 | You should have received a copy of the GNU General Public License |
---|
25 | along with this program; if not, write to the Free Software |
---|
26 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
27 | 02111-1307, USA. |
---|
28 | */ |
---|
29 | |
---|
30 | #include "AveragerWeighted.h" |
---|
31 | |
---|
32 | #include <gsl/gsl_cdf.h> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace statistics { |
---|
37 | |
---|
38 | /// |
---|
39 | /// @brief Class for Student's t-test. |
---|
40 | /// |
---|
41 | /// See <a href="http://en.wikipedia.org/wiki/Student's_t-test"> |
---|
42 | /// http://en.wikipedia.org/wiki/Student's_t-test</a> for more |
---|
43 | /// details on the t-test. |
---|
44 | /// |
---|
45 | class tTest |
---|
46 | { |
---|
47 | |
---|
48 | public: |
---|
49 | /// |
---|
50 | /// @brief Default Constructor. |
---|
51 | /// |
---|
52 | tTest(void); |
---|
53 | |
---|
54 | |
---|
55 | /** |
---|
56 | Adding a data value to tTest. |
---|
57 | */ |
---|
58 | void add(double value, bool target, double weight=1.0); |
---|
59 | |
---|
60 | /** |
---|
61 | Calculates the t-score, i.e. the ratio between difference in |
---|
62 | mean and standard deviation of this difference. The t-score is |
---|
63 | calculated as |
---|
64 | \f$ t = \frac{ m_x - m_y }{ |
---|
65 | s\sqrt{\frac{1}{n_x}+\frac{1}{n_y}}} \f$ where \f$ m \f$ is the |
---|
66 | weighted mean, n is the weighted version of number of data |
---|
67 | points \f$ \frac{\left(\sum w_i\right)^2}{\sum w_i^2} \f$, and |
---|
68 | \f$ s^2 \f$ is an estimation of the variance \f$ s^2 = \frac{ |
---|
69 | \sum_i w_i(x_i-m_x)^2 + \sum_i w_i(y_i-m_y)^2 }{ n_x + n_y - 2 |
---|
70 | } \f$ |
---|
71 | |
---|
72 | \see AveragerWeighted |
---|
73 | |
---|
74 | If all weights are equal to unity this boils down to |
---|
75 | \f$ t = \frac{ m_x - m_y } |
---|
76 | {s\sqrt{\frac{1}{n_x}+\frac{1}{n_y}}} \f$ where \f$ m \f$ is |
---|
77 | the mean, \f$ n \f$ is the number of data points and \f$ s^2 = |
---|
78 | \frac{ \sum_i (x_i-m_x)^2 + \sum_i (y_i-m_y)^2 }{ n_x + n_y - 2 |
---|
79 | } \f$ |
---|
80 | |
---|
81 | \see Averager |
---|
82 | |
---|
83 | \return t-score. |
---|
84 | */ |
---|
85 | double score(void); |
---|
86 | |
---|
87 | /// |
---|
88 | /// Calculates the p-value, i.e. the probability of observing a |
---|
89 | /// t-score equally or larger if the null hypothesis is true. If P |
---|
90 | /// is near zero, this casts doubt on this hypothesis. The null |
---|
91 | /// hypothesis is that the means of the two distributions are |
---|
92 | /// equal. Assumtions for this test is that the two distributions |
---|
93 | /// are normal distributions with equal variance. The latter |
---|
94 | /// assumtion is dropped in Welch's t-test. |
---|
95 | /// |
---|
96 | /// @return the two-sided p-value |
---|
97 | /// |
---|
98 | double p_value() const; |
---|
99 | |
---|
100 | /// |
---|
101 | /// @return One-sided P-value |
---|
102 | /// |
---|
103 | double p_value_one_sided(void) const; |
---|
104 | |
---|
105 | private: |
---|
106 | |
---|
107 | double dof_; |
---|
108 | bool updated_; |
---|
109 | double t_; |
---|
110 | AveragerWeighted pos_; |
---|
111 | AveragerWeighted neg_; |
---|
112 | |
---|
113 | }; |
---|
114 | |
---|
115 | }}} // of namespace statistics, yat, and theplu |
---|
116 | |
---|
117 | #endif |
---|