Research breakthrough possible @S-Logix pro@slogix.in

Office Address

  • 2nd Floor, #7a, High School Road, Secretariat Colony Ambattur, Chennai-600053 (Landmark: SRM School) Tamil Nadu, India
  • pro@slogix.in
  • +91- 81240 01111

Social List

How to calculate Skewness and Kurtosis from a file in java?

Description

To Find the value of skewness and kurtosis from a file in java.

Process
Skewness:

   It is a measure of the asymmetry of the probability distribution of a real-valued random variable about its mean.

  The skewness value can be positive or negative, or undefined.

Kurtosis:

  Kurtosis is the measure of thickness or heaviness of the given distribution.

  Its actually represents the height of the distribution.

Sample Code

package Stat;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;

public class mean {
public static void main(String[] args) throws FileNotFoundException, IOException {

BufferedReader in = new BufferedReader(new FileReader(new File("file.csv")));
PrintWriter out = new PrintWriter(new File("file2.csv"));
File file = new File("file2.csv");

int skipIndex = 1;
String line1;
while ((line1 = in.readLine()) != null) {
String[] parts = line1.split(" ");
System.out.println(Arrays.toString(parts));
StringBuilder outLine = new StringBuilder();
for (int i = 0; i < parts.length; i++) {
if (i + 1 != skipIndex) {
outLine.append(" " + parts[i]);
}

}

out.println(outLine.toString().trim());

}

in.close();
out.close();

String line;

Scanner sc = null;

try {
sc = new Scanner(file);
sc.next();
} catch (FileNotFoundException e) {
System.out.println("File not found");
e.printStackTrace();
return;
}

ArrayList list = new ArrayList();

while (sc.hasNextFloat())
list.add(sc.nextFloat());

int size = list.size();

if (size == 0) {
System.out.println("Empty list");
return;
}

Collections.sort(list);

for (int i = 0; i < size; i++) {
System.out.print(list.get(i) + " ");
}

System.out.println();

// mean value
float sum = 0;
for (float x : list) {
sum += x;
}

float mean = sum / size;
#variance
double variance;
double m = mean;
double temp = 0;
for(double a :list)
temp += (a-m)*(a-m);
variance= temp/(size-1);
double sd=Math.sqrt(variance);
# Skewness
double skew1=mean-mode;
double skew=skew1/sd;
#Kurtosiss
double kurt1=skew1/size;
double kurt=(kurt1/sd1)-3;

System.out.println("Skewness=" + skew);
System.out.println("Kurtosis=" + kurt);

}
}

Screenshots

Calculate Skewness and Kurtosis from a file in java