In: Computer Science
I want to calculate the max, min, mean, standard deviation and the percentage of data within one standard deviation from a file already existing in my directory using JAVA. As you can see it below, I have found max, min, mean and stand deviation. How can I get the percentages of data within one standard deviation in the following code? import java.io.*; import java.util.Scanner; public class DataFile { public static void main(String[] args) { // declare variables double number, maximum, minimum, sum, mean, standardDeviation; int count; Scanner file = null; /* -------------------------------------------------------------------------- */ try { file = new Scanner(new File("RawData.txt")); } catch(FileNotFoundException e) { System.out.print("Error; The program was terminated!"); System.exit(1); } /* -------------------------------------------------------------------------- */ // initialize variables maximum = file.nextDouble(); minimum = maximum; sum = 0; count = 1; while(file.hasNextDouble()) { number = file.nextDouble(); if(number > maximum) maximum = number; else if(number < minimum) minimum = number; sum += number; count += 1; } // end while loop file.close(); /* -------------------------------------------------------------------------- */ // mean calculation mean = sum / count; // standard deviation calculation
double stdDevSum = 0; double stdDevMean = 0; double stdDev = 0; double sumOfSquares = squareSum - ((Math.pow(computationalSum, 2)/(count-1))); double sSquared = sumOfSquares/(count-1); double otherStdDev = Math.sqrt(sSquared); // display statistics System.out.println("Maximum ------------> " + maximum ); System.out.println("Minimum ------------> " + minimum ); System.out.println("Sum ----------------> " + sum ); System.out.println("Count --------------> " + count ); System.out.println("Mean ---------------> " + mean ); System.out.println("StdDev -------------> " + otherStdDev);
} // end method main } // end class DataFile
I can tell you how to calculate percentage of data within one standard deviation
For example, you have find the value of the mean as 15.24 and value of standard deviation is 3.25. That means that the range of one standard deviation is from 15.24 + 3.25 to 15.24 -3.25 or 18.49 to 11.99
By rounding the values, I can say that values which falls between the range 12 to 18 to the total number of values in the file
percentage of data within one standard deviation = ( values between 12 to 18 / total number values in the file )
I have modified your code to make it work
import java.io.*;
import java.util.Scanner;
public class DataFile {
public static void main(String[] args) {
// declare variables
double number, maximum, minimum, sum, mean, standardDeviation, percentageOfStandardDeviation;
int count;
Scanner file = null;
try {
file = new Scanner(new File("RawData.txt"));
} catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}
maximum = file.nextDouble();
minimum = maximum;
sum = 0;
count = 1;
while(file.hasNextDouble()) {
number = file.nextDouble();
if(number > maximum)
maximum = number;
else if(number < minimum)
minimum = number;
sum += number;
count += 1;
}
file.close();
// mean calculation
mean = sum / count;
// Standard Deviation
standardDeviation = 0;
try {
file = new Scanner(new File("RawData.txt"));
} catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}
while(file.hasNextDouble()) {
number = file.nextDouble();
standardDeviation += Math.pow(number-mean, 2);
}
file.close();
standardDeviation = Math.sqrt(standardDeviation/count);
//calculating percentage of data within one standard deviation
try {
file = new Scanner(new File("RawData.txt"));
} catch(FileNotFoundException e) {
System.out.print("Error; The program was terminated!");
System.exit(1);
}
double standardDeviationMin = Math.round(mean - standardDeviation);
double standardDeviationMax = Math.round(mean + standardDeviation);
int anotherCount = 0;
while(file.hasNextDouble()) {
number = file.nextDouble();
if(number >= standardDeviationMin && number <= standardDeviationMax) {
anotherCount++;
}
}
file.close();
System.out.println(anotherCount);
percentageOfStandardDeviation = ((double)(anotherCount*100/count));
// display statistics
System.out.println("Maximum --------------> " + maximum);
System.out.println("Minimum --------------> " + minimum);
System.out.println("Sum ------------------> " + sum);
System.out.println("Count ----------------> " + count);
System.out.println("Mean -----------------> " + mean);
System.out.println("StdDev ---------------> " + standardDeviation);
System.out.println("percentageOfStdDev----> " + percentageOfStandardDeviation);
}
}
The output of the above code is:
I hope this helps you out. Thank you