Java Decimal Format createMeanRmsString(double values[])

Here you can find the source of createMeanRmsString(double values[])

Description

creates a predefined string, that contains useful statistical information.

License

Open Source License

Parameter

Parameter Description
values the values for which to calc the stats

Return

the string

Declaration

public final static String createMeanRmsString(double values[]) 

Method Source Code

//package com.java2s;
/*******************************************************************************
*
* This file is part of JMad./*from   ww  w .  j a v  a 2  s  . c om*/
* 
* Copyright (c) 2008-2011, CERN. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* 
******************************************************************************/

import java.text.NumberFormat;

public class Main {
    /** the formatter for the mean and rms-output */
    private final static NumberFormat formatter = NumberFormat.getInstance();

    /**
     * creates a predefined string, that contains useful statistical information.
     * 
     * @param values the values for which to calc the stats
     * @return the string
     */
    public final static String createMeanRmsString(double values[]) {
        double mean = calcMean(values);
        double rms = calcRms(values);
        double meanrms = calcStandardDeviation(values, mean);

        String outString = "rms=" + formatter.format(rms) + "; mean=" + formatter.format(mean) + ";  stddev="
                + formatter.format(meanrms);
        return outString;
    }

    /**
     * calculates the average of the given values
     * 
     * @param values the values to calc the average from
     * @return the average
     */
    public final static double calcMean(double[] values) {
        double sum = 0.0;
        for (int i = 0; i < values.length; i++) {
            sum += values[i];
        }
        if (values.length > 0) {
            return sum / values.length;
        } else {
            return 0;
        }
    }

    /**
     * calculates the rms of the given array of values
     * 
     * @param values the values to calc the rms from
     * @return the rms
     */
    public final static double calcRms(double values[]) {
        double sum = 0;
        for (int i = 0; i < values.length; i++) {
            sum += Math.pow(values[i], 2);
        }

        if (values.length > 0) {
            /* we divide by N */
            sum /= (values.length);
        }
        return Math.sqrt(sum);
    }

    /**
     * calculates an estimate for the standard deviation with respect to the given mean.
     * 
     * @param values the values for which to calc the diff-rms
     * @param meanValue the value to calc the diff to for each value.
     * @return the diff-rms
     */
    public final static double calcStandardDeviation(double values[], double meanValue) {
        double sum = 0;
        for (int i = 0; i < values.length; i++) {
            sum += Math.pow(values[i] - meanValue, 2);
        }

        if (values.length > 1) {
            /* we divide by (N-1)! */
            sum /= (values.length - 1);
        }
        return Math.sqrt(sum);
    }
}

Related

  1. addDouble(Integer i, Double d)
  2. adjust(Double lng, Double lat, Double distance, Double angle)
  3. adjustDoubleNumber(Double doubleNumber, int maxIntPart, int maxFloatPart)
  4. decimal2percent(double decimal, int pos)
  5. decimal2string(BigDecimal arg, Locale loc)
  6. decimalConversation(double amount)
  7. decimalFormat()