List of usage examples for org.apache.commons.math.stat.ranking NaturalRanking NaturalRanking
public NaturalRanking()
From source file:dr.evomodel.continuous.ContinuousDiffusionStatistic.java
private static double getSpearmanRho(double[] data1, double[] data2) { double data1Ranks[] = new NaturalRanking().rank(data1); double data2Ranks[] = new NaturalRanking().rank(data2); int counter = 0; double d_i = 0; while (counter < data1Ranks.length) { d_i += Math.pow(data1Ranks[counter] - data2Ranks[counter], 2); counter++;//from www .j a v a 2 s . c om } return (1 - (6 * d_i) / (data1Ranks.length * (Math.pow(data1Ranks.length, 2) - 1))); }
From source file:org.rascalmpl.library.analysis.statistics.Inferences.java
public IValue gini(IList dataValues) { if (dataValues.length() < 2) throw RuntimeExceptionFactory.illegalArgument(dataValues, null, null, "At least 2 observations required"); double sum = 0; double g = 0; double N = 0; double xvalues[] = new double[dataValues.length()]; for (int i = 0; i < dataValues.length(); i++) { ITuple T = (ITuple) dataValues.get(i); xvalues[i] = ((INumber) T.get(0)).toReal().doubleValue(); }//from w ww . j a v a2 s . c om // Create a natural ranking: largest first. double rank[] = new NaturalRanking().rank(xvalues); for (int i = 0; i < rank.length; i++) { rank[i] = rank.length - rank[i] + 1; // invert the ranking: smallest come now first. System.err.println("rank[" + i + "] = " + rank[i]); } for (int i = 0; i < dataValues.length(); i++) { ITuple T = (ITuple) dataValues.get(i); double Y = ((INumber) T.get(1)).toReal().doubleValue(); if (Y < 0) throw RuntimeExceptionFactory.illegalArgument(T, null, null, "Frequency should be positive"); g += xvalues[i] * rank[i] * Y; N += Y; sum += xvalues[i] * Y; } double avg = sum / N; return values.real((N + 1) / (N - 1) - (2 * g) / (N * (N - 1) * avg)); }