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

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

Introduction

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

Prototype

public double getSlope() 

Source Link

Document

Returns the slope of the estimated regression line.

Usage

From source file:de.qaware.chronix.timeseries.RegressionTest.java

@Test
public void testRegression() {

    SimpleRegression regression = new SimpleRegression();
    regression.addData(0.0, 1.0);/*from   w  ww  . j  ava2s .  c o m*/
    regression.addData(1.0, 2.5);
    regression.addData(2.0, 3.0);

    double slope = regression.getSlope();
    double intercept = regression.getIntercept();
    long n = regression.getN();
    double err = regression.getMeanSquareError();
}

From source file:com.facebook.presto.operator.aggregation.TestDoubleRegrSlopeAggregation.java

@Override
public Object getExpectedValue(int start, int length) {
    if (length <= 1) {
        return null;
    }//from   w ww .j  av  a2 s .  co m
    SimpleRegression regression = new SimpleRegression();
    for (int i = start; i < start + length; i++) {
        regression.addData(i + 2, i);
    }
    return regression.getSlope();
}

From source file:com.facebook.presto.operator.aggregation.TestDoubleRegrSlopeAggregation.java

private void testNonTrivialAggregation(Double[] y, Double[] x) {
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < x.length; i++) {
        regression.addData(x[i], y[i]);//from www .  jav a  2 s .  c  om
    }
    double expected = regression.getSlope();
    checkArgument(Double.isFinite(expected) && expected != 0.0, "Expected result is trivial");
    testAggregation(expected, createDoublesBlock(y), createDoublesBlock(x));
}

From source file:com.facebook.presto.operator.aggregation.TestFloatRegrSlopeAggregation.java

@Override
public Object getExpectedValue(int start, int length) {
    if (length <= 1) {
        return null;
    }/*from  w  w  w .j a v  a2s .  c  om*/
    SimpleRegression regression = new SimpleRegression();
    for (int i = start; i < start + length; i++) {
        regression.addData(i + 2, i);
    }
    return (float) regression.getSlope();
}

From source file:com.facebook.presto.operator.aggregation.TestFloatRegrSlopeAggregation.java

private void testNonTrivialAggregation(Float[] y, Float[] x) {
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < x.length; i++) {
        regression.addData(x[i], y[i]);/* ww  w.j a  v  a  2 s  .  c o m*/
    }
    float expected = (float) regression.getSlope();
    checkArgument(Float.isFinite(expected) && expected != 0.0f, "Expected result is trivial");
    testAggregation(expected, createFloatsBlock(y), createFloatsBlock(x));
}

From source file:com.facebook.presto.operator.aggregation.TestRealRegrSlopeAggregation.java

private void testNonTrivialAggregation(Float[] y, Float[] x) {
    SimpleRegression regression = new SimpleRegression();
    for (int i = 0; i < x.length; i++) {
        regression.addData(x[i], y[i]);/* www .j  av a 2  s  .c o  m*/
    }
    float expected = (float) regression.getSlope();
    checkArgument(Float.isFinite(expected) && expected != 0.0f, "Expected result is trivial");
    testAggregation(expected, createBlockOfReals(y), createBlockOfReals(x));
}

From source file:com.twitter.heron.healthmgr.common.ComponentMetricsHelper.java

public void computeBufferSizeTrend() {
    for (InstanceMetrics instanceMetrics : componentMetrics.getMetrics().values()) {
        Map<Instant, Double> bufferMetrics = instanceMetrics.getMetrics().get(METRIC_BUFFER_SIZE.text());
        if (bufferMetrics == null || bufferMetrics.size() < 3) {
            // missing of insufficient data for creating a trend line
            continue;
        }/*from   ww w.j a v  a2 s.  c om*/

        SimpleRegression simpleRegression = new SimpleRegression(true);
        for (Instant timestamp : bufferMetrics.keySet()) {
            simpleRegression.addData(timestamp.getEpochSecond(), bufferMetrics.get(timestamp));
        }

        double slope = simpleRegression.getSlope();
        instanceMetrics.addMetric(METRIC_WAIT_Q_GROWTH_RATE.text(), slope);

        if (maxBufferChangeRate < slope) {
            maxBufferChangeRate = slope;
        }
    }
}

From source file:net.anthonypoon.ngram.rollingregression.RollingRegressionReducer.java

