Exemplo n.º 1
0
UExpression Vector::create(Reader *r) {
  r->pop_token();

  auto without_square_close = without(closeTypes, TokenType::SquareClose);

  std::list<UExpression> l;
  read_until(r, TokenType::SquareClose, without_square_close, l);

  return make_unique<Vector>(l);
}
Exemplo n.º 2
0
UExpression Set::create(Reader *r) {
  r->pop_token();

  auto without_curly_close = without(closeTypes, TokenType::CurlyClose);

  std::list<UExpression> l;
  read_until(r, TokenType::CurlyClose, without_curly_close, l);

  return make_unique<Set>(l);
}
Exemplo n.º 3
0
UExpression List::create(Reader *r) {
  r->pop_token();

  auto without_round_close = without(closeTypes, TokenType::RoundClose);

  std::list<UExpression> l;
  read_until(r, TokenType::RoundClose, without_round_close, l);

  return make_unique<List>(l);
}
static FILE * withAndWithout( std::string filename, std::string time_of_day_hint )
{
    string with( filename+"_"+time_of_day_hint+BASE_EXTENSION );
    FILE  *fp = VSFileSystem::vs_open( with.c_str(), "r" );
    if (!fp) {
        string without( filename+BASE_EXTENSION );
        fp = VSFileSystem::vs_open( without.c_str(), "r" );
    }
    return fp;
}
Exemplo n.º 5
0
UExpression Map::create(Reader *r) {
  r->pop_token();

  auto without_curly_close = without(closeTypes, TokenType::CurlyClose);

  std::list<UExpression> l;
  read_until(r, TokenType::CurlyClose, without_curly_close, l);

  if (l.size() % 2 == 1) {
    throw ReaderException("Map entries should be even");
  }

  return make_unique<Map>(l);
}
Exemplo n.º 6
0
/**
 * Run the global analysis.  Put all data and covariates in a logistic regression model.
 *
 * Compute likelihood ratio statistic.
 * This uses a likelihood statistic where n-1 haplotypes are tested.
 * 
 * @return pvalue.
 */
ZaykinGlobalStatsResults Zaykin::runGlobal(){

	ZaykinGlobalStatsResults stats;

	vector<vector<double> > haps;
	vector<double> ones;

	prepHaplotypes(ones, haps);

	#if DEBUG_ZAY_PROGRESS
		cout << "Zaykin start LR portion" << endl;
	#endif
	LogisticRegression with(params->getRegressionConditionNumberThreshold()), without(params->getRegressionConditionNumberThreshold());
	/*
	 * Run without the haplotypes:
	 */
	vector<vector<double> > inv_infmatrixWithOut;
	vector<double> betasWithOut;

	vector<vector<double> > inWithout;
	inWithout = cov;
	inWithout.push_back(ones);

	inv_infmatrixWithOut = vecops::getDblVec(inWithout.size() , inWithout.size());

	int retry = 0;
	double startVal = 0;  // value to start betas with.

	while(retry < 3){
		try{
			betasWithOut = without.newtonRaphson(inWithout, phenotype, inv_infmatrixWithOut, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute reduced model: information matrix was singular.");
		}catch(ConditionNumberEx err){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(inWithout, phenotype);
			
			string message;
			if (separableVariable < 0){
				// Error: poor conditioning.
				stringstream ss;
				ss << "Unable to compute reduced model: Poor conditioning in information matrix. ";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
				message = ss.str();
			}else{
				stringstream ss;
				ss << "Unable to compute reduced model: Separable data matrix.";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
				message = ss.str();
			}
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(ADTException e){
			// This one is generic.
			string message = "Unable to compute reduced model: Newton-Raphson error.";
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute reduced model due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry, ss.str());
			if (retry >= 3) return stats;
		}
	}

	/*
	 * Run with the haplotypes:
	 */
	vector<vector<double> > inv_infmatrixWith, inWith;
	vector<double> betasWith;

	inWith = inWithout;
	for (unsigned int i=0; i < haps.size()-1; i++){ // NOTE: Don't push the very last haplotype.
		inWith.push_back(haps.at(i));
	}

	inv_infmatrixWith = vecops::getDblVec(inWith.size() , inWith.size());

	retry = 0;
	startVal = 0;  // value to start betas with.

	while(retry < 3){
		try{
			betasWith = with.newtonRaphson(inWith, phenotype, inv_infmatrixWith, startVal);
			break;
		}catch(NewtonRaphsonFailureEx){
			handleException(stats, startVal, retry, "Unable to compute full model: Newton-Raphson setup failure.");
		}catch(NewtonRaphsonIterationEx){
			handleException(stats, startVal, retry, "Unable to compute full model: max iterations hit.");
		}catch(SingularMatrixEx){
			handleException(stats, startVal, retry, "Unable to compute full model: information matrix was singular.");
		}catch(ConditionNumberEx err){
			
			LogisticRegression lr;
			int separableVariable = lr.dataIsSeparable(inWith, phenotype);
			
			stringstream ss;
			if (separableVariable < 0){
				// Error: poor conditioning.
				ss << "Unable to compute reduced model: Poor conditioning in information matrix. ";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
			}else{
				ss << "Unable to compute reduced model: Separable data matrix.";
				ss << "Condition number (1-norm) is " << err.conditionNumber;
			}
			string message = ss.str();
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;	
		}catch(ADTException e){
			string message = "Unable to compute full model: Newton-Raphson error.";
			handleException(stats, startVal, retry, message);
			if (retry >= 3) return stats;
		}catch(alglib::ap_error err){
			stringstream ss;
			ss << "Unable to compute full model due to linalg exception: " << err.msg;
			handleException(stats, startVal, retry,ss.str());
			if (retry >= 3) return stats;
		}
	}

	double likeRatio =  with.likelihoodRatio(betasWithOut, inWithout, betasWith, inWith, phenotype);
	try{
		stats.pvalue = Statistics::chi2prob(likeRatio, betasWith.size() - betasWithOut.size());
		stats.testStat = likeRatio;
		stats.degFreedom = betasWith.size() - betasWithOut.size();
	}catch(...){

		stringstream ss;
		ss << "Zaykin's method: unable to compute chi square: " << likeRatio << " " << betasWith.size() - betasWithOut.size() << endl;
		Logger::Instance()->writeLine(ss.str());

		stats.fillDefault();
		return stats;
	}
	return stats;
}