1 | // $Id: MultiDimensional.cc 4207 2022-08-26 04:36:28Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) 2005 Jari Häkkinen |
---|
5 | Copyright (C) 2006, 2007, 2008 Jari Häkkinen, Peter Johansson |
---|
6 | Copyright (C) 2011, 2012, 2020, 2022 Peter Johansson |
---|
7 | |
---|
8 | This file is part of the yat library, http://dev.thep.lu.se/yat |
---|
9 | |
---|
10 | The yat library is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License as |
---|
12 | published by the Free Software Foundation; either version 3 of the |
---|
13 | License, or (at your option) any later version. |
---|
14 | |
---|
15 | The yat library is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with yat. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | */ |
---|
23 | |
---|
24 | #include <config.h> |
---|
25 | |
---|
26 | #include "MultiDimensional.h" |
---|
27 | #include "yat/utility/Exception.h" |
---|
28 | #include "yat/utility/Matrix.h" |
---|
29 | #include "yat/utility/VectorBase.h" |
---|
30 | #include "yat/utility/Vector.h" |
---|
31 | |
---|
32 | #include <cassert> |
---|
33 | |
---|
34 | namespace theplu { |
---|
35 | namespace yat { |
---|
36 | namespace regression { |
---|
37 | |
---|
38 | |
---|
39 | MultiDimensional::MultiDimensional(void) |
---|
40 | : chisquare_(0), work_(NULL) |
---|
41 | { |
---|
42 | } |
---|
43 | |
---|
44 | |
---|
45 | MultiDimensional::~MultiDimensional(void) |
---|
46 | { |
---|
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 | fit2(x, y); |
---|
61 | } |
---|
62 | |
---|
63 | |
---|
64 | void MultiDimensional::fit2(const utility::MatrixBase& x, |
---|
65 | const utility::VectorBase& y) |
---|
66 | { |
---|
67 | assert(x.rows()==y.size()); |
---|
68 | covariance_.resize(x.columns(),x.columns()); |
---|
69 | fit_parameters_.resize(x.columns()); |
---|
70 | gsl_multifit_linear_free(work_); |
---|
71 | if (!(work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size()))) |
---|
72 | throw utility::GSL_error("MultiDimensional::fit failed to allocate memory"); |
---|
73 | |
---|
74 | int status = gsl_multifit_linear(x.gsl_matrix_p(), y.gsl_vector_p(), |
---|
75 | fit_parameters_.gsl_vector_p(), |
---|
76 | covariance_.gsl_matrix_p(), &chisquare_, |
---|
77 | work_); |
---|
78 | if (status) |
---|
79 | throw utility::GSL_error(std::string("MultiDimensional::fit",status)); |
---|
80 | |
---|
81 | s2_ = chisquare_/(x.rows()-x.columns()); |
---|
82 | } |
---|
83 | |
---|
84 | |
---|
85 | const utility::Vector& MultiDimensional::fit_parameters(void) const |
---|
86 | { |
---|
87 | return fit_parameters_; |
---|
88 | } |
---|
89 | |
---|
90 | |
---|
91 | double MultiDimensional::chisq(void) const |
---|
92 | { |
---|
93 | return chisquare_; |
---|
94 | } |
---|
95 | |
---|
96 | |
---|
97 | double MultiDimensional::predict(const utility::VectorBase& x) const |
---|
98 | { |
---|
99 | assert(x.size()==fit_parameters_.size()); |
---|
100 | return fit_parameters_ * x; |
---|
101 | } |
---|
102 | |
---|
103 | |
---|
104 | double MultiDimensional::prediction_error2(const utility::VectorBase& x) const |
---|
105 | { |
---|
106 | return standard_error2(x) + s2_; |
---|
107 | } |
---|
108 | |
---|
109 | |
---|
110 | double MultiDimensional::standard_error2(const utility::VectorBase& x) const |
---|
111 | { |
---|
112 | double s2 = 0; |
---|
113 | for (size_t i=0; i<x.size(); ++i){ |
---|
114 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
115 | for (size_t j=i+1; j<x.size(); ++j) |
---|
116 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
117 | } |
---|
118 | return s2; |
---|
119 | } |
---|
120 | |
---|
121 | }}} // of namespaces regression, yat, and theplu |
---|