source: trunk/yat/statistics/Anova.h @ 4114

Last change on this file since 4114 was 4114, checked in by Peter, 9 months ago

new class for one-way ANOVA test

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.2 KB
Line 
1#ifndef _theplu_yat_statistics_anova_
2#define _theplu_yat_statistics_anova_
3
4// $Id: Anova.h 4114 2021-10-13 04:36:16Z peter $
5
6/*
7  Copyright (C) 2021 Peter Johansson
8
9  This file is part of the yat library, https://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 <https://www.gnu.org/licenses/>.
23*/
24
25#include <yat/statistics/Averager.h>
26
27#include <cstddef>
28#include <vector>
29
30namespace theplu {
31namespace yat {
32namespace statistics {
33
34  /**
35     \brief one-way ANOVA
36
37     One-way Analysis of Variance is an extension of a t-test to
38     compare also more than two groups.
39
40     \since New in yat 0.20
41   */
42  class Anova
43  {
44  public:
45    /**
46       \param n number of groups compared
47     */
48    Anova(size_t n);
49
50    /**
51       \brief add a data point
52
53       \param x value of data point
54       \param g group the data belongs to
55       \param n number of data points
56     */
57    void add(double x, size_t g, long int n=1);
58
59    /**
60       F is calculated as the ratio between within-group variance and
61       between-group variance.
62
63       \f$ \frac
64       {\frac{1}{G-1} \sum_g^G n_g (m_g - m)^2}
65       {\frac{1}{N-G} \sum_{g,i}^{G, n_G} (x_{g,i}-m_g)^2}
66       \f$
67
68       If null hypothesis is true, F follows an F(a,b) distribution
69       where a is inter_df() and b is intra_df.
70     */
71    double F(void) const;
72
73    /**
74       Number of samples minus one.
75     */
76    size_t inter_df(void) const;
77
78    /**
79       Number of groups minus one
80     */
81    size_t intra_df(void) const;
82
83    /**
84       Calculates the probability to get F (or larger) given the null
85       hypothesis that there is no difference between the groups.
86
87       \return the one-sided p-value
88     */
89    double p_value(void) const;
90
91    /**
92       \brief remove all data
93     */
94    void reset(void);
95
96  private:
97    std::vector<Averager> aver_;
98    Averager total_;
99  };
100
101}}}
102
103#endif
Note: See TracBrowser for help on using the repository browser.