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
|
/*
This file is part of the BOLT-LMM linear mixed model software package
developed by Po-Ru Loh. Copyright (C) 2014-2018 Harvard University.
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
#include <string>
#include <utility>
#include <algorithm>
#include "BoltParEstCV.hpp"
#include "MemoryUtils.hpp"
#include "NumericUtils.hpp"
#include "StatsUtils.hpp"
#include "Jackknife.hpp"
namespace LMM {
using std::vector;
using std::string;
using std::cout;
using std::cerr;
using std::endl;
BoltParEstCV::ParamData::ParamData(double _f2, double _p) : f2(_f2), p(_p) {}
bool BoltParEstCV::ParamData::operator < (const BoltParEstCV::ParamData ¶mData2) const {
return StatsUtils::zScoreDiff(PVEs, paramData2.PVEs) < -2;
}
BoltParEstCV::BoltParEstCV
(const SnpData& _snpData, const DataMatrix& _covarDataT, const double subMaskIndivs[],
const vector < std::pair <std::string, DataMatrix::ValueType> > &_covars, int covarMaxLevels,
bool _covarUseMissingIndic, int mBlockMultX, int Nautosomes)
: snpData(_snpData), covarDataT(_covarDataT),
bolt(_snpData, _covarDataT, subMaskIndivs, _covars, covarMaxLevels, _covarUseMissingIndic,
mBlockMultX, Nautosomes),
covars(_covars), covarUseMissingIndic(_covarUseMissingIndic) {
}
/**
* (f2, p) parameter estimation via cross-validation
* - after each fold, compare PVEs of putative (f2, p) param pairs
* - eliminate clearly suboptimal param pairs from future folds
* - stop when only one param pair left
*
* return: iterations used in last CV fold
*/
int BoltParEstCV::estMixtureParams
(double *f2Est, double *pEst, double *predBoost, const vector <double> &pheno,
double logDeltaEst, double sigma2Kest, int CVfoldsSplit, int CVfoldsCompute, bool CVnoEarlyExit,
double predBoostMin, bool MCMC, int maxIters, double approxLLtol, int mBlockMultX,
int Nautosomes) const {
if (CVfoldsCompute <= 0) {
const int Nwant = 10000, Nrep = bolt.getNused() / CVfoldsSplit + 1;
CVfoldsCompute = std::min(CVfoldsSplit, (Nwant+Nrep-1) / Nrep);
cout << "Max CV folds to compute = " << CVfoldsCompute
<< " (to have > " << Nwant << " samples)" << endl << endl;
}
if (CVfoldsCompute > CVfoldsSplit) {
cerr << "WARNING: CVfoldsCompute > CVfoldsSplit; setting CVfoldsCompute to " << CVfoldsSplit
<< endl << endl;
CVfoldsCompute = CVfoldsSplit;
}
int usedIters = 0;
// try a fixed set of (f2, p) mixture param pairs
const int NUM_F2S = 3; const double f2s[NUM_F2S] = {0.5, 0.3, 0.1};
const int NUM_PS = 6; const double ps[NUM_PS] = {0.5, 0.2, 0.1, 0.05, 0.02, 0.01};
//const int NUM_PS = 9; const double ps[NUM_PS] = {0.5, 0.2, 0.1, 0.05, 0.02, 0.01, 0.005, 0.002, 0.001};
// all (f2, p) pairs are in play at the start; this list will be pruned after each fold
// important: first pair corresponds to infinitesimal model
vector <ParamData> paramDataAll;
for (int f2i = 0; f2i < NUM_F2S; f2i++)
for (int pi = 0; pi < NUM_PS; pi++)
paramDataAll.push_back(ParamData(f2s[f2i], ps[pi]));
const double *maskIndivs = bolt.getMaskIndivs(); // possibly a subset of snpData.maskIndivs
uint64 Nstride = snpData.getNstride();
uint64 M = snpData.getM();
// divide indivs into CVfoldsSplit folds
vector <int> foldAssignments(Nstride, -1); // -1 for masked indivs
int indCtr = 0;
for (uint64 n = 0; n < Nstride; n++)
if (maskIndivs[n])
foldAssignments[n] = (indCtr++) % CVfoldsSplit;
double *foldMaskIndivs = ALIGNED_MALLOC_DOUBLES(Nstride);
vector <double> baselineMSEs;
// run OOS pred for each fold in turn
for (int fold = 0; fold < CVfoldsCompute; fold++) {
cout << "====> Starting CV fold " << (fold+1) << " <====" << endl << endl;
// set up fold assignment mask
for (uint64 n = 0; n < Nstride; n++)
foldMaskIndivs[n] = (foldAssignments[n] != fold && foldAssignments[n] != -1);
// create Bolt instance for predicting using non-left-out indivs
int foldCovarMaxLevels = 1<<30; // no need to re-check covar max levels
const Bolt boltFold(snpData, covarDataT, foldMaskIndivs, covars, foldCovarMaxLevels,
covarUseMissingIndic, mBlockMultX, Nautosomes);
vector <double> PVEs; double baselinePredMSE;
{ // set up arguments and call Bayes-iter
uint64 B = paramDataAll.size(); // number of remaining (f2, p) pairs in play
double *phenoResidCovCompVecs = ALIGNED_MALLOC_DOUBLES(B*boltFold.getNCstride());
boltFold.maskFillCovCompVecs(phenoResidCovCompVecs, &pheno[0], B);
double *betasTrans = ALIGNED_MALLOC_DOUBLES(M*B);
uchar *batchMaskSnps = ALIGNED_MALLOC_UCHARS(M*B);
const uchar *projMaskSnpsFold = boltFold.getProjMaskSnps();
for (uint64 m = 0; m < M; m++)
memset(batchMaskSnps + m*B, projMaskSnpsFold[m], B*sizeof(batchMaskSnps[0]));
uint64 MprojMaskFold = boltFold.getMprojMask();
vector <uint64> Ms(B, MprojMaskFold);
vector <double> logDeltas(B, logDeltaEst);
vector <double> sigma2Ks(B, sigma2Kest);
vector <double> varFrac2Ests(B), pEsts(B);
for (uint64 b = 0; b < B; b++) {
varFrac2Ests[b] = paramDataAll[b].f2;
pEsts[b] = paramDataAll[b].p;
}
// fit the models, one for each (f2, p) pair
usedIters =
boltFold.batchComputeBayesIter(phenoResidCovCompVecs, betasTrans, batchMaskSnps,
&Ms[0], &logDeltas[0], &sigma2Ks[0], &varFrac2Ests[0],
&pEsts[0], B, MCMC, maxIters, approxLLtol);
// reset fold assignment mask to prediction indivs
for (uint64 n = 0; n < Nstride; n++)
foldMaskIndivs[n] = (foldAssignments[n] == fold);
// build predictions and compute PVEs
PVEs = boltFold.batchComputePredPVEs(&baselinePredMSE, &pheno[0], betasTrans, B,
foldMaskIndivs);
ALIGNED_FREE(batchMaskSnps);
ALIGNED_FREE(betasTrans);
ALIGNED_FREE(phenoResidCovCompVecs);
}
baselineMSEs.push_back(baselinePredMSE);
for (uint64 b = 0; b < paramDataAll.size(); b++) {
paramDataAll[b].PVEs.push_back(PVEs[b]);
paramDataAll[b].MSEs.push_back(baselinePredMSE * (1-PVEs[b]));
}
#ifdef VERBOSE
cout << endl << "Average PVEs obtained by param pairs tested (high to low):" << endl;
vector < std::pair <double, string> > avgPVEs;
for (uint64 b = 0; b < paramDataAll.size(); b++) {
char buf[100]; sprintf(buf, "f2=%g, p=%g", paramDataAll[b].f2, paramDataAll[b].p);
avgPVEs.push_back(std::make_pair(NumericUtils::mean(paramDataAll[b].PVEs), string(buf)));
std::sort(avgPVEs.begin(), avgPVEs.end(), std::greater < std::pair <double, string> > ());
}
if (avgPVEs.size() <= 5)
for (uint64 b = 0; b < avgPVEs.size(); b++)
printf("%15s: %f\n", avgPVEs[b].second.c_str(), avgPVEs[b].first);
else {
for (int b = 0; b < 3; b++)
printf("%15s: %f\n", avgPVEs[b].second.c_str(), avgPVEs[b].first);
printf("%15s\n", "...");
printf("%15s: %f\n", avgPVEs.back().second.c_str(), avgPVEs.back().first);
}
cout << endl;
#endif
double bestPVE = *std::max_element(PVEs.begin(), PVEs.end());
uint bestInd = std::max_element(PVEs.begin(), PVEs.end())-PVEs.begin();
char bestPars[100]; sprintf(bestPars, "f2=%g, p=%g", paramDataAll[bestInd].f2,
paramDataAll[bestInd].p);
#ifdef VERBOSE
cout << "Detailed CV fold results:" << endl;
printf(" Absolute prediction MSE baseline (covariates only): %g\n", baselinePredMSE);
if (paramDataAll[0].f2 == 0.5 && paramDataAll[0].p == 0.5)
printf(" Absolute prediction MSE using standard LMM: %g\n",
paramDataAll[0].MSEs.back());
printf(" Absolute prediction MSE, fold-best %14s: %g\n", bestPars,
paramDataAll[bestInd].MSEs.back());
for (uint b = 0; b < paramDataAll.size(); b++) {
char buf[100]; sprintf(buf, "f2=%g, p=%g", paramDataAll[b].f2, paramDataAll[b].p);
printf(" Absolute pred MSE using %15s: %f\n", buf, paramDataAll[b].MSEs.back());
}
cout << endl;
#endif
// prune out significantly suboptimal param settings
if (!CVnoEarlyExit)
for (int b = paramDataAll.size()-1; b >= 0; b--)
for (uint64 b2 = 0; b2 < paramDataAll.size(); b2++)
if (paramDataAll[b] < paramDataAll[b2]) {
paramDataAll.erase(paramDataAll.begin() + b);
break;
}
#ifdef VERBOSE
cout << "====> End CV fold " << (fold+1) << ": " << paramDataAll.size()
<< " remaining param pair(s) <====" << endl << endl;
#endif
if (fold == 0) { // set predBoost: 1 - (smallest MSE) / (inf model MSE)
printf("Estimated proportion of variance explained using inf model: %.3f\n", PVEs[0]);
*predBoost = 1 - (1-bestPVE) / (1-PVEs[0]);
printf("Relative improvement in prediction MSE using non-inf model: %.3f\n\n", *predBoost);
if (*predBoost < predBoostMin && !CVnoEarlyExit) {
printf("Exiting CV: non-inf model does not substantially improve prediction\n");
break;
}
}
// early exit if only one pair left
if (paramDataAll.size() == 1) {
cout << "Finished cross-validation; params sufficiently constrained after "
<< (fold+1) << " folds" << endl;
break;
}
}
if (CVnoEarlyExit) {
cout << "*** Combined results across all folds ***" << endl;
printf("Baseline MSE: %g\n", NumericUtils::mean(baselineMSEs));
vector < std::pair <double, double> > MSEsToBaselineMeanStds(paramDataAll.size());
for (uint b = 0; b < paramDataAll.size(); b++)
MSEsToBaselineMeanStds[b] =
Jackknife::ratioOfSumsMeanStd(paramDataAll[b].MSEs, baselineMSEs);
uint bestInd = min_element(MSEsToBaselineMeanStds.begin(), MSEsToBaselineMeanStds.end())
- MSEsToBaselineMeanStds.begin();
printf("Pred R^2 and MSE using standard LMM: %6.3f (%.3f) %g\n",
1-MSEsToBaselineMeanStds[0].first, MSEsToBaselineMeanStds[0].second,
NumericUtils::mean(paramDataAll[0].MSEs));
printf("Pred R^2 and MSE using best non-inf: %6.3f (%.3f) %g\n",
1-MSEsToBaselineMeanStds[bestInd].first, MSEsToBaselineMeanStds[bestInd].second,
NumericUtils::mean(paramDataAll[bestInd].MSEs));
for (uint b = 0; b < paramDataAll.size(); b++) {
char buf[100]; sprintf(buf, "f2=%g, p=%g", paramDataAll[b].f2, paramDataAll[b].p);
printf(" Pred R^2 and MSE using %15s: %6.3f (%.3f) %g\n", buf,
1-MSEsToBaselineMeanStds[b].first, MSEsToBaselineMeanStds[b].second,
NumericUtils::mean(paramDataAll[b].MSEs));
}
cout << endl;
}
// find best PVE; store corresponding f2, p in output params
double bestMeanPVE = -1e100;
for (uint64 b = 0; b < paramDataAll.size(); b++) {
double meanPVE = NumericUtils::mean(paramDataAll[b].PVEs);
if (meanPVE > bestMeanPVE) {
bestMeanPVE = meanPVE;
*f2Est = paramDataAll[b].f2;
*pEst = paramDataAll[b].p;
}
}
cout << "Optimal mixture parameters according to CV: f2 = " << *f2Est
<< ", p = " << *pEst << endl;
ALIGNED_FREE(foldMaskIndivs);
return usedIters;
}
// for use in PhenoBuilder to generate random phenotypes
const Bolt &BoltParEstCV::getBoltRef(void) const {
return bolt;
}
}
|