Java calculate confidence interval

跟風遠走 提交于 2019-12-11 09:54:28

问题


I'm looking for some method that takes or does not take parameters for calculate confidence interval.
I don't want the apache methods, just a simple method or som type of code that does this.


回答1:


here is you go this is the code calculate Confidence Interval

/**
 *
 * @author alaaabuzaghleh
 */
public class TestCI {
    public static void main(String[] args) { 
        int maximumNumber = 100000;
        int num = 0;
        double[] data = new double[maximumNumber];

        // first pass: read in data, compute sample mean
        double dataSum = 0.0;
        while (num<maximumNumber) {
            data[num] = num*10;
            dataSum  += data[num];
            num++;
        }
        double ave = dataSum / num;


        double variance1 = 0.0;
        for (int i = 0; i < num; i++) {
            variance1 += (data[i] - ave) * (data[i] - ave);
        }
        double variance = variance1 / (num - 1);
        double standardDaviation= Math.sqrt(variance);
        double lower = ave - 1.96 * standardDaviation;
        double higher = ave + 1.96 * standardDaviation;

        // print results
        System.out.println("average          = " + ave);
        System.out.println("sample variance  = " + variance);
        System.out.println("sample standard daviation    = " + standardDaviation);
        System.out.println("approximate confidence interval");
        System.out.println("[ " + lower + ", " + higher + " ]");
}
}



回答2:


My knowledge is restricted, it basically boils down to completing an online task against an expected set of answers (https://www.hackerrank.com/challenges/stat-warmup).

However, as far as I read up, there are mistakes in the given answer, and I'd like to correct these. My source is pretty much wikipedia https://en.wikipedia.org/wiki/Confidence_interval#Basic_Steps

/**
 *
 * @return int[]{lower, upper}, i.e. int array with Lower and Upper Boundary of the 95% Confidence Interval for the given numbers
 */
private static double[] calculateLowerUpperConfidenceBoundary95Percent(int[] givenNumbers) {

    // calculate the mean value (= average)
    double sum = 0.0;
    for (int num : givenNumbers) {
        sum += num;
    }
    double mean = sum / givenNumbers.length;

    // calculate standard deviation
    double squaredDifferenceSum = 0.0;
    for (int num : givenNumbers) {
        squaredDifferenceSum += (num - mean) * (num - mean);
    }
    double variance = squaredDifferenceSum / givenNumbers.length;
    double standardDeviation = Math.sqrt(variance);

    // value for 95% confidence interval, source: https://en.wikipedia.org/wiki/Confidence_interval#Basic_Steps
    double confidenceLevel = 1.96;
    double temp = confidenceLevel * standardDeviation / Math.sqrt(givenNumbers.length);
    return new double[]{mean - temp, mean + temp};
}


来源:https://stackoverflow.com/questions/30340551/java-calculate-confidence-interval

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!