我将如何计算浮点数文件的标准偏差? [英] How would I calculate the standard deviation from a file of floating point numbers?
问题描述
我正在尝试编写一个 Java 程序来在读取一个充满浮点数的文本文件后计算最大值、最小值、平均值和标准差.如您所见,我已经计算了最大值、最小值、平均值,但对于标准偏差,我很困惑.任何想法我应该如何实施?
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.
这是我的代码:
/*
* 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
推荐答案
正如 Przemysław MoskalI 所建议的那样,我相信这样的事情正是您所追求的!列表在 Java 中非常有用.这很快就完成了,所以请确保您对最终的计算结果感到满意!编程快乐!
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!
已编辑 - 由于 Bathsheba 的建议,使用了计算公式,它允许您根本不使用列表(这样可以提高内存效率).
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).
请参阅此网站以了解计算的简单方法:https://www.mathsisfun.com/data/standard-deviation-formulas.html
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
这篇关于我将如何计算浮点数文件的标准偏差?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!