List of usage examples for org.apache.commons.math3.stat.regression SimpleRegression clear
public void clear()
From source file:mase.mason.generic.systematic.SystematicEvaluator.java
@Override protected void postSimulation(MasonSimState sim) { if (timeMode == TimeMode.mean) { // Make averages double[] res = new double[size + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double sum = 0; int count = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { sum += d;//from w ww . j a va2s. c o m count++; } } res[i] = (double) (count == 0 ? 0 : sum / count); } res[size] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.last) { double[] res = new double[size + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double last = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { last = d; } } res[i] = (double) last; } res[size] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.meanlast) { double[] res = new double[size * 2 + 1]; for (int i = 0; i < size; i++) { List<Double> featureSample = features.get(i); double last = 0; double sum = 0; int count = 0; for (Double d : featureSample) { if (!Double.isNaN(d)) { sum += d; count++; last = d; } } res[i * 2] = (double) (count == 0 ? 0 : sum / count); res[i * 2 + 1] = (double) last; } res[size * 2] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.simplereg || timeMode == TimeMode.meanslope) { SimpleRegression reg = new SimpleRegression(true); double[] res = new double[size * 2 + 1]; for (int i = 0; i < size; i++) { reg.clear(); double sum = 0; List<Double> featureSample = features.get(i); for (int j = 0; j < featureSample.size(); j++) { double d = featureSample.get(j); if (!Double.isNaN(d)) { reg.addData(j, d); sum += d; } } if (timeMode == TimeMode.simplereg) { res[i * 2] = (double) (reg.getN() >= 2 ? reg.getIntercept() : 0); } else if (timeMode == TimeMode.meanslope) { res[i * 2] = (double) (reg.getN() > 0 ? sum / reg.getN() : 0); } res[i * 2 + 1] = (double) (reg.getN() >= 2 ? reg.getSlope() : 0); } res[size * 2] = sim.schedule.getSteps(); vbr = new SystematicResult(res); } else if (timeMode == TimeMode.frames) { int len = features.get(0).size(); int frameLen = (int) Math.ceil((double) len / timeFrames); // DEBUG for (int i = 0; i < size; i++) { if (features.get(i).size() != len) { System.out.println( "DIFFERENT FEATURE LENGTHS. Expected: " + len + " Got: " + features.get(i).size()); for (List<Double> sample : features) { System.out.print(sample.size() + " "); } System.out.println(); } } double[] res = new double[size * timeFrames + 1]; for (int t = 0; t < timeFrames; t++) { for (int i = 0; i < size; i++) { double sum = 0; int count = 0; for (int x = t * frameLen; x < Math.min(len, (t + 1) * frameLen); x++) { double d = features.get(i).get(x); if (!Double.isNaN(d)) { sum += d; count++; } } res[t * size + i] = (double) (count == 0 ? 0 : sum / count); } } res[size * timeFrames] = features.get(0).size(); vbr = new SystematicResult(res); } }
From source file:de.hshannover.f4.trust.irondetectprocedures.TrendByValueCW.java
/** * * @param featureSet//from w w w . j a v a 2s. com * @param contextSet */ @Override public void train(List<Feature> featureSet, List<Context> contextSet, Calendar startOfTraining, Calendar endOfTraining) { // sort features by context parameter timestamp Collections.sort(featureSet, new FeatureComparatorForCtxp(ContextParamType.DATETIME)); // get the training time int trainingTime = durationInDays(startOfTraining, endOfTraining); logger.info("start training on data from " + trainingTime + " days"); ArrayList<Feature> tmpList = new ArrayList<Feature>(); SimpleRegression tmpRegression = new SimpleRegression(); double tmpSlope = 0; Feature tmpFeature; double timestamp; for (int i = 0; i < (featureSet.size() - freshness); i++) { tmpList.clear(); tmpRegression.clear(); // get featues equal to freshness size for (int j = 0; j < freshness; j++) { tmpFeature = featureSet.get(i + j); // get timestamp in UNIX time ContextParameter contextParam = tmpFeature .getContextParameterByType(new ContextParamType(ContextParamType.DATETIME)); Calendar cal = Helper.getXsdStringAsCalendar(contextParam.getValue()); timestamp = cal.getTime().getTime() / 1000L; if (this.startTimeStampAvailable == false) { setStartTimeStamp((long) timestamp); } this.x = getDeltaTime((long) timestamp); this.y = round(new Double(tmpFeature.getValue()).doubleValue() / 1000); // add data and calculate new slope tmpRegression.addData(x, y); tmpSlope = tmpRegression.getSlope(); logger.info("training step: " + tmpSlope); if (!Double.isNaN(tmpSlope)) { trained = (trained + tmpSlope) / 2; } } } this.trainingDone = true; logger.info("training was done. value is " + this.trained); }
From source file:nl.systemsgenetics.functionenrichmentoftransqtls.CorrelateSumChi2ToPathways.java
/** * @param args the command line arguments *//* w ww .j a v a2s.c o m*/ public static void main(String[] args) throws IOException { final File pathwayMatrixFile = new File(args[0]); final File significantTermsFile = new File(args[1]); final File sumChi2MatrixFile = new File(args[2]); final File transQtlEnrichmentsMatrixFile = new File(args[3]); System.out.println("Pathway file: " + pathwayMatrixFile.getPath()); System.out.println("Pathway significant terms file: " + significantTermsFile.getPath()); System.out.println("SumChi2 file: " + sumChi2MatrixFile.getPath()); System.out.println("Output file: " + transQtlEnrichmentsMatrixFile.getPath()); LinkedHashSet<String> significantTerms = loadSignificantTerms(significantTermsFile); DoubleMatrixDataset<String, String> pathwayMatrix = DoubleMatrixDataset .loadDoubleData(pathwayMatrixFile.getPath()); DoubleMatrixDataset<String, String> sumChi2Matrix = DoubleMatrixDataset .loadDoubleData(sumChi2MatrixFile.getPath()); LinkedHashSet<String> genesInBoth = new LinkedHashSet<String>(); for (String gene : pathwayMatrix.getHashRows().keySet()) { if (sumChi2Matrix.containsRow(gene)) { genesInBoth.add(gene); } } pathwayMatrix = pathwayMatrix.viewColSelection(significantTerms); pathwayMatrix = pathwayMatrix.viewRowSelection(genesInBoth); DoubleMatrixDataset<String, String> transQtlEnrichmentsMatrix = new DoubleMatrixDataset<String, String>( pathwayMatrix.getHashCols(), sumChi2Matrix.getHashCols()); sumChi2Matrix = sumChi2Matrix.viewRowSelection(genesInBoth); System.out.println("Genes in both datasets: " + genesInBoth.size()); System.out.println("Pathways to test: " + pathwayMatrix.columns()); final SimpleRegression regression = new SimpleRegression(); final DoubleRandomEngine randomEngine = new DRand(); StudentT tDistColt = new StudentT(sumChi2Matrix.rows() / 2 - 2, randomEngine); for (String trait : sumChi2Matrix.getColObjects()) { System.out.println("Trait: " + trait); DoubleMatrix1D traitSumChi2 = sumChi2Matrix.getCol(trait); for (String pathway : pathwayMatrix.getColObjects()) { DoubleMatrix1D pathwayScores = pathwayMatrix.getCol(pathway); regression.clear(); for (int i = 0; i < traitSumChi2.size(); ++i) { //System.out.println(traitSumChi2.get(i) + " & " + pathwayScores.get(i)); regression.addData(traitSumChi2.get(i), pathwayScores.get(i)); } double r = regression.getR(); //System.out.println(trait + " " + pathway + " " + r); double t = r / (Math.sqrt((1 - r * r) / (double) (traitSumChi2.size() / 2 - 2))); double pValue; double zScore; if (t < 0) { pValue = tDistColt.cdf(t); if (pValue < 2.0E-323) { pValue = 2.0E-323; } zScore = Probability.normalInverse(pValue); } else { pValue = tDistColt.cdf(-t); if (pValue < 2.0E-323) { pValue = 2.0E-323; } zScore = -Probability.normalInverse(pValue); } pValue *= 2; transQtlEnrichmentsMatrix.setElement(pathway, trait, zScore); } } transQtlEnrichmentsMatrix.save(transQtlEnrichmentsMatrixFile); }