/************************************************************************************************** * Procedure * * * * Description: convertMatrixToParams * * Class : UnscentedExpectedImprovement * **************************************************************************************************/ void UnscentedExpectedImprovement::convertMatrixToParams(bopt_params& params, const matrixd px) { if (px.size1() != px.size2()) return; size_t dim = px.size1(); for (size_t row = 0; row < dim; ++row) { for (size_t col = 0; col < dim; ++col) { params.crit_params[4 + col + (row * dim)] = px(row, col); params.input.noise[0 + col + (row * dim)] = px(row, col); } } }
/************************************************************************************************** * Procedure * * * * Description: getSigmaPoints * * Class : UnscentedExpectedImprovement * **************************************************************************************************/ void UnscentedExpectedImprovement::getSigmaPoints(const vectord& x , const double scale , const int dim , const matrixd& matrix_noise , std::vector<vectord>& xx , std::vector<double>& w , const bool matrix_convert) { const size_t n = dim; assert(matrix_noise.size1() == n); assert(matrix_noise.size2() == n); assert(x.size() == n); matrixd px; if (matrix_convert) px = UnscentedExpectedImprovement::convertMatrixNoise(matrix_noise, scale, dim); else px = matrix_noise; // Output variable intialization xx = std::vector<vectord>(); w = std::vector<double>(); xx.push_back(x); w .push_back(scale / (dim + scale)); // Calculate query_i for (size_t col = 0; col < n; col += 1) { xx.push_back(x - boost::numeric::ublas::column(px, col)); xx.push_back(x + boost::numeric::ublas::column(px, col)); w .push_back(0.5 / (dim + scale)); w .push_back(0.5 / (dim + scale)); } }
double HierarchicalGaussianProcess::negativeTotalLogLikelihood() { /*This is the restricted version. For the unrestricted, make p=0 and remove the last term of loglik*/ const matrixd K = computeCorrMatrix(); const size_t n = K.size1(); const size_t p = mFeatM.size1(); matrixd L(n,n); utils::cholesky_decompose(K,L); matrixd KF(ublas::trans(mFeatM)); inplace_solve(L,KF,ublas::lower_tag()); matrixd FKF = prod(ublas::trans(KF),KF); matrixd L2(p,p); utils::cholesky_decompose(FKF,L2); vectord Ky(mGPY); inplace_solve(L,Ky,ublas::lower_tag()); vectord wML = prod(Ky,KF); utils::cholesky_solve(L2,wML,ublas::lower()); vectord alpha = mGPY - prod(wML,mFeatM); inplace_solve(L,alpha,ublas::lower_tag()); double sigma = ublas::inner_prod(alpha,alpha)/(n-p); double loglik = .5*(n-p)*log(ublas::inner_prod(alpha,alpha)) + utils::log_trace(L) + utils::log_trace(L2); return loglik; }
inline void MeanModel::addNewPoint(const vectord &x) { using boost::numeric::ublas::column; mFeatM.resize(mFeatM.size1(),mFeatM.size2()+1); column(mFeatM,mFeatM.size2()-1) = mMean->getFeatures(x); }
void Dataset::setSamples(const matrixd &x) { for (size_t i=0; i<x.size1(); ++i) { mX.push_back(row(x,i)); } };
void Dataset::setSamples(const matrixd &x, const vectord &y) { // WARNING: It assumes mX is empty mY = y; for (size_t i=0; i<x.size1(); ++i) { mX.push_back(row(x,i)); updateMinMax(i); } };
/************************************************************************************************** * Procedure * * * * Description: isDiag * * Class : UnscentedExpectedImprovement * **************************************************************************************************/ bool UnscentedExpectedImprovement::isDiag(matrixd matrix) { if (matrix.size1() == matrix.size2()) { for (size_t row = 0; row < matrix.size1(); ++row) { for (size_t col = 0; col < matrix.size2(); ++col) { if (row != col) { if (std::abs(matrix(row, col)) > std::numeric_limits<double>::epsilon()) { return false; } } } } return true; } return false; }
double GaussianProcess::negativeLogLikelihood() { const matrixd K = computeCorrMatrix(); const size_t n = K.size1(); matrixd L(n,n); utils::cholesky_decompose(K,L); vectord alpha(mData.mY-mMean.muTimesFeat()); inplace_solve(L,alpha,ublas::lower_tag()); double loglik = ublas::inner_prod(alpha,alpha)/(2*mSigma); loglik += utils::log_trace(L); return loglik; }
void KernelModel::computeCorrMatrix(const vecOfvec& XX, matrixd& corrMatrix, double nugget) { assert(corrMatrix.size1() == XX.size()); assert(corrMatrix.size2() == XX.size()); const size_t nSamples = XX.size(); for (size_t ii=0; ii< nSamples; ++ii) { for (size_t jj=0; jj < ii; ++jj) { corrMatrix(ii,jj) = (*mKernel)(XX[ii], XX[jj]); corrMatrix(jj,ii) = corrMatrix(ii,jj); } corrMatrix(ii,ii) = (*mKernel)(XX[ii],XX[ii]) + nugget; } }
void KernelModel::computeDerivativeCorrMatrix(const vecOfvec& XX, matrixd& corrMatrix, int dth_index) { assert(corrMatrix.size1() == XX.size()); assert(corrMatrix.size2() == XX.size()); const size_t nSamples = XX.size(); for (size_t ii=0; ii< nSamples; ++ii) { for (size_t jj=0; jj < ii; ++jj) { corrMatrix(ii,jj) = mKernel->gradient(XX[ii],XX[jj], dth_index); corrMatrix(jj,ii) = corrMatrix(ii,jj); } corrMatrix(ii,ii) = mKernel->gradient(XX[ii],XX[ii],dth_index); } }