float Interpolate1D::predict(const float & x) const { DenseMatrix<float > xTest(1,1); xTest.column(0)[0] = x - m_xMean->v()[0]; Covariance<float, RbfKernel<float> > covTest; covTest.create(xTest, *m_xTrain, *m_rbf); DenseMatrix<float> KxKtraininv(covTest.K().numRows(), m_covTrain->Kinv().numCols() ); covTest.K().mult(KxKtraininv, m_covTrain->Kinv() ); /// yPred = Ktest * inv(Ktrain) * yTrain DenseMatrix<float> yPred(1,1); KxKtraininv.mult(yPred, *m_yTrain); return (yPred.column(0)[0] + m_yMean->v()[0]); }
/* * run cross validation for folds from idxStartFold to idxEndFold * @param idxStartFold index of the first fold * @param idxEndFold index of the last fold */ void Cvlars::subrun(int idxStartFold,int idxEndFold) { //create test and control container STK::CArrayXX XControl( n_ - sizePartition_[idxStartFold], p_); STK::CVectorX yControl( n_ - sizePartition_[idxStartFold] ); STK::CArrayXX XTest(sizePartition_[idxStartFold], p_); STK::CVectorX yTest(sizePartition_[idxStartFold] ); STK::CVectorX yPred(sizePartition_[idxStartFold] ); for(int i = idxStartFold ; i <= idxEndFold ; i++) { //fill the container int index = 1; int index2 = 1; for(int j = 1; j <= n_; j++) { if(partition_[j-1] != i) { yControl[index] = (*p_y_)[j]; XControl.row(index)=p_X_->row(j); index++; } else { yTest[index2] = (*p_y_)[j]; XTest.row(index2)=p_X_->row(j); index2++; } } //run lars on control data set HD::Lars lars(XControl,yControl,maxSteps_,intercept_,eps_); lars.run(); for(int s = 1 ; s <= (int) index_.size(); s++) { //we compute the prediction of the y associated to XTest lars.predict(XTest,index_[s-1], lambdaMode_, yPred); //compute the residuals residuals_(s,i+1) = (yPred-yTest).square().sum()/sizePartition_[i]; } } }
void Cvlars::run2() { //search the first and last fold with the same size std::vector<int> startIndex(1,0),endIndex(1,k_-1); int k = 0; for(int i = 1; i < k_; i++) { if(sizePartition_[i]!= sizePartition_[startIndex[k]]) { startIndex.push_back(i); endIndex[k] = i-1; endIndex.push_back(k_-1); k++; } } //run for each size of fold //create test and control container #pragma omp parallel { #pragma omp for schedule(dynamic,1) for(int i = 0; i < k_ ; i++) { STK::CArrayXX XControl( n_ - sizePartition_[i], p_); STK::CVectorX yControl( n_ - sizePartition_[i] ); STK::CArrayXX XTest(sizePartition_[i], p_); STK::CVectorX yTest(sizePartition_[i] ); STK::CVectorX yPred(sizePartition_[i] ); //fill the container int index = 1; int index2 = 1; for(int j = 1; j <= n_; j++) { if(partition_[j-1] != i) { yControl[index] = (*p_y_)[j]; XControl.row(index)=p_X_->row(j); index++; } else { yTest[index2] = (*p_y_)[j]; XTest.row(index2)=p_X_->row(j); index2++; } } //run lars on control data set HD::Lars lars(XControl,yControl,maxSteps_,intercept_,eps_); lars.run(); for(int s = 1 ; s <= (int) index_.size(); s++) { //we compute the prediction of the y associated to XTest lars.predict(XTest,index_[s-1], lambdaMode_, yPred); //compute the residuals residuals_(s,i+1) = (yPred-yTest).square().sum()/sizePartition_[i]; } } }//end parallel // compute mean prediction error for each index STK::CVectorX one(k_,1); cv_ = (residuals_ * one) / k_; // compute mean standard deviation of cv_ for each index for(int i = 1; i <= (int) index_.size(); i++) residuals_.row(i) -= cv_[i]; residuals_ = residuals_.square(); cvError_ = (residuals_ * one)/(k_-1)/k_; cvError_ = cvError_.sqrt(); }