Example usage for org.apache.commons.math3.stat StatUtils normalize

List of usage examples for org.apache.commons.math3.stat StatUtils normalize

Introduction

In this page you can find the example usage for org.apache.commons.math3.stat StatUtils normalize.

Prototype

public static double[] normalize(final double[] sample) 

Source Link

Document

Normalize (standardize) the sample, so it is has a mean of 0 and a standard deviation of 1.

Usage

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 w  w.java  2 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:org.apache.solr.client.solrj.io.eval.NormalizeEvaluator.java

@Override
public Object doWork(Object value) {
    if (null == value) {
        return null;
    } else if (value instanceof List) {
        return Arrays
                .stream(StatUtils.normalize(((List<?>) value).stream()
                        .mapToDouble(innerValue -> ((Number) innerValue).doubleValue()).toArray()))
                .mapToObj(Double::new).collect(Collectors.toList());
    } else if (value instanceof Matrix) {
        Matrix matrix = (Matrix) value;//from   w  ww  . j  a  va2s  .  c  o  m
        double[][] data = matrix.getData();
        double[][] standardized = new double[data.length][];
        for (int i = 0; i < data.length; i++) {
            double[] row = data[i];
            standardized[i] = StatUtils.normalize(row);
        }
        return new Matrix(standardized);
    } else {
        return doWork(Arrays.asList((BigDecimal) value));
    }
}

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

@Test
public void test() throws IOException {

    int[] ints = new int[] { 3, 5, 6, 7, 8 };
    long[] longs = new long[] { 2L, 3L, 5L, 8L };
    double[] doubles = new double[] { 3.4, 4.5, 6.7 };
    int[] singleInt = new int[] { 6 };

    values.clear();//from w  ww.j a  va  2s  . c o  m
    values.put("ints", ints);
    values.put("longs", longs);
    values.put("doubles", doubles);
    values.put("singleInt", singleInt);
    Tuple tuple = new Tuple(values);

    assertSimilar(StatUtils.normalize(Arrays.stream(ints).mapToDouble(Double::new).toArray()),
            factory.constructEvaluator("norm(ints)").evaluate(tuple));
    assertSimilar(StatUtils.normalize(Arrays.stream(longs).mapToDouble(Double::new).toArray()),
            factory.constructEvaluator("norm(longs)").evaluate(tuple));
    assertSimilar(StatUtils.normalize(doubles), factory.constructEvaluator("norm(doubles)").evaluate(tuple));
    assertSimilar(StatUtils.normalize(Arrays.stream(singleInt).mapToDouble(Double::new).toArray()),
            factory.constructEvaluator("norm(singleInt)").evaluate(tuple));
}

From source file:org.apache.solr.client.solrj.io.stream.NormalizeEvaluator.java

public List<Number> evaluate(Tuple tuple) throws IOException {
    StreamEvaluator colEval1 = subEvaluators.get(0);

    List<Number> numbers1 = (List<Number>) colEval1.evaluate(tuple);
    double[] column1 = new double[numbers1.size()];

    for (int i = 0; i < numbers1.size(); i++) {
        column1[i] = numbers1.get(i).doubleValue();
    }/*from   www . j  a  va  2 s . com*/

    double[] normalized = StatUtils.normalize(column1);

    List<Number> normalizeList = new ArrayList(normalized.length);
    for (double d : normalized) {
        normalizeList.add(d);
    }

    return normalizeList;
}

From source file:tech.tablesaw.columns.numbers.NumberMapFunctions.java

/**
 * Returns a transformation of the data in this column such that the result has a mean of 0, and a
 * standard deviation of 1/*from  w  w  w. j  a v  a  2 s  . com*/
 */
default DoubleColumn normalize() {
    double[] result = StatUtils.normalize(asDoubleArray());
    return DoubleColumn.create(name() + " normalized", result);
}