@Override
protected void reduce(Text key, Iterable<Text> values, Context context)
        throws IOException, InterruptedException {
    TreeMap<String, Double> currElement = new TreeMap();
    boolean pastThreshold = false;
    for (Text val : values) {
        String[] strArray = val.toString().split("\t");
        if (Double.valueOf(strArray[1]) > threshold) {
            pastThreshold = true;/*w  w  w. ja va2s  . c o m*/
        }
        currElement.put(strArray[0], Math.log(Double.valueOf(strArray[1])));
    }
    if (pastThreshold) {
        for (Integer i = 0; i <= upbound - lowbound; i++) {
            if (!currElement.containsKey(String.valueOf(lowbound + i))) {
                if (i != 0) {
                    currElement.put(String.valueOf(lowbound + i),
                            currElement.get(String.valueOf(lowbound + i - 1)));
                } else {
                    currElement.put(String.valueOf(lowbound + i), 0.0);
                }
            }

        }
        TreeMap<String, Double> result = new TreeMap();
        for (Integer i = 0 + range; i <= upbound - lowbound - range; i++) {
            SimpleRegression regression = new SimpleRegression();
            for (Integer l = -range; l <= range; l++) {
                regression.addData(l.doubleValue(), currElement.get(String.valueOf(i + lowbound + l)));
            }
            if (!Double.isNaN(regression.getSlope())) {
                if (!positiveOnly || regression.getSlope() > 0) {
                    result.put(String.valueOf(lowbound + i), regression.getSlope());
                }
            }
        }
        for (Map.Entry<String, Double> pair : result.entrySet()) {
            context.write(key, new Text(pair.getKey() + "\t" + String.format("%.5f", pair.getValue())));
        }
    }
}

From source file:de.qaware.chronix.spark.api.java.ChronixRDD.java

/**
 * Action: Calculates the slope of a linear regression of every time series.
 *
 * Where: value = slope * timestamp/*from  w ww.  j  a va  2  s.c  o  m*/
 * .. or:     y = slope * x
 *
 * @return the slopes (simple linear regression) of each an every time series in the RDD
 */
public JavaDoubleRDD getSlopes() {
    return this.mapToDouble((DoubleFunction<MetricTimeSeries>) mts -> {
        SimpleRegression regression = new SimpleRegression();
        mts.points().forEach(p -> regression.addData(p.getTimestamp(), p.getValue()));
        return regression.getSlope();
    });
}

From source file:cn.edu.pku.cbi.mosaichunter.filter.ExomeParameterFilter.java

@Override
public void close() throws IOException {
    Collections.sort(sites, new Comparator<SimpleSite>() {
        public int compare(SimpleSite a, SimpleSite b) {
            if (a.depth > b.depth) {
                return 1;
            } else if (a.depth < b.depth) {
                return -1;
            } else {
                return 0;
            }// w ww  .  j  a v  a  2  s .co  m
        }
    });

    int n = sites.size();
    if (n == 0) {
        System.out.println("No data.");
        return;
    }
    int[] depth = new int[n];
    long totalDepth = 0;
    double[] af = new double[n];
    for (int i = 0; i < n; ++i) {
        depth[i] = sites.get(i).depth;
        af[i] = sites.get(i).altAf;
        totalDepth += depth[i];
    }

    int maxGroups = n / minGroupSize + 2;
    int m = 0;
    double afMeanAll = 0;

    int[] groupSize = new int[maxGroups];
    int[] groupPos = new int[maxGroups];
    double[] afMean = new double[maxGroups];
    double[] depthMid = new double[maxGroups];
    double[] depthMidR = new double[maxGroups];
    double[] afSd = new double[maxGroups];
    double[] afSd2 = new double[maxGroups];
    for (int i = 0; i <= n; ++i) {
        if (i == n || (i > 0 && depth[i] != depth[i - 1] && groupSize[m] >= minGroupSize)) {
            afMean[m] /= groupSize[m];
            for (int j = groupPos[m]; j < i; ++j) {
                afSd2[m] += (af[j] - afMean[m]) * (af[j] - afMean[m]);
            }
            if (groupSize[m] > 1) {
                afSd2[m] /= groupSize[m] - 1;
            }
            afSd[m] = Math.sqrt(afSd2[m]);
            depthMid[m] = depth[(i + groupPos[m]) / 2];
            depthMidR[m] = 1.0 / depthMid[m];
            m++;
            if (i == n) {
                break;
            }
            groupPos[m] = i;
        }
        afMeanAll += af[i];
        groupSize[m]++;
        afMean[m] += af[i];
    }
    afMeanAll /= n;

    SimpleRegression sr = new SimpleRegression();
    for (int i = 0; i < m; ++i) {
        sr.addData(depthMidR[i], afSd2[i]);
    }

    double k = sr.getSlope();
    double d = sr.getIntercept();

    double v = k / optimalDepth + d - afMeanAll * (1 - afMeanAll) / optimalDepth;
    double alpha = ((1 - afMeanAll) / v - 1 / afMeanAll) * afMeanAll * afMeanAll;
    double beta = alpha * (1 / afMeanAll - 1);

    long averageDepth = n == 0 ? 0 : totalDepth / n;
    System.out.println("average depth: " + averageDepth);
    System.out.println("alpha: " + Math.round(alpha));
    System.out.println("beta: " + Math.round(beta));

    if (rDataWriter != null) {
        rDataWriter.close();
    }
}