I am to find the standard deviation. When calculating the standard deviation, my outputs are way higher than the given sample. How do I go calculating the standard deviation of each column?
Here is my code as reference:
double[] average = new double[arrScores.length];
for (int i = 0; i < arrScores[i].length; i ) {
double sum = 0;
for (int j = 0; j < arrScores.length; j ) {
sum = sum arrScores[j][i];
}
average[i] = sum / arrScores.length;
System.out.printf("Test#%d: %.2f with Std Deviation: \n", i, average[i]);
average[i]=0;
The array given is as follows:
100.00 90.00 100.00 80.00 70.00
50.00 60.00 70.00 80.00 100.00
60.00 70.00 100.00 80.00 90.00
69.50 70.50 80.50 30.50 0.00
78.30 69.50 48.00 90.00 100.00
88.50 95.00 100.00 99.00 0.00
Output to be made is:
Test#0 Average: 74.38 with Std Deviation: 16.81
Test#1 Average: 75.83 with Std Deviation: 12.39
Test#2 Average: 83.08 with Std Deviation: 19.44
Test#3 Average: 76.58 with Std Deviation: 21.76
Test#4 Average: 60.00 with Std Deviation: 43.59
EDIT:
double Stats = 0;
double Class = 0;
double average = 0;
double[] arr=new double[arrScores.length];
for(int i = 0; i < arrScores[i].length; i ){
for(int j = 0; j < 6; j ){
average = average array[j][i];
arr[j]=array[j][i];
}
average =average / arrScores[i].length;
for(double num; num < arrScores.length; num ){
Stats = Stats Math.pow(num-average,2);
}
Class=Math.sqrt(Stats / arrScores[i].length);
This is what I had before changing to to what I had above.
CodePudding user response:
In your sample data, each row represents the test scores for a single student and each column represents a single test. In order to calculate the average score for each test, you first need to get the total for each column. According to the sample data in your question, the total for the first column is:
100 50 60 69.5 78.3 88.5 = 446.3
Then you divide that by the number of students who took the test – which is 6 since there are six rows in your sample data.
After you calculate the averages, you can then calculate the standard deviations for each test. I used the following formula:
For each test score (in the same column), you subtract the average for that test, you square the resulting difference and add them all together. Once you have those totals, you divide each total by the number of students who took the test, i.e. 6 and take the square root of the result of the division. This gives you the standard deviation for each test.
public class Scores {
public static void main(String[] args) {
double[][] scores = {{100.00, 90.00, 100.00, 80.00, 70.00},
{ 50.00, 60.00, 70.00, 80.00, 100.00},
{ 60.00, 70.00, 100.00, 80.00, 90.00},
{ 69.50, 70.50, 80.50, 30.50, 0.00},
{ 78.30, 69.50, 48.00, 90.00, 100.00},
{ 88.50, 95.00, 100.00, 99.00, 0.00}};
double[] tests = new double[scores[0].length];
for (int row = 0; row < scores.length; row ) {
for (int column = 0; column < scores[row].length; column ) {
tests[column] = scores[row][column];
}
}
double[] averages = new double[tests.length];
for (int i = 0; i < averages.length; i ) {
averages[i] = tests[i] / scores.length;
}
double[] stdDevs = new double[averages.length];
for (int row = 0; row < scores.length; row ) {
for (int column = 0; column < stdDevs.length; column ) {
stdDevs[column] = Math.pow((scores[row][column] - averages[column]), 2);
}
}
for (int i = 0; i < stdDevs.length; i ) {
stdDevs[i] = Math.sqrt(stdDevs[i] / scores.length);
System.out.printf("Test %d average: %.2f with Std Deviation: %.2f%n",
i,
averages[i],
stdDevs[i]);
}
}
}
Running the above code produces the following output:
Test 0 average: 74.38 with Std Deviation: 16.81
Test 1 average: 75.83 with Std Deviation: 12.39
Test 2 average: 83.08 with Std Deviation: 19.44
Test 3 average: 76.58 with Std Deviation: 21.76
Test 4 average: 60.00 with Std Deviation: 43.59