source: trunk/yat/regression/Polynomial.cc @ 2533

Last change on this file since 2533 was 2533, checked in by Peter, 10 years ago

avoid copy

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: Polynomial.cc 2533 2011-07-31 16:54:19Z peter $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen
5  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, 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 "Polynomial.h"
24#include "yat/utility/Matrix.h"
25#include "yat/utility/VectorBase.h"
26
27namespace theplu {
28namespace yat {
29namespace regression {
30
31  Polynomial::Polynomial(size_t power)
32    : OneDimensional(), power_(power)
33  {
34  }
35
36
37  Polynomial::~Polynomial(void)
38  {
39  }
40
41
42  const utility::Matrix& Polynomial::covariance(void) const
43  {
44    return md_.covariance();
45  }
46
47
48  void Polynomial::fit(const utility::VectorBase& x, 
49                       const utility::VectorBase& y)
50  {
51    add(ap_, x.begin(), x.end(), y.begin());
52    utility::Matrix X(x.size(),power_+1,1);
53    for (size_t i=0; i<X.rows(); ++i)
54      for (size_t j=1; j<X.columns(); ++j)
55        X(i,j)=X(i,j-1)*x(i);
56    md_.fit(X,y);
57    chisq_ = md_.chisq();
58  }
59
60
61  const utility::Vector& Polynomial::fit_parameters(void) const
62  {
63    return md_.fit_parameters(); 
64  }
65
66
67  double Polynomial::predict(const double x) const
68  {
69    utility::Vector vec(power_+1,1);
70    for (size_t i=1; i<=power_; ++i)
71      vec(i) = vec(i-1)*x;
72    return md_.predict(vec);
73  }
74
75
76  double Polynomial::s2(void) const
77  {
78    return chisq()/(ap_.n()-power_-1);
79  }
80
81
82  double Polynomial::standard_error2(const double x) const
83  {
84    utility::Vector vec(power_+1,1);
85    for (size_t i=1; i<=power_; ++i)
86      vec(i) = vec(i-1)*x;
87    return md_.standard_error2(vec);
88  }
89
90}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.