Example usage for org.apache.commons.math3.stat.regression SimpleRegression getRSquare

List of usage examples for org.apache.commons.math3.stat.regression SimpleRegression getRSquare

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat.regression SimpleRegression getRSquare.

Prototype

public double getRSquare() 

Source Link

Document

Returns the <a href="http://www.xycoon.com/coefficient1.htm"> coefficient of determination</a>, usually denoted r-square.

Usage

From source file:dynetica.algorithm.DataFit.java

public void runExponentialFit() {
    //want to fit equation y = alpha*e^(Beta*x)

    double[] xvals;
    double[] yvals;
    if (independent == "Time")
        xvals = system.getTimer().getTimePoints();
    else {//from   w w w.  jav  a2 s. c o m
        if (independent.indexOf("Rate of") < 0) {
            xvals = system.getSubstance(independent).getValues();
        } else {
            xvals = system.getSubstance(independent.split("\\s+")[independent.split("\\s+").length - 1])
                    .getRates();
        }
    }

    if (dependent.indexOf("Rate of") < 0) {
        yvals = system.getSubstance(dependent).getValues();
    } else {
        yvals = system.getSubstance(dependent.split("\\s+")[dependent.split("\\s+").length - 1]).getRates();
    }

    SimpleRegression model = new SimpleRegression(true);
    double x, y;
    //need to use arraylist since we may not be using all indexed values in xvals
    ArrayList<Double> xValuesList = new ArrayList<Double>();
    for (int i = 0; i < xvals.length; i++) {
        if (yvals[i] == 0) {
            continue;
        } else {
            y = log(yvals[i]);
            model.addData(xvals[i], y);
            xValuesList.add(xvals[i]);
        }
    }

    double alpha = exp(model.getIntercept());
    double Beta = exp(model.getSlope());

    fitParameters.put("alpha", alpha);
    fitParameters.put("Beta", Beta);
    fitParameters.put("r-squared", model.getRSquare());

    // now create best fit line
    xValues = new double[xValuesList.size()];
    yValues = new double[xValuesList.size()];
    for (int i = 0; i < xValuesList.size(); i++) {
        xValues[i] = xValuesList.get(i);
        yValues[i] = alpha * exp(Beta * xValuesList.get(i));
    }

}

From source file:model.plate.ANATestResult.java

public void diagnose2(double control) {
    //titer,positivity,r2,pattern
    final Comparator<DiagnosisConstant.ANA_Titer> titerComparator = new Comparator<DiagnosisConstant.ANA_Titer>() {
        @Override/*from  w w w  .j  a  v a  2s  .  c o m*/
        public int compare(DiagnosisConstant.ANA_Titer t, DiagnosisConstant.ANA_Titer t1) {
            if (t.getId() < 0) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t1.getId() < 0) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t.getId() > 6) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t1.getId() > 6) {
                throw new RuntimeException("Titer: " + t.name());
            }
            return t.getId() < t1.getId() ? -1 : t.getId() == t1.getId() ? 0 : 1;
        }
    };
    TreeMap<DiagnosisConstant.ANA_Titer, Double> decreasingSignals = new TreeMap<>(titerComparator);
    decreasingSignals.putAll(signals);
    SimpleRegression regression = new SimpleRegression();
    Iterator<DiagnosisConstant.ANA_Titer> it = decreasingSignals.keySet().iterator();
    DiagnosisConstant.ANA_Titer t;
    Double signal;
    while (it.hasNext()) {
        t = it.next();
        signal = decreasingSignals.get(t);
        if (signal == null)
            continue;
        //            posCtrl=signal>posCtrl?signal:posCtrl; ??1:40, 
        regression.addData((double) t.getId(), signal);
        if (signal > control) {// * PlateConstants.PositiveCutOffRatio
            titer = t;
        }
    }

    r2 = regression.getRSquare();
    if (r2 < PlateConstants.R2_TH) {
        warningMessage.add(WarningMessage.SampleLinearity.getId());
    }
    if (titer == null)
        titer = DiagnosisConstant.ANA_Titer.ANA_LESS_1_40;
    if (DiagnosisConstant.ANA_Titer.ANA_LESS_1_40.equals(titer) || titer.getId() < 2) {//1:40

        System.out.println();
        for (DiagnosisConstant.ANA_Titer t1 : decreasingSignals.keySet()) {
            System.out.println(
                    this.julien_barcode + " Sample vs Control (th=" + PlateConstants.PositiveCutOffRatio + ")");
            System.out.println(t1 + ": signal=" + decreasingSignals.get(t1) + "\tv.s.\tcontrol=" + control
                    + " (" + decreasingSignals.get(t1) / control + ")");
        }
        System.out.println();
        positivity = DiagnosisConstant.ANA_Result.NEGATIVE;
        warningMessage.add(WarningMessage.WeakPositive.getId());

    } else {
        positivity = DiagnosisConstant.ANA_Result.POSITIVE;
    }
}

From source file:dynetica.algorithm.DataFit.java

