Created
May 21, 2017 11:51
-
-
Save francisnnumbi/1f808e214490f66af190f46f558bc3c0 to your computer and use it in GitHub Desktop.
Simple implementation of Descriptive Statistics, calculating : sum, count, average, mode, median, variance, standard deviation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.*; | |
/** | |
* For Descriptive Statistics | |
* @AUTHOR: Francis Nduba Numbi | |
* @YEAR: 2017 | |
* | |
* REMEMBER : AFTER ALL DATA HAVE BEEN ADDED, call update(). | |
* else some calculations will be wrong. | |
*/ | |
public class Data { | |
private ArrayList<Datum> list; | |
public Data() { | |
list = new ArrayList<Datum>(); | |
} | |
public int count() { | |
int i = 0; | |
for (Datum d : list) { | |
i += d.f; | |
} | |
return i; | |
} | |
public void add(int f, double x) { | |
list.add(new Datum(f, x)); | |
sort(); | |
} | |
public void remove(int f, double x) { | |
Iterator<Datum> iter = list.iterator(); | |
while (iter.hasNext()) { | |
Datum d = iter.next(); | |
if (d.f == f && d.x == x) { | |
iter.remove(); | |
} | |
} | |
sort(); | |
} | |
public double sum() { | |
double sum = 0; | |
for (Datum d : list) { | |
sum += d.y; | |
} | |
return sum; | |
} | |
public double average() { | |
return sum() / count(); | |
} | |
public double mode() { | |
sort(); | |
float ii = 0f; | |
for (Datum d : list) { | |
if (d.f > ii) { | |
ii = d.f; | |
} | |
} | |
double pl = 0; | |
int ct = 0; | |
for (Datum d : list) { | |
if (d.f == ii) { | |
pl += d.x; | |
ct++; | |
} | |
} | |
pl /= ct; | |
return pl; | |
} | |
public double median() { | |
sort(); | |
float ii = 0f; | |
for (Datum d : list) { | |
ii += d.f; | |
if (ii > (float)(count() / 2)) { | |
return d.x; | |
} | |
} | |
return 0; | |
} | |
public ArrayList<Datum> list() { | |
return list; | |
} | |
/** */ | |
public ArrayList<Double> pondList() { | |
ArrayList<Double> pl = new ArrayList<Double>(); | |
for (Datum d : list) { | |
pl.add(d.y); | |
} | |
return pl; | |
} | |
public ArrayList<Double> deviationList() { | |
ArrayList<Double> pl = new ArrayList<Double>(); | |
for (Datum d : list) { | |
pl.add(d.X); | |
} | |
return pl; | |
} | |
public ArrayList<Double> deviationListSqr() { | |
ArrayList<Double> pl = new ArrayList<Double>(); | |
for (Datum d : list) { | |
pl.add(d.XX); | |
} | |
return pl; | |
} | |
/** variance */ | |
public double var() { | |
double sum = 0; | |
for (Datum d : list) { | |
sum += d.XX; | |
} | |
return sum / count(); | |
} | |
/** standard deviation */ | |
public double stDev() { | |
return Math.sqrt(var()); | |
} | |
private void sort() { | |
Collections.sort(list); | |
} | |
@Override | |
public String toString() { | |
return list.toString(); | |
} | |
/** after all data have been added, update */ | |
public void update() { | |
for (Datum d : list) { | |
d.X = d.x - average(); | |
d.XX = d.X * d.X; | |
} | |
} | |
/** inner class Datum that hold and wrap data */ | |
class Datum implements Comparable { | |
public int f; // frequency | |
public double x; // value | |
public double y; // ponderation (f*x); | |
public double X; // deviation from the mean | |
public double XX; // deviation squared | |
public Datum(int f, double x) { | |
this.f = f; | |
this.x = x; | |
y = x * f; | |
} | |
// compareTo() and equals() methods are necessary for sorting | |
// the 2 methods use the same comparison process for integrity | |
@Override | |
public int compareTo(Object p1) { | |
// TODO: Implement this method | |
return Double.compare(x, ((Datum)p1).x); | |
} | |
@Override | |
public boolean equals(Object o) { | |
// TODO: Implement this method | |
return Double.compare(x, ((Datum)o).x) == 1; | |
} | |
@Override | |
public String toString() { | |
// TODO: Implement this method | |
return "| " + f + " | " + x + " | " + y + " | " + X + " | " + XX + " |\n"; | |
} | |
} // end of inner class Datum | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment