$search
00001 #include "gaussian_process/covarianceFunctions.h" 00002 00003 CovFunc1D::CovFunc1D() { 00004 hyperParam = vector<double>(2); 00005 setHyperparameter(-1.6,1.4); 00006 } 00007 00008 CovFunc1D::CovFunc1D(double ell_0, double sigma) { 00009 hyperParam = vector<double>(2); 00010 setHyperparameter(ell_0,sigma); 00011 } 00012 00013 void CovFunc1D::setHyperparameter(vector<double>& newHyperParam) { 00014 CovFunc<TDoubleVector>::setHyperparameter(newHyperParam); 00015 00016 // do some pre-evaluation 00017 this->ell_0 = exp(newHyperParam[0]); 00018 this->sigma = exp(2*newHyperParam[1]); 00019 } 00020 00021 void CovFunc1D::setHyperparameter(double ell0,double sigma) { 00022 hyperParam[0] = ell0; 00023 hyperParam[1] = sigma; 00024 00025 // do some pre-evaluation 00026 this->ell_0 = exp(hyperParam[0]); 00027 this->sigma = exp(2*hyperParam[1]); 00028 } 00029 00030 00031 double CovFunc1D::getCov( const TDoubleVector &x, const TDoubleVector &y ) { 00032 00033 double dist_0 = fabs(x[0]-y[0]); 00034 00035 double tt2 = 1./(ell_0 * ell_0) * dist_0 * dist_0; 00036 00037 double rr2 = sigma * exp(-0.5 * tt2); 00038 00039 return rr2; 00040 } 00041 00042 00043 double CovFunc1D::getDerivative(const TDoubleVector &x, const TDoubleVector &y, int parameter) { 00044 00045 double r=0; 00046 switch (parameter) { 00047 case 0: 00048 r = getCov(x, y) * (fabs((x[0]-y[0])/ell_0) * fabs((x[0]-y[0])/ell_0)) ; 00049 break; 00050 //covariance for sigma 00051 case 1: 00052 r = 2*getCov(x,y); 00053 break; 00054 } 00055 return r; 00056 } 00057 00058 int CovFunc1D::getNumParameter() { 00059 return 2; 00060 } 00061 00062 //------------------------------------------------------------------------------------------------- 00063 00064 CovFunc2D::CovFunc2D() { 00065 hyperParam = vector<double>(3); 00066 setHyperparameter(-1.6,-1.6,1.4); 00067 } 00068 00069 CovFunc2D::CovFunc2D(double ell_0, double ell_1, double sigma) { 00070 hyperParam = vector<double>(3); 00071 setHyperparameter(ell_0,ell_1,sigma); 00072 } 00073 00074 void CovFunc2D::setHyperparameter(vector<double>& hyperParam) { 00075 CovFunc<TDoubleVector>::setHyperparameter(hyperParam); 00076 00077 // do some pre-evaluation 00078 this->ell_0 = exp(hyperParam[0]); 00079 this->ell_1 = exp(hyperParam[1]); 00080 this->sigma = exp(2*hyperParam[2]); 00081 } 00082 00083 void CovFunc2D::setHyperparameter(double ell_0, double ell_1,double sigma) { 00084 hyperParam[0] = ell_0; 00085 hyperParam[1] = ell_1; 00086 hyperParam[2] = sigma; 00087 00088 // do some pre-evaluation 00089 this->ell_0 = exp(hyperParam[0]); 00090 this->ell_1 = exp(hyperParam[1]); 00091 this->sigma = exp(2*hyperParam[2]); 00092 } 00093 00094 double CovFunc2D::getCov( const TDoubleVector &x, const TDoubleVector &y ) { 00095 00096 double dist_0 = fabs(x[0]-y[0]); 00097 double dist_1 = fabs(x[1]-y[1]); 00098 double tt2 = 1./(ell_0 * ell_0) * dist_0 * dist_0 + 1./(ell_1 * ell_1) * dist_1 * dist_1; 00099 double rr2 = sigma * exp(-0.5 * tt2); 00100 00101 return rr2; 00102 } 00103 00104 double CovFunc2D::getDerivative(const TDoubleVector &x, const TDoubleVector &y, int parameter) { 00105 00106 double r=0; 00107 switch (parameter) { 00108 case 0: 00109 //covariance times squared difference 00110 r = getCov(x, y) * (fabs((x[0]-y[0])/ell_0) * fabs((x[0]-y[0])/ell_0)) ; 00111 break; 00112 00113 case 1: 00114 //covariance times squared difference 00115 r = getCov(x, y) * (fabs((x[1]-y[1])/ell_1) * fabs((x[1]-y[1])/ell_1)) ; 00116 break; 00117 //covariance for sigma 00118 case 2: 00119 r = 2*getCov(x,y); 00120 break; 00121 } 00122 return r; 00123 } 00124 00125 int CovFunc2D::getNumParameter() { 00126 return 3; 00127 } 00128 00129 00130 //------------------------------------------------------------------------------------------------- 00131 CovFuncND::CovFuncND() { 00132 hyperParam = vector<double>(2); 00133 evalParam = vector<double>(2); 00134 n = 1; 00135 hyperParam[0] = 5; 00136 hyperParam[1] = 0.8; 00137 00138 setHyperparameter(hyperParam); 00139 } 00140 00141 CovFuncND::CovFuncND(int n,double ell, double sigma) { 00142 hyperParam = vector<double>(n+1); 00143 evalParam = vector<double>(n+1); 00144 for(int i=0;i<n;i++) 00145 hyperParam[i] = ell; 00146 hyperParam[n] = sigma; 00147 00148 setHyperparameter(hyperParam); 00149 } 00150 00151 void CovFuncND::setHyperparameter(vector<double>& hyperParam) { 00152 CovFunc<TDoubleVector>::setHyperparameter(hyperParam); 00153 n = hyperParam.size()-1; 00154 00155 // do some pre-evaluation 00156 evalParam = vector<double>(n+1); 00157 for(int i=0;i<n;i++) 00158 evalParam[i] = exp(hyperParam[i]); 00159 evalParam[n] = exp(2*hyperParam[n]); 00160 } 00161 00162 double CovFuncND::getCov( const TDoubleVector &x, const TDoubleVector &y ) { 00163 double tt2=0; 00164 double dist; 00165 for(int i=0;i<n;i++) { 00166 dist = fabs(x[i]-y[i]); 00167 tt2 += 1./(evalParam[i] * evalParam[i]) * dist*dist; 00168 } 00169 double rr2 = evalParam[n] * exp(-0.5 * tt2); 00170 00171 return rr2; 00172 } 00173 00174 double CovFuncND::getDerivative(const TDoubleVector &x, const TDoubleVector &y, int parameter) { 00175 if(parameter<n) { 00176 return getCov(x, y) 00177 * fabs((x[parameter]-y[parameter])/evalParam[parameter]) 00178 * fabs((x[parameter]-y[parameter])/evalParam[parameter]); 00179 } else { 00180 return 2*getCov(x,y); 00181 } 00182 } 00183 00184 int CovFuncND::getNumParameter() { 00185 return n+1; 00186 } 00187