1 | // $Id: PolynomialWeighted.cc 1797 2009-02-12 18:07:10Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
5 | |
---|
6 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
7 | |
---|
8 | The yat library is free software; you can redistribute it and/or |
---|
9 | modify it under the terms of the GNU General Public License as |
---|
10 | published by the Free Software Foundation; either version 3 of the |
---|
11 | License, or (at your option) any later version. |
---|
12 | |
---|
13 | The yat library is distributed in the hope that it will be useful, |
---|
14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
16 | General Public License for more details. |
---|
17 | |
---|
18 | You should have received a copy of the GNU General Public License |
---|
19 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
20 | */ |
---|
21 | |
---|
22 | #include "PolynomialWeighted.h" |
---|
23 | #include "yat/utility/Matrix.h" |
---|
24 | #include "yat/utility/Vector.h" |
---|
25 | |
---|
26 | #include <cassert> |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace regression { |
---|
31 | |
---|
32 | PolynomialWeighted::PolynomialWeighted(size_t power) |
---|
33 | : OneDimensionalWeighted(), power_(power) |
---|
34 | { |
---|
35 | } |
---|
36 | |
---|
37 | PolynomialWeighted::~PolynomialWeighted(void) |
---|
38 | { |
---|
39 | } |
---|
40 | |
---|
41 | void PolynomialWeighted::fit(const utility::VectorBase& x, |
---|
42 | const utility::VectorBase& y, |
---|
43 | const utility::VectorBase& w) |
---|
44 | { |
---|
45 | assert(x.size()==y.size()); |
---|
46 | assert(y.size()==w.size()); |
---|
47 | ap_.reset(); |
---|
48 | // AveragerPairWeighted requires 2 weights but works only on the |
---|
49 | // product wx*wy, so we can send in w and a dummie to get what we |
---|
50 | // want. |
---|
51 | utility::Vector dummy(x.size(), 1.0); |
---|
52 | add(ap_,x.begin(), x.end(),y.begin(),dummy.begin(),w.begin()); |
---|
53 | utility::Matrix X=utility::Matrix(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,w); |
---|
58 | chisq_=md_.chisq(); |
---|
59 | } |
---|
60 | |
---|
61 | |
---|
62 | const utility::Vector& PolynomialWeighted::fit_parameters(void) const |
---|
63 | { |
---|
64 | return md_.fit_parameters(); |
---|
65 | } |
---|
66 | |
---|
67 | |
---|
68 | double PolynomialWeighted::s2(const double w) const |
---|
69 | { |
---|
70 | return md_.s2(w); |
---|
71 | } |
---|
72 | |
---|
73 | double PolynomialWeighted::predict(const double x) const |
---|
74 | { |
---|
75 | utility::Vector vec(power_+1,1); |
---|
76 | for (size_t i=1; i<=power_; ++i) |
---|
77 | vec(i) = vec(i-1)*x; |
---|
78 | return md_.predict(vec); |
---|
79 | } |
---|
80 | |
---|
81 | double PolynomialWeighted::standard_error2(const double x) const |
---|
82 | { |
---|
83 | utility::Vector vec(power_+1,1); |
---|
84 | for (size_t i=1; i<=power_; ++i) |
---|
85 | vec(i) = vec(i-1)*x; |
---|
86 | return md_.standard_error2(vec); |
---|
87 | } |
---|
88 | |
---|
89 | }}} // of namespaces regression, yat, and theplu |
---|