LSSTApplications  10.0+286,10.0+36,10.0+46,10.0-2-g4f67435,10.1+152,10.1+37,11.0,11.0+1,11.0-1-g47edd16,11.0-1-g60db491,11.0-1-g7418c06,11.0-2-g04d2804,11.0-2-g68503cd,11.0-2-g818369d,11.0-2-gb8b8ce7
LSSTDataManagementBasePackage
GaussianProcess.h
Go to the documentation of this file.
1 // -*- LSST-C++ -*-
2 
3 /*
4  * LSST Data Management System
5  * Copyright 2008, 2009, 2010 LSST Corporation.
6  *
7  * This product includes software developed by the
8  * LSST Project (http://www.lsst.org/).
9  *
10  * This program is free software: you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation, either version 3 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the LSST License Statement and
21  * the GNU General Public License along with this program. If not,
22  * see <http://www.lsstcorp.org/LegalNotices/>.
23  */
24 
25 
26 #ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
27 #define LSST_AFW_MATH_GAUSSIAN_PROCESS_H
28 
29 #include <Eigen/Dense>
30 #include <stdexcept>
31 
32 #include "ndarray/eigen.h"
33 #include "boost/shared_ptr.hpp"
34 
35 #include "lsst/daf/base/Citizen.h"
36 #include "lsst/daf/base/DateTime.h"
37 #include "lsst/pex/exceptions.h"
38 #include "lsst/pex/policy.h"
39 #include "lsst/pex/policy/Policy.h"
40 
41 namespace lsst {
42 namespace afw {
43 namespace math {
44 
67 
68 public:
69 
71 
76  void reset();
77 
81  void start();
82 
86  void addToEigen();
87 
91  void addToVariance();
92 
96  void addToSearch();
97 
101  void addToIteration();
102 
108  void addToTotal(int i);
109 
113  void display();
114 
115 private:
119 };
120 
121 
130 template <typename T>
132  #ifndef SWIG
133  , private boost::noncopyable
134  #endif
135 {
136 public:
137  virtual ~Covariogram();
138 
142  explicit Covariogram():lsst::daf::base::Citizen(typeid(this)){};
143 
144 
152  virtual T operator() (ndarray::Array<const T,1,1> const &p1,
154  ) const;
155 
156 };
157 
167 template <typename T>
169 
170 public:
171  virtual ~SquaredExpCovariogram();
172 
173  explicit SquaredExpCovariogram();
174 
179  void setEllSquared(double ellSquared);
180 
181  virtual T operator() (ndarray::Array<const T,1,1> const &,
183  ) const;
184 
185 private:
186  double _ellSquared;
187 
188 };
189 
202 template <typename T>
204 
205 public:
206  virtual ~NeuralNetCovariogram();
207 
208  explicit NeuralNetCovariogram();
209 
213  void setSigma0(double sigma0);
214 
218  void setSigma1(double sigma1);
219 
220  virtual T operator() (ndarray::Array<const T,1,1> const &,
222  ) const;
223 
224 private:
225  double _sigma0,_sigma1;
226 
227 
228 };
229 
240 template <typename T>
241 class KdTree
242  #ifndef SWIG
243  : private boost::noncopyable
244  #endif
245 {
246 public:
247 
256  void Initialize(ndarray::Array<T,2,2> const &dt);
257 
276  int n_nn) const;
277 
278 
286  T getData(int ipt, int idim) const;
287 
288 
310  ndarray::Array<T,1,1> getData(int ipt) const;
311 
319  void addPoint(ndarray::Array<const T,1,1> const &v);
320 
329  void removePoint(int dex);
330 
334  int getPoints() const;
335 
343  void getTreeNode(ndarray::Array<int,1,1> const &v,int dex) const;
344 
345 
346 private:
350 
352 
353  //_tree stores the relationships between data points
354  //_tree[i][DIMENSION] is the dimension on which the ith node segregates its daughters
355  //
356  //_tree[i][LT] is the branch of the tree down which the daughters' DIMENSIONth component
357  //is less than the parent's
358  //
359  //_tree[i][GEQ] is the branch of the tree down which the daughters' DIMENSIONth component is
360  //greather than or equal to the parent's
361  //
362  //_tree[i][PARENT] is the parent node of the ith node
363 
364  //_data actually stores the data points
365 
366 
369 
370  //_room denotes the capacity of _data and _tree. It will usually be larger
371  //than _pts so that we do not have to reallocate
372  //_tree and _data every time we add a new point to the tree
373 
376 
389  void _organize(ndarray::Array<int,1,1> const &use,
390  int ct,
391  int parent,
392  int dir);
393 
399  int _findNode(ndarray::Array<const T,1,1> const &v) const;
400 
418  int consider,
419  int from) const;
420 
424  int _testTree() const;
425 
439  int _walkUpTree(int target,
440  int dir,
441  int root) const;
442 
451  void _count(int where,int *ct) const;
452 
458  void _descend(int root);
459 
465  void _reassign(int target);
466 
470  double _distance(ndarray::Array<const T,1,1> const &p1,
471  ndarray::Array<const T,1,1> const &p2) const;
472 };
473 
474 
498 template <typename T>
500  #ifndef SWIG
501  : private boost::noncopyable
502  #endif
503 {
504 
505 public:
506 
520  ndarray::Array<T,1,1> const &ff,
521  boost::shared_ptr< Covariogram<T> > const &covarIn);
522 
543  ndarray::Array<T,1,1> const &mn,
544  ndarray::Array<T,1,1> const &mx,
545  ndarray::Array<T,1,1> const &ff,
546  boost::shared_ptr< Covariogram<T> > const &covarIn);
560  ndarray::Array<T,2,2> const &ff,
561  boost::shared_ptr< Covariogram<T> > const &covarIn);
579  ndarray::Array<T,1,1> const &mn,
580  ndarray::Array<T,1,1> const &mx,
581  ndarray::Array<T,2,2> const &ff,
582  boost::shared_ptr< Covariogram<T> > const &covarIn);
583 
602  ndarray::Array<T,1,1> const &vin,
603  int numberOfNeighbors) const;
620  ndarray::Array<T,1,1> variance,
621  ndarray::Array<T,1,1> const &vin,
622  int numberOfNeighbors) const;
623 
644  int dex,
645  int numberOfNeighbors) const;
646 
662  ndarray::Array<T,1,1> variance,
663  int dex,
664  int numberOfNeighbors) const;
665 
688  ndarray::Array<T,1,1> variance,
689  ndarray::Array<T,2,2> const &queries) const;
690 
709  ndarray::Array<T,2,2> const &queries) const;
710 
716  ndarray::Array<T,2,2> variance,
717  ndarray::Array<T,2,2> const &queries) const;
718 
724  ndarray::Array<T,2,2> const &queries) const;
725 
726 
743  void addPoint(ndarray::Array<T,1,1> const &vin,T f);
744 
754  void addPoint(ndarray::Array<T,1,1> const &vin,ndarray::Array<T,1,1> const &f);
755 
767  void removePoint(int dex);
768 
775  void setKrigingParameter(T kk);
776 
783  void setCovariogram(boost::shared_ptr< Covariogram<T> > const &covar);
784 
796  void setLambda(T lambda);
797 
798 
814 
815  private:
817 
819 
822 
824 
825  boost::shared_ptr< Covariogram<T> > _covariogram;
827 
828 };
829 
830 }}}
831 
832 
833 
834 #endif //#ifndef LSST_AFW_MATH_GAUSSIAN_PROCESS_H
ndarray::Array< double, 1, 1 > _neighborDistances
int _findNode(ndarray::Array< const T, 1, 1 > const &v) const
Find the point already in the tree that would be the parent of a point not in the tree...
void setSigma1(double sigma1)
set the _sigma1 hyper parameter
void addPoint(ndarray::Array< const T, 1, 1 > const &v)
Add a point to the tree. Allot more space in _tree and data if needed.
ndarray::Array< int, 1, 1 > _neighborCandidates
T getData(int ipt, int idim) const
Return one element of one node on the tree.
Eigen matrix objects that present a view into an ndarray::Array.
void setSigma0(double sigma0)
set the _sigma0 hyper parameter
void _organize(ndarray::Array< int, 1, 1 > const &use, int ct, int parent, int dir)
Find the daughter point of a node in the tree and segregate the points around it. ...
Stores values of a function sampled on an image and allows you to interpolate the function to unsampl...
void addToTotal(int i)
Adds time to _totalTime and adds counts to _interpolationCount.
Include files required for standard LSST Exception handling.
This is a structure for keeping track of how long the interpolation methods spend on different parts ...
ndarray::Array< T, 1, 1 > _min
void _lookForNeighbors(ndarray::Array< const T, 1, 1 > const &v, int consider, int from) const
This method actually looks for the neighbors, determining whether or not to descend branches of the t...
void setCovariogram(boost::shared_ptr< Covariogram< T > > const &covar)
Assign a different covariogram to this GaussianProcess.
void findNeighbors(ndarray::Array< int, 1, 1 > neighdex, ndarray::Array< double, 1, 1 > dd, ndarray::Array< const T, 1, 1 > const &v, int n_nn) const
Find the nearest neighbors of a point.
The parent class of covariogram functions for use in Gaussian Process interpolation.
ndarray::Array< int, 2, 2 > _tree
void setKrigingParameter(T kk)
Assign a value to the Kriging paramter.
double _distance(ndarray::Array< const T, 1, 1 > const &p1, ndarray::Array< const T, 1, 1 > const &p2) const
calculate the Euclidean distance between the points p1 and p2
void start()
Starts the timer for an individual call to an interpolation routine.
void _count(int where, int *ct) const
A method which counts the number of nodes descended from a given node (used by removePoint(int)) ...
void addToVariance()
Adds time to _varianceTime.
void setLambda(T lambda)
set the value of the hyperparameter _lambda
ndarray::Array< T, 2, 2 > _function
void Initialize(ndarray::Array< T, 2, 2 > const &dt)
Build a KD Tree to store the data for GaussianProcess.
T selfInterpolate(ndarray::Array< T, 1, 1 > variance, int dex, int numberOfNeighbors) const
This method will interpolate the function on a data point for purposes of optimizing hyper parameters...
void batchInterpolate(ndarray::Array< T, 1, 1 > mu, ndarray::Array< T, 1, 1 > variance, ndarray::Array< T, 2, 2 > const &queries) const
Interpolate a list of query points using all of the input data (rather than nearest neighbors) ...
ndarray::Array< T, 1, 1 > _max
void addToIteration()
Adds time to _iterationTime.
The data for GaussianProcess is stored in a KD tree to facilitate nearest-neighbor searches...
a Covariogram that falls off as the negative exponent of the square of the distance between the point...
void _descend(int root)
Descend the tree from a node which has been removed, reassigning severed nodes as you go...
a Covariogram that recreates a neural network with one hidden layer and infinite units in that layer ...
GaussianProcessTimer & getTimes() const
Give the user acces to _timer, an object keeping track of the time spent on various processes within ...
Interface for DateTime class.
Covariogram()
construct a Covariogram assigning default values to the hyper parameters
A multidimensional strided array.
Definition: Array.h:47
Citizen(const std::type_info &)
Definition: Citizen.cc:173
afw::table::Key< double > sigma1
void removePoint(int dex)
Remove a point from the tree. Reorganize what remains so that the tree remains self-consistent.
void getTreeNode(ndarray::Array< int, 1, 1 > const &v, int dex) const
Return the _tree information for a given data point.
ndarray::Array< int, 1, 1 > _inn
virtual T operator()(ndarray::Array< const T, 1, 1 > const &, ndarray::Array< const T, 1, 1 > const &) const
Actually evaluate the covariogram function relating two points you want to interpolate from...
void addToSearch()
Adds time to _searchTime.
int _walkUpTree(int target, int dir, int root) const
A method to make sure that every data point in the tree is in the correct position relative to its pa...
GaussianProcess(ndarray::Array< T, 2, 2 > const &dataIn, ndarray::Array< T, 1, 1 > const &ff, boost::shared_ptr< Covariogram< T > > const &covarIn)
This is the constructor you call if you do not wish to normalize the positions of your data points an...
void removePoint(int dex)
This will remove a point from the data set.
int _testTree() const
Make sure that the tree is properly constructed. Returns 1 of it is. Return zero if not...
ndarray::Array< T, 2, 2 > _data
virtual T operator()(ndarray::Array< const T, 1, 1 > const &, ndarray::Array< const T, 1, 1 > const &) const
Actually evaluate the covariogram function relating two points you want to interpolate from...
virtual T operator()(ndarray::Array< const T, 1, 1 > const &p1, ndarray::Array< const T, 1, 1 > const &p2) const
Actually evaluate the covariogram function relating two points you want to interpolate from...
void addPoint(ndarray::Array< T, 1, 1 > const &vin, T f)
Add a point to the pool of data used by GaussianProcess for interpolation.
Citizen is a class that should be among all LSST classes base classes, and handles basic memory manag...
Definition: Citizen.h:56
int getPoints() const
return the number of data points stored in the tree
boost::shared_ptr< Covariogram< T > > _covariogram
T interpolate(ndarray::Array< T, 1, 1 > variance, ndarray::Array< T, 1, 1 > const &vin, int numberOfNeighbors) const
Interpolate the function value at one point using a specified number of nearest neighbors.
void setEllSquared(double ellSquared)
set the _ellSquared hyper parameter (the square of the characteristic length scale of the covariogram...
void display()
Displays the current values of all times and _interpolationCount.
void _reassign(int target)
Reassign nodes to the tree that were severed by a call to removePoint(int)
void reset()
Resets all of the data members of GaussianProcessTimer to zero.
void addToEigen()
Adds time to _eigenTime.