public void runHillFit() {
    double[] xvals;
    double[] yvals;
    if (independent == "Time")
        xvals = system.getTimer().getTimePoints();
    else {//w ww .  ja v a 2 s.  co m
        if (independent.indexOf("Rate of") < 0) {
            xvals = system.getSubstance(independent).getValues();
        } else {
            xvals = system.getSubstance(independent.split("\\s+")[independent.split("\\s+").length - 1])
                    .getRates();
        }
    }

    if (dependent.indexOf("Rate of") < 0) {
        yvals = system.getSubstance(dependent).getValues();
    } else {
        yvals = system.getSubstance(dependent.split("\\s+")[dependent.split("\\s+").length - 1]).getRates();
    }

    double maxRate = 0;
    for (int i = 0; i < yvals.length; i++) {
        if (yvals[i] > maxRate)
            maxRate = yvals[i];
    }

    SimpleRegression model = new SimpleRegression(true);
    double x, y;
    //need to use arraylist since we may not be using all indexed values in xvals
    ArrayList<Double> xValuesList = new ArrayList<Double>();
    for (int i = 0; i < xvals.length; i++) {
        double v = yvals[i] / maxRate;
        if (v == 0) {
            //dont include this x and y
            continue;
        } else {
            x = log(xvals[i]);
            y = log(v / (1 - v));
            model.addData(x, y);
            //add x to xResults
            xValuesList.add(xvals[i]);
        }
    }

    double hillCoefficient = model.getSlope();
    double Km = exp(-(model.getIntercept()) / model.getSlope());
    System.out.println(Km);
    System.out.println(hillCoefficient);

    //add parameters of best fit to hashmap, including r-squared value
    fitParameters.put("n", hillCoefficient);
    fitParameters.put("Km", Km);
    fitParameters.put("r-squared", model.getRSquare());

    // now create best fit line
    xValues = new double[xValuesList.size()];
    yValues = new double[xValuesList.size()];
    for (int i = 0; i < xValuesList.size(); i++) {
        xValues[i] = xValuesList.get(i);
        yValues[i] = Math.pow(xValues[i], hillCoefficient)
                / (Math.pow(xValues[i], hillCoefficient) + Math.pow(Km, hillCoefficient));
    }

}

From source file:model.plate.ANATestResult.java

public boolean initPosCtrl2(double negControl) {
    final Comparator<DiagnosisConstant.ANA_Titer> titerComparator = new Comparator<DiagnosisConstant.ANA_Titer>() {
        @Override//from ww w . j a va2s  .c  o  m
        public int compare(DiagnosisConstant.ANA_Titer t, DiagnosisConstant.ANA_Titer t1) {
            if (t.getId() < 0) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t1.getId() < 0) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t.getId() > 6) {
                throw new RuntimeException("Titer: " + t.name());
            }
            if (t1.getId() > 6) {
                throw new RuntimeException("Titer: " + t.name());
            }
            return t.getId() < t1.getId() ? -1 : t.getId() == t1.getId() ? 0 : 1;
        }
    };
    TreeMap<DiagnosisConstant.ANA_Titer, Double> decreasingSignals = new TreeMap<>(titerComparator);
    decreasingSignals.putAll(signals);
    SimpleRegression regression = new SimpleRegression();
    Iterator<DiagnosisConstant.ANA_Titer> it = decreasingSignals.keySet().iterator();
    DiagnosisConstant.ANA_Titer t;
    double signal, posCtrl = getFirstPlateSignal();

    while (it.hasNext()) {
        t = it.next();
        signal = decreasingSignals.get(t);
        //            posCtrl=signal>posCtrl?signal:posCtrl; ??1:40, 
        regression.addData((double) t.getId(), signal);
        if (signal > posCtrl * PlateConstants.PositiveCutOffRatio
                || signal > negControl * PlateConstants.NegativeCutOffRatio) {
            titer = t;

        }
    }
    if (titer.getId() >= DiagnosisConstant.ANA_Titer.ANA_1_320.getId()) {
        positivity = DiagnosisConstant.ANA_Result.POSITIVE;
        System.out.println("found titer for " + plateID + " : " + titer);
        System.out.println();
        System.out.println();
    }
    r2 = regression.getRSquare();
    if (r2 < PlateConstants.R2_TH) {
        warningMessage.add(WarningMessage.PositiveControlLinearity.getId());
    }
    if (titer == null || titer.getId() < DiagnosisConstant.ANA_Titer.ANA_1_320.getId()) {//1:40
        titer = DiagnosisConstant.ANA_Titer.ANA_LESS_1_40;
        System.out.println();
        for (DiagnosisConstant.ANA_Titer t1 : decreasingSignals.keySet()) {
            System.out.println(plateID + " Control Sample Compare");
            System.out.println(t1 + ": posCtrl=" + decreasingSignals.get(t1) + "\tv.s.\tnegCtrl=" + negControl
                    + " (" + decreasingSignals.get(t1) / negControl + ")");
        }
        System.out.println();
        positivity = DiagnosisConstant.ANA_Result.NEGATIVE;
        System.out.println("barcode " + this.julien_barcode);
        warningMessage.add(WarningMessage.PositiveNegativeControlComparison.getId());

    } else {
        positivity = DiagnosisConstant.ANA_Result.POSITIVE;
    }
    if (posCtrl < negControl * PlateConstants.CTRL_RATIO_TH) {
        this.warningMessage.add(WarningMessage.PosCtrlFailed.getId());
        return false;
    }
    return true;
}

From source file:com.medlog.webservice.vo.DiaryAnalysisSummaryVO.java

