1 | // $Id: MultiDimensional.cc 703 2006-12-18 00:47:44Z jari $ |
---|
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 | void MultiDimensional::fit(const utility::matrix& x, const utility::vector& y) |
---|
47 | { |
---|
48 | covariance_=utility::matrix(x.columns(),x.columns()); |
---|
49 | fit_parameters_=utility::vector(x.columns()); |
---|
50 | if (work_) |
---|
51 | gsl_multifit_linear_free(work_); |
---|
52 | work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size()); |
---|
53 | gsl_multifit_linear(x.gsl_matrix_p(),y.gsl_vector_p(), |
---|
54 | fit_parameters_.gsl_vector_p(), |
---|
55 | covariance_.gsl_matrix_p(),&chisquare_,work_); |
---|
56 | } |
---|
57 | |
---|
58 | |
---|
59 | double MultiDimensional::prediction_error(const utility::vector& x) const |
---|
60 | { |
---|
61 | double s2 = 0; |
---|
62 | for (size_t i=0; i<x.size(); ++i){ |
---|
63 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
64 | for (size_t j=i+1; j<x.size(); ++j) |
---|
65 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
66 | } |
---|
67 | return sqrt(s2+chisquare_); |
---|
68 | } |
---|
69 | |
---|
70 | |
---|
71 | double MultiDimensional::standard_error(const utility::vector& x) const |
---|
72 | { |
---|
73 | double s2 = 0; |
---|
74 | for (size_t i=0; i<x.size(); ++i){ |
---|
75 | s2 += covariance_(i,i)*x(i)*x(i); |
---|
76 | for (size_t j=i+1; j<x.size(); ++j) |
---|
77 | s2 += 2*covariance_(i,j)*x(i)*x(j); |
---|
78 | } |
---|
79 | return sqrt(s2); |
---|
80 | } |
---|
81 | |
---|
82 | }}} // of namespaces regression, yat, and theplu |
---|