1 | // $Id: PolynomialWeighted.cc 1487 2008-09-10 08:41:36Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 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 "PolynomialWeighted.h" |
---|
24 | #include "yat/utility/Matrix.h" |
---|
25 | #include "yat/utility/Vector.h" |
---|
26 | |
---|
27 | #include <cassert> |
---|
28 | |
---|
29 | namespace theplu { |
---|
30 | namespace yat { |
---|
31 | namespace regression { |
---|
32 | |
---|
33 | PolynomialWeighted::PolynomialWeighted(size_t power) |
---|
34 | : OneDimensionalWeighted(), power_(power) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | PolynomialWeighted::~PolynomialWeighted(void) |
---|
39 | { |
---|
40 | } |
---|
41 | |
---|
42 | void PolynomialWeighted::fit(const utility::VectorBase& x, |
---|
43 | const utility::VectorBase& y, |
---|
44 | const utility::VectorBase& w) |
---|
45 | { |
---|
46 | assert(x.size()==y.size()); |
---|
47 | assert(y.size()==w.size()); |
---|
48 | ap_.reset(); |
---|
49 | // AveragerPairWeighted requires 2 weights but works only on the |
---|
50 | // product wx*wy, so we can send in w and a dummie to get what we |
---|
51 | // want. |
---|
52 | utility::Vector dummy(x.size(), 1.0); |
---|
53 | add(ap_,x.begin(), x.end(),y.begin(),dummy.begin(),w.begin()); |
---|
54 | utility::Matrix X=utility::Matrix(x.size(),power_+1,1); |
---|
55 | for (size_t i=0; i<X.rows(); ++i) |
---|
56 | for (size_t j=1; j<X.columns(); ++j) |
---|
57 | X(i,j)=X(i,j-1)*x(i); |
---|
58 | md_.fit(X,y,w); |
---|
59 | chisq_=md_.chisq(); |
---|
60 | } |
---|
61 | |
---|
62 | |
---|
63 | const utility::Vector& PolynomialWeighted::fit_parameters(void) const |
---|
64 | { |
---|
65 | return md_.fit_parameters(); |
---|
66 | } |
---|
67 | |
---|
68 | |
---|
69 | double PolynomialWeighted::s2(const double w) const |
---|
70 | { |
---|
71 | return md_.s2(w); |
---|
72 | } |
---|
73 | |
---|
74 | double PolynomialWeighted::predict(const double x) const |
---|
75 | { |
---|
76 | utility::Vector vec(power_+1,1); |
---|
77 | for (size_t i=1; i<=power_; ++i) |
---|
78 | vec(i) = vec(i-1)*x; |
---|
79 | return md_.predict(vec); |
---|
80 | } |
---|
81 | |
---|
82 | double PolynomialWeighted::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 |
---|