private void populateCorrelation() {
    SpearmansCorrelation c = new SpearmansCorrelation();

    getCorr()[IDX_MOOD] = c.correlation(getMood(), getMood());
    getCorr()[IDX_AGREEABLENESS_BIG5] = c.correlation(getMood(), getAgreeablenessBig5());
    getCorr()[IDX_MOOD] = c.correlation(getMood(), getMood());
    getCorr()[IDX_AGREEABLENESS_BIG5] = c.correlation(getMood(), getAgreeablenessBig5());
    getCorr()[IDX_ANALYTICAL] = c.correlation(getMood(), getAnalytical());
    getCorr()[IDX_ANGER] = c.correlation(getMood(), getAnger());
    getCorr()[IDX_CONFIDENT] = c.correlation(getMood(), getConfident());
    getCorr()[IDX_CONSCIENTIOUSNESS_BIG5] = c.correlation(getMood(), getConscientiousnessBig5());
    getCorr()[IDX_DISGUST] = c.correlation(getMood(), getDisgust());
    getCorr()[IDX_EMOTIONALRANGE_BIG5] = c.correlation(getMood(), getEmotionalRangeBig5());
    getCorr()[IDX_EXTRAVERSION_BIG5] = c.correlation(getMood(), getExtraversionBig5());
    getCorr()[IDX_FEAR] = c.correlation(getMood(), getFear());
    getCorr()[IDX_JOY] = c.correlation(getMood(), getJoy());
    getCorr()[IDX_OPENNESS_BIG5] = c.correlation(getMood(), getOpennessBig5());
    getCorr()[IDX_SADNESS] = c.correlation(getMood(), getSadness());
    getCorr()[IDX_TENTATIVE] = c.correlation(getMood(), getTentative());
    double corrTemp;
    for (int k = 0; k < getCorr().length; k++) {
        corrTemp = Math.pow(getCorr()[k], 2);
        if (StrUtl.matchOR(k, IDX_ANGER, IDX_DISGUST, IDX_FEAR, IDX_SADNESS)) {
            corrTemp = .99 - corrTemp;/*w  ww .  j  a v a2 s .c  o  m*/
        }
        getrSquared()[k] = corrTemp;
    }
    double q3 = org.apache.commons.math3.stat.StatUtils.percentile(getrSquared(), .75);

    double q1 = org.apache.commons.math3.stat.StatUtils.percentile(getrSquared(), .25);
    double var = StatUtils.variance(getrSquared());
    System.out.println("q1" + q1);
    System.out.println("q3" + q3);
    System.out.println("var" + var);
    double max = StatUtils.max(getrSquared());

    double min = StatUtils.max(getrSquared());

    setSum(StatUtils.sum(getrSquared()) - 1.0);
    System.out.println("sum" + getSum());
    for (int j = 1; j < 14; j++) {
        getCorrRanked()[j] = getrSquared()[j] / getSum();
    }
    System.out.println("pmf sum" + StatUtils.sum(getCorrRanked()));
    double[] cRCopy = ArrayUtils.clone(getCorrRanked());
    Arrays.sort(cRCopy);
    ArrayUtils.reverse(cRCopy);
    setToneList(new ArrayList<ToneKeyValuePair>());
    for (int j = 1; j < 14; j++) {
        ArrayUtils.indexOf(cRCopy, getCorrRanked()[j]);
        //ToneKeyValuePair t = ToneKeyValuePair.builder().key(CORR_STR[j]).value(getrSquared()[j]).weightedValue(getCorrRanked()[j]).rank(ArrayUtils.indexOf(cRCopy, getCorrRanked()[j])).build();
        getToneList().add(ToneKeyValuePair.builder().key(CORR_STR[j]).value(getrSquared()[j])
                .weightedValue(getCorrRanked()[j]).rank(ArrayUtils.indexOf(cRCopy, getCorrRanked()[j]) + 1)
                .historicalRawAvg(getAverageScores()[j]).build());
        //            corrRanked[j] = rSquared[j] / sum;
    }

    double guess = 0;

    for (ToneKeyValuePair t : getToneList()) {
        guess += (t.getWeightedValue() * 10 * getValFromKeyPct(t.getKey()));//t.getWeightedValue());

        System.out.println(t.toString());
        System.out.println("com.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation() Guess with "
                + t.getRank() + "== " + guess);
    }
    SimpleRegression sg = new SimpleRegression(false);
    populateLineGuessPoints(sg);

    getGuesses()[1] = sg.predict(getToneCurrentAvgX());
    System.out
            .println("\n\n\ncom.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation() GUESS === >");

    System.out.printf("Weighted (history) Guess ------> %.3f%n", (guess));
    System.out.printf("Best fit line Guess -----------> %.3f%n", sg.predict(getToneCurrentAvgX()));
    guess /= 10;
    guess = Math.round(guess);
    System.out.printf("Weighted (history) Guess adj.-----> %.3f%n", (guess));
    System.out.println("-------------------------------------------\n");
    getGuesses()[0] = guess;
    getLineEq()[SLOPE] = sg.getSlope();
    getLineEq()[YINT] = sg.getIntercept();
    getLineEq()[R] = sg.getRSquare();
    double[] norm = StatUtils.normalize(getCorrRanked());

    if (DEBUG) {
        System.out.println("com.medlog.webservice.vo.DiaryAnalysisSummaryVO.populateCorrelation()"
                + Arrays.toString(norm));
    }
    getFiveSummary()[0] = StatUtils.min(norm);
    getFiveSummary()[1] = StatUtils.percentile(norm, 25);

    getFiveSummary()[2] = StatUtils.percentile(norm, 50);

    getFiveSummary()[3] = StatUtils.percentile(norm, 75);

    getFiveSummary()[4] = StatUtils.max(norm);

}

