1 | #ifndef _theplu_yat_statistics_ttest_ |
---|
2 | #define _theplu_yat_statistics_ttest_ |
---|
3 | |
---|
4 | // $Id: tTest.h 1451 2008-08-28 20:46:10Z peter $ |
---|
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://dev.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 | \brief Set everything to zero |
---|
62 | |
---|
63 | \since New in yat 0.5 |
---|
64 | */ |
---|
65 | void reset(void); |
---|
66 | |
---|
67 | /** |
---|
68 | Calculates the t-score, i.e. the ratio between difference in |
---|
69 | mean and standard deviation of this difference. The t-score is |
---|
70 | calculated as |
---|
71 | \f$ t = \frac{ m_x - m_y }{ |
---|
72 | s\sqrt{\frac{1}{n_x}+\frac{1}{n_y}}} \f$ where \f$ m \f$ is the |
---|
73 | weighted mean, n is the weighted version of number of data |
---|
74 | points \f$ \frac{\left(\sum w_i\right)^2}{\sum w_i^2} \f$, and |
---|
75 | \f$ s^2 \f$ is an estimation of the variance \f$ s^2 = \frac{ |
---|
76 | \sum_i w_i(x_i-m_x)^2 + \sum_i w_i(y_i-m_y)^2 }{ n_x + n_y - 2 |
---|
77 | } \f$ |
---|
78 | |
---|
79 | \see AveragerWeighted |
---|
80 | |
---|
81 | If all weights are equal to unity this boils down to |
---|
82 | \f$ t = \frac{ m_x - m_y } |
---|
83 | {s\sqrt{\frac{1}{n_x}+\frac{1}{n_y}}} \f$ where \f$ m \f$ is |
---|
84 | the mean, \f$ n \f$ is the number of data points and \f$ s^2 = |
---|
85 | \frac{ \sum_i (x_i-m_x)^2 + \sum_i (y_i-m_y)^2 }{ n_x + n_y - 2 |
---|
86 | } \f$ |
---|
87 | |
---|
88 | \see Averager |
---|
89 | |
---|
90 | \return t-score. |
---|
91 | */ |
---|
92 | double score(void); |
---|
93 | |
---|
94 | /// |
---|
95 | /// Calculates the p-value, i.e. the probability of observing a |
---|
96 | /// t-score equally or larger if the null hypothesis is true. If P |
---|
97 | /// is near zero, this casts doubt on this hypothesis. The null |
---|
98 | /// hypothesis is that the means of the two distributions are |
---|
99 | /// equal. Assumtions for this test is that the two distributions |
---|
100 | /// are normal distributions with equal variance. The latter |
---|
101 | /// assumtion is dropped in Welch's t-test. |
---|
102 | /// |
---|
103 | /// @return the two-sided p-value |
---|
104 | /// |
---|
105 | double p_value() const; |
---|
106 | |
---|
107 | /// |
---|
108 | /// @return One-sided P-value |
---|
109 | /// |
---|
110 | double p_value_one_sided(void) const; |
---|
111 | |
---|
112 | private: |
---|
113 | |
---|
114 | double dof_; |
---|
115 | bool updated_; |
---|
116 | double t_; |
---|
117 | AveragerWeighted pos_; |
---|
118 | AveragerWeighted neg_; |
---|
119 | |
---|
120 | }; |
---|
121 | |
---|
122 | }}} // of namespace statistics, yat, and theplu |
---|
123 | |
---|
124 | #endif |
---|