1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
|
// -*- Mode: C++; tab-width: 2; -*-
// vi: set ts=2:
//
//
#include <BALL/QSAR/regressionValidation.h>
#include <BALL/QSAR/statistics.h>
#include <BALL/QSAR/regressionModel.h>
#include <BALL/QSAR/kernelModel.h>
#include <BALL/QSAR/latentVariableModel.h>
#include <BALL/QSAR/registry.h>
#include <boost/random/mersenne_twister.hpp>
using namespace std;
namespace BALL
{
namespace QSAR
{
RegressionValidation::RegressionValidation(RegressionModel* m) : Validation(m)
{
ssR_ = 0; ssE_ = 0; ssY_ = 0; Q2_ = -1; F_cv_ = -1; R2_ = -1; std_err_ = -1; F_regr_ = -1; max_error_ = -1;
regr_model_ = m;
predQualFetcher_ = &RegressionValidation::getQ2;
fitQualFetcher_ = &RegressionValidation::getR2;
selectStat(0); // choose standard == Q^2
}
RegressionValidation::~RegressionValidation()
{
}
void RegressionValidation::setQ2(double d)
{
Q2_ = d;
}
void RegressionValidation::crossValidation(int k, bool restore)
{
crossValidation(k, NULL, restore);
}
void RegressionValidation::backupTrainingResults()
{
backup_data_.descriptor_matrix = regr_model_->descriptor_matrix_;
backup_data_.training_result = regr_model_->training_result_;
backup_data_.Y = regr_model_->Y_;
KernelModel* k_model = dynamic_cast<KernelModel*>(regr_model_);
LatentVariableModel* lv_model = dynamic_cast<LatentVariableModel*>(regr_model_);
if (k_model)
{
backup_data_.K = k_model->K_;
}
if (lv_model)
{
backup_data_.latent_variables = *lv_model->getLatentVariables();
backup_data_.loadings = *lv_model->getLoadings();
backup_data_.weights = *lv_model->getWeights();
}
}
void RegressionValidation::restoreTrainingResults()
{
regr_model_->descriptor_matrix_ = backup_data_.descriptor_matrix;
regr_model_->training_result_ = backup_data_.training_result;
regr_model_->Y_ = backup_data_.Y;
KernelModel* k_model = dynamic_cast<KernelModel*>(regr_model_);
LatentVariableModel* lv_model = dynamic_cast<LatentVariableModel*>(regr_model_);
if (k_model)
{
k_model->K_ = backup_data_.K;
}
if (lv_model)
{
lv_model->latent_variables_ = backup_data_.latent_variables;
lv_model->loadings_ = backup_data_.loadings;
lv_model->weights_ = backup_data_.weights;
}
}
void RegressionValidation::crossValidation(int k, MatrixVector* results, bool restore)
{
if (model_->data->descriptor_matrix_.size() == 0 || model_->data->Y_.size() == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Data must be fetched from input-files by QSARData before cross-validation can be done!");
}
if (restore) backupTrainingResults();
int lines = model_->data->descriptor_matrix_[0].size();
int col = model_->data->descriptor_matrix_.size();
if (!model_->descriptor_IDs_.empty())
{
col = model_->descriptor_IDs_.size();
}
Q2_ = 0; ssE_ = 0; ssR_ = 0; F_cv_ = 0;
// test k times
for (int i = 0; i < k; i++)
{
int test_size = (lines+i)/k;
int training_size = lines-test_size;
model_->Y_.resize(training_size, model_->data->Y_.size());
model_->descriptor_matrix_.resize(training_size, col);
test_substances_.resize(test_size);
test_Y_.resize(test_size, model_->data->Y_.size());
int train_line = 0; // no of line in descriptor_matrix_ of model_
int test_line = 0;
//copy data to training and test data set
for (int line = 0; line < lines; line++)
{
if ((line+1+i)%k == 0)
{
setTestLine(test_line, line);
test_line++;
}
else
{
setTrainingLine(train_line, line);
train_line++;
}
}
// test Model with model_->predict() for each line of test-data
model_->train();
if (results != NULL){ results->push_back(*regr_model_->getTrainingResult()); }
testAllSubstances(0); // do not transform cross-validation test-data again...
Q2_ += quality_;
}
Q2_ = Q2_/k;
std_err_ = std_err_ / ((k-1)*lines);
if (restore) restoreTrainingResults();
}
void RegressionValidation::testAllSubstances(bool transform)
{
quality_ = 0; ssE_ = 0; ssR_ = 0; std_err_ = 0; ssY_ = 0;
Eigen::VectorXd mean_Y(test_Y_.cols()); // mean of each activity
//RowVector sum_of_squares(test_Y_.cols());
/// In case of external test data (for which 'transform' == 1), data in test_Y_ has been backtransformed to original space and model_->predict(.., 1) will return the activity value in original space.
/// In case of internal testing ('transform' == 0), data in test_Y_ is in transformed space and model->predict(.., 0) will return the activity value in the same space.
for (int i = 0; i < test_Y_.cols(); i++)
{
mean_Y(i) = Statistics::getMean(test_Y_, i);
}
//ssT_ = sum_of_squares.Sum();
for (unsigned int i = 0; i < test_substances_.size(); i++)
{
Eigen::VectorXd rv = model_->predict(test_substances_[i], transform);
double error = 0;
for (int k = 0; k < test_Y_.cols(); k++)
{
error += pow(test_Y_(i, k)-rv(k), 2);
ssR_ += pow(mean_Y(k)-rv(k), 2);
ssY_ += pow(mean_Y(k)-test_Y_(i, k), 2);
}
if (error > max_error_)
{
max_error_ = error;
}
ssE_ += error;
// if (model_->type_ == "GP")
// {
// std_err_ += model_->calculateStdErr();
// }
}
(this->*qualCalculation)();
/* if (model_->type_ == "GP")
{
std_err_ = std_err_/test_substances_.size();
}*/
}
void RegressionValidation::testInputData(bool transform)
{
if (model_->data->descriptor_matrix_.size() == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Data must be fetched from input-files by QSARData before a model_'s fit to it can be evaluated!");
}
if (model_->type_ != "ALL" && model_->type_ != "SVR" && model_->type_ != "KNN") // do not check dimensions of traning results for ALL, since ALL does no training
{
//unsigned int des = model_->descriptor_IDs_.size();
//unsigned int data_cols = model_->data->descriptor_matrix_.size();
unsigned int res_rows = regr_model_->training_result_.rows();
//unsigned int desmat_cols = model_->descriptor_matrix_.cols();
if (res_rows == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained before its fit to the input data can be evaluated!");
}
unsigned int test_col = model_->descriptor_IDs_.size(); // no of columns of X^T X (linear model)
unsigned int kernel_test_col = model_->descriptor_matrix_.rows(); // no of columns of X X^T (nonlinear model)
if (test_col == 0)
{
test_col = model_->data->getNoDescriptors();
}
if (!transform && test_col != res_rows && kernel_test_col != res_rows)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained before its fit to the input data can be evaluated!");
}
// else if ( des == 0 && data_cols != res_rows && data_cols != desmat_cols)
// {
// throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained with data containing the same number of features as the test data set!");
// }
// else if (des != 0 && res_rows != des && data_cols != desmat_cols)
// {cout<<data_cols<<" "<<res_rows<<" "<<desmat_cols<<endl;cout.flush();
// throw Exception::InconsistentUsage(__FILE__, __LINE__, "Model must be trained with data containing the same number of features as the test data set!");
// }
}
R2_ = 0; ssE_ = 0; ssR_ = 0; std_err_ = 0; F_regr_ = 0;
int lines = model_->data->descriptor_matrix_[0].size();
test_substances_.resize(lines);
test_Y_.resize(lines, model_->data->Y_.size());
bool back_transform = 0;
if (transform && model_->data->descriptor_transformations_.size() != 0)
{
// if test data is to be transformed according to centering of training data, BUT has already been centered itself
back_transform = 1;
}
for (int i = 0; i < lines; i++)
{
setTestLine(i, i, back_transform);
}
testAllSubstances(transform);
R2_ = quality_;
int col = model_->data->descriptor_matrix_.size();
if (!model_->descriptor_IDs_.empty())
{
col = model_->descriptor_IDs_.size();
}
}
void RegressionValidation::calculateCoefficientStdErrors(int k, bool b)
{
if (model_->data->descriptor_matrix_.size() == 0 || model_->data->Y_.size() == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Data must be fetched from input-files by QSARData before standart errors of coefficients can be calculated!");
}
if (dynamic_cast < KernelModel* > (model_))
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Calculation of the standard deviation of regression coefficients can only be done for _linear_ regression models in a meaningful way!");
}
backupTrainingResults();
int no_activities = model_->data->Y_.size();
MatrixVector* results = new MatrixVector;
int no_descriptors = model_->data->descriptor_matrix_.size();
if (!model_->descriptor_IDs_.empty())
{
no_descriptors = model_->descriptor_IDs_.size();
}
coefficient_stderr_.resize(no_descriptors, no_activities);
if (b == 1)
{
bootstrap(k, results, false);
}
else
{
crossValidation(k, results, false);
}
for (int c = 0; c < no_activities; c++) // for all modelled activities
{
for (int m = 0; m < no_descriptors; m++) // for all descriptors
{
double mean_mc = 0;
double sumsquares_mc = 0;
for (int i = 0; i < k; i++) // for all training results
{
mean_mc += (*results)[i](m, c);
sumsquares_mc += pow((*results)[i](m, c), 2);
}
mean_mc /= k;
// calculate standard deviation of coefficient
// = sqrt(1/k * \sum_{i = 1}^k (x_i \^bar x)^2)
// <=> sqrt(1/k (\sum_{i = 1}^k x_i - k*\bar x^2))
coefficient_stderr_(m, c) = sqrt(abs(sumsquares_mc-k*pow(mean_mc, 2))/(k-1));
// standard-error == standard-deviation/sqrt(k)
//coefficient_stderr_(m, c) /= sqrt(k);
}
}
delete results;
restoreTrainingResults();
}
void RegressionValidation::bootstrap(int k, bool restore)
{
bootstrap(k, NULL, restore);
}
void RegressionValidation::bootstrap(int k, MatrixVector* results, bool restore)
{
if (model_->data->descriptor_matrix_.size() == 0 || model_->data->Y_.size() == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Data must be fetched from input-files by QSARData before bootstrapping can be done!");
}
if (restore) backupTrainingResults();
Q2_ = 0; double r2 = 0; max_error_ = 0;
int N = model_->data->descriptor_matrix_[0].size();
int no_descriptors = model_->data->descriptor_matrix_.size();
if (!model_->descriptor_IDs_.empty())
{
no_descriptors = model_->descriptor_IDs_.size();
}
boost::mt19937 rng(PreciseTime::now().getMicroSeconds());
for (int i = 0; i < k; i++) // create and evaluate k bootstrap samples
{
vector<int> sample_substances(N, 0); // numbers of occurences of substances within this sample
/// create training matrix and train the model_
model_->descriptor_matrix_.resize(N, no_descriptors);
model_->Y_.resize(N, model_->data->Y_.size());
for (int j = 0; j < N; j++)
{
int pos = rng() % N;
setTrainingLine(j, pos);
sample_substances[pos]++;
}
model_->train(); // train the model_ on current bootstrap sample
/// find size of test data set
int test_size = 0;
for (int j = 0; j < N; j++)
{
if (sample_substances[j] > 0)
{
continue;
}
test_size++;
}
test_substances_.resize(test_size);
test_Y_.resize(test_size, model_->data->Y_.size());
/// create test data set and calculate Q^2
int test_line = 0;
for (int j = 0; j < N; j++)
{
if (sample_substances[j] == 0)
{
setTestLine(test_line, j);
test_line++;
}
}
if (results != NULL){ results->push_back(*regr_model_->getTrainingResult()); }
testAllSubstances(0);
Q2_ += quality_;
/// create test data set and calculate R^2
test_substances_.resize(N);
test_Y_.resize(N, model_->data->Y_.size());
test_line = 0;
for (int j = 0; j < N; j++)
{
while (sample_substances[j] > 0) // insert substance as often as it occurs in the training data set
{
setTestLine(test_line, j);
test_line++;
sample_substances[j]--;
}
}
testAllSubstances(0);
//r2 += 1-(ssE_/(ssE_+ssR_));
r2 += quality_;
}
Q2_ = Q2_/k;
r2 = r2/k;
Q2_ = 0.632*Q2_ + 0.368*r2;
if (restore) restoreTrainingResults();
}
const Eigen::MatrixXd& RegressionValidation::yRandomizationTest(int runs, int k)
{
if (model_->data->descriptor_matrix_.size() == 0 || model_->data->Y_.size() == 0)
{
throw Exception::InconsistentUsage(__FILE__, __LINE__, "Data must be fetched from input-files by QSARData object before response permutation tests can be done!");
}
backupTrainingResults();
vector<vector<double> > dataY_backup = model_->data->Y_;
yRand_results_.resize(runs, 2);
yRand_results_.setConstant(-1);
for (int i = 0; i < runs; i++)
{
yRand(); // randomize all columns of Y_
crossValidation(k, NULL, 0);
model_->readTrainingData();
model_->train();
testInputData(0);
yRand_results_(i, 0) = R2_;
yRand_results_(i, 1) = Q2_;
}
restoreTrainingResults();
QSARData* data = const_cast <QSARData*> (model_->data);
data->Y_ = dataY_backup;
return yRand_results_;
}
void RegressionValidation::calculateQOF()
{
quality_ = (ssY_-ssE_)/ssY_;
}
double RegressionValidation::getQ2()
{
return Q2_;
}
double RegressionValidation::getFcv()
{
return F_cv_;
}
double RegressionValidation::getFregr()
{
return F_regr_;
}
double RegressionValidation::getMaxError()
{
return max_error_;
}
double RegressionValidation::getR2()
{
return R2_;
}
double RegressionValidation::getCVRes()
{
//return getQ2();
return (this->*predQualFetcher_)();
}
double RegressionValidation::getFitRes()
{
return (this->*fitQualFetcher_)();
}
void RegressionValidation::selectStat(int s)
{
predQualFetcher_ = &RegressionValidation::getQ2;
fitQualFetcher_ = &RegressionValidation::getR2;
if (s == 0)
{
validation_statistic_ = 0;
qualCalculation = &RegressionValidation::calculateQOF;
}
else
{
throw BALL::Exception::GeneralException(__FILE__, __LINE__, "RegressionValidation error", "Validation statistic "+String(s)+" is unknown!");
}
}
void RegressionValidation::setCVRes(double d)
{
setQ2(d);
}
const Eigen::MatrixXd* RegressionValidation::getCoefficientStdErrors()
{
return &coefficient_stderr_;
}
void RegressionValidation::setCoefficientStdErrors(const Eigen::MatrixXd* sterr)
{
coefficient_stderr_ = *sterr;
}
void RegressionValidation::saveToFile(string filename) const
{
saveToFile(filename, R2_, Q2_, coefficient_stderr_, yRand_results_);
}
void RegressionValidation::saveToFile(string filename, const double& r2, const double& q2, const Eigen::MatrixXd& coefficient_stddev, const Eigen::MatrixXd& yRand_results) const
{
ofstream out(filename.c_str());
Registry reg;
out<<"# used quality statistic: "<<reg.getRegressionStatisticName(validation_statistic_)<<endl<<endl;
out << "Fit to training data = "<<r2<<endl;
out << "Predictive quality = "<<q2<<endl;
if (coefficient_stddev.cols() > 0)
{
out<<endl;
out<<"[Coefficient stddev]"<<endl;
out<<"dimensions = "<<coefficient_stddev.rows()<<" "<<coefficient_stddev.cols()<<endl;
out<<coefficient_stddev<<endl;
}
if (yRand_results.cols() > 0)
{
out<<endl;
out<<"[Response Permutation]"<<endl;
out<<"dimensions = "<<yRand_results.rows()<<" "<<yRand_results.cols()<<endl;
out<<yRand_results<<endl;
}
}
void RegressionValidation::readFromFile(string filename)
{
ifstream in(filename.c_str());
bool stddev_section = 0;
bool yRand_section = 0;
while (in)
{
String line;
getline(in, line);
line.trimLeft();
if(line=="" || line.hasPrefix("#") || line.hasPrefix("//") || line.hasPrefix("%"))
{
continue;
}
if (stddev_section)
{
if (line.hasPrefix("dimensions"))
{
line = ((String)line.after("="));
unsigned int no_rows = line.getField(0).toInt();
unsigned int no_cols = line.getField(1).toInt();
model_->readMatrix(coefficient_stderr_, in, no_rows, no_cols);
}
stddev_section = 0;
}
else if (yRand_section)
{
if (line.hasPrefix("dimensions"))
{
line = ((String)line.after("="));
unsigned int no_rows = line.getField(0).toInt();
unsigned int no_cols = line.getField(1).toInt();
model_->readMatrix(yRand_results_, in, no_rows, no_cols);
}
yRand_section = 0;
}
if (line.hasPrefix("Fit to training data"))
{
R2_ = ((String)line.after("=")).trimLeft().toDouble();
}
else if (line.hasPrefix("Predictive quality"))
{
Q2_ = ((String)line.after("=")).trimLeft().toDouble();
}
else if (line.hasPrefix("[Coefficient stddev]"))
{
yRand_section = 0;
stddev_section = 1;
}
else if (line.hasPrefix("[Response Permutation]"))
{
yRand_section = 1;
stddev_section = 0;
}
}
}
}
}
|