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
20template<typename T, typename U=T>
22{
23public:
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
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
58protected:
59 T S, S_x, S_y, S_xx, S_xy;
60};
61
62#endif
63
T get_value() const
U get_variance() const
Estimate< T, U > get_slope() const
Return the slope, b.
Definition StraightLine.h:50
StraightLine()
Default constructor.
Definition StraightLine.h:26
void add_coordinate(double x, const Estimate< T, U > &d)
Addition operator.
Definition StraightLine.h:29
Estimate< T, U > get_intercept() const
Return the intercept, a.
Definition StraightLine.h:41

Generated using doxygen 1.14.0