1 | // $Id: Polynomial.cc 1437 2008-08-25 17:55:00Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen |
---|
5 | Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2008 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://dev.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/VectorBase.h" |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace 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::VectorBase& x, |
---|
52 | const utility::VectorBase& y) |
---|
53 | { |
---|
54 | add(ap_, x.begin(), x.end(), y.begin()); |
---|
55 | utility::Matrix X=utility::Matrix(x.size(),power_+1,1); |
---|
56 | for (size_t i=0; i<X.rows(); ++i) |
---|
57 | for (size_t j=1; j<X.columns(); ++j) |
---|
58 | X(i,j)=X(i,j-1)*x(i); |
---|
59 | md_.fit(X,y); |
---|
60 | chisq_ = md_.chisq(); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | const utility::Vector& Polynomial::fit_parameters(void) const |
---|
65 | { |
---|
66 | return md_.fit_parameters(); |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | double Polynomial::predict(const double x) const |
---|
71 | { |
---|
72 | utility::Vector vec(power_+1,1); |
---|
73 | for (size_t i=1; i<=power_; ++i) |
---|
74 | vec(i) = vec(i-1)*x; |
---|
75 | return md_.predict(vec); |
---|
76 | } |
---|
77 | |
---|
78 | |
---|
79 | double Polynomial::s2(void) const |
---|
80 | { |
---|
81 | return chisq()/(ap_.n()-power_-1); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | double Polynomial::standard_error2(const double x) const |
---|
86 | { |
---|
87 | utility::Vector vec(power_+1,1); |
---|
88 | for (size_t i=1; i<=power_; ++i) |
---|
89 | vec(i) = vec(i-1)*x; |
---|
90 | return md_.standard_error2(vec); |
---|
91 | } |
---|
92 | |
---|
93 | }}} // of namespaces regression, yat, and theplu |
---|