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

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

Fixes #159. Also removed some inlines in OneDimensionalWeighted? by adding source file. Refs #81

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