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

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

Removed redundant #includes.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 3.0 KB
Line 
1// $Id: LinearWeighted.cc 724 2007-01-01 16:12:32Z 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
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_err(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_err(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::mse(void) const
95  {
96    return mse_;
97  }
98
99  double LinearWeighted::prediction_error(const double x, const double w) const
100  {
101    return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_)+s2(w));
102  }
103
104  double LinearWeighted::s2(double w) const
105  {
106    return s2_/w;
107  }
108
109  double LinearWeighted::standard_error(const double x) const
110  {
111    return sqrt(alpha_var_ + beta_var_*(x-m_x_)*(x-m_x_) );
112  }
113
114  double LinearWeighted::sxx(void) const
115  {
116    return ap_.x_averager().sum_xx_centered();
117  }
118
119  double LinearWeighted::sxy(void) const
120  {
121    return ap_.sum_xy_centered();
122  }
123
124  double LinearWeighted::syy(void) const
125  {
126    return ap_.y_averager().sum_xx_centered();
127  }
128
129}}} // of namespaces regression, yat, and theplu
Note: See TracBrowser for help on using the repository browser.