1 | // $Id: PolynomialWeighted.cc 741 2007-01-13 14:41:40Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.thep.lu.se/trac/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 2 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 this program; if not, write to the Free Software |
---|
20 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
---|
21 | 02111-1307, USA. |
---|
22 | */ |
---|
23 | |
---|
24 | #include "PolynomialWeighted.h" |
---|
25 | #include "yat/utility/matrix.h" |
---|
26 | #include "yat/utility/vector.h" |
---|
27 | |
---|
28 | #include <cassert> |
---|
29 | |
---|
30 | namespace theplu { |
---|
31 | namespace yat { |
---|
32 | namespace regression { |
---|
33 | |
---|
34 | PolynomialWeighted::PolynomialWeighted(size_t power) |
---|
35 | : OneDimensionalWeighted(), power_(power) |
---|
36 | { |
---|
37 | } |
---|
38 | |
---|
39 | PolynomialWeighted::~PolynomialWeighted(void) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | void PolynomialWeighted::fit(const utility::vector& x, |
---|
44 | const utility::vector& y, |
---|
45 | const utility::vector& w) |
---|
46 | { |
---|
47 | assert(x.size()==y.size()); |
---|
48 | assert(y.size()==w.size()); |
---|
49 | ap_.reset(); |
---|
50 | // AveragerPairWeighted requires 2 weights but works only on the |
---|
51 | // product wx*wy, so we can send in w and a dummie to get what we |
---|
52 | // want. |
---|
53 | ap_.add_values(x,y,utility::vector(x.size(),1),w); |
---|
54 | utility::matrix X=utility::matrix(x.size(),power_+1,1); |
---|
55 | for (size_t i=0; i<X.rows(); ++i) |
---|
56 | for (u_int 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=1.0) 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 |
---|