Go to the documentation of this file.00001 #ifndef _GP_SPARSE_UMFPACK_REGRESSION_HPP_
00002 #define _GP_SPARSE_UMFPACK_REGRESSION_HPP_
00003
00004 #include <vector>
00005
00006 #include <assert.h>
00007
00008 #include "gpReg/covarianceFunction.hpp"
00009
00010
00011 #include <boost/numeric/ublas/matrix.hpp>
00012 #include <boost/numeric/ublas/io.hpp>
00013 #include "gpReg/Matrix.hh"
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 #define TVector boost::numeric::ublas::vector<double>
00026 #define TMatrix boost::numeric::ublas::compressed_matrix<double>
00027
00028
00029 using namespace std;
00030
00031 #define PROFILING
00032
00033 #ifdef PROFILING
00034 #include "gpReg/profiler.hpp"
00035 #endif
00036
00037
00038
00039 class GPSparseRegression
00040 {
00041
00042 public:
00043
00044
00045
00046
00047 GPSparseRegression( CovarianceFunction &covFunc, double sigmaNoise,
00048 GPSparseRegression *noiseGP = NULL ) :
00049 m_dataPoints(0), m_covFunc(covFunc), m_sigmaNoise(sigmaNoise)
00050 {
00051 s_iC = NULL;
00052 s_t = NULL;
00053 s_k = NULL;
00054 s_iCt = NULL;
00055 s_Cs = NULL;
00056 m_numDataPoints = 0;
00057 m_noiseGP = noiseGP;
00058 }
00059
00060
00061
00062
00063
00064 GPSparseRegression( const GPSparseRegression &orig ) : m_covFunc(orig.m_covFunc),
00065 m_numDataPoints(orig.m_numDataPoints), m_dataPoints(orig.m_dataPoints),
00066 s_t(orig.s_t), m_sigmaNoise(orig.m_sigmaNoise)
00067 {
00068 fprintf( stderr, "ERROR (no copy const)\n" );
00069 assert( false );
00070 }
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 ~GPSparseRegression()
00109 {
00110 if (s_iC) delete (s_iC);
00111 if (s_t) delete (s_t);
00112 if (s_k) delete (s_k);
00113 if (s_iCt) delete (s_iCt);
00114 if (s_Cs) delete (s_Cs);
00115 }
00116
00117
00118
00119
00120 void setSigmaNoise( double sigma ) { m_sigmaNoise = sigma; }
00121
00122
00123
00124
00125 void setDataPoints( vector<Vector> &dataPoints, vector<double> &dataTargets )
00126 {
00127
00128 m_dataPoints = dataPoints;
00129 m_numDataPoints = dataPoints.size();
00130 if (s_t) delete (s_t);
00131 s_t = new (TVector)(m_numDataPoints);
00132 for (int i=0; i<m_numDataPoints; i++) {
00133 (*s_t)(i) = dataTargets[i];
00134 }
00135 }
00136
00137
00138
00139
00140 void buildGP()
00141 {
00142 assert( m_numDataPoints>0 );
00143 using namespace boost::numeric::ublas;
00144 using namespace umfpack;
00145
00146
00147
00148
00149
00150 TMatrix s_C( m_numDataPoints, m_numDataPoints );
00151 double cov;
00152 double logNoise;
00153 for (int i1=0; i1<m_numDataPoints; i1++) {
00154 for (int i2=0; i2<m_numDataPoints; i2++) {
00155 cov = m_covFunc.getCov( m_dataPoints[i1], m_dataPoints[i2] );
00156 if (i1==i2) {
00157 cov += exp(2.0 * m_sigmaNoise);
00158 if (m_noiseGP) {
00159 m_noiseGP->evalGP(m_dataPoints[i1],logNoise);
00160 cov += exp(logNoise);
00161 }
00162 }
00163 s_C(i1,i2) = cov;
00164 }
00165 }
00166
00167
00168 if (s_k) delete (s_k);
00169 s_k = new TVector(m_numDataPoints);
00170 if (s_Cs) delete (s_Cs);
00171 s_Cs = new umfpack_matrix_operator<double>::matrix_type(s_C);
00172
00173
00174 if (s_iC) delete (s_iC);
00175 s_iC = new (umfpack_matrix_operator<double>)( *s_Cs );
00176
00177
00178 if (s_iCt) delete (s_iCt);
00179 s_iCt = new umfpack_matrix_operator<double>::vector_type(m_numDataPoints);
00180
00181
00182 s_iC->apply( *s_t, *s_iCt );
00183
00184
00185
00186
00187 }
00188
00189
00190
00191
00192 void evalGP( Vector &x, double &mean, double &var )
00193 {
00194 using namespace boost::numeric::ublas;
00195 using namespace umfpack;
00196
00197 for (int i1=0; i1<m_numDataPoints; i1++) {
00198 (*s_k)(i1) = m_covFunc.getCov( x, m_dataPoints[i1] );
00199 }
00200
00201 TVector s_iCk(m_numDataPoints);
00202 s_iC->apply( *s_k, s_iCk );
00203
00204 mean = inner_prod( *s_k, *s_iCt );
00205 var = m_covFunc.getCov(x,x) - (inner_prod( *s_k, s_iCk ) );
00206
00207 if (m_noiseGP) {
00208 double logNoise;
00209 m_noiseGP->evalGP( x, logNoise );
00210 var += exp(logNoise);
00211 }
00212 }
00213
00214
00215
00216
00217 void evalGP( Vector &x, double &mean )
00218 {
00219 using namespace boost::numeric::ublas;
00220 using namespace umfpack;
00221
00222 for (int i1=0; i1<m_numDataPoints; i1++) {
00223 (*s_k)(i1) = m_covFunc.getCov( x, m_dataPoints[i1] );
00224 }
00225
00226 mean = inner_prod( *s_k, *s_iCt );
00227 }
00228
00229
00230
00231
00232 void save()
00233 {}
00234
00235 void load()
00236 {}
00237
00238
00239
00240
00241 double getObservationLikelihood( vector<Vector> &dataPoints, vector<double> &observations )
00242 {
00243 int numObs = dataPoints.size();
00244 using namespace boost::numeric::ublas;
00245
00246
00247
00248
00249 TMatrix sK__(numObs,numObs);
00250 double cov;
00251 double logNoise;
00252 for (int i=0; i<numObs; i++) {
00253 for (int j=0; j<numObs; j++) {
00254 cov = m_covFunc.getCov( dataPoints[i], dataPoints[j] );
00255 if (i==j) {
00256 cov += exp(2.0 * m_sigmaNoise);
00257 if (m_noiseGP) {
00258 m_noiseGP->evalGP( dataPoints[i], logNoise );
00259 cov += exp(logNoise);
00260 }
00261 }
00262 sK__(i,j) = cov;
00263 }
00264 }
00265
00266
00267
00268 TMatrix sK_(numObs,m_numDataPoints);
00269 for (int i=0; i<numObs; i++) {
00270 for (int j=0; j<m_numDataPoints; j++) {
00271 sK_(i,j) = m_covFunc.getCov( dataPoints[i], m_dataPoints[j] );
00272 }
00273 }
00274
00275 TVector y_(numObs);
00276 double mean;
00277 for (int i=0; i<numObs; i++) {
00278 evalGP( dataPoints[i], mean );
00279 y_(i) = (observations[i]-mean);
00280 }
00281
00282 double y__ = inner_prod( y_, TVector(prod(sK__,y_)) );
00283
00284
00285 TVector ytB = prod( trans(sK_), y_ );
00286
00287 TVector iCytB(m_numDataPoints);
00288 s_iC->apply( ytB, iCytB );
00289
00290 double dotProd = inner_prod( ytB, iCytB );
00291
00292
00293 double dataFit = -0.5 * ( y__ - dotProd );
00294
00295
00296
00297
00298 return dataFit;
00299
00300
00301
00302
00303
00304
00305
00306
00307 }
00308
00309
00310
00311 public:
00312 int m_numDataPoints;
00313 vector<Vector> m_dataPoints;
00314 vector<double> m_dataVariances;
00315 CovarianceFunction &m_covFunc;
00316 double m_sigmaNoise;
00317 GPSparseRegression *m_noiseGP;
00318
00319
00320 umfpack::umfpack_matrix_operator<double> *s_iC;
00321 TVector *s_t;
00322 TVector *s_k;
00323 TVector *s_iCt;
00324 umfpack::umfpack_matrix_operator<double>::matrix_type *s_Cs;
00325
00326 };
00327
00328
00329
00330 #endif //_GP_SPARSE_UMFPACK_REGRESSION_HPP_