此代码应该在ArrayList
中找到随机整数的Standard deviation。但是,我的标准偏差代码没有显示正确的结果。它显示了预期的另一个数字。
我做错了什么?
import java.io.*;
import java.util.*;
public class Assignment4 {
public static void main(String[] args)
{
ArrayList<Integer> values = new ArrayList<Integer>();
int count = 0;
int total = 0;
Random r = new Random();
for (int i = 1; i <= 10; i++) {
values.add(r.nextInt(90)+ 1);
System.out.println(values);
}
System.out.println(mean(values));
System.out.println(sd(values));
}
public static double mean (ArrayList<Integer> table)
{
int total = 0;
for ( int i= 0;i < table.size(); i++)
{
int currentNum = table.get(i);
total+= currentNum;
}
return total/table.size();
}
public static double sd (ArrayList<Integer> table)
{
double mean= mean(table);
double temp =0;
for ( int i= 0; i <table.size(); i++)
{
temp= Math.pow(i-mean, 2);
}
return Math.sqrt(mean( table));
}
public static void selectionSort(ArrayList<Integer> table)
{
int count = table.size();
for(int pos = 0; pos < count - 1; pos++)
{
int locMin = pos;
for(int i = pos + 1; i < count; i++)
{
if(table.get(i) < table.get(locMin))
locMin = i;
}
int temp = table.get(pos);
table.set(pos, table.get(locMin) );
table.set(locMin, temp);
}
}
}
答案 0 :(得分:6)
您的标准偏差计算有错误:
这是查找标准差的算法:
Step 1: Find the mean.
Step 2: For each data point, find the square of its distance to the mean.
Step 3: Sum the values from Step 2.
Step 4: Divide by the number of data points.
Step 5: Take the square root.
因此,您的代码应如下所示:
public static double sd (ArrayList<Integer> table)
{
// Step 1:
double mean = mean(table);
double temp = 0;
for (int i = 0; i < table.size(); i++)
{
int val = table.get(i);
// Step 2:
double squrDiffToMean = Math.pow(val - mean, 2);
// Step 3:
temp += squrDiffToMean;
}
// Step 4:
double meanOfDiffs = (double) temp / (double) (table.size());
// Step 5:
return Math.sqrt(meanOfDiffs);
}
注意:您的平均计算精度有所下降。
你有:
return total/table.size();
应该是:
return (double) total / (double) table.size();
答案 1 :(得分:1)
以下是标准偏差计划示例:
// Beginning of class Deviation
public class Deviation
{
// Beginning of method main
public static void main(String[] args)
{
// Declare and create an array for 10 numbers
double[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// Print numbers
printArray(numbers);
// Display mean and deviation
System.out.println("The mean is " + findMean(numbers));
System.out.println("The standard deviation is " +
findDeviation(numbers));
} // End of main
/* Method for computing deviation of double values */
// Beginning of double findDeviation(double[])
public static double findDeviation(double[] nums)
{
double mean = findMean(nums);
double squareSum = 0;
for (int i = 0; i < nums.length; i++)
{
squareSum += Math.pow(nums[i] - mean, 2);
}
return Math.sqrt((squareSum) / (nums.length - 1));
} // End of double findDeviation(double[])
/* Method for computing deviation of int values */
// Beginning of double findDeviation(int[])
public static double findDeviation(int[] nums)
{
double mean = findMean(nums);
double squareSum = 0;
for (int i = 0; i < nums.length; i++)
{
squareSum += Math.pow(nums[i] - mean, 2);
}
return Math.sqrt((squareSum) / (nums.length - 1));
} // End of double findDeviation(int[])
/** Method for computing mean of an array of double values */
// Beginning of double findMean(double[])
public static double findMean(double[] nums)
{
double sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum / nums.length;
} // End of double getMean(double[])
/** Method for computing mean of an array of int values */
// Beginning of double findMean(int[])
public static double findMean(int[] nums)
{
double sum = 0;
for (int i = 0; i < nums.length; i++)
{
sum += nums[i];
}
return sum / nums.length;
} // End of double getMean(int[])
/* Method for printing array */
// Beginning of void printArray(double[])
public static void printArray(double[] nums)
{
for (int i = 0; i < nums.length; i++)
{
System.out.print(nums[i] + " ");
}
System.out.println();
} // End of void printArray(double[])
} // End of class Deviation