How would I calculate the standard deviation from a file of floating point numbers?

余生颓废 提交于 2021-02-10 18:49:07

问题


I'm trying to write a Java program to calculate the maximum, minimum, mean and standard deviation after reading a text file full of floating point numbers. As you can see, I've calculated the max, min, mean, but for the standard deviation, I'm confused. Any ideas how I should implement this?

Also, I'm fairly new to programming, so sorry if things aren't structured correctly.

Here's my code:

/*
 * Create a Java program to read a file of floating point numbers and compute
 * the following statistics from the data file:
 * 
 * 1. Maximum
 * 2. Minimum
 * 3. Arithmetic Average (Mean)
 * 4. Standard Deviation
 * 
 * Do not assume anything about the large numbers in the file. They could be
 * positive or negative, and their magnitude could be extremely large or
 * extremely small.
 */

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
    // .....

    // 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                      );

} // end method main

} // end class DataFile

回答1:


As suggested by Przemysław MoskalI, I believe something like this is what you are after! Lists are incredibly useful in Java. This was done very quickly, so make sure you are happy with the final calcs! Happy Programming!

Edited - used computational formula thanks to a suggestion from Bathsheba, which allows you to not make use of a list at all (which is more memory efficient).

See this website for an easy way to understand the calculation: https://www.mathsisfun.com/data/standard-deviation-formulas.html

// initialize variables
maximum = file.nextDouble();
minimum = maximum;
sum = 0;
count = 1;
double computationalSum = 0;
double squareSum = 0;

while(file.hasNextDouble()) {
    number = file.nextDouble();

    squareSum += Math.pow(number, 2);
    computationalSum += number;

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



回答2:


It the while loop you could put each of number into the ArrayList that is initialized before this loop. When you reach the end of a file you could change the values in your ArrayList - you need to provide a loop to store in thata list the difference between the values already in list and the mean and you square that difference. After you loop entire ArrayList, you sum the values from ArrayList, divide it with the count and then you just need the element of that result.



来源:https://stackoverflow.com/questions/47688365/how-would-i-calculate-the-standard-deviation-from-a-file-of-floating-point-numbe

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