yat/statistics/Anova.h

Code
Comments
Other
Rev Date Author Line
4114 13 Oct 21 peter 1 #ifndef _theplu_yat_statistics_anova_
4114 13 Oct 21 peter 2 #define _theplu_yat_statistics_anova_
4114 13 Oct 21 peter 3
4114 13 Oct 21 peter 4 // $Id$
4114 13 Oct 21 peter 5
4114 13 Oct 21 peter 6 /*
4114 13 Oct 21 peter 7   Copyright (C) 2021 Peter Johansson
4114 13 Oct 21 peter 8
4114 13 Oct 21 peter 9   This file is part of the yat library, https://dev.thep.lu.se/yat
4114 13 Oct 21 peter 10
4114 13 Oct 21 peter 11   The yat library is free software; you can redistribute it and/or
4114 13 Oct 21 peter 12   modify it under the terms of the GNU General Public License as
4114 13 Oct 21 peter 13   published by the Free Software Foundation; either version 3 of the
4114 13 Oct 21 peter 14   License, or (at your option) any later version.
4114 13 Oct 21 peter 15
4114 13 Oct 21 peter 16   The yat library is distributed in the hope that it will be useful,
4114 13 Oct 21 peter 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
4114 13 Oct 21 peter 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
4114 13 Oct 21 peter 19   General Public License for more details.
4114 13 Oct 21 peter 20
4114 13 Oct 21 peter 21   You should have received a copy of the GNU General Public License
4114 13 Oct 21 peter 22   along with yat. If not, see <https://www.gnu.org/licenses/>.
4114 13 Oct 21 peter 23 */
4114 13 Oct 21 peter 24
4114 13 Oct 21 peter 25 #include <yat/statistics/Averager.h>
4114 13 Oct 21 peter 26
4114 13 Oct 21 peter 27 #include <cstddef>
4114 13 Oct 21 peter 28 #include <vector>
4114 13 Oct 21 peter 29
4114 13 Oct 21 peter 30 namespace theplu {
4114 13 Oct 21 peter 31 namespace yat {
4114 13 Oct 21 peter 32 namespace statistics {
4114 13 Oct 21 peter 33
4114 13 Oct 21 peter 34   /**
4114 13 Oct 21 peter 35      \brief one-way ANOVA
4114 13 Oct 21 peter 36
4114 13 Oct 21 peter 37      One-way Analysis of Variance is an extension of a t-test to
4114 13 Oct 21 peter 38      compare also more than two groups.
4114 13 Oct 21 peter 39
4114 13 Oct 21 peter 40      \since New in yat 0.20
4114 13 Oct 21 peter 41    */
4114 13 Oct 21 peter 42   class Anova
4114 13 Oct 21 peter 43   {
4114 13 Oct 21 peter 44   public:
4114 13 Oct 21 peter 45     /**
4114 13 Oct 21 peter 46        \param n number of groups compared
4114 13 Oct 21 peter 47      */
4114 13 Oct 21 peter 48     Anova(size_t n);
4114 13 Oct 21 peter 49
4114 13 Oct 21 peter 50     /**
4114 13 Oct 21 peter 51        \brief add a data point
4114 13 Oct 21 peter 52
4114 13 Oct 21 peter 53        \param x value of data point
4114 13 Oct 21 peter 54        \param g group the data belongs to
4114 13 Oct 21 peter 55        \param n number of data points
4114 13 Oct 21 peter 56      */
4114 13 Oct 21 peter 57     void add(double x, size_t g, long int n=1);
4114 13 Oct 21 peter 58
4114 13 Oct 21 peter 59     /**
4114 13 Oct 21 peter 60        F is calculated as the ratio between within-group variance and
4114 13 Oct 21 peter 61        between-group variance.
4114 13 Oct 21 peter 62
4114 13 Oct 21 peter 63        \f$ \frac
4114 13 Oct 21 peter 64        {\frac{1}{G-1} \sum_g^G n_g (m_g - m)^2}
4114 13 Oct 21 peter 65        {\frac{1}{N-G} \sum_{g,i}^{G, n_G} (x_{g,i}-m_g)^2}
4114 13 Oct 21 peter 66        \f$
4114 13 Oct 21 peter 67
4114 13 Oct 21 peter 68        If null hypothesis is true, F follows an F(a,b) distribution
4114 13 Oct 21 peter 69        where a is inter_df() and b is intra_df.
4114 13 Oct 21 peter 70      */
4114 13 Oct 21 peter 71     double F(void) const;
4114 13 Oct 21 peter 72
4114 13 Oct 21 peter 73     /**
4114 13 Oct 21 peter 74        Number of samples minus one.
4114 13 Oct 21 peter 75      */
4114 13 Oct 21 peter 76     size_t inter_df(void) const;
4114 13 Oct 21 peter 77
4114 13 Oct 21 peter 78     /**
4114 13 Oct 21 peter 79        Number of groups minus one
4114 13 Oct 21 peter 80      */
4114 13 Oct 21 peter 81     size_t intra_df(void) const;
4114 13 Oct 21 peter 82
4114 13 Oct 21 peter 83     /**
4114 13 Oct 21 peter 84        Calculates the probability to get F (or larger) given the null
4114 13 Oct 21 peter 85        hypothesis that there is no difference between the groups.
4114 13 Oct 21 peter 86
4114 13 Oct 21 peter 87        \return the one-sided p-value
4114 13 Oct 21 peter 88      */
4114 13 Oct 21 peter 89     double p_value(void) const;
4114 13 Oct 21 peter 90
4114 13 Oct 21 peter 91     /**
4114 13 Oct 21 peter 92        \brief remove all data
4114 13 Oct 21 peter 93      */
4114 13 Oct 21 peter 94     void reset(void);
4114 13 Oct 21 peter 95
4114 13 Oct 21 peter 96   private:
4114 13 Oct 21 peter 97     std::vector<Averager> aver_;
4114 13 Oct 21 peter 98     Averager total_;
4114 13 Oct 21 peter 99   };
4114 13 Oct 21 peter 100
4114 13 Oct 21 peter 101 }}}
4114 13 Oct 21 peter 102
4114 13 Oct 21 peter 103 #endif