Last active
August 29, 2015 14:05
-
-
Save amihalik/f46cbfeb738a61eae094 to your computer and use it in GitHub Desktop.
NormalizedGini
This file contains hidden or 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.Arrays; | |
import java.util.Comparator; | |
public class NormalizedGini { | |
private static double gini(double[] a, double[] p, double[] w) { | |
int len = a.length; | |
if (p.length != len || w.length != len) { | |
throw new IllegalArgumentException("array length not equal"); | |
} | |
double[][] array = new double[a.length][3]; | |
for (int i = 0; i < len; i++) { | |
array[i][0] = a[i] * w[i]; | |
array[i][1] = p[i]; | |
array[i][2] = w[i]; | |
} | |
Arrays.sort(array, new Comparator<double[]>() { | |
public int compare(double[] o1, double[] o2) { | |
// Sort descending... | |
return Double.compare(o2[1], o1[1]); | |
} | |
}); | |
double wSum = 0.; | |
double awSum = 0.; | |
// create sums and modify array to have cumsums | |
for (double[] d : array) { | |
awSum += d[0]; | |
wSum += d[2]; | |
d[0] = awSum; | |
d[2] = wSum; | |
} | |
double giniSum = 0.; | |
for (int i = 0; i < len - 1; i++) { | |
giniSum += (array[i + 1][0] / awSum * array[i][2] / wSum) - (array[i][0] / awSum * array[i + 1][2] / wSum); | |
} | |
return giniSum; | |
} | |
public static double giniNormal(double[] a, double[] p, double[] w) { | |
return gini(a, p, w) / gini(a, a, w); | |
} | |
public static void main(String[] args) { | |
double[] var11 = new double[] {1, 2, 5, 4, 3}; | |
double[] pred = new double[] {0.1, 0.4, 0.3, 1.2, 0.0}; | |
double[] target = new double[] {0, 0, 1, 0, 1}; | |
System.out.println(giniNormal(target, pred, var11)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment