1 | // $Id: MultiDimensional.cc 726 2007-01-04 14:38:56Z peter $ |
---|
2 | |
---|
3 | /* |
---|
4 | Copyright (C) The authors contributing to this file. |
---|
5 | |
---|
6 | This file is part of the yat library, http://lev.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 "MultiDimensional.h" |
---|
25 | #include "yat/utility/matrix.h" |
---|
26 | #include "yat/utility/vector.h" |
---|
27 | |
---|
28 | namespace theplu { |
---|
29 | namespace yat { |
---|
30 | namespace regression { |
---|
31 | |
---|
32 | |
---|
33 | MultiDimensional::MultiDimensional(void) |
---|
34 | : chisquare_(0), work_(NULL) |
---|
35 | { |
---|
36 | } |
---|
37 | |
---|
38 | |
---|
39 | MultiDimensional::~MultiDimensional(void) |
---|
40 | { |
---|
41 | if (work_) |
---|
42 | gsl_multifit_linear_free(work_); |
---|
43 | } |
---|
44 | |
---|
45 | |
---|
46 | const utility::matrix& MultiDimensional::covariance(void) const |
---|
47 | { |
---|
48 | return covariance_; |
---|
49 | } |
---|
50 | |
---|
51 | |
---|
52 | void MultiDimensional::fit(const utility::matrix& x, const utility::vector& y) |
---|
53 | { |
---|
54 | covariance_=utility::matrix(x.columns(),x.columns()); |
---|
55 | fit_parameters_=utility::vector(x.columns()); |
---|
56 | if (work_) |
---|
57 | gsl_multifit_linear_free(work_); |
---|
58 | work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size()); |
---|
59 | gsl_multifit_linear(x.gsl_matrix_p(),y.gsl_vector_p(), |
---|
60 | fit_parameters_.gsl_vector_p(), |
---|
61 | covariance_.gsl_matrix_p(),&chisquare_,work_); |
---|
62 | } |
---|
63 | |
---|
64 | const utility::vector& MultiDimensional::fit_parameters(void) const |
---|
65 | { |
---|
66 | return fit_parameters_; |
---|
67 | } |
---|
68 | |
---|
69 | |
---|
70 | double MultiDimensional::chisq(void) const |
---|
71 | { |
---|
72 | return chisquare_; |
---|
73 | } |
---|
74 | |
---|
75 | |
---|
76 | double MultiDimensional::predict(const utility::vector& x) const |
---|
77 | { |
---|
78 | return fit_parameters_ * x; |
---|
79 | } |
---|
80 | |
---|
81 | |
---|
82 | double MultiDimensional::prediction_error(const utility::vector& x) const |
---|
83 | { |
---|
84 | double s2 = 0; |
---|
85 | for (size_t i=0; i<x.size(); ++i){ |
---|
86 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
87 | for (size_t j=i+1; j<x.size(); ++j) |
---|
88 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
89 | } |
---|
90 | return sqrt(s2+chisquare_); |
---|
91 | } |
---|
92 | |
---|
93 | |
---|
94 | double MultiDimensional::standard_error(const utility::vector& x) const |
---|
95 | { |
---|
96 | double s2 = 0; |
---|
97 | for (size_t i=0; i<x.size(); ++i){ |
---|
98 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
99 | for (size_t j=i+1; j<x.size(); ++j) |
---|
100 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
101 | } |
---|
102 | return sqrt(s2); |
---|
103 | } |
---|
104 | |
---|
105 | }}} // of namespaces regression, yat, and theplu |
---|