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

Last change on this file since 1000 was 1000, checked in by Jari Häkkinen, 15 years ago

trac moved to new location.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.1 KB
Line 
1// $Id: Polynomial.cc 1000 2007-12-23 20:09:15Z jari $
2
3/*
4  Copyright (C) 2005 Jari Häkkinen
5  Copyright (C) 2006 Jari Häkkinen, Peter Johansson
6  Copyright (C) 2007 Peter Johansson
7
8  This file is part of the yat library, http://trac.thep.lu.se/yat
9
10  The yat library is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License as
12  published by the Free Software Foundation; either version 2 of the
13  License, or (at your option) any later version.
14
15  The yat library is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18  General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with this program; if not, write to the Free Software
22  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23  02111-1307, USA.
24*/
25
26#include "Polynomial.h"
27#include "yat/utility/matrix.h"
28#include "yat/utility/vector.h"
29
30namespace theplu {
31namespace yat {
32namespace regression {
33
34  Polynomial::Polynomial(size_t power)
35    : OneDimensional(), power_(power)
36  {
37  }
38
39
40  Polynomial::~Polynomial(void)
41  {
42  }
43
44
45  const utility::matrix& Polynomial::covariance(void) const
46  {
47    return md_.covariance();
48  }
49
50
51  void Polynomial::fit(const utility::vector& x, const utility::vector& y)
52  {
53    ap_.add_values(x,y);
54    utility::matrix X=utility::matrix(x.size(),power_+1,1);
55    for (size_t i=0; i<X.rows(); ++i)
56      for (u_int j=1; j<X.columns(); j++)
57        X(i,j)=X(i,j-1)*x(i);
58    md_.fit(X,y);
59    chisq_ = md_.chisq();
60  }
61
62
63  const utility::vector& Polynomial::fit_parameters(void) const
64  {
65    return md_.fit_parameters(); 
66  }
67
68
69  double Polynomial::predict(const double x) const
70  {
71    utility::vector vec(power_+1,1);
72    for (size_t i=1; i<=power_; ++i)
73      vec(i) = vec(i-1)*x;
74    return md_.predict(vec);
75  }
76
77
78  double Polynomial::s2(void) const
79  {
80    return chisq()/(ap_.n()-power_-1);
81  }
82
83
84  double Polynomial::standard_error2(const double x) const
85  {
86    utility::vector vec(power_+1,1);
87    for (size_t i=1; i<=power_; ++i)
88      vec(i) = vec(i-1)*x;
89    return md_.standard_error2(vec);
90  }
91
92}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.