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