source: trunk/yat/regression/PolynomialWeighted.cc @ 1020

Last change on this file since 1020 was 1020, checked in by Peter, 15 years ago

passing VectorBase? in regression::OneDimesionalWeighted? - refs #256

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: PolynomialWeighted.cc 1020 2008-02-01 17:17:26Z 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
31namespace theplu {
32namespace yat {
33namespace 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    ap_.add_values(x,y,utility::vector(x.size(),1),w);
55    utility::matrix X=utility::matrix(x.size(),power_+1,1);
56    for (size_t i=0; i<X.rows(); ++i)
57      for (u_int j=1; j<X.columns(); j++)
58        X(i,j)=X(i,j-1)*x(i);
59    md_.fit(X,y,w);
60    chisq_=md_.chisq();
61  }
62
63
64  const utility::vector& PolynomialWeighted::fit_parameters(void) const
65  {
66    return md_.fit_parameters(); 
67  }
68
69
70  double PolynomialWeighted::s2(const double w) const
71  {
72    return md_.s2(w);
73  }
74
75  double PolynomialWeighted::predict(const double x) const
76  {
77    utility::vector vec(power_+1,1);
78    for (size_t i=1; i<=power_; ++i)
79      vec(i) = vec(i-1)*x;
80    return md_.predict(vec);
81  }
82
83  double PolynomialWeighted::standard_error2(const double x) const
84  {
85    utility::vector vec(power_+1,1);
86    for (size_t i=1; i<=power_; ++i)
87      vec(i) = vec(i-1)*x;
88    return md_.standard_error2(vec);
89  }
90
91}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.