IMP logo
IMP Reference Guide  2.9.0
The Integrative Modeling Platform
GaussianProcessInterpolation.h
Go to the documentation of this file.
1 /**
2  * \file IMP/isd/GaussianProcessInterpolation.h
3  * \brief Normal distribution of Function
4  *
5  * Copyright 2007-2018 IMP Inventors. All rights reserved.
6  */
7 
8 #ifndef IMPISD_GAUSSIAN_PROCESS_INTERPOLATION_H
9 #define IMPISD_GAUSSIAN_PROCESS_INTERPOLATION_H
10 
11 #include <IMP/isd/isd_config.h>
12 #include <IMP/macros.h>
13 #include <boost/scoped_ptr.hpp>
16 #include <IMP/isd/Scale.h>
17 #include <Eigen/Dense>
18 #include <Eigen/Cholesky>
19 
20 IMPISD_BEGIN_NAMESPACE
21 
22 class GaussianProcessInterpolationRestraint;
23 class GaussianProcessInterpolationScoreState;
24 
25 //! GaussianProcessInterpolation
26 /** This class provides methods to perform bayesian interpolation/smoothing of
27  * data using a gaussian process prior on the function to be interpolated.
28  * It takes a dataset as input (via its sufficient statistics) along with prior
29  * mean and covariance functions. It outputs the value of the posterior mean and
30  * covariance functions at points requested by the user.
31  */
32 class IMPISDEXPORT GaussianProcessInterpolation : public Object {
33  public:
34  /** Constructor for the gaussian process
35  * \param [in] x : a list of coordinates in N-dimensional space
36  * corresponding to the abscissa of each observation
37  * \param [in] sample_mean \f$I\f$ : vector of mean observations at each of
38  * the previously defined coordinates
39  * \param [in] sample_std \f$s\f$ : vector of sample standard deviations
40  * \param [in] n_obs \f$N\f$ : sample size for mean and std
41  * \param [in] mean_function \f$m\f$ : a pointer to the prior mean function
42  * to use. Should be compatible with
43  * the size of x(i).
44  * \param [in] covariance_function \f$w\f$: prior covariance function.
45  * \param [in] sigma : ISD Scale (proportionality factor to S)
46  * \param [in] sparse_cutoff : when to consider that a matrix entry is zero
47  *
48  * Computes the necessary matrices and inverses when called.
49  */
51  Floats sample_std, unsigned n_obs,
52  UnivariateFunction *mean_function,
53  BivariateFunction *covariance_function,
54  Particle *sigma,
55  double sparse_cutoff = 1e-7);
56 
57  /** Get posterior mean and covariance functions, at the points requested
58  * Posterior mean is defined as
59  * \f[\hat{I}(x) = m(x)
60  * + {}^t\mathbf{w}(q)
61  * (\mathbf{W}+\mathbf{S})^{-1}
62  * (\mathbf{I}-\mathbf{m}) \f]
63  * Posterior covariance
64  * \f[\hat{\sigma}^2(x,x') =
65  * w(x,x') - {}^t\mathbf{w}(x)
66  * (\mathbf{W} + \mathbf{S})^{-1}
67  * \mathbf{w}(x') \f]
68  * where \f$\mathbf{m}\f$ is the vector built by evaluating the prior mean
69  * function at the observation points; \f$\mathbf{w}(x)\f$ is the vector of
70  * covariances between each observation point and the current point;
71  * \f$\mathbf{W}\f$ is the prior covariance matrix built by evaluating the
72  * covariance function at each of the observations; \f$\mathbf{S}\f$ is the
73  * diagonal covariance matrix built from sample_std and n_obs.
74  *
75  * Both functions will check if the mean or covariance functions have
76  *changed
77  * since the last call, and will recompute
78  * \f$(\mathbf{W} + \mathbf{S})^{-1}\f$ if necessary.
79  */
80  double get_posterior_mean(Floats x) const;
81  double get_posterior_covariance(Floats x1, Floats x2) const;
82 #ifndef SWIG
83  // c++ only
84  Eigen::MatrixXd get_posterior_covariance_matrix(FloatsList x) const;
85 #endif
86  // for Python
87  FloatsList get_posterior_covariance_matrix(FloatsList x, bool) const;
88 
89 #ifndef SWIG
90 // derivative: d(mean(q))/(dparticle_i)
91 // Eigen::VectorXd get_posterior_mean_derivative(Floats x) const;
92 #endif
93 
94 #ifndef SWIG
95  // derivative: d(cov(q,q))/(dparticle_i)
96  Eigen::VectorXd get_posterior_covariance_derivative(Floats x) const;
97 #endif
98  // for Python
99  Floats get_posterior_covariance_derivative(Floats x, bool) const;
100 
101 #ifndef SWIG
102 // hessian: d(mean(q))/(dparticle_i dparticle_j)
103 // Eigen::MatrixXd get_posterior_mean_hessian(Floats x) const;
104 #endif
105 
106 #ifndef SWIG
107  // hessian: d(cov(q,q))/(dparticle_i dparticle_j)
108  Eigen::MatrixXd get_posterior_covariance_hessian(Floats x) const;
109 #endif
110  // for Python
111  FloatsList get_posterior_covariance_hessian(Floats x, bool) const;
112 
113  // needed for restraints using gpi
114  ModelObjectsTemp get_inputs() const;
115 
116  // call these if you called update() on the mean or covariance function.
117  // it will force update any internal variables dependent on these functions.
118  void force_mean_update();
119  void force_covariance_update();
120 
121  // returns the number of particles that control m's values.
122  // public for testing purposes
123  unsigned get_number_of_m_particles() const;
124 
125  // returns true if a particle is optimized
126  // public for testing purposes
127  bool get_m_particle_is_optimized(unsigned i) const;
128 
129  // returns the number of particles that control Omega's values.
130  // public for testing purposes
131  unsigned get_number_of_Omega_particles() const;
132 
133  // returns true if a particle is optimized
134  // public for testing purposes
135  bool get_Omega_particle_is_optimized(unsigned i) const;
136 
137  // returns data
138  FloatsList get_data_abscissa() const;
139  Floats get_data_mean() const;
140  FloatsList get_data_variance() const;
141 
143  friend class GaussianProcessInterpolationScoreState;
144 
146 
147  protected:
148  // returns updated data vector
149  Eigen::VectorXd get_I() const { return I_; }
150  // returns updated prior mean vector
151  Eigen::VectorXd get_m() const;
152  // returns dm/dparticle
153  Eigen::VectorXd get_m_derivative(unsigned particle) const;
154  // returns d2m/(dparticle_1 dparticle_2)
155  Eigen::VectorXd get_m_second_derivative(unsigned particle1,
156  unsigned particle2) const;
157  // returns updated prior covariance vector
158  void add_to_m_particle_derivative(unsigned particle, double value,
159  DerivativeAccumulator &accum);
160  // returns updated prior covariance vector
161  Eigen::VectorXd get_wx_vector(Floats xval) const;
162  // returns updated data covariance matrix
163  Eigen::DiagonalMatrix<double, Eigen::Dynamic> get_S() const {
164  return S_;
165  }
166  // returns updated prior covariance matrix
167  Eigen::MatrixXd get_W() const;
168  // returns Omega=(W+S/N)
169  Eigen::MatrixXd get_Omega() const;
170  // returns dOmega/dparticle
171  Eigen::MatrixXd get_Omega_derivative(unsigned particle) const;
172  // returns d2Omega/(dparticle_1 dparticle_2)
173  Eigen::MatrixXd get_Omega_second_derivative(unsigned particle1,
174  unsigned particle2) const;
175  // returns updated prior covariance vector
176  void add_to_Omega_particle_derivative(unsigned particle, double value,
177  DerivativeAccumulator &accum);
178  // returns LDLT decomp of omega
179  Eigen::LDLT<Eigen::MatrixXd, Eigen::Upper> get_ldlt() const;
180  // returns updated Omega^{-1}
181  Eigen::MatrixXd get_Omi() const;
182  // returns updated Omega^{-1}(I-m)
183  Eigen::VectorXd get_OmiIm() const;
184 
185  private:
186  // ensures the mean/covariance function has updated parameters. Signals an
187  // update by changing the state flags. Returns true if the function has
188  // changed. This is used by GaussianProcessInterpolationRestraint.
189  void update_flags_mean();
190  void update_flags_covariance();
191 
192  // compute prior covariance matrix
193  void compute_W();
194  // compute \f$(\mathbf{W} + \frac{\sigma}{N}\mathbf{S})^{-1}\f$.
195  void compute_Omega();
196  // compute LDLT decomposition of Omega
197  void compute_ldlt();
198  // compute \f$(\mathbf{W} + \frac{\sigma}{N}\mathbf{S})^{-1}\f$.
199  void compute_Omi();
200  // compute (W+sigma*S/N)^{-1} (I-m)
201  void compute_OmiIm();
202 
203  // compute dw(q)/dparticle_i
204  Eigen::VectorXd get_wx_vector_derivative(Floats q, unsigned i) const;
205  // compute dw(q)/(dparticle_i * dparticle_j)
206  Eigen::VectorXd get_wx_vector_second_derivative(Floats q, unsigned i,
207  unsigned j) const;
208 
209  // compute dcov(q,q)/dw(q)
210  Eigen::VectorXd get_dcov_dwq(Floats q) const;
211  // compute dcov(q,q)/dOmega
212  Eigen::MatrixXd get_dcov_dOm(Floats q) const;
213  // compute d2cov(q,q)/(dw(q) dw(q)) (independent of q !)
214  Eigen::MatrixXd get_d2cov_dwq_dwq() const;
215  // compute d2cov(q,q)/(dw(q)_m dOmega)
216  Eigen::MatrixXd get_d2cov_dwq_dOm(Floats q, unsigned m) const;
217  // compute d2cov(q,q)/(dOmega dOmega_mn)
218  Eigen::MatrixXd get_d2cov_dOm_dOm(Floats q, unsigned m, unsigned n) const;
219 
220  // compute mean observations
221  void compute_I(Floats mean);
222  // compute diagonal covariance matrix of observations
223  void compute_S(Floats std);
224  // compute prior mean vector
225  void compute_m();
226 
227  private:
228  unsigned N_; // number of dimensions of the abscissa
229  unsigned M_; // number of observations to learn from
230  FloatsList x_; // abscissa
231  unsigned n_obs_; // number of observations
232  // pointer to the prior mean function
234  // pointer to the prior covariance function
235  IMP::PointerMember<BivariateFunction> covariance_function_;
236  Eigen::VectorXd I_, m_;
237  Eigen::MatrixXd W_, Omega_, Omi_; // Omi = Omega^{-1}
238  Eigen::DiagonalMatrix<double, Eigen::Dynamic> S_;
239  Eigen::VectorXd OmiIm_; // Omi * (I - m)
240  bool flag_m_, flag_m_gpir_, flag_Omi_, flag_OmiIm_, flag_W_, flag_Omega_,
241  flag_Omega_gpir_, flag_ldlt_;
243  double cutoff_;
244  double sigma_val_; // to know if an update is needed
245  Eigen::LDLT<Eigen::MatrixXd, Eigen::Upper> ldlt_;
246 };
247 
248 IMPISD_END_NAMESPACE
249 
250 #endif /* IMPISD_GAUSSIAN_PROCESS_INTERPOLATION_H */
Base class for functions of one variable.
#define IMP_OBJECT_METHODS(Name)
Define the basic things needed by any Object.
Definition: object_macros.h:25
A decorator for scale parameters particles.
Classes for general functions.
Various general useful macros for IMP.
Common base class for heavy weight IMP objects.
Definition: Object.h:106
A smart pointer to a ref-counted Object that is a class member.
Definition: Pointer.h:146
Base class for functions of two variables.
Class to handle individual particles of a Model object.
Definition: Particle.h:41
Class for adding derivatives from restraints to the model.
Classes for general functions.