source: trunk/c++_tools/statistics/MultiDimensional.cc @ 586

Last change on this file since 586 was 586, checked in by Peter, 15 years ago

closes #23 redesign of regression classes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 1.2 KB
Line 
1// $Id: MultiDimensional.cc 586 2006-06-19 09:56:04Z peter $
2
3#include <c++_tools/statistics/MultiDimensional.h>
4#include <c++_tools/gslapi/matrix.h>
5#include <c++_tools/gslapi/vector.h>
6
7namespace theplu {
8namespace statistics {
9namespace regression {
10
11
12  void MultiDimensional::fit(const gslapi::matrix& x, const gslapi::vector& y)
13  {
14    covariance_=gslapi::matrix(x.columns(),x.columns());
15    fit_parameters_=gslapi::vector(x.columns());
16    if (work_)
17      gsl_multifit_linear_free(work_);
18    work_=gsl_multifit_linear_alloc(x.rows(),fit_parameters_.size());
19    gsl_multifit_linear(x.gsl_matrix_p(),y.gsl_vector_p(),
20                        fit_parameters_.gsl_vector_p(),
21                        covariance_.gsl_matrix_p(),&chisquare_,work_);
22  }
23
24
25  double MultiDimensional::prediction_error(const gslapi::vector& x) const
26  {
27    double s2 = 0;
28    for (size_t i=0; i<x.size(); ++i){
29      s2 += covariance_(i,i)*x(i)*x(i);
30      for (size_t j=i+1; j<x.size(); ++j)
31        s2 += 2*covariance_(i,j)*x(i)*x(j);
32    }
33    return sqrt(s2+chisquare_);
34  }
35
36
37  double MultiDimensional::standard_error(const gslapi::vector& x) const
38  {
39    double s2 = 0;
40    for (size_t i=0; i<x.size(); ++i){
41      s2 += covariance_(i,i)*x(i)*x(i);
42      for (size_t j=i+1; j<x.size(); ++j)
43        s2 += 2*covariance_(i,j)*x(i)*x(j);
44    }
45    return sqrt(s2);
46  }
47
48}}} // of namespaces regression, statisitcs and thep
Note: See TracBrowser for help on using the repository browser.