source: trunk/yat/regression/MultiDimensionalWeighted.cc @ 916

Last change on this file since 916 was 916, checked in by Peter, 14 years ago

Sorry this commit is a bit to big.

Adding a yat_assert. The yat assert are turned on by providing a
'-DYAT_DEBUG' flag to preprocessor if normal cassert is turned
on. This flag is activated for developers running configure with
--enable-debug. The motivation is that we can use these yat_asserts in
header files and the yat_asserts will be invisible to the normal user
also if he uses C-asserts.

added output operator in DataLookup2D and removed output operator in
MatrixLookup?

Removed template function add_values in Averager and weighted version

Added function to AveragerWeighted? taking iterator to four ranges.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.2 KB
Line 
1// $Id: MultiDimensionalWeighted.cc 916 2007-09-30 00:50:10Z peter $
2
3/*
4  Copyright (C) 2006, 2007 Jari Häkkinen, Peter Johansson
5
6  This file is part of the yat library, http://trac.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 "MultiDimensionalWeighted.h"
25#include "yat/statistics/AveragerWeighted.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  MultiDimensionalWeighted::MultiDimensionalWeighted(void)
36    : chisquare_(0), work_(NULL)
37  {
38  }
39
40  MultiDimensionalWeighted::~MultiDimensionalWeighted(void)
41  {
42    if (work_)
43      gsl_multifit_linear_free(work_);
44  }
45
46
47  double MultiDimensionalWeighted::chisq() const
48  {
49    return chisquare_;
50  }
51
52
53  void MultiDimensionalWeighted::fit(const utility::matrix& x, 
54                                     const utility::vector& y,
55                                     const utility::vector& w)
56  {
57    assert(y.size()==w.size());
58    assert(x.rows()==y.size());
59
60    covariance_.clone(utility::matrix(x.columns(),x.columns()));
61    fit_parameters_.clone(utility::vector(x.columns()));
62    if (work_)
63      gsl_multifit_linear_free(work_);
64    if (!(work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size())))
65      throw utility::GSL_error("MultiDimensionalWeighted::fit failed to allocate memory");
66    int status = gsl_multifit_wlinear(x.gsl_matrix_p(), w.gsl_vector_p(),
67                                      y.gsl_vector_p(), 
68                                      fit_parameters_.gsl_vector_p(),
69                                      covariance_.gsl_matrix_p(), &chisquare_,
70                                      work_);
71    if (status)
72      throw utility::GSL_error(std::string("MultiDimensionalWeighted::fit",
73                                           status));
74
75    statistics::AveragerWeighted aw;
76    add(aw, y.begin(), y.end(), w.begin());
77    s2_ = chisquare_ / (aw.n()-fit_parameters_.size());
78    covariance_ *= s2_;
79  }
80
81
82  const utility::vector& MultiDimensionalWeighted::fit_parameters(void) const
83  {
84    return fit_parameters_;
85  }
86
87
88  double MultiDimensionalWeighted::predict(const utility::vector& x) const
89  {
90    assert(x.size()==fit_parameters_.size());
91    return fit_parameters_ * x;
92  }
93
94
95  double MultiDimensionalWeighted::prediction_error2(const utility::vector& x,
96                                                     const double w) const
97  {
98    return standard_error2(x) + s2(w);
99  }
100
101
102  double MultiDimensionalWeighted::s2(const double w) const
103  {
104    return s2_/w;
105  }
106
107
108  double 
109  MultiDimensionalWeighted::standard_error2(const utility::vector& x) const
110  {
111    double c = 0;
112    for (size_t i=0; i<x.size(); ++i){
113      c += covariance_(i,i)*x(i)*x(i);
114      for (size_t j=i+1; j<x.size(); ++j)
115        c += 2*covariance_(i,j)*x(i)*x(j);
116    }
117    return c;
118  }
119
120}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.