From source file:erigo.filewatch.FileWatch.java

/**
 * Write data we've collected in the HashMap to the output file
 */// ww w .j  a  v  a2 s .  c  o  m
void processData(String filenameI) {

    //
    // Firewall
    //
    if (fileData.isEmpty()) {
        System.err.println("No data to write out");
        return;
    }

    //
    // Pre-process the data to calculate all needed metrics
    //
    List<Double> latencyList = new ArrayList<Double>();
    List<String> filenameList = new ArrayList<String>();
    List<Long> sinkCreationTimeList = new ArrayList<Long>();
    List<Double> normalizedSinkCreationTimeList = new ArrayList<Double>();
    List<Long> sourceCreationTimeList = new ArrayList<Long>();
    List<Double> normalizedSourceCreationTimeList = new ArrayList<Double>();
    List<Integer> cumulativeNumFilesList = new ArrayList<Integer>();
    List<Integer> sequenceNumberList = new ArrayList<Integer>();
    List<Boolean> outOfOrderList = new ArrayList<Boolean>();
    int cumulativeNumberOfFiles = 0;
    long earliestSourceCreationTime = Long.MAX_VALUE;
    double latency_sum = 0.0;
    double latency_min = Double.MAX_VALUE;
    double latency_max = -Double.MAX_VALUE;
    int previousIndex = 0;
    for (Map.Entry<Double, String> entry : fileData.entrySet()) {

        // Strip the decimal part of the time off - it was only added in processEvents() to make unique hash keys
        long sinkCreationTime = (long) Math.floor(entry.getKey());

        // Parse the filename
        String filename = entry.getValue();
        FilenameInfo fiObj = checkFilename(filename);
        if (fiObj.errStr != null) {
            System.err.println(fiObj.errStr);
            ++num_skipped_files;
            continue;
        }
        long sourceCreationTime = fiObj.sourceCreationTime;
        int sequenceNumber = fiObj.sequenceNumber;

        // Save data in Lists
        earliestSourceCreationTime = Math.min(earliestSourceCreationTime, sourceCreationTime);
        double latency = (sinkCreationTime - sourceCreationTime) / 1000.0;
        latency_sum = latency_sum + latency;
        latencyList.add(new Double(latency));
        latency_min = Math.min(latency_min, latency);
        latency_max = Math.max(latency_max, latency);
        filenameList.add(filename);
        sinkCreationTimeList.add(new Long(sinkCreationTime));
        sourceCreationTimeList.add(new Long(sourceCreationTime));
        cumulativeNumberOfFiles = cumulativeNumberOfFiles + 1;
        cumulativeNumFilesList.add(new Integer(cumulativeNumberOfFiles));
        sequenceNumberList.add(new Integer(sequenceNumber));
        if (sequenceNumber != (previousIndex + 1)) {
            outOfOrderList.add(Boolean.valueOf(true));
        } else {
            outOfOrderList.add(Boolean.valueOf(false));
        }
        previousIndex = sequenceNumber;
    }

    // Calculate latency statistics (average and standard deviation)
    int num_entries = latencyList.size();
    double latency_avg = latency_sum / num_entries;
    double latency_variance = 0.0;
    for (double next_latency : latencyList) {
        latency_variance = latency_variance + Math.pow((next_latency - latency_avg), 2.0);
    }
    latency_variance = latency_variance / ((double) num_entries);
    double latency_stddev = Math.sqrt(latency_variance);

    // Calculate normalized source and sink creation times
    for (int i = 0; i < num_entries; ++i) {
        long sourceCreationTime = sourceCreationTimeList.get(i).longValue();
        long sinkCreationTime = sinkCreationTimeList.get(i).longValue();
        double normalizedSourceCreationTime = (sourceCreationTime - earliestSourceCreationTime) / 1000.0;
        normalizedSourceCreationTimeList.add(new Double(normalizedSourceCreationTime));
        double normalizedSinkCreationTime = (sinkCreationTime - earliestSourceCreationTime) / 1000.0;
        normalizedSinkCreationTimeList.add(new Double(normalizedSinkCreationTime));
    }

    // Check that the data arrays are all the same length
    if ((latencyList.size() != num_entries) || (filenameList.size() != num_entries)
            || (sinkCreationTimeList.size() != num_entries)
            || (normalizedSinkCreationTimeList.size() != num_entries)
            || (sourceCreationTimeList.size() != num_entries)
            || (normalizedSourceCreationTimeList.size() != num_entries)
            || (cumulativeNumFilesList.size() != num_entries) || (sequenceNumberList.size() != num_entries)
            || (outOfOrderList.size() != num_entries)) {
        System.err.println("ERROR: data arrays are not the same size!");
        return;
    }

    //
    // Calculate test metrics:
    //     o actual source rate
    //     o Rku (files/sec)
    //     o Lav (sec)
    //     o Lmax (sec)
    //     o Lgr (sec/file)
    //     o Lmax (sec) = maximum latency = Lav + 3 * (stddev of latency)
    //
    // To calculate these we need the following linear regressions:
    // 1. index from file vs. create time at source, normalized
    //       slope = the actual source rate
    // 2. cumulative number of files at sink vs. create time at sink, normalized
    //       slope = keep up rate, Rku (files/sec)
    //       only applicable for a "fast" file test
    // 3. latency vs. create time at source, normalized (sec)
    //       y-intercept = average latency, Lav (sec)
    //       only applicable for a "slow" file test
    // 4. latency vs. index from file
    //       slope = latency growth rate, Lgr (sec/file)
    //       only applicable for a "slow" file test
    //
    // We use the Simple regression class available from Apache Commons Math:
    // http://commons.apache.org/proper/commons-math/userguide/stat.html#a1.4_Simple_regression
    // http://commons.apache.org/proper/commons-math/apidocs/org/apache/commons/math4/stat/regression/SimpleRegression.html
    //
    SimpleRegression actual_source_rate_reg = new SimpleRegression();
    SimpleRegression Rku_reg = new SimpleRegression();
    SimpleRegression Lav_reg = new SimpleRegression();
    SimpleRegression Lgr_reg = new SimpleRegression();
    for (int i = 0; i < num_entries; ++i) {
        // Here's the data columns written out in order
        // filenameList.get(i)
        // sourceCreationTimeList.get(i).longValue()
        // normalizedSourceCreationTimeList.get(i).doubleValue()
        // sinkCreationTimeList.get(i).longValue()
        // normalizedSinkCreationTimeList.get(i).doubleValue()
        // latencyList.get(i).doubleValue()
        // cumulativeNumFilesList.get(i).intValue()
        // sequenceNumberList.get(i).intValue()
        // outOfOrderList.get(i).booleanValue()
        actual_source_rate_reg.addData(normalizedSourceCreationTimeList.get(i).doubleValue(),
                (double) sequenceNumberList.get(i).intValue());
        Rku_reg.addData(normalizedSinkCreationTimeList.get(i).doubleValue(),
                (double) cumulativeNumFilesList.get(i).intValue());
        Lav_reg.addData(normalizedSourceCreationTimeList.get(i).doubleValue(),
                latencyList.get(i).doubleValue());
        Lgr_reg.addData((double) sequenceNumberList.get(i).intValue(), latencyList.get(i).doubleValue());
    }
    double Lmax = Lav_reg.getIntercept() + 3.0 * (latency_stddev);

    //
    // If we are in recaster mode, see if user wants to output results
    //
    if (bRecast && !bOutputResultsInRecastMode) {
        // we're done
        return;
    }

    //
    // Write out data to console
    //
    System.err.println("\nTest metrics:");
    System.err.println(
            "Actual source rate = " + String.format("%5g", actual_source_rate_reg.getSlope()) + " files/sec");
    System.err.println("Metrics applicable to \"fast\" file tests:");
    System.err.println("   Rku = " + String.format("%5g", Rku_reg.getSlope()) + " files/sec");
    System.err.println("Metrics applicable to \"slow\" file tests:");
    final double latencyMultiplier;
    if (bAdjustLatencyMetrics) {
        System.err.println(
                "(NOTE: these latency metrics have been divided by 2 to represent the equivalent one-way metrics values)");
        latencyMultiplier = 0.5;
    } else {
        latencyMultiplier = 1.0;
    }
    System.err.println("   Lav = " + String.format("%5g", Lav_reg.getIntercept() * latencyMultiplier) + " sec");
    System.err.println("   Lmax observed = " + String.format("%.3f", latency_max * latencyMultiplier) + " sec");
    System.err.println("   Lmax calculated (Lav + 3*(stddev of latency)) = "
            + String.format("%.3f", Lmax * latencyMultiplier) + " sec");
    System.err
            .println("   Lgr = " + String.format("%5g", Lgr_reg.getSlope() * latencyMultiplier) + " sec/file");

    //
    // Write out data to file
    //
    BufferedWriter writer = null;
    try {

        File dataFile = new File(filenameI);
        writer = new BufferedWriter(new FileWriter(dataFile));

        // Write header, including statistics and metrics
        writer.write(
                "\nFile sharing test\ntest start time\t" + earliestSourceCreationTime + "\tmsec since epoch\n");
        if (bAdjustLatencyMetrics) {
            writer.write(
                    "NOTE: the latency metrics that follow have been divided by 2 to represent the equivalent one-way metrics values; Regression values and the raw data below represent the full round-trip values\n");
        }
        writer.write("Actual source rate\t" + String.format("%5g", actual_source_rate_reg.getSlope())
                + "\tfiles/sec\n");
        writer.write("Rku\t" + String.format("%5g", Rku_reg.getSlope()) + "\tfiles/sec\n");
        writer.write("Lav\t" + String.format("%5g", Lav_reg.getIntercept() * latencyMultiplier) + "\tsec\n");
        writer.write("Lmax observed\t" + String.format("%.3f", latency_max * latencyMultiplier) + "\tsec\n");
        writer.write(
                "std dev of latency\t" + String.format("%.3f", latency_stddev * latencyMultiplier) + "\tsec\n");
        writer.write("Lmax calculated (Lav + 3*(stddev of latency))\t"
                + String.format("%.3f", Lmax * latencyMultiplier) + "\tsec\n");
        writer.write("Lgr\t" + String.format("%5g", Lgr_reg.getSlope() * latencyMultiplier) + "\tsec/file\n");
        // Write out regression data
        writer.write("Regression\tslope\tintercept\tR-squared\tMetric of interest\n\t\t\t\tname\tvalue\n");
        double slope = actual_source_rate_reg.getSlope();
        double intercept = actual_source_rate_reg.getIntercept();
        double rsquared = actual_source_rate_reg.getRSquare();
        writer.write("source rate\t" + String.format("%5g", slope) + "\t" + String.format("%5g", intercept)
                + "\t" + String.format("%5g", rsquared) + "\tsource rate, files/sec (slope)\t"
                + String.format("%5g", slope) + "\n");
        slope = Rku_reg.getSlope();
        intercept = Rku_reg.getIntercept();
        rsquared = Rku_reg.getRSquare();
        writer.write("actual sink rate\t" + String.format("%5g", slope) + "\t" + String.format("%5g", intercept)
                + "\t" + String.format("%5g", rsquared) + "\tRku, files/sec (slope)\t"
                + String.format("%5g", slope) + "\n");
        slope = Lav_reg.getSlope();
        intercept = Lav_reg.getIntercept();
        rsquared = Lav_reg.getRSquare();
        writer.write("latency vs create time at source\t" + String.format("%5g", slope) + "\t"
                + String.format("%5g", intercept) + "\t" + String.format("%5g", rsquared)
                + "\tLav, avg latency, sec (intercept)\t" + String.format("%5g", intercept) + "\n");
        slope = Lgr_reg.getSlope();
        intercept = Lgr_reg.getIntercept();
        rsquared = Lgr_reg.getRSquare();
        writer.write("latency vs index from file\t" + String.format("%5g", slope) + "\t"
                + String.format("%5g", intercept) + "\t" + String.format("%5g", rsquared)
                + "\tLgr, latency growth, sec/file (slope)\t" + String.format("%5g", slope) + "\n");
        writer.write(
                "\nFilename\tCreate time at source (msec)\tCreate time at source, normalized (sec)\tCreate time at sink (msec)\tCreate time at sink, normalized (sec)\tLatency (sec)\tCumulative number of files at sink\tIndex from file\tOut of order or missing?\n");

        // Write data for each file
        for (int i = 0; i < num_entries; ++i) {
            writer.write(filenameList.get(i) + "\t" + sourceCreationTimeList.get(i).longValue() + "\t"
                    + String.format("%.3f", normalizedSourceCreationTimeList.get(i).doubleValue()) + "\t"
                    + sinkCreationTimeList.get(i).longValue() + "\t"
                    + String.format("%.3f", normalizedSinkCreationTimeList.get(i).doubleValue()) + "\t"
                    + String.format("%.3f", latencyList.get(i).doubleValue()) + "\t"
                    + cumulativeNumFilesList.get(i).intValue() + "\t" + sequenceNumberList.get(i).intValue()
                    + "\t" + outOfOrderList.get(i).toString() + "\n");
            writer.flush();
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        writer.close();
    } catch (IOException e) {
        // nothing to do
    }

    //
    // Display plots
    //
    if (bAdjustLatencyMetrics) {
        latencyPlotTitle = "Latency (one-way)";
    }
    if ((customPlotTitle != null) && (!customPlotTitle.isEmpty())) {
        latencyPlotTitle = new String(customPlotTitle + ", " + latencyPlotTitle);
        throughputPlotTitle = new String(customPlotTitle + ", " + throughputPlotTitle);
    }
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            // For the plot, multiply latency data by latencyMultiplier, which will either leave the
            // latency data as-is or will divide it by 2 (if user wanted the adjusted, one-way equivalent data)
            List<Double> adjustedLatencyList = new ArrayList<Double>();
            for (int i = 0; i < num_entries; ++i) {
                adjustedLatencyList.add(latencyList.get(i) * latencyMultiplier);
            }
            String yAxisTitle = "Latency (sec)";
            if (bAdjustLatencyMetrics) {
                yAxisTitle = "Latency, one-way equivalent (sec)";
            }
            new DisplayPlot(latencyPlotTitle, "Create time at source (sec)", yAxisTitle,
                    normalizedSourceCreationTimeList, adjustedLatencyList, 300, 300).setVisible(true);
            // Need to convert cumulativeNumFilesList to List<Double> to send it to the plot routine
            List<Double> doubleList = new ArrayList<Double>();
            for (int i = 0; i < num_entries; ++i) {
                doubleList.add(new Double((double) cumulativeNumFilesList.get(i).intValue()));
            }
            new DisplayPlot(throughputPlotTitle, "Create time at sink (sec)", "Number of files at sink",
                    normalizedSinkCreationTimeList, doubleList, 400, 400).setVisible(true);
        }
    });

}

