1 | // $Id: Polynomial.cc 2564 2011-09-25 20:03:41Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen |
---|
5 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2011 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 3 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 yat. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "Polynomial.h" |
---|
25 | #include "yat/utility/Matrix.h" |
---|
26 | #include "yat/utility/VectorBase.h" |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace regression { |
---|
31 | |
---|
32 | Polynomial::Polynomial(size_t power) |
---|
33 | : OneDimensional(), power_(power) |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | |
---|
38 | Polynomial::~Polynomial(void) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | |
---|
43 | const utility::Matrix& Polynomial::covariance(void) const |
---|
44 | { |
---|
45 | return md_.covariance(); |
---|
46 | } |
---|
47 | |
---|
48 | |
---|
49 | void Polynomial::fit(const utility::VectorBase& x, |
---|
50 | const utility::VectorBase& y) |
---|
51 | { |
---|
52 | add(ap_, x.begin(), x.end(), y.begin()); |
---|
53 | utility::Matrix X(x.size(),power_+1,1); |
---|
54 | for (size_t i=0; i<X.rows(); ++i) |
---|
55 | for (size_t j=1; j<X.columns(); ++j) |
---|
56 | X(i,j)=X(i,j-1)*x(i); |
---|
57 | md_.fit(X,y); |
---|
58 | chisq_ = md_.chisq(); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | const utility::Vector& Polynomial::fit_parameters(void) const |
---|
63 | { |
---|
64 | return md_.fit_parameters(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | double Polynomial::predict(const double x) const |
---|
69 | { |
---|
70 | utility::Vector vec(power_+1,1); |
---|
71 | for (size_t i=1; i<=power_; ++i) |
---|
72 | vec(i) = vec(i-1)*x; |
---|
73 | return md_.predict(vec); |
---|
74 | } |
---|
75 | |
---|
76 | |
---|
77 | double Polynomial::s2(void) const |
---|
78 | { |
---|
79 | return chisq()/(ap_.n()-power_-1); |
---|
80 | } |
---|
81 | |
---|
82 | |
---|
83 | double Polynomial::standard_error2(const double x) const |
---|
84 | { |
---|
85 | utility::Vector vec(power_+1,1); |
---|
86 | for (size_t i=1; i<=power_; ++i) |
---|
87 | vec(i) = vec(i-1)*x; |
---|
88 | return md_.standard_error2(vec); |
---|
89 | } |
---|
90 | |
---|
91 | }}} // of namespaces regression, yat, and theplu |
---|