source: trunk/yat/regression/PolynomialWeighted.cc

Last change on this file was 4293, checked in by Peter, 8 weeks ago

minor style changes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 2.4 KB
Line 
1// $Id: PolynomialWeighted.cc 4293 2023-02-04 04:58:12Z peter $
2
3/*
4  Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson
5  Copyright (C) 2012, 2022 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
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    utility::Vector dummy(x.size(), 1.0);
55    add(ap_,x.begin(), x.end(),y.begin(),dummy.begin(),w.begin());
56    utility::Matrix X(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
77  double PolynomialWeighted::predict(const double x) const
78  {
79    utility::Vector vec(power_+1,1);
80    for (size_t i=1; i<=power_; ++i)
81      vec(i) = vec(i-1)*x;
82    return md_.predict(vec);
83  }
84
85
86  double PolynomialWeighted::standard_error2(const double x) const
87  {
88    utility::Vector vec(power_+1,1);
89    for (size_t i=1; i<=power_; ++i)
90      vec(i) = vec(i-1)*x;
91    return md_.standard_error2(vec);
92  }
93
94}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.