1 | // $Id: PolynomialWeighted.cc 1043 2008-02-06 19:30:03Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Peter Johansson |
---|
5 | Copyright (C) 2007 Peter Johansson |
---|
6 | |
---|
7 | This file is part of the yat library, http://trac.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 2 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 this program; if not, write to the Free Software |
---|
21 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
22 | 02111-1307, USA. |
---|
23 | */ |
---|
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 (u_int 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 |
---|