From source file:nl.systemsgenetics.cellTypeSpecificAlleleSpecificExpression.CTSlinearRegression.java

public CTSlinearRegression(ArrayList<IndividualSnpData> all_individuals) {

    //basic information, get the zero instance.
    snpName = all_individuals.get(0).getSnpName();
    chromosome = all_individuals.get(0).getChromosome();
    position = all_individuals.get(0).getPosition();

    //isolate heterozygotes
    ArrayList<IndividualSnpData> het_individuals = UtilityMethods
            .isolateValidHeterozygotesFromIndividualSnpData(all_individuals);
    numberOfHets = het_individuals.size();

    hetSampleNames = new ArrayList<String>();
    asRef = new ArrayList<Integer>();
    asAlt = new ArrayList<Integer>();
    asNo = new ArrayList<Integer>();

    cellProp = new ArrayList<Double>();
    int total_overlap = 0;

    //Get the basic data without doing any tests.
    for (IndividualSnpData temp_het : het_individuals) {
        //Do nothing if there is no data in het_individuals

        hetSampleNames.add(temp_het.getSampleName());

        asRef.add(temp_het.getRefNum());
        asAlt.add(temp_het.getAltNum());
        asNo.add(temp_het.getNoNum());/*w  ww.  jav a 2  s.c o  m*/
        cellProp.add(temp_het.getCellTypeProp());

        //this is used to check if we will continue with calculations.
        //BASED on the minHets and MinReads
        total_overlap += temp_het.getRefNum() + temp_het.getAltNum();
    }

    //Check if we do a test.
    if ((total_overlap >= GlobalVariables.minReads) && (numberOfHets >= GlobalVariables.minHets)
            && (numberOfHets >= 3)) {

        ASScatterPlot plotThis = null;

        if (!GlobalVariables.plotDir.equals("")) {
            plotThis = new ASScatterPlot(400);
        }

        SimpleRegression thisRegression = new SimpleRegression();
        for (int i = 0; i < asRef.size(); i++) {
            Double asRatio;
            //do this check, otherwise the denominator will be zero.

            if (asRef.get(i) != 0) {
                asRatio = ((double) asRef.get(i)) / ((double) (asRef.get(i) + asAlt.get(i)));
            } else {
                asRatio = 0.0;
            }

            Double phenoRatio = cellProp.get(i);
            thisRegression.addData(phenoRatio, asRatio);
            if (!GlobalVariables.plotDir.equals("")) {
                plotThis.plot(asRatio, phenoRatio);
            }
        }

        if (!GlobalVariables.plotDir.equals("")) {
            plotThis.draw(GlobalVariables.plotDir + "/" + snpName + "_ASratio_Pheno_Plot.png");
        }

        slope = thisRegression.getSlope();
        intercept = thisRegression.getIntercept();
        Rsquared = thisRegression.getRSquare();
        stdErrorIntercept = thisRegression.getInterceptStdErr();
        stdErrorSlope = thisRegression.getSlopeStdErr();

        pValue = thisRegression.getSignificance();

        if (GlobalVariables.verbosity >= 10) {
            System.out.println("\n--- Starting cell type specific linear regression ---");
            System.out.println("\tSlope:                   " + Double.toString(slope));
            System.out.println("\tStdError of Slope:       " + Double.toString(stdErrorSlope) + "\n");
            System.out.println("\tIntercept:               " + Double.toString(intercept));
            System.out.println("\tStdError of Intercept:   " + Double.toString(stdErrorIntercept) + "\n");
            System.out.println("\tP value:                 " + Double.toString(pValue));
            System.out.println("--------------------------------------------------------------");

        }

        testPerformed = true;
    }

}

