Java calculate confidence interval

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.

Upvotes: 2

Views: 8259

Answers (2)

RobertG
RobertG

Reputation: 1706

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};
}

Upvotes: 4

Alaa Abuzaghleh
Alaa Abuzaghleh

Reputation: 1009

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 + " ]");
}
}

Upvotes: -2

Related Questions