yat/regression/Linear.cc

Code
Comments
Other
Rev Date Author Line
193 27 Oct 04 peter 1 // $Id$
193 27 Oct 04 peter 2
675 10 Oct 06 jari 3 /*
4359 23 Aug 23 peter 4   Copyright (C) 2004, 2005, 2006 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 5   Copyright (C) 2007 Peter Johansson
4359 23 Aug 23 peter 6   Copyright (C) 2008 Jari Häkkinen, Peter Johansson
4359 23 Aug 23 peter 7   Copyright (C) 2012 Peter Johansson
193 27 Oct 04 peter 8
1437 25 Aug 08 peter 9   This file is part of the yat library, http://dev.thep.lu.se/yat
193 27 Oct 04 peter 10
675 10 Oct 06 jari 11   The yat library is free software; you can redistribute it and/or
675 10 Oct 06 jari 12   modify it under the terms of the GNU General Public License as
1486 09 Sep 08 jari 13   published by the Free Software Foundation; either version 3 of the
675 10 Oct 06 jari 14   License, or (at your option) any later version.
675 10 Oct 06 jari 15
675 10 Oct 06 jari 16   The yat library is distributed in the hope that it will be useful,
675 10 Oct 06 jari 17   but WITHOUT ANY WARRANTY; without even the implied warranty of
675 10 Oct 06 jari 18   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
675 10 Oct 06 jari 19   General Public License for more details.
675 10 Oct 06 jari 20
675 10 Oct 06 jari 21   You should have received a copy of the GNU General Public License
1487 10 Sep 08 jari 22   along with yat. If not, see <http://www.gnu.org/licenses/>.
675 10 Oct 06 jari 23 */
675 10 Oct 06 jari 24
2881 18 Nov 12 peter 25 #include <config.h>
2881 18 Nov 12 peter 26
680 11 Oct 06 jari 27 #include "Linear.h"
682 11 Oct 06 jari 28 #include "yat/statistics/AveragerPair.h"
1019 01 Feb 08 peter 29 #include "yat/utility/VectorBase.h"
675 10 Oct 06 jari 30
193 27 Oct 04 peter 31 namespace theplu {
680 11 Oct 06 jari 32 namespace yat {
383 12 Aug 05 jari 33 namespace regression {
193 27 Oct 04 peter 34
703 18 Dec 06 jari 35   Linear::Linear(void)
729 05 Jan 07 peter 36     : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0)
703 18 Dec 06 jari 37   {
703 18 Dec 06 jari 38   }
703 18 Dec 06 jari 39
703 18 Dec 06 jari 40   Linear::~Linear(void)
703 18 Dec 06 jari 41   {
703 18 Dec 06 jari 42   }
713 21 Dec 06 peter 43
718 26 Dec 06 jari 44   double Linear::alpha(void) const
718 26 Dec 06 jari 45   {
718 26 Dec 06 jari 46     return alpha_;
718 26 Dec 06 jari 47   }
718 26 Dec 06 jari 48
726 04 Jan 07 peter 49   double Linear::alpha_var(void) const
718 26 Dec 06 jari 50   {
726 04 Jan 07 peter 51     return alpha_var_;
718 26 Dec 06 jari 52   }
718 26 Dec 06 jari 53
718 26 Dec 06 jari 54   double Linear::beta(void) const
718 26 Dec 06 jari 55   {
718 26 Dec 06 jari 56     return beta_;
718 26 Dec 06 jari 57   }
718 26 Dec 06 jari 58
726 04 Jan 07 peter 59   double Linear::beta_var(void) const
718 26 Dec 06 jari 60   {
726 04 Jan 07 peter 61     return beta_var_;
718 26 Dec 06 jari 62   }
718 26 Dec 06 jari 63
4078 26 Aug 21 peter 64   void Linear::fit(const utility::VectorBase& x, const utility::VectorBase& y)
221 30 Dec 04 peter 65   {
429 08 Dec 05 peter 66     ap_.reset();
221 30 Dec 04 peter 67     for (size_t i=0; i<x.size(); i++)
429 08 Dec 05 peter 68       ap_.add(x(i),y(i));
221 30 Dec 04 peter 69
429 08 Dec 05 peter 70     alpha_ = ap_.y_averager().mean();
730 06 Jan 07 peter 71     beta_ = ap_.sum_xy_centered() / ap_.x_averager().sum_xx_centered();
221 30 Dec 04 peter 72
586 19 Jun 06 peter 73     // calculating deviation between data and model
726 04 Jan 07 peter 74     chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
726 04 Jan 07 peter 75               ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() );
726 04 Jan 07 peter 76     alpha_var_ = s2() / x.size();
726 04 Jan 07 peter 77     beta_var_ = s2() / ap_.x_averager().sum_xx_centered();
221 30 Dec 04 peter 78   }
221 30 Dec 04 peter 79
586 19 Jun 06 peter 80   double Linear::predict(const double x) const
4078 26 Aug 21 peter 81   {
4078 26 Aug 21 peter 82     return alpha_ + beta_ * (x - ap_.x_averager().mean());
221 30 Dec 04 peter 83   }
221 30 Dec 04 peter 84
726 04 Jan 07 peter 85   double Linear::s2(void) const
726 04 Jan 07 peter 86   {
726 04 Jan 07 peter 87     return chisq()/(ap_.n()-2);
726 04 Jan 07 peter 88   }
726 04 Jan 07 peter 89
727 04 Jan 07 peter 90   double Linear::standard_error2(const double x) const
586 19 Jun 06 peter 91   {
727 04 Jan 07 peter 92     return alpha_var_+beta_var_*(x-ap_.x_averager().mean())*
4078 26 Aug 21 peter 93       (x-ap_.x_averager().mean());
586 19 Jun 06 peter 94   }
383 12 Aug 05 jari 95
681 11 Oct 06 jari 96 }}} // of namespaces regression, yat, and theplu