List of usage examples for java.lang Math signum
public static float signum(float f)
From source file:com.caseystella.analytics.outlier.batch.rpca.RPCA.java
private double[][] softThreshold(double[][] x, double penalty) { for (int i = 0; i < x.length; i++) { for (int j = 0; j < x[i].length; j++) { x[i][j] = Math.signum(x[i][j]) * Math.max(Math.abs(x[i][j]) - penalty, 0); }/*www.j a v a 2 s. co m*/ } return x; }
From source file:gedi.util.math.stat.testing.WilcoxonUnpaired.java
public double computePval(H1 h1, double[] x, double[] y) { double[] conc = ArrayUtils.concat(x, y); double[] r = naturalRanking.rank(conc); double[] rsort = r.clone(); Arrays.sort(rsort);//from www .j a v a 2 s .c om int nx = x.length; int ny = y.length; double w = xsum(r, nx) - nx * (nx + 1) / 2; double tiesSum = 0; int s = 0; for (int i = 1; i < rsort.length; i++) { if (Double.compare(rsort[s], rsort[i]) < 0) { double d = i - s; if (d > 1) tiesSum += d * d * d - d; s = i; } } int d = r.length - s; if (d > 1) tiesSum += d * d * d * -d; double z = w - nx * ny / 2; double SIGMA = Math.sqrt((nx * ny / 12) * ((nx + ny + 1) - tiesSum / ((nx + ny) * (nx + ny - 1)))); double CORRECTION = Math.signum(z) * 0.5; if (h1 == H1.GREATER_THAN) CORRECTION = 0.5; else if (h1 == H1.LESS_THAN) CORRECTION = 0.5; z = (z - CORRECTION) / SIGMA; double PVAL = norm.cumulativeProbability(z); if (h1 == H1.GREATER_THAN) PVAL = 1 - PVAL; else if (h1 == H1.NOT_EQUAL) PVAL = 2 * Math.min(PVAL, 1 - PVAL); return PVAL; }
From source file:org.nuxeo.ecm.platform.semanticentities.EntitySuggestion.java
@Override public int compareTo(EntitySuggestion o) { return -(int) Math.signum(score - o.score); }
From source file:com.opengamma.analytics.math.statistics.distribution.StudentTDistribution.java
/** * {@inheritDoc}/*from www . j a v a 2 s . co m*/ * The inverse cdf is given by: * $$ * \begin{align*} * F(P) &= \mathrm{sign}(p - \frac{1}{2})\sqrt{\frac{\nu}{x - 1}}\\ * x &= B(2 \min(p, 1-p)) * \end{align*} * $$ * where $B$ is the inverse incomplete Beta function ({@link com.opengamma.analytics.math.function.special.InverseIncompleteBetaFunction}). */ @Override public double getInverseCDF(final Double p) { Validate.notNull(p); Validate.isTrue(p >= 0 && p <= 1, "Probability must be >= 0 and <= 1"); final double x = _beta.evaluate(2 * Math.min(p, 1 - p)); return Math.signum(p - 0.5) * Math.sqrt(_degFreedom * (1. / x - 1)); }
From source file:de.interoberlin.lymbo.view.activities.SwipeRefreshBaseActivity.java
/** * Indicates that the main content has scrolled (for the purposes of showing/hiding * the action bar for the "action bar auto hide" effect). currentY and deltaY may be exact * (if the underlying view supports it) or may be approximate indications: * deltaY may be INT_MAX to mean "scrolled forward indeterminately" and INT_MIN to mean * "scrolled backward indeterminately". currentY may be 0 to mean "somewhere close to the * start of the list" and INT_MAX to mean "we don't know, but not at the start of the list" *//*from w w w . jav a2s. c om*/ private void onMainContentScrolled(int currentY, int deltaY) { if (deltaY > actionBarAutoHideSensivity) { deltaY = actionBarAutoHideSensivity; } else if (deltaY < -actionBarAutoHideSensivity) { deltaY = -actionBarAutoHideSensivity; } if (Math.signum(deltaY) * Math.signum(actionBarAutoHideSignal) < 0) { // deltaY is a motion opposite to the accumulated signal, so reset signal actionBarAutoHideSignal = deltaY; } else { // add to accumulated signal actionBarAutoHideSignal += deltaY; } boolean shouldShow = currentY < actionBarAutoHideMinY || (actionBarAutoHideSignal <= -actionBarAutoHideSensivity); autoShowOrHideActionBar(shouldShow); }
From source file:at.meikel.dmrl.webapp.rest.PlayerService.java
@RequestMapping(value = { "/bangolfArena/allplayers.txt" }, method = RequestMethod.GET) public String bangolfArenaAllPlayers() { StringBuilder result = new StringBuilder(); List<Player> list = null; if (server != null) { list = server.getRankingList().getAllPlayers(); }/*www. j a v a2 s. c o m*/ if (list == null) { list = new Vector<Player>(); } Collections.sort(list, new Comparator<Player>() { @Override public int compare(Player p1, Player p2) { if (p1 == null) { return p2 == null ? 0 : 1; } else { if (p2 == null) { return -1; } else { return (int) Math.signum(p1.getRanglistenwert() - p2.getRanglistenwert()); } } } }); for (Player player : list) { if (result.length() != 0) { // result.append(System.getProperty("line.separator")); result.append("BREAK"); } // D // H // JM // JW // SCHM // SCHW // SM I // SM II // SW I // SW II String kat = player.getKategorie(); if ("D".equalsIgnoreCase(player.getKategorie())) { kat = "ds"; // Damen } else if ("H".equalsIgnoreCase(player.getKategorie())) { kat = "hs"; // Herren } else if ("JM".equalsIgnoreCase(player.getKategorie())) { kat = "hj"; // Jugend mnnlich } else if ("JW".equalsIgnoreCase(player.getKategorie())) { kat = "dj"; // Jugend weiblich } else if ("SCHM".equalsIgnoreCase(player.getKategorie())) { kat = "pa"; // Schler mnnlich } else if ("SCHW".equalsIgnoreCase(player.getKategorie())) { kat = "fa"; // Schler weiblich } else if ("SM I".equalsIgnoreCase(player.getKategorie())) { kat = "ob"; // Senioren mnnlich I } else if ("SM II".equalsIgnoreCase(player.getKategorie())) { kat = "hv"; // Senioren mnnlich II } else if ("SW I".equalsIgnoreCase(player.getKategorie())) { kat = "og"; // Senioren weiblich I } else if ("SW II".equalsIgnoreCase(player.getKategorie())) { kat = "dv"; // Senioren weiblich II } result.append(kat); result.append("#"); result.append(player.getPassnummer()); result.append("#"); result.append(player.getVorname()); result.append("#"); result.append(player.getNachname()); result.append("#"); result.append(player.getVerein()); // if (result.length() > 500) { // break; // } } return result.toString(); }
From source file:edu.oregonstate.eecs.mcplan.domains.voyager.policies.EmphasizeProductionPolicy.java
@Override public VoyagerAction getAction() { final ArrayList<Planet> friendly_planets = Voyager.playerPlanets(s_, self_); Collections.sort(friendly_planets, new Comparator<Planet>() { @Override/*from w w w .j av a2 s. co m*/ public int compare(final Planet a, final Planet b) { final double d = Voyager.investment(a, a.nextProduced()) - Voyager.investment(b, b.nextProduced()); return (int) Math.signum(d); } }); for (final Planet p : friendly_planets) { if (p.nextProduced() != type_) { return new SetProductionAction(p, type_); } } return new NothingAction(); }
From source file:com.opengamma.analytics.financial.var.JohnsonSUDeltaGammaVaRCalculator.java
@SuppressWarnings("unchecked") @Override// w w w . j a va 2s. c o m public VaRCalculationResult evaluate(final NormalVaRParameters parameters, final T... data) { ArgumentChecker.notNull(parameters, "parameters"); ArgumentChecker.notNull(data, "data"); // TODO if skewness is positive then need to fit to -x and take from upper tail of distribution final double k = _kurtosisCalculator.evaluate(data); if (k < 0) { throw new IllegalArgumentException( "Johnson SU distribution cannot be used for data with negative excess kurtosis"); } final double mult = parameters.getTimeScaling(); final double z = parameters.getZ(); final double t = _skewCalculator.evaluate(data); final double mu = _meanCalculator.evaluate(data) * mult * mult; final double sigma = _stdCalculator.evaluate(data) * mult; if (t == 0 && k == 0) { return new VaRCalculationResult(z * sigma - mu, null); } final double wUpper = Math.sqrt(Math.sqrt(2 * (k + 2)) - 1); final double wLower = Math.max(getW0(t), getW1(k + 3)); final double w = ROOT_FINDER.getRoot(getFunction(t, k), wLower, wUpper); final double w2 = w * w; final double l = 4 + 2 * (w2 - (k + 6) / (w2 + 2 * w + 3)); if (l < 0) { throw new IllegalArgumentException("Tried to find the square root of a negative number"); } final double m = -2 + Math.sqrt(l); if (m == 0 || (m < 0 && w > -1) || (m > 0 && w < -1) || (w - 1 - m) < 0) { throw new IllegalArgumentException("Invalid parameters"); } final double sign = Math.signum(t); final double u = Math.sqrt(Math.log(w)); final double v = Math.sqrt((w + 1) * (w - 1 - m) / (2 * w * m)); final double omega = -sign * TrigonometricFunctionUtils.asinh(v); final double delta = 1. / u; final double gamma = omega / u; final double lambda = sigma / (w - 1) * Math.sqrt(2 * m / (w + 1)); final double ksi = mu - sign * sigma * Math.sqrt(w - 1 - m) / (w - 1); return new VaRCalculationResult(-lambda * Math.sinh((-z - gamma) / delta) - ksi, null); }
From source file:com.opengamma.analytics.financial.interestrate.swaption.method.SwaptionPhysicalFixedIborG2ppApproximationMethod.java
/** * Computes the present value of the Physical delivery swaption through approximation.. * @param swaption The swaption./*from ww w . j av a 2 s . c o m*/ * @param cfe The swaption cash flow equivalent. * @param g2Data The G2++ parameters and the curves. * @return The present value. */ public CurrencyAmount presentValue(final SwaptionPhysicalFixedIbor swaption, final AnnuityPaymentFixed cfe, final G2ppPiecewiseConstantDataBundle g2Data) { ArgumentChecker.notNull(swaption, "Swaption"); ArgumentChecker.notNull(cfe, "cash-flow equivalent"); ArgumentChecker.notNull(g2Data, "G2++ data"); final YieldAndDiscountCurve dsc = g2Data .getCurve(swaption.getUnderlyingSwap().getFixedLeg().getDiscountCurve()); final int nbCf = cfe.getNumberOfPayments(); final double[] cfa = new double[nbCf]; final double[] t = new double[nbCf]; for (int loopcf = 0; loopcf < nbCf; loopcf++) { cfa[loopcf] = -Math.signum(cfe.getNthPayment(0).getAmount()) * cfe.getNthPayment(loopcf).getAmount(); t[loopcf] = cfe.getNthPayment(loopcf).getPaymentTime(); } final double rhog2pp = g2Data.getG2ppParameter().getCorrelation(); final double[][] ht0 = MODEL_G2PP.volatilityMaturityPart(g2Data.getG2ppParameter(), t[0], t); final double[] dfswap = new double[nbCf]; final double[] p0 = new double[nbCf]; final double[] cP = new double[nbCf]; for (int loopcf = 0; loopcf < nbCf; loopcf++) { dfswap[loopcf] = dsc.getDiscountFactor(t[loopcf]); p0[loopcf] = dfswap[loopcf] / dfswap[0]; cP[loopcf] = cfa[loopcf] * p0[loopcf]; } final double k = -cfa[0]; double b0 = 0.0; for (int loopcf = 1; loopcf < nbCf; loopcf++) { b0 += cP[loopcf]; } final double[] alpha0 = new double[nbCf - 1]; final double[] beta0 = new double[2]; for (int loopcf = 0; loopcf < nbCf - 1; loopcf++) { alpha0[loopcf] = cfa[loopcf + 1] * p0[loopcf + 1] / b0; beta0[0] += alpha0[loopcf] * ht0[0][loopcf + 1]; beta0[1] += alpha0[loopcf] * ht0[1][loopcf + 1]; } final double[][] gamma = MODEL_G2PP.gamma(g2Data.getG2ppParameter(), 0, swaption.getTimeToExpiry()); final double[] tau = new double[nbCf]; for (int loopcf = 0; loopcf < nbCf; loopcf++) { tau[loopcf] = gamma[0][0] * ht0[0][loopcf] * ht0[0][loopcf] + gamma[1][1] * ht0[1][loopcf] * ht0[1][loopcf] + 2 * rhog2pp * gamma[0][1] * ht0[0][loopcf] * ht0[1][loopcf]; } double xbarnum = 0.0; double xbarde = 0.0; for (int loopcf = 0; loopcf < nbCf; loopcf++) { xbarnum += cP[loopcf] - cP[loopcf] * tau[loopcf] * tau[loopcf] / 2.0; xbarde += cP[loopcf] * tau[loopcf]; } final double xbar = xbarnum / xbarde; final double[] pK = new double[nbCf]; for (int loopcf = 0; loopcf < nbCf; loopcf++) { pK[loopcf] = p0[loopcf] * (1.0 - tau[loopcf] * xbar - tau[loopcf] * tau[loopcf] / 2.0); } final double[] alphaK = new double[nbCf - 1]; final double[] betaK = new double[2]; for (int loopcf = 0; loopcf < nbCf - 1; loopcf++) { alphaK[loopcf] = cfa[loopcf + 1] * pK[loopcf + 1] / k; betaK[0] += alphaK[loopcf] * ht0[0][loopcf + 1]; betaK[1] += alphaK[loopcf] * ht0[1][loopcf + 1]; } final double[] betaBar = new double[] { (beta0[0] + betaK[0]) / 2.0, (beta0[1] + betaK[1]) / 2.0 }; final double sigmaBar2 = gamma[0][0] * betaBar[0] * betaBar[0] + gamma[1][1] * betaBar[1] * betaBar[1] + 2 * rhog2pp * gamma[0][1] * betaBar[0] * betaBar[1]; final double sigmaBar = Math.sqrt(sigmaBar2); final EuropeanVanillaOption option = new EuropeanVanillaOption(k, 1, !swaption.isCall()); final BlackPriceFunction blackFunction = new BlackPriceFunction(); final BlackFunctionData dataBlack = new BlackFunctionData(b0, dfswap[0], sigmaBar); final Function1D<BlackFunctionData, Double> func = blackFunction.getPriceFunction(option); final double price = func.evaluate(dataBlack) * (swaption.isLong() ? 1.0 : -1.0); return CurrencyAmount.of(swaption.getCurrency(), price); }
From source file:com.opengamma.analytics.financial.interestrate.swaption.method.SwaptionPhysicalFixedIborLMMDDMethod.java
/** * Computes the present value of the Physical delivery swaption. * @param swaption The swaption.//from ww w . j av a 2 s .c om * @param lmmBundle The LMM parameters and the curves. * @return The present value. */ public CurrencyAmount presentValue(final SwaptionPhysicalFixedIbor swaption, final LiborMarketModelDisplacedDiffusionDataBundle lmmBundle) { // 1. Swaption CFE preparation AnnuityPaymentFixed cfe = CFEC.visit(swaption.getUnderlyingSwap(), lmmBundle); YieldAndDiscountCurve dsc = lmmBundle.getCurve(cfe.getDiscountCurve()); int nbCFInit = cfe.getNumberOfPayments(); double multFact = Math.signum(cfe.getNthPayment(0).getAmount()); boolean isCall = (cfe.getNthPayment(0).getAmount() < 0); double[] cftInit = new double[nbCFInit]; double[] cfaInit = new double[nbCFInit]; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { cftInit[loopcf] = cfe.getNthPayment(loopcf).getPaymentTime(); cfaInit[loopcf] = cfe.getNthPayment(loopcf).getAmount() * -multFact; } double timeToExpiry = swaption.getTimeToExpiry(); // 2. Model data int nbFactor = lmmBundle.getLmmParameter().getNbFactor(); double[][] volLMM = lmmBundle.getLmmParameter().getVolatility(); double[] timeLMM = lmmBundle.getLmmParameter().getIborTime(); // 3. Link cfe dates to lmm int[] indCFDate = new int[nbCFInit]; int indStart = nbCFInit - 1; int indEnd = 0; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { indCFDate[loopcf] = Arrays.binarySearch(timeLMM, cftInit[loopcf]); if (indCFDate[loopcf] < 0) { if (timeLMM[-indCFDate[loopcf] - 1] - cftInit[loopcf] < TIME_TOLERANCE) { indCFDate[loopcf] = -indCFDate[loopcf] - 1; } else { if (cftInit[loopcf] - timeLMM[-indCFDate[loopcf] - 2] < TIME_TOLERANCE) { indCFDate[loopcf] = -indCFDate[loopcf] - 2; } else { Validate.isTrue(true, "Instrument time incompatible with LMM"); //TODO really? } } } if (indCFDate[loopcf] < indStart) { indStart = indCFDate[loopcf]; } if (indCFDate[loopcf] > indEnd) { indEnd = indCFDate[loopcf]; } } int nbCF = indEnd - indStart + 1; double[] cfa = new double[nbCF]; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { cfa[indCFDate[loopcf] - indStart] = cfaInit[loopcf]; } double[] cft = new double[nbCF]; System.arraycopy(timeLMM, indStart, cft, 0, nbCF); double[] dfLMM = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { dfLMM[loopcf] = dsc.getDiscountFactor(cft[loopcf]); } double[][] gammaLMM = new double[nbCF - 1][nbFactor]; double[] deltaLMM = new double[nbCF - 1]; System.arraycopy(lmmBundle.getLmmParameter().getAccrualFactor(), indStart, deltaLMM, 0, nbCF - 1); double[] aLMM = new double[nbCF - 1]; System.arraycopy(lmmBundle.getLmmParameter().getDisplacement(), indStart, aLMM, 0, nbCF - 1); double[] liborLMM = new double[nbCF - 1]; double amr = lmmBundle.getLmmParameter().getMeanReversion(); for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { gammaLMM[loopcf] = volLMM[indStart + loopcf]; liborLMM[loopcf] = (dfLMM[loopcf] / dfLMM[loopcf + 1] - 1.0d) / deltaLMM[loopcf]; } // TODO: 4. cfe modification (for roller coasters) double[] cfaMod = new double[nbCF + 1]; double cfaMod0 = cfa[0]; cfaMod[0] = cfaMod0; // modified strike cfaMod[1] = 0.0; System.arraycopy(cfa, 1, cfaMod, 2, nbCF - 1); // 5. Pricing algorithm double[] p0 = new double[nbCF]; double[] dP = new double[nbCF]; double b0 = 0; for (int loopcf = 0; loopcf < nbCF; loopcf++) { p0[loopcf] = dfLMM[loopcf] / dfLMM[0]; dP[loopcf] = cfaMod[loopcf + 1] * p0[loopcf]; b0 += dP[loopcf]; } double bK = -cfaMod0; double bM = (b0 + bK) / 2.0d; double meanReversionImpact = Math.abs(amr) < 1.0E-6 ? timeToExpiry : (Math.exp(2.0d * amr * timeToExpiry) - 1.0d) / (2.0d * amr); // To handle 0 mean reversion. double[] rate0Ratio = new double[nbCF - 1]; double[][] mu0 = new double[nbCF - 1][nbFactor]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { rate0Ratio[loopcf] = (liborLMM[loopcf] + aLMM[loopcf]) / (liborLMM[loopcf] + 1 / deltaLMM[loopcf]); } for (int loopfact = 0; loopfact < nbFactor; loopfact++) { mu0[0][loopfact] = rate0Ratio[0] * gammaLMM[0][loopfact]; } for (int loopcf = 1; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { mu0[loopcf][loopfact] = mu0[loopcf - 1][loopfact] + rate0Ratio[loopcf] * gammaLMM[loopcf][loopfact]; } } double[] tau = new double[nbCF]; double[] tau2 = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { tau2[loopcf + 1] += mu0[loopcf][loopfact] * mu0[loopcf][loopfact]; } tau2[loopcf + 1] = tau2[loopcf + 1] * meanReversionImpact; tau[loopcf + 1] = Math.sqrt(tau2[loopcf + 1]); } double sumNum = -bM; double sumDen = 0; for (int loopcf = 0; loopcf < nbCF; loopcf++) { sumNum += dP[loopcf] - dP[loopcf] * tau2[loopcf] / 2.0; sumDen += dP[loopcf] * tau[loopcf]; } double xBar = sumNum / sumDen; double[] pM = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { pM[loopcf] = p0[loopcf] * (1 - xBar * tau[loopcf] - tau2[loopcf] / 2.0); } double[] liborM = new double[nbCF - 1]; double[] alphaM = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { liborM[loopcf] = (pM[loopcf] / pM[loopcf + 1] - 1.0d) / deltaLMM[loopcf]; } for (int loopcf = 0; loopcf < nbCF; loopcf++) { alphaM[loopcf] = cfaMod[loopcf + 1] * pM[loopcf] / bM; } double[] rateMRatio = new double[nbCF - 1]; double[][] muM = new double[nbCF - 1][nbFactor]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { rateMRatio[loopcf] = (liborM[loopcf] + aLMM[loopcf]) / (liborM[loopcf] + 1 / deltaLMM[loopcf]); } for (int loopfact = 0; loopfact < nbFactor; loopfact++) { muM[0][loopfact] = rateMRatio[0] * gammaLMM[0][loopfact]; } for (int loopcf = 1; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { muM[loopcf][loopfact] = muM[loopcf - 1][loopfact] + rateMRatio[loopcf] * gammaLMM[loopcf][loopfact]; } } double normSigmaM = 0; double[] sigmaM = new double[nbFactor]; for (int loopfact = 0; loopfact < nbFactor; loopfact++) { for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { sigmaM[loopfact] += alphaM[loopcf + 1] * muM[loopcf][loopfact]; } normSigmaM += sigmaM[loopfact] * sigmaM[loopfact]; } double impliedBlackVol = Math.sqrt(normSigmaM * meanReversionImpact); EuropeanVanillaOption option = new EuropeanVanillaOption(bK, 1, isCall); final BlackPriceFunction blackFunction = new BlackPriceFunction(); final BlackFunctionData dataBlack = new BlackFunctionData(b0, 1.0, impliedBlackVol); final Function1D<BlackFunctionData, Double> func = blackFunction.getPriceFunction(option); final double pv = dfLMM[0] * func.evaluate(dataBlack); return CurrencyAmount.of(swaption.getUnderlyingSwap().getFirstLeg().getCurrency(), pv * (swaption.isLong() ? 1.0 : -1.0)); }