From source file:org.apache.solr.client.solrj.io.eval.RegressionEvaluator.java

@Override
public Object doWork(Object first, Object second) throws IOException {
    if (null == first) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the first value", toExpression(constructingFactory)));
    }/*w w  w . j  a va 2s .c o  m*/
    if (null == second) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - null found for the second value", toExpression(constructingFactory)));
    }
    if (!(first instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the first value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }
    if (!(second instanceof List<?>)) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - found type %s for the second value, expecting a list of numbers",
                toExpression(constructingFactory), first.getClass().getSimpleName()));
    }

    List<?> l1 = (List<?>) first;
    List<?> l2 = (List<?>) second;

    if (l2.size() < l1.size()) {
        throw new IOException(String.format(Locale.ROOT,
                "Invalid expression %s - first list (%d) has more values than the second list (%d)",
                toExpression(constructingFactory), l1.size(), l2.size()));
    }

    SimpleRegression regression = new SimpleRegression();
    for (int idx = 0; idx < l1.size(); ++idx) {
        regression.addData(((BigDecimal) l1.get(idx)).doubleValue(), ((BigDecimal) l2.get(idx)).doubleValue());
    }

    Map<String, Number> map = new HashMap<>();
    map.put("slope", regression.getSlope());
    map.put("intercept", regression.getIntercept());
    map.put("R", regression.getR());
    map.put("N", regression.getN());
    map.put("RSquare", regression.getRSquare());
    map.put("regressionSumSquares", regression.getRegressionSumSquares());
    map.put("slopeConfidenceInterval", regression.getSlopeConfidenceInterval());
    map.put("interceptStdErr", regression.getInterceptStdErr());
    map.put("totalSumSquares", regression.getTotalSumSquares());
    map.put("significance", regression.getSignificance());
    map.put("meanSquareError", regression.getMeanSquareError());

    return new RegressionTuple(regression, map);
}

