source: trunk/yat/regression/LinearWeighted.cc @ 718

Last change on this file since 718 was 718, checked in by Jari Häkkinen, 15 years ago

Addresses #170.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: LinearWeighted.cc 718 2006-12-26 09:56:26Z 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 "LinearWeighted.h"
25#include "yat/statistics/AveragerPairWeighted.h"
26#include "yat/utility/vector.h"
27
28#include <gsl/gsl_fit.h>
29
30namespace theplu {
31namespace yat {
32namespace regression {
33
34  LinearWeighted::LinearWeighted(void)
35    : OneDimensionalWeighted(), alpha_(0), alpha_var_(0), beta_(0),
36      beta_var_(0), m_x_(0), s2_(0)
37  {
38  }
39
40  LinearWeighted::~LinearWeighted(void)
41  {
42  }
43
44  double LinearWeighted::alpha(void) const
45  {
46    return alpha_;
47  }
48
49  double LinearWeighted::alpha_err(void) const
50  {
51    return sqrt(alpha_var_);
52  }
53
54  double LinearWeighted::beta(void) const
55  {
56    return beta_;
57  }
58
59  double LinearWeighted::beta_err(void) const
60  {
61    return sqrt(beta_var_);
62  }
63
64  void LinearWeighted::fit(const utility::vector& x,
65                           const utility::vector& y,
66                           const utility::vector& w)
67  {
68    // AveragerPairWeighted requires 2 weights but works only on the
69    // product wx*wy, so we can send in w and a dummie to get what we
70    // want.
71    ap_.reset();
72    ap_.add_values(x,y,utility::vector(x.size(),1),w);
73
74    // estimating the noise level. see attached document for motivation
75    // of the expression.
76    s2_= (syy()-sxy()*sxy()/sxx())/(w.sum()-2*(w*w)/w.sum()) ;
77   
78    alpha_ = m_y();
79    beta_ = sxy()/sxx();
80    alpha_var_ = ap_.y_averager().standard_error() * 
81      ap_.y_averager().standard_error();
82    beta_var_ = s2_/sxx(); 
83    m_x_=m_x();
84  }
85
86  double LinearWeighted::m_x(void) const
87  {
88    return ap_.x_averager().mean();
89  }
90
91  double LinearWeighted::m_y(void) const
92  {
93    return ap_.y_averager().mean();
94  }
95
96  double LinearWeighted::mse(void) const
97  {
98    return mse_;
99  }
100
101  double LinearWeighted::prediction_error(const double x, const double w) const
102  {
103    return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2(w));
104  }
105
106  double LinearWeighted::s2(double w) const
107  {
108    return s2_/w;
109  }
110
111  double LinearWeighted::standard_error(const double x) const
112  {
113    return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) );
114  }
115
116  double LinearWeighted::sxx(void) const
117  {
118    return ap_.x_averager().sum_xx_centered();
119  }
120
121  double LinearWeighted::sxy(void) const
122  {
123    return ap_.sum_xy_centered();
124  }
125
126  double LinearWeighted::syy(void) const
127  {
128    return ap_.y_averager().sum_xx_centered();
129  }
130
131}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.