IMP  2.0.1
The Integrative Modeling Platform
LinearFit.h
Go to the documentation of this file.
1 /**
2  * \file IMP/algebra/LinearFit.h
3  * \brief Linear fit of data points
4  *
5  * Copyright 2007-2013 IMP Inventors. All rights reserved.
6  *
7  */
8 
9 #ifndef IMPALGEBRA_LINEAR_FIT_H
10 #define IMPALGEBRA_LINEAR_FIT_H
11 
12 #include "Vector2D.h"
13 #include "GeometricPrimitiveD.h"
14 #include <vector>
15 
16 IMPALGEBRA_BEGIN_NAMESPACE
17 
18 //! Calculate line that fits best the input data points (Linear least squares)
19 class IMPALGEBRAEXPORT LinearFit2D:
20  public GeometricPrimitiveD<2> {
21 public:
22  //! Constructor
23  /**
24  \param[in] data vector of pairs (VectorD<2>) with x and their
25  corresponding y values (linear least squares)
26  \param[in] error_bars vector of pairs (VectorD<3>) with x,
27  corresponding y values and y errors (weighted linear least squares)
28  */
29  LinearFit2D(const Vector2Ds& data,
30  const Floats &error_bars=Floats());
31 
32  //! fit error
33  double get_fit_error() const { return error_; }
34 
35  //! get a value (a*x)
36  double get_a() const { return a_; }
37 
38  //! get b value (constant)
39  double get_b() const { return b_; }
40 
41  //! show equation
43  {out << "y = " << a_ << "x + " << b_ << std::endl;
44  out << "Error = " << error_ << std::endl;
45  });
46 
47  private:
48  void find_regression(const Vector2Ds& data, const Floats &errors);
49  void evaluate_error(const Vector2Ds& data, const Floats &errors);
50  double a_, b_;
51  double error_;
52 };
53 
55 
56 #ifndef IMP_DOXYGEN
57 // backwards compat
58 typedef LinearFit2D LinearFit;
59 #endif
60 
61 IMPALGEBRA_END_NAMESPACE
62 
63 #endif /* IMPALGEBRA_LINEAR_FIT_H */