2015-02-07 6 views
1

csv file.csv 파일의 단일 추출 된 열의 standard deviation (σ = √ [(Σ (x - 평균)) 2 ÷ n])은 약 45000 개의 인스턴스와 17 개의 속성 ';'로 단장했습니다. 표준 편차를 찾기 위해 Xi의 substact에 대한 while 루프의 모든 반복에서 MEAN 값이 필요합니다. 그래서 나는 MEAN이 표준 편차를 찾기 위해 반복 루프를 반복해야한다고 생각합니다. 그러나 이것을 수행하는 방법을 모르거나이를 수행 할 방법이 있습니다. 여기 붙어있다. 그때 나는 오래된 Xi를 새로운 Xi로 대체하기위한 코드를 사용했다. 새로운 csv 파일을 작성 (생성)하십시오.csv 파일의 표준 편차를 찾았습니다

import java.io.File; 
import java.io.FileNotFoundException; 
import java.util.Scanner; 
import java.util.logging.Level; 
import java.util.logging.Logger; 
import java.io.FileWriter; 
import java.io.*; 
import static java.lang.Math.sqrt; 

public class Main { 

    public static void main(String[] args) throws IOException { 

     String filename = "ly.csv"; 
     File file = new File(filename); 
     BufferedWriter writer = null; 

    try { 
      writer = new BufferedWriter(new FileWriter("bank-full_updated.csv")); 
     } 
    catch (IOException e) { 
     } 
    try { 

      double Tuple,avg; 
      double temp; 
      Tuple = 0; 
      double stddev=0; 

      Scanner inputStream = new Scanner(file); 
      inputStream.next(); 
      while (inputStream.hasNext()) { 
      String data1 = inputStream.next();     
      String[] values = data1.split(";"); 
      double Xi = Double.parseDouble(values[1]); 
      //now finding standard deviation 

      temp1 += (Xi-MEAN);     
      // temp2=(temp1*temp1); 
      // temp3=(temp2/count); 
      // standard deviation=Math.sqrt(temp3); 
      Xi=standard deviation * Xi 

      //now replace new Xi to original values1 
      values[1] = String.valueOf(Xi); 

      // iterate through the values and build a string out of them for write a new file 
      StringBuilder sb = new StringBuilder(); 
      String newData = sb.toString(); 

     for (int i = 0; i < values.length; i++) { 
       sb.append(values[i]); 
     if (i < values.length - 1) { 
       sb.append(";"); 
      } 
      } 
      // get the new string 
      System.out.println(sb.toString()); 

      writer.write(sb.toString()+"\n"); 
      } 

      writer.close(); 

      inputStream.close(); 
      } 

     catch (FileNotFoundException ex) { 
      Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex); 
     }  

    } 
} 
+0

이것은 비교적 간단한 수학 문제가 아니라 프로그래밍 문제입니다. 먼저 수학을 이해하십시오 (실제로 이해하는 방식으로). (당신이 참조하는 페이지의 수식이 틀렸다는 것이 확실합니다.) –

답변

2

단일 패스에서 표준 편차를 계산할 수 있습니다. Donald Knuth 교수는 Kahan 합산을 사용하는 알고리즘을 가지고 있습니다. 여기에 종이입니다 : http://researcher.ibm.com/files/us-ytian/stability.pdf

Here 다른 방법이지만 반올림 오류를 앓고 :

double std_dev2(double a[], int n) { 
    if(n == 0) 
     return 0.0; 
    double sum = 0; 
    double sq_sum = 0; 
    for(int i = 0; i < n; ++i) { 
     sum += a[i]; 
     sq_sum += a[i] * a[i]; 
    } 
    double mean = sum/n; 
    double variance = sq_sum/n - mean * mean; 
    return sqrt(variance); 
}