StraightLine.h
1 //-*-C++-*-
2 /***************************************************************************
3  *
4  * Copyright (C) 2008 by Willem van Straten
5  * Licensed under the Academic Free License version 2.1
6  *
7  ***************************************************************************/
8 
9 // psrchive/Util/genutil/StraightLine.h
10 
11 #ifndef __StraightLine_h
12 #define __StraightLine_h
13 
14 #include "Estimate.h"
15 
17 
20 template<typename T, typename U=T>
22 {
23 public:
24 
26  StraightLine () { S = S_x = S_y = S_xx = S_xy = 0; }
27 
29  void add_coordinate (double x, const Estimate<T,U>& d)
30  {
31  T y = d.get_value();
32  U w = 1.0/d.get_variance();
33  S += w;
34  S_x += x * w;
35  S_y += y * w;
36  S_xx += x*x * w;
37  S_xy += x*y * w;
38  }
39 
42  {
43  T delta = S * S_xx - S_x * S_x;
44  T slope = (S_xx * S_y - S_x * S_xy) / delta;
45  T var = S_xx / delta;
46  return Estimate<T,U> (slope, var);
47  }
48 
50  Estimate<T,U> get_slope () const
51  {
52  T delta = S * S_xx - S_x * S_x;
53  T intercept = (S * S_xy - S_x * S_y) / delta;
54  T var = S / delta;
55  return Estimate<T,U> (intercept, var);
56  }
57 
58 protected:
59  T S, S_x, S_y, S_xx, S_xy;
60 };
61 
62 #endif
63 
Estimates with a value, , and a variance, .
Definition: Estimate.h:33
StraightLine()
Default constructor.
Definition: StraightLine.h:31
Estimate< T, U > get_intercept() const
Return the intercept, a.
Definition: StraightLine.h:46
void add_coordinate(double x, const Estimate< T, U > &d)
Addition operator.
Definition: StraightLine.h:34
Estimate< T, U > get_slope() const
Return the slope, b.
Definition: StraightLine.h:55
Linear least squares fit to a straight line.
Definition: StraightLine.h:21
T get_value() const
U get_variance() const

Generated using doxygen 1.8.17