IMP logo
IMP Reference Guide  2.20.0
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-2022 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 <cereal/access.hpp>
15 #include <vector>
16 
17 IMPALGEBRA_BEGIN_NAMESPACE
18 
19 //! Calculate line that fits best the input data points (Linear least squares)
20 class IMPALGEBRAEXPORT LinearFit2D : public GeometricPrimitiveD<2> {
21  public:
22  LinearFit2D() {}
23 
24  //! Constructor
25  /**
26  \param[in] data vector of pairs (VectorD<2>) with x and their
27  corresponding y values (linear least squares)
28  \param[in] error_bars vector of pairs (VectorD<3>) with x,
29  corresponding y values and y errors (weighted linear least squares)
30  */
31  LinearFit2D(const Vector2Ds& data, const Floats& error_bars = Floats());
32 
33  //! fit error
34  double get_fit_error() const { return error_; }
35 
36  //! get a value (a*x)
37  double get_a() const { return a_; }
38 
39  //! get b value (constant)
40  double get_b() const { return b_; }
41 
42  //! show equation
44  out << "y = " << a_ << "x + " << b_ << std::endl;
45  out << "Error = " << error_ << std::endl;
46  });
47 
48  private:
49  void find_regression(const Vector2Ds& data, const Floats& errors);
50  void evaluate_error(const Vector2Ds& data, const Floats& errors);
51  double a_, b_;
52  double error_;
53 
54  friend class cereal::access;
55 
56  template<class Archive> void serialize(Archive &ar) {
57  ar(a_, b_, error_);
58  }
59 };
60 
62 
63 #ifndef IMP_DOXYGEN
64 // backwards compat
65 typedef LinearFit2D LinearFit;
66 #endif
67 
68 IMPALGEBRA_END_NAMESPACE
69 
70 #endif /* IMPALGEBRA_LINEAR_FIT_H */
Base class for geometric types.
#define IMP_SHOWABLE_INLINE(Name, how_to_show)
Declare the methods needed by an object that can be printed.
IMP::Vector< Float > Floats
Standard way to pass a bunch of Float values.
Definition: types.h:46
Simple 2D vector class.
double get_b() const
get b value (constant)
Definition: LinearFit.h:40
Vector< VectorD< 2 > > Vector2Ds
Definition: VectorD.h:423
#define IMP_VALUES(Name, PluralName)
Define the type for storing sets of values.
Definition: value_macros.h:23
Base class for geometric types.
double get_a() const
get a value (a*x)
Definition: LinearFit.h:37
Calculate line that fits best the input data points (Linear least squares)
Definition: LinearFit.h:20
double get_fit_error() const
fit error
Definition: LinearFit.h:34