List of usage examples for org.apache.commons.math3.distribution NormalDistribution NormalDistribution
public NormalDistribution()
From source file:simulacion.Operaciones.java
public int pReordenOpt(float k, float h, float cp) { NormalDistribution d = new NormalDistribution(); double mL = obtenerMedia(entrega); double mD = obtenerMedia(demanda); double mX = mL * mD; //System.out.println(obtenerVarianza(entrega)); double desvX = Math.sqrt((mL * obtenerVarianza(demanda)) + ((mD * mD) * obtenerVarianza(entrega))); double p = (h * Qopt(k, h)) / (cp * mD * 365); double valor = d.inverseCumulativeProbability(p); double z = 1 - valor; return (int) Math.round((z * desvX) + mX); }
From source file:stats.KendallsCorrelation.java
/** * Computes the Kendall's Tau rank correlation coefficient between the two * arrays.// w ww. j a va 2 s . co m * * @param xArray first data array * @param yArray second data array * @return Returns Kendall's Tau rank correlation coefficient for the two * arrays * @throws DimensionMismatchException if the arrays lengths do not match */ public Pair<Double, Double> correlation(final double[] xArray, final double[] yArray) throws DimensionMismatchException { if (xArray.length != yArray.length) { throw new DimensionMismatchException(xArray.length, yArray.length); } final int n = xArray.length; final double numPairs = ((double) n) * (n - 1) / 2; // to avoid overflow @SuppressWarnings("unchecked") Pair<Double, Double>[] pairs = new Pair[n]; for (int i = 0; i < n; i++) { pairs[i] = new Pair<Double, Double>(xArray[i], yArray[i]); } Arrays.sort(pairs, new Comparator<Pair<Double, Double>>() { @Override public int compare(Pair<Double, Double> pair1, Pair<Double, Double> pair2) { int compareFirst = pair1.getFirst().compareTo(pair2.getFirst()); return compareFirst != 0 ? compareFirst : pair1.getSecond().compareTo(pair2.getSecond()); } }); long tiedXPairs = 0; long tiedXYPairs = 0; long vt = 0; long consecutiveXTies = 1; long consecutiveXTiesSecondOrder = 1; long consecutiveXYTies = 1; Pair<Double, Double> prev = pairs[0]; for (int i = 1; i < n; i++) { final Pair<Double, Double> curr = pairs[i]; if (curr.getFirst().equals(prev.getFirst())) { consecutiveXTies++; if (curr.getSecond().equals(prev.getSecond())) { consecutiveXYTies++; } else { tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } } else { tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; consecutiveXTiesSecondOrder += consecutiveXTies * (consecutiveXTies - 1) * (consecutiveXTies - 2); vt += consecutiveXTies * (consecutiveXTies - 1) * (2 * consecutiveXTies + 5); consecutiveXTies = 1; tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; consecutiveXYTies = 1; } prev = curr; } tiedXPairs += consecutiveXTies * (consecutiveXTies - 1) / 2; vt += consecutiveXTies * (consecutiveXTies - 1) * (2 * consecutiveXTies + 5); consecutiveXTiesSecondOrder += consecutiveXTies * (consecutiveXTies - 1) * (consecutiveXTies - 2); tiedXYPairs += consecutiveXYTies * (consecutiveXYTies - 1) / 2; double swaps = 0; @SuppressWarnings("unchecked") Pair<Double, Double>[] pairsDestination = new Pair[n]; for (int segmentSize = 1; segmentSize < n; segmentSize <<= 1) { for (int offset = 0; offset < n; offset += 2 * segmentSize) { int i = offset; final int iEnd = FastMath.min(i + segmentSize, n); int j = iEnd; final int jEnd = FastMath.min(j + segmentSize, n); int copyLocation = offset; while (i < iEnd || j < jEnd) { if (i < iEnd) { if (j < jEnd) { if (pairs[i].getSecond().compareTo(pairs[j].getSecond()) <= 0) { pairsDestination[copyLocation] = pairs[i]; i++; } else { pairsDestination[copyLocation] = pairs[j]; j++; swaps += iEnd - i; } } else { pairsDestination[copyLocation] = pairs[i]; i++; } } else { pairsDestination[copyLocation] = pairs[j]; j++; } copyLocation++; } } final Pair<Double, Double>[] pairsTemp = pairs; pairs = pairsDestination; pairsDestination = pairsTemp; } long tiedYPairs = 0; long vu = 0; long consecutiveYTies = 1; long consecutiveYTiesSecondOrder = 0; prev = pairs[0]; for (int i = 1; i < n; i++) { final Pair<Double, Double> curr = pairs[i]; if (curr.getSecond().equals(prev.getSecond())) { consecutiveYTies++; } else { tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; consecutiveYTiesSecondOrder += consecutiveYTies * (consecutiveYTies - 1) * (consecutiveYTies - 2); vu += consecutiveYTies * (consecutiveYTies - 1) * (2 * consecutiveYTies + 5); consecutiveYTies = 1; } prev = curr; } tiedYPairs += consecutiveYTies * (consecutiveYTies - 1) / 2; consecutiveYTiesSecondOrder += consecutiveYTies * (consecutiveYTies - 1) * (consecutiveYTies - 2); vu += consecutiveYTies * (consecutiveYTies - 1) * (2 * consecutiveYTies + 5); double concordantMinusDiscordant = numPairs - tiedXPairs - tiedYPairs + tiedXYPairs - 2 * swaps; double denom = FastMath.sqrt((numPairs - tiedXPairs) * (numPairs - tiedYPairs)); double v0 = n * (n - 1) * (2 * n + 5); double v1 = (double) tiedXPairs * tiedYPairs / numPairs; double v2 = n < 3 ? 0 : consecutiveXTiesSecondOrder * consecutiveYTiesSecondOrder / (9.0 * n * (n - 1) * (n - 2)); double v = ((v0 - vt - vu) / 18) + v1 + v2; double zB = concordantMinusDiscordant / FastMath.sqrt(v); return new Pair(denom != 0 ? concordantMinusDiscordant / denom : 0., 2 * new NormalDistribution().cumulativeProbability(-1 * Math.abs(zB))); }
From source file:tv.dyndns.kishibe.qmaclone.server.Game.java
@VisibleForTesting void calculateRating(List<PlayerStatus> players) { NormalDistribution normalDistribution = new NormalDistribution(); // /*from w w w . jav a2 s . com*/ // http://topcoder.g.hatena.ne.jp/n4_t/20081222/ // http://apps.topcoder.com/wiki/display/tc/Algorithm+Competition+Rating+System Preconditions.checkState(2 <= players.size()); int numCoders = players.size(); double sumRating = 0.0; for (PlayerStatus player : players) { sumRating += player.getRating(); } double aveRating = sumRating / numCoders; // The competition factor is calculated: double sumVolatility2 = 0.0; double sumDiffRatingAveRating = 0.0; for (PlayerStatus player : players) { sumVolatility2 += player.getVolatility() * player.getVolatility(); double diffRatingAveRating = player.getRating() - aveRating; sumDiffRatingAveRating += diffRatingAveRating * diffRatingAveRating; } double cf = Math.sqrt(sumVolatility2 / numCoders + sumDiffRatingAveRating / (numCoders - 1)); // ?? Collections.sort(players, new Comparator<PlayerStatus>() { @Override public int compare(PlayerStatus o1, PlayerStatus o2) { int black1; int black2; try { int userCode1 = o1.getUserCode(); int rating1 = o1.getRating(); int userCode2 = o2.getUserCode(); int rating2 = o2.getRating(); black1 = (restrictedUserUtils.checkAndUpdateRestrictedUser(userCode1, "127.0.0.1", RestrictionType.MATCH) && rating1 > 1700) ? 1 : 0; black2 = (restrictedUserUtils.checkAndUpdateRestrictedUser(userCode2, "127.0.0.1", RestrictionType.MATCH) && rating2 > 1700) ? 1 : 0; } catch (DatabaseException e) { throw Throwables.propagate(e); } return black1 != black2 ? black1 - black2 : o2.getScore() - o1.getScore(); } }); for (int i = 0; i < players.size(); ++i) { if (0 < i && players.get(i - 1).getScore() == players.get(i).getScore()) { // ????? players.get(i).setHumanRank(players.get(i - 1).getHumanRank()); } else { players.get(i).setHumanRank(i + 1); } } // ?????? // http://kishibe.dyndns.tv/qmaclone/wiki/wiki.cgi?page=BugTrack-QMAClone%2F490 // for (PlayerStatus playerStatus : players) { // if (badUserManager.isLimitedUser(playerStatus.getUserCode(), null)) { // playerStatus.setHumanRank(players.size()); // } // } for (PlayerStatus my : players) { if (!my.isHuman()) { continue; } double myRating = my.getRating(); double myVolatility = my.getVolatility(); // Win Probability Estimation Algorithm: double eRank = 0.5; for (PlayerStatus player : players) { double hisVolatility = player.getVolatility(); double wp = 0.5; wp = 0.5 * (Erf .erf((player.getRating() - myRating) / Math.sqrt(2 * (hisVolatility * hisVolatility + myVolatility * myVolatility))) + 1.0); // BugTrack-QMAClone/603 - QMAClone wiki // http://kishibe.dyndns.tv/qmaclone/wiki/wiki.cgi?page=BugTrack%2DQMAClone%2F603 if (my != player && my.getUserCode() == player.getUserCode()) { wp = 0.0; } eRank += wp; } // The expected performance of the coder is calculated: double ePerf = -normalDistribution.inverseCumulativeProbability((eRank - 0.5) / numCoders); // The actual performance of each coder is calculated: double aPerf = -normalDistribution.inverseCumulativeProbability((my.getHumanRank() - 0.5) / numCoders); // The performed as rating of the coder is calculated: double perfAs = myRating + cf * (aPerf - ePerf); // The weight of the competition for the coder is calculated: double weight = 1.0 / (1 - (0.42 / (my.getPlayCount() + 1) + 0.18)) - 1.0; // A cap is calculated: double cap = 150 + 1500 / (my.getPlayCount() + 2); // The new rating of the coder is calculated: double newRating = (myRating + weight * perfAs) / (1.0 + weight); newRating = Math.min(newRating, myRating + cap); newRating = Math.max(newRating, myRating - cap); // The new volatility of the coder is calculated: double diffRating = newRating - myRating; double newVolatility = Math .sqrt(diffRating * diffRating / weight + myVolatility * myVolatility / (weight + 1)); my.setNewRating((int) Math.rint(newRating)); my.setNewVolatility((int) Math.rint(newVolatility)); } // ????? Collections.sort(players, new Comparator<PlayerStatus>() { @Override public int compare(PlayerStatus o1, PlayerStatus o2) { return o2.getScore() - o1.getScore(); } }); for (int i = 0; i < players.size(); ++i) { if (0 < i && players.get(i - 1).getScore() == players.get(i).getScore()) { // ????? players.get(i).setHumanRank(players.get(i - 1).getHumanRank()); } else { players.get(i).setHumanRank(i + 1); } } }
From source file:weka.attributeSelection.BiNormalSeperationEval.java
/** * Initializes an BNS attribute evaluator. * Discretizes all attributes that are numeric. * * @param data set of instances serving as training data * @throws Exception if the evaluator has not been * generated successfully//from w ww . jav a 2 s.co m */ public void buildEvaluator(Instances data) throws Exception { // can evaluator handle data? getCapabilities().testWithFail(data); int classIndex = data.classIndex(); int numInstances = data.numInstances(); int numClasses = data.attribute(classIndex).numValues(); double[] tp = new double[data.numAttributes()]; double[] fp = new double[data.numAttributes()]; double[] totalPos = new double[data.numAttributes()]; double[] totalNeg = new double[data.numAttributes()]; // Initialize values for (int i = 0; i < data.numAttributes(); i++) { tp[i] = 0; fp[i] = 0; totalPos[i] = 0; totalNeg[i] = 0; } Instance curInst; String classValue; double attValue; for (int i = 0; i < numInstances; i++) { curInst = data.get(i); classValue = curInst.stringValue(classIndex); for (int j = 0; j < data.numAttributes(); j++) { if (j != classIndex) { attValue = curInst.value(j); if (classValue.equals("1")) totalPos[j]++; if (classValue.equals("0")) totalNeg[j]++; if (classValue.equals("1") && attValue > 0) tp[j]++; if (classValue.equals("0") && attValue == 0) fp[j]++; } } } double[] tpr = new double[data.numAttributes()]; double[] fpr = new double[data.numAttributes()]; NormalDistribution nd = new NormalDistribution(); m_zScores = new double[data.numAttributes()]; for (int i = 0; i < data.numAttributes(); i++) { tpr[i] = tp[i] / totalPos[i]; fpr[i] = fp[i] / totalNeg[i]; if (tp[i] == 0) tpr[i] = 0.00005; if (fp[i] == 0) fpr[i] = 0.00005; m_zScores[i] = nd.inverseCumulativeProbability(tpr[i]) - nd.inverseCumulativeProbability(fpr[i]); } }