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