From source file:org.bonej.wrapperPlugins.FractalDimensionWrapper.java

private double getRSquared(final Iterable<ValuePair<DoubleType, DoubleType>> pairs) {
    final SimpleRegression regression = new SimpleRegression();
    pairs.forEach(pair -> regression.addData(pair.a.get(), pair.b.get()));
    return regression.getRSquare();
}

From source file:saffranexperiment.Main.java

/**
 * Calculates the r<sup>2</sup> and RMSE values for each participant type
 * repeat.//  w  w w .  j a  v  a 2  s  .c o  m
 * 
 * @param data A 4D {@link java.util.Arrays Array} with the following 
 * structure (can be obtained from {@link 
 * #calculateMeanSdTAndPValuesForEachExperimentRepeat(double[][][][][])}:
 * 
 * <ul>
 *  <li>First dimension: participant types</li>
 *  <li>Second dimension: repeats</li>
 *  <li>Third dimension: experiments</li>
 *  <li>Fourth dimension:</li> 
 *    <ol type="1">
 *      <li>Familiar word presentation time mean</li>
 *      <li>Familiar word presentation time standard deviation</li>
 *      <li>Novel word presentation time mean</li>
 *      <li>Novel word presentation time standard deviation</li>
 *      <li>t value for familiar/novel word presentation time means</li>
 *      <li>p value for familiar/novel word presentation time means</li>
 *    </ol>
 * </ul>
 * 
 * For example, invoking data[5][3][1][4] would return the t-value associated 
 * with the second experiment of the fourth repeat for participant type 6.  
 * 
 * @return A 3D {@link java.util.Arrays Array}:
 * <ul>
 *  <li>1st dimension: participant types</li>
 *  <li>2nd dimension: repeats</li>
 *  <li>3rd dimension</li>
 *    <ol type="1">
 *      <li>R-square</li>
 *      <li>RMSE</li>
 *    </ol>
 * </ul>
 * 
 * For example, assigning the result to a variable j and invoking j[5][3][1] 
 * would return the RMSE value associated with the fourth repeat of 
 * experiments for participant type 6.
 */
