In: Computer Science
write this java code:
One statistical operation that is sometimes performed on a set of data values is to remove values that are far from the average. Write a program that reads real values from a text file, one per line. Store the data values as Double objects in an instance of the class java.util.ArrayList. Then Use an iterator to compute the average and standard deviation of the values. Display these results. Use a second iterator to remove any value that is more than two standard deviations away from the average. Use a for-each loop to display the remaining values and compute the new average. Display the new average. If the data values are , their average μ is their sum divided by n, and their standard deviation is
solution:
given data:
The program is given below: that read data from read_data.txt line by line and store into arraylist arr1, then find average and standarad deviation, then remove any value that is more than 2 standard deviation away from the average (this is done by adding value into arraylist arr2 which is not more than 2 standard deviation away from the average), then display values of arr2 using for each loop , then calculate and print new average.
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) {
//create ArrayList object arr1
ArrayList<Double> arr1= new
ArrayList<Double>();
//create ArrayList object
arr2
ArrayList<Double> arr2= new
ArrayList<Double>();
try {
double
sum=0,avg=0,std_sum=0,standard_deviation=0;
int n,i;
//create file instance
File file1 = new File("read_data.txt");
BufferedReader br= new BufferedReader(new FileReader(file1));
String Line="";
//read line by line
while ((Line=br.readLine())!= null) {
//convert string to Double and add object into arrayList
arr1.add(Double.parseDouble(Line));
}
//calculate sum
for(double val:arr1) {
sum=sum+val;
}
n=arr1.size();
//calculate Average
avg=sum/n;
//calculate Standard deviation
//first perform sum of (val-avg)^2
for(double val:arr1) {
std_sum=std_sum+Math.pow((val-avg),2);
}
//devide sum of (val-avg)^2 by n and take square root
standard_deviation=Math.sqrt(std_sum/n);
//print Average and standard_deviation
System.out.println("Average: "+avg);
System.out.println("Standard deviation:
"+standard_deviation);
//remove value that is more than 2 standard_deviation away from the
Average
for(i=0;i<n;i++) {
//System.out.println(i+ " " +arr1.get(i));
if(arr1.get(i)>=(avg-(2*standard_deviation)) &&
arr1.get(i)<=(avg+(2*standard_deviation)))
{
arr2.add(arr1.get(i));
}
}
double new_sum=0,new_avg=0;
//display remaining value using foreach loop
System.out.println("data: ");
for(double val1:arr2)
{
System.out.println(val1);
new_sum=new_sum+val1;
}
//calculate new Average
new_avg=new_sum/arr2.size();
//display new_avg
System.out.println("New Average: "+new_avg);
} catch (IOException e) {
e.printStackTrace();
}
}
}
The screenshot of code is given below:
read_data.txt
12.50
13.23
11.50
200.10
30.20
15
14
19
50.10
Output:
please give me thumb up