1 | // $Id: MultiDimensional.cc 1486 2008-09-09 21:17:19Z jari $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen |
---|
5 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, 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 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 "MultiDimensional.h" |
---|
26 | #include "yat/utility/Exception.h" |
---|
27 | #include "yat/utility/Matrix.h" |
---|
28 | #include "yat/utility/VectorBase.h" |
---|
29 | #include "yat/utility/Vector.h" |
---|
30 | |
---|
31 | #include <cassert> |
---|
32 | |
---|
33 | namespace theplu { |
---|
34 | namespace yat { |
---|
35 | namespace regression { |
---|
36 | |
---|
37 | |
---|
38 | MultiDimensional::MultiDimensional(void) |
---|
39 | : chisquare_(0), work_(NULL) |
---|
40 | { |
---|
41 | } |
---|
42 | |
---|
43 | |
---|
44 | MultiDimensional::~MultiDimensional(void) |
---|
45 | { |
---|
46 | if (work_) |
---|
47 | gsl_multifit_linear_free(work_); |
---|
48 | } |
---|
49 | |
---|
50 | |
---|
51 | const utility::Matrix& MultiDimensional::covariance(void) const |
---|
52 | { |
---|
53 | return covariance_; |
---|
54 | } |
---|
55 | |
---|
56 | |
---|
57 | void MultiDimensional::fit(const utility::Matrix& x, |
---|
58 | const utility::VectorBase& y) |
---|
59 | { |
---|
60 | assert(x.rows()==y.size()); |
---|
61 | covariance_.resize(x.columns(),x.columns()); |
---|
62 | fit_parameters_ = utility::Vector(x.columns()); |
---|
63 | if (work_) |
---|
64 | gsl_multifit_linear_free(work_); |
---|
65 | if (!(work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size()))) |
---|
66 | throw utility::GSL_error("MultiDimensional::fit failed to allocate memory"); |
---|
67 | |
---|
68 | int status = gsl_multifit_linear(x.gsl_matrix_p(), y.gsl_vector_p(), |
---|
69 | fit_parameters_.gsl_vector_p(), |
---|
70 | covariance_.gsl_matrix_p(), &chisquare_, |
---|
71 | work_); |
---|
72 | if (status) |
---|
73 | throw utility::GSL_error(std::string("MultiDimensional::fit",status)); |
---|
74 | |
---|
75 | s2_ = chisquare_/(x.rows()-x.columns()); |
---|
76 | } |
---|
77 | |
---|
78 | const utility::Vector& MultiDimensional::fit_parameters(void) const |
---|
79 | { |
---|
80 | return fit_parameters_; |
---|
81 | } |
---|
82 | |
---|
83 | |
---|
84 | double MultiDimensional::chisq(void) const |
---|
85 | { |
---|
86 | return chisquare_; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | double MultiDimensional::predict(const utility::VectorBase& x) const |
---|
91 | { |
---|
92 | assert(x.size()==fit_parameters_.size()); |
---|
93 | return fit_parameters_ * x; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | double MultiDimensional::prediction_error2(const utility::VectorBase& x) const |
---|
98 | { |
---|
99 | return standard_error2(x) + s2_; |
---|
100 | } |
---|
101 | |
---|
102 | |
---|
103 | double MultiDimensional::standard_error2(const utility::VectorBase& x) const |
---|
104 | { |
---|
105 | double s2 = 0; |
---|
106 | for (size_t i=0; i<x.size(); ++i){ |
---|
107 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
108 | for (size_t j=i+1; j<x.size(); ++j) |
---|
109 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
110 | } |
---|
111 | return s2; |
---|
112 | } |
---|
113 | |
---|
114 | }}} // of namespaces regression, yat, and theplu |
---|