private static double[][][] calculateRSquareAndRmseForEachRepeat(double[][][][] data) {

    double[][][] rsquareAndRmseValues = new double[_totalParticipantTypes][_totalRepeats][2];

    for (int participantType = 0; participantType < _totalParticipantTypes; participantType++) {
        for (int repeat = 0; repeat < _totalRepeats; repeat++) {

            double[][] humanModelFamilAndNovelRecTimesMeans = new double[4][2];

            humanModelFamilAndNovelRecTimesMeans[0][0] = 7.97;
            humanModelFamilAndNovelRecTimesMeans[0][1] = data[participantType][repeat][0][0];
            humanModelFamilAndNovelRecTimesMeans[1][0] = 8.85;
            humanModelFamilAndNovelRecTimesMeans[1][1] = data[participantType][repeat][0][2];

            humanModelFamilAndNovelRecTimesMeans[2][0] = 6.77;
            humanModelFamilAndNovelRecTimesMeans[2][1] = data[participantType][repeat][1][0];
            humanModelFamilAndNovelRecTimesMeans[3][0] = 7.60;
            humanModelFamilAndNovelRecTimesMeans[3][1] = data[participantType][repeat][0][2];

            /****************************/
            /**** Calculate R-square ****/
            /****************************/
            SimpleRegression repeatRegression = new SimpleRegression();
            repeatRegression.addData(humanModelFamilAndNovelRecTimesMeans);
            rsquareAndRmseValues[participantType][repeat][0] = repeatRegression.getRSquare();

            /************************/
            /**** Calculate RMSE ****/
            /************************/

            //The Apache Stats library gives incorrect RMSEs if 
            //Math.sqrt(repeatRegression.getMeanSquaredError()) is used.  Therefore, 
            //the RMSE for a repeat is calculated "by hand".

            //First, get the difference between the mean famil/novel times in each
            //experiment and square them.
            double expt1FamilWordTimeHumanModelDiffSquared = Math.pow(
                    humanModelFamilAndNovelRecTimesMeans[0][0] - humanModelFamilAndNovelRecTimesMeans[0][1],
                    2.0);
            double expt1NovelWordTimeHumanModelDiffSquared = Math.pow(
                    humanModelFamilAndNovelRecTimesMeans[1][0] - humanModelFamilAndNovelRecTimesMeans[1][1],
                    2.0);
            double expt2FamilWordTimeHumanModelDiffSquared = Math.pow(
                    humanModelFamilAndNovelRecTimesMeans[2][0] - humanModelFamilAndNovelRecTimesMeans[2][1],
                    2.0);
            double expt2NovelWordTimeHumanModelDiffSquared = Math.pow(
                    humanModelFamilAndNovelRecTimesMeans[3][0] - humanModelFamilAndNovelRecTimesMeans[3][1],
                    2.0);

            //Sum the differences calculated above.
            double sumHumanModelDiffSquares = expt1FamilWordTimeHumanModelDiffSquared
                    + expt1NovelWordTimeHumanModelDiffSquared + expt2FamilWordTimeHumanModelDiffSquared
                    + expt2NovelWordTimeHumanModelDiffSquared;

            //Divide the sum above by the number of data points.
            double meanSquaredError = sumHumanModelDiffSquares / 8;

            //Finally, square root the mean squared error.
            double rootMeanSquaredError = Math.sqrt(meanSquaredError);
            rsquareAndRmseValues[participantType][repeat][1] = rootMeanSquaredError;
        }
    }

    return rsquareAndRmseValues;
}