source: trunk/yat/regression/Linear.cc @ 1703

Last change on this file since 1703 was 1487, checked in by Jari Häkkinen, 13 years ago

Addresses #436. GPL license copy reference should also be updated.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 2.2 KB
Line 
1// $Id: Linear.cc 1487 2008-09-10 08:41:36Z jari $
2
3/*
4  Copyright (C) 2004, 2005, 2006, 2007 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2008 Peter Johansson
6
7  This file is part of the yat library, http://dev.thep.lu.se/yat
8
9  The yat library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License as
11  published by the Free Software Foundation; either version 3 of the
12  License, or (at your option) any later version.
13
14  The yat library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with yat. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "Linear.h"
24#include "yat/statistics/AveragerPair.h"
25#include "yat/utility/VectorBase.h"
26
27namespace theplu {
28namespace yat {
29namespace regression {
30
31  Linear::Linear(void)
32    : OneDimensional(), alpha_(0), alpha_var_(0), beta_(0), beta_var_(0)
33  {
34  }
35
36  Linear::~Linear(void)
37  {
38  }
39
40  double Linear::alpha(void) const
41  {
42    return alpha_;
43  }
44
45  double Linear::alpha_var(void) const
46  {
47    return alpha_var_;
48  }
49
50  double Linear::beta(void) const
51  {
52    return beta_;
53  }
54
55  double Linear::beta_var(void) const
56  {
57    return beta_var_;
58  }
59
60  void Linear::fit(const utility::VectorBase& x, const utility::VectorBase& y) 
61  {
62    ap_.reset();
63    for (size_t i=0; i<x.size(); i++)
64      ap_.add(x(i),y(i));
65
66    alpha_ = ap_.y_averager().mean();
67    beta_ = ap_.sum_xy_centered() / ap_.x_averager().sum_xx_centered();
68
69    // calculating deviation between data and model
70    chisq_ = (ap_.y_averager().sum_xx_centered() - ap_.sum_xy_centered()*
71              ap_.sum_xy_centered()/ap_.x_averager().sum_xx_centered() );
72    alpha_var_ = s2() / x.size();
73    beta_var_ = s2() / ap_.x_averager().sum_xx_centered();
74  }
75
76  double Linear::predict(const double x) const
77  { 
78    return alpha_ + beta_ * (x - ap_.x_averager().mean()); 
79  }
80
81  double Linear::s2(void) const
82  {
83    return chisq()/(ap_.n()-2);
84  }
85
86  double Linear::standard_error2(const double x) const
87  {
88    return alpha_var_+beta_var_*(x-ap_.x_averager().mean())*
89      (x-ap_.x_averager().mean()); 
90  }
91
92}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.