List of usage examples for java.lang Math signum
public static float signum(float f)
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("signum") @ScalarFunction("sign") @SqlType(StandardTypes.TINYINT)/*w w w.jav a 2 s.c o m*/ public static long signTinyint(@SqlType(StandardTypes.TINYINT) long num) { return (long) Math.signum(num); }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("signum") @ScalarFunction/*from ww w . j a v a 2 s .c om*/ @SqlType(StandardTypes.DOUBLE) public static double sign(@SqlType(StandardTypes.DOUBLE) double num) { return Math.signum(num); }
From source file:com.facebook.presto.operator.scalar.MathFunctions.java
@Description("signum") @ScalarFunction("sign") @SqlType(StandardTypes.REAL)// w w w. j a v a 2 s .c o m public static long signFloat(@SqlType(StandardTypes.REAL) long num) { return floatToRawIntBits((Math.signum(intBitsToFloat((int) num)))); }
From source file:com.opengamma.analytics.financial.model.volatility.BlackScholesFormulaRepository.java
/** * Dual charm, minus of second order derivative of option value, once strike and once time to maturity * @param spot The spot value of the underlying * @param strike The strike // www .ja v a2 s .c om * @param timeToExpiry The time to expiry * @param lognormalVol The log-normal volatility * @param interestRate The interest rate * @param costOfCarry The cost of carry * @param isCall True for call * @return The dual charm */ public static double dualCharm(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall) { ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot); ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike); ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol); ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN"); ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN"); final double rootT = Math.sqrt(timeToExpiry); double sigmaRootT = lognormalVol * rootT; if (Double.isNaN(sigmaRootT)) { sigmaRootT = 1.; //ref value is returned } double discount = Math.exp(-interestRate * timeToExpiry); if (discount < SMALL) { return 0.; } if (Double.isNaN(discount)) { discount = 1.; //ref value is returned } final int sign = isCall ? 1 : -1; double d1 = 0.; double d2 = 0.; if (Math.abs(spot - strike) < SMALL || (spot > LARGE && strike > LARGE) || sigmaRootT > LARGE) { final double coefD1 = Double.isNaN(Math.abs(costOfCarry) / lognormalVol) ? Math.signum(costOfCarry) + 0.5 * lognormalVol : (costOfCarry / lognormalVol + 0.5 * lognormalVol); final double tmpD1 = Math.abs(coefD1) < SMALL ? 0. : coefD1 * rootT; d1 = Double.isNaN(tmpD1) ? Math.signum(coefD1) : tmpD1; final double coefD2 = Double.isNaN(Math.abs(costOfCarry) / lognormalVol) ? Math.signum(costOfCarry) - 0.5 * lognormalVol : (costOfCarry / lognormalVol - 0.5 * lognormalVol); final double tmpD2 = Math.abs(coefD2) < SMALL ? 0. : coefD2 * rootT; d2 = Double.isNaN(tmpD2) ? Math.signum(coefD2) : tmpD2; } else { if (sigmaRootT < SMALL) { final double scnd = (Math.abs(costOfCarry) > LARGE && rootT < SMALL) ? Math.signum(costOfCarry) : costOfCarry * rootT; final double tmp = (Math.log(spot / strike) / rootT + scnd) / lognormalVol; d1 = Double.isNaN(tmp) ? 0. : tmp; d2 = d1; } else { final double tmp = costOfCarry * rootT / lognormalVol; final double sig = (costOfCarry >= 0.) ? 1. : -1.; final double scnd = Double.isNaN(tmp) ? ((lognormalVol < LARGE && lognormalVol > SMALL) ? sig / lognormalVol : sig * rootT) : tmp; final double d1Tmp = Math.log(spot / strike) / sigmaRootT + scnd + 0.5 * sigmaRootT; final double d2Tmp = Math.log(spot / strike) / sigmaRootT + scnd - 0.5 * sigmaRootT; d1 = Double.isNaN(d1Tmp) ? 0. : d1Tmp; d2 = Double.isNaN(d2Tmp) ? 0. : d2Tmp; } } // if (Double.isNaN(d1) || Double.isNaN(d2)) { // throw new IllegalArgumentException("NaN found"); // } double coefPdf = 0.; if (timeToExpiry < SMALL) { coefPdf = (Math.abs(spot - strike) < SMALL || (spot > LARGE && strike > LARGE)) ? 1. / sigmaRootT : Math.log(spot / strike) / sigmaRootT / timeToExpiry; } else { double cocMod = costOfCarry / sigmaRootT; if (Double.isNaN(cocMod)) { cocMod = 1.; } double tmp = d1 / timeToExpiry; tmp = Double.isNaN(tmp) ? (d1 >= 0. ? 1. : -1.) : tmp; coefPdf = cocMod - 0.5 * tmp; } final double normPdf = NORMAL.getPDF(d2); final double normCdf = NORMAL.getCDF(sign * d2); final double first = normPdf < SMALL ? 0. : (Double.isNaN(coefPdf) ? 0. : normPdf * coefPdf); final double second = normCdf < SMALL ? 0. : interestRate * normCdf; final double res = discount * (first - sign * second); return Double.isNaN(res) ? 0. : res; }
From source file:com.github.shareme.gwsmaterialuikit.library.advancerv.draggable.RecyclerViewDragDropManager.java
private void handleScrollOnDraggingInternal(RecyclerView rv, boolean horizontal) { final int edge = (horizontal) ? rv.getWidth() : rv.getHeight(); if (edge == 0) { return;//w ww.jav a 2s . co m } final float invEdge = (1.0f / edge); final float normalizedTouchPos = (horizontal ? mLastTouchX : mLastTouchY) * invEdge; final float threshold = SCROLL_THRESHOLD; final float invThreshold = (1.0f / threshold); final float centerOffset = normalizedTouchPos - 0.5f; final float absCenterOffset = Math.abs(centerOffset); final float acceleration = Math.max(0.0f, threshold - (0.5f - absCenterOffset)) * invThreshold; final int mask = mScrollDirMask; final DraggingItemDecorator decorator = mDraggingItemDecorator; int scrollAmount = (int) Math.signum(centerOffset) * (int) (SCROLL_AMOUNT_COEFF * mDragEdgeScrollSpeed * mDisplayDensity * acceleration + 0.5f); int actualScrolledAmount = 0; final ItemDraggableRange range = mDraggableRange; final int firstVisibleChild = CustomRecyclerViewUtils.findFirstCompletelyVisibleItemPosition(mRecyclerView); final int lastVisibleChild = CustomRecyclerViewUtils.findLastCompletelyVisibleItemPosition(mRecyclerView); boolean reachedToFirstHardLimit = false; boolean reachedToFirstSoftLimit = false; boolean reachedToLastHardLimit = false; boolean reachedToLastSoftLimit = false; if (firstVisibleChild != RecyclerView.NO_POSITION) { if (firstVisibleChild <= range.getStart()) { reachedToFirstSoftLimit = true; } if (firstVisibleChild <= (range.getStart() - 1)) { reachedToFirstHardLimit = true; } } if (lastVisibleChild != RecyclerView.NO_POSITION) { if (lastVisibleChild >= range.getEnd()) { reachedToLastSoftLimit = true; } if (lastVisibleChild >= (range.getEnd() + 1)) { reachedToLastHardLimit = true; } } // apply mask if (scrollAmount > 0) { if ((mask & (horizontal ? SCROLL_DIR_RIGHT : SCROLL_DIR_DOWN)) == 0) { scrollAmount = 0; } } else if (scrollAmount < 0) { if ((mask & (horizontal ? SCROLL_DIR_LEFT : SCROLL_DIR_UP)) == 0) { scrollAmount = 0; } } // scroll if ((!reachedToFirstHardLimit && (scrollAmount < 0)) || (!reachedToLastHardLimit && (scrollAmount > 0))) { safeEndAnimationsIfRequired(rv); actualScrolledAmount = (horizontal) ? scrollByXAndGetScrolledAmount(scrollAmount) : scrollByYAndGetScrolledAmount(scrollAmount); if (scrollAmount < 0) { decorator.setIsScrolling(!reachedToFirstSoftLimit); } else { decorator.setIsScrolling(!reachedToLastSoftLimit); } decorator.refresh(true); if (mSwapTargetItemOperator != null) { mSwapTargetItemOperator.update(decorator.getDraggingItemTranslationX(), decorator.getDraggingItemTranslationY()); } } else { decorator.setIsScrolling(false); } final boolean actualIsScrolling = (actualScrolledAmount != 0); if (mEdgeEffectDecorator != null) { final float edgeEffectStrength = 0.005f; final int draggingItemTopLeft = (horizontal) ? decorator.getTranslatedItemPositionLeft() : decorator.getTranslatedItemPositionTop(); final int draggingItemBottomRight = (horizontal) ? decorator.getTranslatedItemPositionRight() : decorator.getTranslatedItemPositionBottom(); final int draggingItemCenter = (draggingItemTopLeft + draggingItemBottomRight) / 2; final int nearEdgePosition; if (firstVisibleChild == 0 && lastVisibleChild == 0) { // has only 1 item nearEdgePosition = (scrollAmount < 0) ? draggingItemTopLeft : draggingItemBottomRight; } else { nearEdgePosition = (draggingItemCenter < (edge / 2)) ? draggingItemTopLeft : draggingItemBottomRight; } final float nearEdgeOffset = (nearEdgePosition * invEdge) - 0.5f; final float absNearEdgeOffset = Math.abs(nearEdgeOffset); float edgeEffectPullDistance = 0; if ((absNearEdgeOffset > 0.4f) && (scrollAmount != 0) && !actualIsScrolling) { if (nearEdgeOffset < 0) { if (horizontal ? decorator.isReachedToLeftLimit() : decorator.isReachedToTopLimit()) { edgeEffectPullDistance = -mDisplayDensity * edgeEffectStrength; } } else { if (horizontal ? decorator.isReachedToRightLimit() : decorator.isReachedToBottomLimit()) { edgeEffectPullDistance = mDisplayDensity * edgeEffectStrength; } } } updateEdgeEffect(edgeEffectPullDistance); } ViewCompat.postOnAnimation(mRecyclerView, mCheckItemSwappingRunnable); if (actualScrolledAmount != 0) { if (horizontal) { mDragScrollDistanceX += actualScrolledAmount; } else { mDragScrollDistanceY += actualScrolledAmount; } onItemMoveDistanceUpdated(); } }
From source file:org.openspaces.grid.gsm.machines.BinPackingSolver.java
/** * Balance memory between allocated machines. * Do not allocate memory on a new machine! *//*w w w. j ava2 s. c o m*/ private void rebalanceMemoryCapacity() { long allocatedMemory = getMemoryInMB(allocatedCapacityForPu); int numberOfContainers = (int) (allocatedMemory / containerMemoryCapacityInMB); int numberOfMachines = allocatedCapacityForPu.getAgentUids().size(); int minNumberOfContainersPerMachine = (int) Math.floor(1.0 * numberOfContainers / numberOfMachines); int maxNumberOfContainersPerMachine = (int) Math.ceil(1.0 * numberOfContainers / numberOfMachines); final long minMemoryPerMachine = minNumberOfContainersPerMachine * containerMemoryCapacityInMB; final long maxMemoryPerMachine = maxNumberOfContainersPerMachine * containerMemoryCapacityInMB; List<String> sortedAgentUids = new ArrayList<String>(allocatedCapacityForPu.getAgentUids()); Collections.sort(sortedAgentUids, new Comparator<String>() { public int compare(String agent1, String agent2) { long weight1 = calcMemoryExcessAboveAverage(maxMemoryPerMachine, agent1) - calcMemoryShortageBelowAverage(minMemoryPerMachine, agent1); long weight2 = calcMemoryExcessAboveAverage(maxMemoryPerMachine, agent2) - calcMemoryShortageBelowAverage(minMemoryPerMachine, agent2); return (int) Math.signum(weight1 - weight2); } }); int targetIndex = 0; int sourceIndex = sortedAgentUids.size() - 1; while (targetIndex < sourceIndex) { String sourceAgentUid = sortedAgentUids.get(sourceIndex); String targetAgentUid = sortedAgentUids.get(targetIndex); long sourceMemoryExcess = calcMemoryExcessAboveAverage(maxMemoryPerMachine, sourceAgentUid); long targetMemoryShortage = calcMemoryShortageBelowAverage(minMemoryPerMachine, targetAgentUid); long memoryToRelocate = 0; if (sourceMemoryExcess == 0 && targetMemoryShortage == 0) { sourceIndex--; targetIndex++; } else if (sourceMemoryExcess > 0 && sourceMemoryExcess > targetMemoryShortage) { long freeMemoryOnTarget = calcMemoryShortageBelowAverage(maxMemoryPerMachine, targetAgentUid); if (freeMemoryOnTarget <= sourceMemoryExcess) { memoryToRelocate = freeMemoryOnTarget; // target is maximum, move on targetIndex++; } else { memoryToRelocate = sourceMemoryExcess; sourceIndex--; } } else if (targetMemoryShortage > 0) { long movableMemoryOnSource = calcMemoryExcessAboveAverage(minMemoryPerMachine, sourceAgentUid); if (movableMemoryOnSource <= targetMemoryShortage) { memoryToRelocate = movableMemoryOnSource; // source is minimum, move on sourceIndex--; } else { memoryToRelocate = targetMemoryShortage; targetIndex++; } } memoryToRelocate -= memoryToRelocate % containerMemoryCapacityInMB; if (memoryToRelocate > 0) { deallocateCapacityOnMachine(sourceAgentUid, new MemoryCapacityRequirement(memoryToRelocate)); allocateCapacityOnMachine(targetAgentUid, new MemoryCapacityRequirement(memoryToRelocate)); } } }
From source file:com.opengamma.analytics.financial.interestrate.swaption.provider.SwaptionPhysicalFixedIborLMMDDMethod.java
/** * Computes the present value curve sensitivity of the Physical delivery swaption. * @param swaption The swaption./*from w w w.jav a2s . c om*/ * @param lmmData The LMM and multi-curves provider. * @return The present value. */ public MultipleCurrencyMulticurveSensitivity presentValueCurveSensitivity( final SwaptionPhysicalFixedIbor swaption, //CSIGNORE final LiborMarketModelDisplacedDiffusionProviderInterface lmmData) { ArgumentChecker.notNull(swaption, "Swaption"); ArgumentChecker.notNull(lmmData, "LMM provider"); final Currency ccy = swaption.getCurrency(); final MulticurveProviderInterface multicurves = lmmData.getMulticurveProvider(); final LiborMarketModelDisplacedDiffusionParameters parameters = lmmData.getLMMParameters(); // 1. Swaption CFE preparation final AnnuityPaymentFixed cfe = swaption.getUnderlyingSwap().accept(CFEC, multicurves); final int nbCFInit = cfe.getNumberOfPayments(); final double multFact = Math.signum(cfe.getNthPayment(0).getAmount()); final boolean isCall = (cfe.getNthPayment(0).getAmount() < 0); final double[] cftInit = new double[nbCFInit]; final 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; } final double timeToExpiry = swaption.getTimeToExpiry(); // 2. Model data final int nbFactor = parameters.getNbFactor(); final double[][] volLMM = parameters.getVolatility(); final double[] timeLMM = parameters.getIborTime(); // 3. Link cfe dates to lmm final 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"); } } } if (indCFDate[loopcf] < indStart) { indStart = indCFDate[loopcf]; } if (indCFDate[loopcf] > indEnd) { indEnd = indCFDate[loopcf]; } } final int nbCF = indEnd - indStart + 1; final double[] cfa = new double[nbCF]; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { cfa[indCFDate[loopcf] - indStart] = cfaInit[loopcf]; } final double[] cft = new double[nbCF]; System.arraycopy(timeLMM, indStart, cft, 0, nbCF); final double[] dfLMM = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { dfLMM[loopcf] = multicurves.getDiscountFactor(ccy, cft[loopcf]); } final double[][] gammaLMM = new double[nbCF - 1][nbFactor]; final double[] deltaLMM = new double[nbCF - 1]; System.arraycopy(parameters.getAccrualFactor(), indStart, deltaLMM, 0, nbCF - 1); final double[] aLMM = new double[nbCF - 1]; System.arraycopy(parameters.getDisplacement(), indStart, aLMM, 0, nbCF - 1); final double[] liborLMM = new double[nbCF - 1]; final double amr = parameters.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]; } final double[] cfaMod = new double[nbCF + 1]; final double cfaMod0 = cfa[0]; cfaMod[0] = cfaMod0; // modified strike cfaMod[1] = 0.0; // TODO: 4. cfe modification (for roller coasters) System.arraycopy(cfa, 1, cfaMod, 2, nbCF - 1); // 5. Pricing algorithm final double[] p0 = new double[nbCF]; final 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]; } final double bK = -cfaMod[0]; final double bM = (b0 + bK) / 2.0d; final double[] rate0Ratio = new double[nbCF - 1]; final 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]; } } final double meanReversionImpact = (Math.exp(2.0d * amr * timeToExpiry) - 1.0d) / (2.0d * amr); final double[] tau = new double[nbCF]; final 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]; } final double xBar = sumNum / sumDen; final double[] pM = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { pM[loopcf] = p0[loopcf] * (1 - xBar * tau[loopcf] - tau2[loopcf] / 2.0); } final double[] liborM = new double[nbCF - 1]; final 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; } final double[] rateMRatio = new double[nbCF - 1]; final 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; final 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]; } final double impliedBlackVol = Math.sqrt(normSigmaM * meanReversionImpact); final EuropeanVanillaOption option = new EuropeanVanillaOption(bK, 1, isCall); final BlackPriceFunction blackFunction = new BlackPriceFunction(); final BlackFunctionData dataBlack = new BlackFunctionData(b0, 1.0, impliedBlackVol); final double[] blkAdjoint = blackFunction.getPriceAdjoint(option, dataBlack); // Backward sweep final double pvBar = 1.0; final double impliedBlackVolBar = dfLMM[0] * blkAdjoint[2] * (swaption.isLong() ? 1.0 : -1.0) * pvBar; final double normSigmaMBar = meanReversionImpact / (2.0 * impliedBlackVol) * impliedBlackVolBar; final double[] sigmaMBar = new double[nbFactor]; for (int loopfact = 0; loopfact < nbFactor; loopfact++) { sigmaMBar[loopfact] = 2 * sigmaM[loopfact] * normSigmaMBar; } final double[][] muMBar = new double[nbCF - 1][nbFactor]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { muMBar[loopcf][loopfact] = alphaM[loopcf + 1] * sigmaMBar[loopfact]; } } for (int loopcf = nbCF - 3; loopcf >= 0; loopcf--) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { muMBar[loopcf][loopfact] += muMBar[loopcf + 1][loopfact]; } } final double[] rateMRatioBar = new double[nbCF - 1]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { rateMRatioBar[loopcf] += gammaLMM[loopcf][loopfact] * muMBar[loopcf][loopfact]; } } final double[] alphaMBar = new double[nbCF]; for (int loopfact = 0; loopfact < nbFactor; loopfact++) { for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { alphaMBar[loopcf + 1] += muM[loopcf][loopfact] * sigmaMBar[loopfact]; } } final double[] liborMBar = new double[nbCF - 1]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { liborMBar[loopcf] = ((liborM[loopcf] + 1 / deltaLMM[loopcf]) - (liborM[loopcf] + aLMM[loopcf])) / ((liborM[loopcf] + 1 / deltaLMM[loopcf]) * (liborM[loopcf] + 1 / deltaLMM[loopcf])) * rateMRatioBar[loopcf]; } final double[] pMBar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { pMBar[loopcf] += 1.0 / pM[loopcf + 1] / deltaLMM[loopcf] * liborMBar[loopcf]; pMBar[loopcf + 1] += -pM[loopcf] / (pM[loopcf + 1] * pM[loopcf + 1]) / deltaLMM[loopcf] * liborMBar[loopcf]; } for (int loopcf = 0; loopcf < nbCF; loopcf++) { pMBar[loopcf] += cfaMod[loopcf + 1] / bM * alphaMBar[loopcf]; } double xBarBar = 0.0; for (int loopcf = 0; loopcf < nbCF; loopcf++) { xBarBar += -p0[loopcf] * tau[loopcf] * pMBar[loopcf]; } final double sumNumBar = 1.0 / sumDen * xBarBar; final double sumDenBar = -sumNum / (sumDen * sumDen) * xBarBar; final double[] tauBar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { tauBar[loopcf] = -p0[loopcf] * xBar * pMBar[loopcf]; } for (int loopcf = 0; loopcf < nbCF; loopcf++) { tauBar[loopcf] += dP[loopcf] * sumDenBar; } final double[] tau2Bar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { tau2Bar[loopcf] = -p0[loopcf] / 2.0 * pMBar[loopcf]; } for (int loopcf = 0; loopcf < nbCF; loopcf++) { tau2Bar[loopcf] += -dP[loopcf] / 2.0 * sumNumBar; } for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { tau2Bar[loopcf + 1] += 1.0 / tau[loopcf + 1] / 2.0 * tauBar[loopcf + 1]; } final double[][] mu0Bar = new double[nbCF - 1][nbFactor]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { mu0Bar[loopcf][loopfact] = 2.0 * mu0[loopcf][loopfact] * meanReversionImpact * tau2Bar[loopcf + 1]; } } for (int loopcf = nbCF - 3; loopcf >= 0; loopcf--) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { mu0Bar[loopcf][loopfact] += mu0Bar[loopcf + 1][loopfact]; } } final double[] rate0RatioBar = new double[nbCF - 1]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { for (int loopfact = 0; loopfact < nbFactor; loopfact++) { rate0RatioBar[loopcf] += gammaLMM[loopcf][loopfact] * mu0Bar[loopcf][loopfact]; } } double bMBar = -sumNumBar; for (int loopcf = 0; loopcf < nbCF; loopcf++) { bMBar += -cfaMod[loopcf + 1] * pM[loopcf] / (bM * bM) * alphaMBar[loopcf]; } double bKBar = bMBar / 2.0; bKBar += dfLMM[0] * blkAdjoint[3] * (swaption.isLong() ? 1.0 : -1.0) * pvBar; double b0Bar = bMBar / 2.0; b0Bar += dfLMM[0] * blkAdjoint[1] * (swaption.isLong() ? 1.0 : -1.0) * pvBar; final double[] dPBar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { dPBar[loopcf] = b0Bar + tau[loopcf] * sumDenBar + (1.0 - tau2[loopcf] / 2.0) * sumNumBar; } final double[] p0Bar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { p0Bar[loopcf] = cfaMod[loopcf + 1] * dPBar[loopcf] + (1 - xBar * tau[loopcf] - tau2[loopcf] / 2.0) * pMBar[loopcf]; } final double[] cfaModBar = new double[nbCF + 1]; for (int loopcf = 0; loopcf < nbCF; loopcf++) { cfaModBar[loopcf + 1] = p0[loopcf] * dPBar[loopcf] + pM[loopcf] / bM * alphaMBar[loopcf]; } cfaModBar[0] += -bKBar; final double[] liborLMMBar = new double[nbCF - 1]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { liborLMMBar[loopcf] = (1.0 / (liborLMM[loopcf] + 1 / deltaLMM[loopcf]) - (liborLMM[loopcf] + aLMM[loopcf]) / ((liborLMM[loopcf] + 1 / deltaLMM[loopcf]) * (liborLMM[loopcf] + 1 / deltaLMM[loopcf]))) * rate0RatioBar[loopcf]; } final double[] dfLMMBar = new double[nbCF]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { dfLMMBar[loopcf] += (1.0 / dfLMM[loopcf + 1]) / deltaLMM[loopcf] * liborLMMBar[loopcf]; dfLMMBar[loopcf + 1] += -dfLMM[loopcf] / (dfLMM[loopcf + 1] * dfLMM[loopcf + 1]) / deltaLMM[loopcf] * liborLMMBar[loopcf]; } for (int loopcf = 1; loopcf < nbCF; loopcf++) { dfLMMBar[loopcf] += 1.0 / dfLMM[0] * p0Bar[loopcf]; dfLMMBar[0] += -dfLMM[loopcf] / (dfLMM[0] * dfLMM[0]) * p0Bar[loopcf]; } dfLMMBar[0] += blkAdjoint[0] * (swaption.isLong() ? 1.0 : -1.0) * pvBar; final double[] cfaBar = new double[nbCF]; cfaBar[0] = cfaModBar[0]; System.arraycopy(cfaModBar, 2, cfaBar, 1, nbCF - 1); final double[] cfaInitBar = new double[nbCFInit]; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { cfaInitBar[loopcf] = cfaBar[indCFDate[loopcf] - indStart]; } final List<DoublesPair> listDfSensi = new ArrayList<>(); for (int loopcf = 0; loopcf < nbCF; loopcf++) { final DoublesPair dfSensi = new DoublesPair(cft[loopcf], -cft[loopcf] * dfLMM[loopcf] * dfLMMBar[loopcf]); listDfSensi.add(dfSensi); } final Map<String, List<DoublesPair>> pvsDF = new HashMap<>(); pvsDF.put(multicurves.getName(ccy), listDfSensi); MulticurveSensitivity sensitivity = MulticurveSensitivity.ofYieldDiscounting(pvsDF); final Map<Double, MulticurveSensitivity> cfeCurveSensi = swaption.getUnderlyingSwap().accept(CFECSC, multicurves); for (int loopcf = 0; loopcf < cfe.getNumberOfPayments(); loopcf++) { final MulticurveSensitivity sensiCfe = cfeCurveSensi.get(cfe.getNthPayment(loopcf).getPaymentTime()); if (!(sensiCfe == null)) { // There is some sensitivity to that cfe. sensitivity = sensitivity.plus(sensiCfe.multipliedBy(-multFact * cfaInitBar[loopcf])); } } return MultipleCurrencyMulticurveSensitivity.of(ccy, sensitivity); }
From source file:org.apache.calcite.runtime.SqlFunctions.java
/** SQL <code>SIGN</code> operator applied to double values. */ public static double sign(double b0) { return Math.signum(b0); }
From source file:geogebra.kernel.GeoImplicitPoly.java
public static BigRational toRational(double d) { int s = (int) Math.signum(d); d = Math.abs(d);/*from w ww .j a v a 2 s . co m*/ long p = (int) Math.floor(d); long p1 = 1; long q = 1; long q1 = 0; double res = 1 / (d - p); while (Math.abs(p / (double) q - d) > Kernel.STANDARD_PRECISION && (q1 == 0 || Math.abs(p1 / (double) q1 - d) > Math.abs(p / (double) q - d))) { long b = (int) Math.floor(res); long t = p; p = b * p + p1; p1 = t; t = q; q = b * q + q1; q1 = t; res = 1 / (res - b); } return new BigRational(p * s, q); }
From source file:com.opengamma.analytics.financial.model.volatility.BlackScholesFormulaRepository.java
/** * The spot vega of an option, i.e. the sensitivity of the option's spot price wrt the implied volatility (which is just the the spot vega * divided by the the numeraire)//from w ww . j a v a2 s.c o m * @param spot The spot value of the underlying * @param strike The Strike * @param timeToExpiry The time-to-expiry * @param lognormalVol The log-normal volatility * @param interestRate The interest rate * @param costOfCarry The cost-of-carry rate * @return The spot vega */ @ExternalFunction public static double vega(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry) { ArgumentChecker.isTrue(spot >= 0.0, "negative/NaN spot; have {}", spot); ArgumentChecker.isTrue(strike >= 0.0, "negative/NaN strike; have {}", strike); ArgumentChecker.isTrue(timeToExpiry >= 0.0, "negative/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isTrue(lognormalVol >= 0.0, "negative/NaN lognormalVol; have {}", lognormalVol); ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN"); ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN"); double coef = 0.; if ((interestRate > LARGE && costOfCarry > LARGE) || (-interestRate > LARGE && -costOfCarry > LARGE) || Math.abs(costOfCarry - interestRate) < SMALL) { coef = 1.; //ref value is returned } else { final double rate = costOfCarry - interestRate; if (rate > LARGE) { return costOfCarry > LARGE ? 0. : Double.POSITIVE_INFINITY; } if (-rate > LARGE) { return 0.; } coef = Math.exp(rate * timeToExpiry); } final double rootT = Math.sqrt(timeToExpiry); double sigmaRootT = lognormalVol * rootT; if (Double.isNaN(sigmaRootT)) { sigmaRootT = 1.; //ref value is returned } double factor = Math.exp(costOfCarry * timeToExpiry); if (Double.isNaN(factor)) { factor = 1.; //ref value is returned } double d1 = 0.; if (Math.abs(spot - strike) < SMALL || (spot > LARGE && strike > LARGE) || sigmaRootT > LARGE) { final double coefD1 = (Math.abs(costOfCarry) < SMALL && lognormalVol < SMALL) ? Math.signum(costOfCarry) + 0.5 * lognormalVol : (costOfCarry / lognormalVol + 0.5 * lognormalVol); final double tmp = coefD1 * rootT; d1 = Double.isNaN(tmp) ? 0. : tmp; } else { if (sigmaRootT < SMALL || spot > LARGE * strike || strike > LARGE * spot) { final double scnd = (Math.abs(costOfCarry) > LARGE && rootT < SMALL) ? Math.signum(costOfCarry) : costOfCarry * rootT; final double tmp = (Math.log(spot / strike) / rootT + scnd) / lognormalVol; d1 = Double.isNaN(tmp) ? 0. : tmp; } else { final double tmp = costOfCarry * rootT / lognormalVol; final double sig = (costOfCarry >= 0.) ? 1. : -1.; final double scnd = Double.isNaN(tmp) ? ((lognormalVol < LARGE && lognormalVol > SMALL) ? sig / lognormalVol : sig * rootT) : tmp; d1 = Math.log(spot / strike) / sigmaRootT + scnd + 0.5 * sigmaRootT; } } // if (Double.isNaN(d1)) { // throw new IllegalArgumentException("NaN found"); // } final double norm = NORMAL.getPDF(d1); final double res = norm < SMALL ? 0. : coef * norm * spot * rootT; return Double.isNaN(res) ? Double.POSITIVE_INFINITY : res; }