Normalize double array. - Java java.lang

Java examples for java.lang:double Array

Description

Normalize double array.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] series = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        System.out.println(java.util.Arrays.toString(normalize(series)));
    }//from   www.  j a v  a 2 s . c  om

    /**
     * Normalize timeseries.
     * 
     * @param series The input timeseries.
     * @return the normalized time-series.
     */
    public static double[] normalize(double[] series) {
        double[] res = new double[series.length];
        double mean = mean(series);
        double var = var(series);
        for (int i = 0; i < res.length; i++) {
            res[i] = (series[i] - mean) / var;
        }
        return res;
    }

    /**
     * Compute the mean of the timeseries.
     * 
     * @param series The input timeseries.
     * @return the mean of values.
     */
    public static double mean(double[] series) {
        double res = 0D;
        for (int i = 0; i < series.length; i++) {
            res += series[i];
        }
        return res / ((Integer) series.length).doubleValue();
    }

    /**
     * Compute the variance of the timeseries.
     * 
     * @param series The input timeseries.
     * @return the variance of values.
     */
    public static double var(double[] series) {
        double res = 0D;
        double mean = mean(series);
        for (int i = 0; i < series.length; i++) {
            res += (series[i] - mean) * (series[i] - mean);
        }
        return res / ((Integer) (series.length - 1)).doubleValue();
    }
}

Related Tutorials