集册 Java实例教程 计算(偏差

计算(偏差

欢马劈雪     最近更新时间:2020-01-02 10:19:05

436
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
计算数组的(偏差校正后的样本)标准偏差。


//package com.nowjava;
/** 来 自 n o w j a   v  a . c o m - 时  代  Java**/

public class Main {

    public static void main(String[] argv) throws Exception {

        double[] v = new double[] { 34.45, 35.45, 36.67, 37.78, 37.0000,

                37.1234, 67.2344, 68.34534, 69.87700 };

        System.out.println(standardDeviation(v));

    }


    /**

     * Computes the (bias-corrected sample) standard deviation of an array.

     */

    public static double standardDeviation(double[] v) {

        return (Math.sqrt(variance(v)));

    }


    /**

     * Returns the (bias-corrected sample) standard deviation of an array.

     */

    public static double standardDeviation(int[] v) {

        return (Math.sqrt(variance(v)));

    }//from 时 代 J a v a - nowjava.com


    /**

     * Computes the (bias-corrected sample) variance.

     */

    public static double variance(double[] v) {

        if (v.length > 1) {

            final double m = mean(v);

            double ans = 0.0;

            for (int i = 0; i < v.length; i++)

                ans += (v[i] - m) * (v[i] - m);

            return ans / (v.length - 1);

        } else

            throw new IllegalArgumentException(

                    "Array length must be of 2 or greater.");

    }


    /**

     * Computes the (bias-corrected sample) variance.

     */

    public static double variance(int[] v) {

        if (v.length > 1) {

            final double m = mean(v);

            double ans = 0.0;

            for (int i = 0; i < v.length; i++)

                ans += (v[i] - m) * (v[i] - m);

            return ans / (v.length - 1);

        } else

            throw new IllegalArgumentException(

                    "Array length must be of 2 or greater.");

    }


    /**

     * Computes the mean.

     */

    public static double mean(double[] v) {

        if (v.length == 0)

            throw new IllegalArgumentException(

                    "Nothing to compute! The array must have at least one element.");

        return (mass(v) / (double) v.length);

    }


    /**

     * Computes the mean.

     */

    public static double mean(int[] v) {

        if (v.length == 0)

            throw new IllegalArgumentException(

                    "Nothing to compute! The array must have at least one element.");

        return (mass(v) / (double) v.length);

    }


    /**

     * Returns the sum of the elements of the array.

     */

    public static 
展开阅读全文