$search
00001 /* 00002 * SingleGP.cpp 00003 * 00004 * Created on: Feb 10, 2010 00005 * Author: sturm 00006 */ 00007 00008 00009 #include "gaussian_process/SingleGP.h" 00010 00011 namespace gaussian_process { 00012 00013 #define sqr(a) ((a)*(a)) 00014 00015 SingleGP::SingleGP(CovFuncND initialCovFunc,double initialSigmaNoise) 00016 : 00017 initialCovFunc(initialCovFunc), initialSigmaNoise(initialSigmaNoise), 00018 covFunc(initialCovFunc), sigmaNoise(initialSigmaNoise), 00019 GP(&covFunc, &sigmaNoise) 00020 { 00021 GP.m_dataPoints = &dataPoints; 00022 GP.m_t = &targetPoints; 00023 GP.m_numDataPoints = 0; 00024 00025 } 00026 00027 SingleGP::~SingleGP() { 00028 GP.m_dataPoints = NULL; 00029 GP.m_t = NULL; 00030 } 00031 00032 void SingleGP::Reset() { 00033 dataPoints.clear(); 00034 targetPoints.clear(); 00035 covFunc = initialCovFunc; 00036 sigmaNoise = initialSigmaNoise; 00037 GP.m_numDataPoints = 0; 00038 } 00039 00040 void SingleGP::SetData(TVector<TDoubleVector> &dataPoints,TVector<double> &targetPoints) { 00041 this->dataPoints = dataPoints; 00042 this->targetPoints = targetPoints; 00043 GP.m_numDataPoints = dataPoints.size(); 00044 GP.m_dataPoints = &this->dataPoints; 00045 GP.m_t = &this->targetPoints; 00046 BuildGP(); 00047 } 00048 00049 void SingleGP::BuildGP() { 00050 if(GP.m_numDataPoints ==0) { 00051 return; 00052 } 00053 00054 if(dataPoints[0].size() == 0) { 00055 double sum = 0; 00056 for(unsigned int i=0;i<dataPoints.size();i++) { 00057 sum += targetPoints[i]; 00058 } 00059 mean = sum/dataPoints.size(); 00060 00061 sum = 0; 00062 for(unsigned int i=0;i<dataPoints.size();i++) { 00063 sum += sqr(targetPoints[i]-mean); 00064 } 00065 var = sum/dataPoints.size(); 00066 sum = 0; 00067 return; 00068 } else { 00069 GP.buildGP(true); // build iCov and targets 00070 } 00071 } 00072 00073 void SingleGP::OptimizeGP() { 00074 if(GP.m_numDataPoints ==0) return; 00075 00076 if(dataPoints[0].size() != 0) { 00077 00078 if(!GP.minimizeGSL(100)) { 00079 covFunc = initialCovFunc; 00080 sigmaNoise = initialSigmaNoise; 00081 // cout << "minimize failed, resetting values"<<endl; 00082 } else { 00083 return; // done! 00084 } 00085 00086 } 00087 BuildGP(); 00088 } 00089 00090 void SingleGP::Evaluate(TDoubleVector data, double &targetMean, double &targetVar) { 00091 targetMean = NAN; 00092 targetVar = NAN; 00093 if(GP.m_numDataPoints ==0) return; 00094 00095 if(dataPoints[0].size() == 0) { 00096 targetMean = mean; 00097 targetVar = var; 00098 } else { 00099 //cout << "datapoitns="<<GP.m_numDataPoints<<endl; 00100 GP.evalGP(data, targetMean, targetVar); 00101 } 00102 } 00103 00104 double SingleGP::GetDataLikelihood() { 00105 if(GP.m_numDataPoints ==0) return(NAN); 00106 00107 if(dataPoints[0].size() == 0) { 00108 return(NAN); 00109 } 00110 00111 return(GP.getDataLikelihood()); 00112 } 00113 00114 } 00115 00116 #include "gaussian_process/gpRegression.hxx"