gtsam  3.2.1
gtsam
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
WhiteNoiseFactor.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
22 #include <gtsam/base/LieScalar.h>
23 #include <cmath>
24 
25 namespace gtsam {
26 
27  const double logSqrt2PI = log(std::sqrt(2.0 * M_PI));
28 
41 
42  private:
43 
44  double z_;
45 
46  Key meanKey_;
47  Key precisionKey_;
48 
49  typedef NonlinearFactor Base;
50 
51  public:
52 
60  static double f(double z, double u, double p) {
61  return logSqrt2PI - 0.5 * log(p) + 0.5 * (z - u) * (z - u) * p;
62  }
63 
74  static HessianFactor::shared_ptr linearize(double z, double u, double p,
75  Key j1, Key j2) {
76  double e = u - z, e2 = e * e;
77  double c = 2 * logSqrt2PI - log(p) + e2 * p;
78  Vector g1 = (Vector(1) << -e * p);
79  Vector g2 = (Vector(1) << 0.5 / p - 0.5 * e2);
80  Matrix G11 = (Matrix(1, 1) << p);
81  Matrix G12 = (Matrix(1, 1) << e);
82  Matrix G22 = (Matrix(1, 1) << 0.5 / (p * p));
84  new HessianFactor(j1, j2, G11, G12, g1, G22, g2, c));
85  }
86 
89 
95  WhiteNoiseFactor(double z, Key meanKey, Key precisionKey) :
96  Base(), z_(z), meanKey_(meanKey), precisionKey_(precisionKey) {
97  }
98 
102 
104  virtual ~WhiteNoiseFactor() {
105  }
106 
110 
112  void print(const std::string& p = "WhiteNoiseFactor",
113  const KeyFormatter& keyFormatter = DefaultKeyFormatter) const {
114  Base::print(p, keyFormatter);
115  std::cout << p + ".z: " << z_ << std::endl;
116  }
117 
121 
123  virtual size_t dim() const {
124  return 2;
125  }
126 
128  inline double error(const Values& x) const {
129  return f(z_, x.at<LieScalar>(meanKey_), x.at<LieScalar>(precisionKey_));
130  }
131 
139  virtual Vector unwhitenedError(const Values& x) const {
140  return (Vector(1) << std::sqrt(2 * error(x)));
141  }
142 
147 // virtual IndexFactor::shared_ptr symbolic(const Ordering& ordering) const {
148 // const Key j1 = ordering[meanKey_], j2 = ordering[precisionKey_];
149 // return IndexFactor::shared_ptr(new IndexFactor(j1, j2));
150 // }
151 
155 
157  virtual boost::shared_ptr<GaussianFactor> linearize(const Values& x) const {
158  double u = x.at<LieScalar>(meanKey_);
159  double p = x.at<LieScalar>(precisionKey_);
160  Key j1 = meanKey_;
161  Key j2 = precisionKey_;
162  return linearize(z_, u, p, j1, j2);
163  }
164 
165  // TODO: Frank commented this out for now, can it go?
166  // /// @return a deep copy of this factor
167  // virtual gtsam::NonlinearFactor::shared_ptr clone() const {
168  // return boost::static_pointer_cast<gtsam::NonlinearFactor>(
169  // gtsam::NonlinearFactor::shared_ptr(new This(*this))); }
170 
172 
173  };
174 // WhiteNoiseFactor
175 
176 }// namespace gtsam
177 
Non-linear factor base classes.
A wrapper around scalar providing Lie compatibility.
virtual void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print
Definition: NonlinearFactor.h:84
const double logSqrt2PI
constant needed below
Definition: WhiteNoiseFactor.h:27
LieScalar is a wrapper around double to allow it to be a Lie type.
Definition: LieScalar.h:29
A Gaussian factor using the canonical parameters (information form)
Definition: HessianFactor.h:131
Contains the HessianFactor class, a general quadratic factor.
This is the base class for all factor types.
Definition: Factor.h:51
A non-templated config holding any types of Manifold-group elements.
Definition: Values.h:75
const ValueType & at(Key j) const
Retrieve a variable by key j.
Definition: Values-inl.h:219
double error(const Values &x) const
Calculate the error of the factor, typically equal to log-likelihood.
Definition: WhiteNoiseFactor.h:128
virtual Vector unwhitenedError(const Values &x) const
Vector of errors "unwhitened" does not make sense for this factor What is meant typically is only "e"...
Definition: WhiteNoiseFactor.h:139
size_t Key
Integer nonlinear key type.
Definition: types.h:59
WhiteNoiseFactor(double z, Key meanKey, Key precisionKey)
Construct from measurement.
Definition: WhiteNoiseFactor.h:95
Binary factor to estimate parameters of zero-mean Gaussian white noise.
Definition: WhiteNoiseFactor.h:40
static HessianFactor::shared_ptr linearize(double z, double u, double p, Key j1, Key j2)
linearize returns a Hessianfactor that approximates error Hessian is Taylor expansion is So f = 2 f...
Definition: WhiteNoiseFactor.h:74
void print(const std::string &p="WhiteNoiseFactor", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
Print.
Definition: WhiteNoiseFactor.h:112
virtual ~WhiteNoiseFactor()
Destructor.
Definition: WhiteNoiseFactor.h:104
virtual boost::shared_ptr< GaussianFactor > linearize(const Values &x) const
linearize returns a Hessianfactor that is an approximation of error(p)
Definition: WhiteNoiseFactor.h:157
virtual size_t dim() const
get the dimension of the factor (number of rows on linearization)
Definition: WhiteNoiseFactor.h:123
Nonlinear factor base class.
Definition: NonlinearFactor.h:54
boost::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: types.h:62
static double f(double z, double u, double p)
negative log likelihood as a function of mean and precision
Definition: WhiteNoiseFactor.h:60
boost::shared_ptr< This > shared_ptr
A shared_ptr to this class.
Definition: HessianFactor.h:140