List of usage examples for java.util Arrays binarySearch
public static int binarySearch(Object[] a, Object key)
From source file:edu.cornell.med.icb.goby.alignments.AlignmentReaderImpl.java
/** * Reposition to a genomic position. The goBack flag, when true, allows to reposition to positions that * we have already passed. When false, reposition will only advance to future positions. * * @param targetIndex index of the target sequence to reposition to. * @param position position of the location to reposition to. * @param goBack If true, the method will reposition to past positions, otherwise, only reposition to future locations. * @throws IOException If an error occurs repositioning. *//*from w ww . j a v a 2 s. c o m*/ private void repositionInternal(final int targetIndex, final int position, boolean goBack) throws IOException { // assert indexLoaded : "index must be loaded to repositionInternal."; if (!indexLoaded) { return; } final long absolutePosition = recodePosition(targetIndex, position); int offsetIndex = Arrays.binarySearch(indexAbsolutePositions.elements(), absolutePosition); offsetIndex = offsetIndex < 0 ? -1 - offsetIndex : offsetIndex; // NB offsetIndex contains absolutePosition in the first entry, but the chunk before it also likely // contains entries with this absolute position. We therefore substract one to position on the chunk // before. offsetIndex = offsetIndex >= indexOffsets.size() ? indexOffsets.size() - 1 : Math.max(offsetIndex - 1, 0); if (offsetIndex < 0) { // empty alignment. return; } // max below ensures we never go back to before the start of the slice the reader was restricted to at // construction time: final long newBytePosition = Math.max(startOffset, indexOffsets.getLong(offsetIndex)); final long currentPosition = alignmentEntryReader.position(); if (newBytePosition >= currentPosition) { seek(newBytePosition); } else { if (goBack) { // only reposition to past locations if we are called directly through reposition. Otherwise, we honor // the skipTo contract and do not reposition to previously visited locations. seek(newBytePosition); } } }
From source file:com.linkedin.pinot.core.segment.creator.impl.SegmentIndexCreationDriverImpl.java
/** * Helper method to binary-search a given key in an input array. * Both input array and the key to search are passed in as 'Object'. * * - Supported data types are int, long, float, double & String. * - Throws an exception for any other data type. * * @param inputArray Input array to search * @param key Key to search for/*from w w w . j ava2s. com*/ * @return */ private int searchValueInArray(Object inputArray, Object key) { if (inputArray instanceof int[]) { return Arrays.binarySearch((int[]) inputArray, (Integer) key); } else if (inputArray instanceof long[]) { return Arrays.binarySearch((long[]) inputArray, (Long) key); } else if (inputArray instanceof float[]) { return Arrays.binarySearch((float[]) inputArray, (Float) key); } else if (inputArray instanceof double[]) { return Arrays.binarySearch((double[]) inputArray, (Double) key); } else if (inputArray instanceof String[]) { return Arrays.binarySearch((String[]) inputArray, key); } else if (inputArray instanceof Object[]) { return Arrays.binarySearch((Object[]) inputArray, key); } else { throw new RuntimeException("Unexpected data type encountered while updating StarTree node" + inputArray.getClass().getName()); } }
From source file:org.apache.geode.distributed.internal.AbstractDistributionConfig.java
public static boolean isWellKnownAttribute(String attName) { return Arrays.binarySearch(dcValidAttributeNames, attName) >= 0; }
From source file:org.apache.sysml.runtime.compress.CompressedMatrixBlock.java
@Override public double quickGetValue(int r, int c) { if (!isCompressed()) { return super.quickGetValue(r, c); }//from w w w.j av a2 s . co m //find column group according to col index ColGroup grp = null; for (ColGroup group : _colGroups) if (Arrays.binarySearch(group.getColIndices(), c) >= 0) { grp = group; break; } //find row value return grp.get(r, c); }
From source file:org.fhcrc.cpl.viewer.quant.gui.PanelWithSpectrumChart.java
License:asdf
protected void placeHeatmapTick(double[] mzValues, double[][] intensityValuesPadded, float mzVal, float intensityForTickMark) { int closestLightMzIndex = Math.abs(Arrays.binarySearch(mzValues, mzVal)); int[] tickXVals = new int[] { 0, 1, 2, 3, 0, 1, 2, 3, intensityValuesPadded.length - 1, intensityValuesPadded.length - 2, intensityValuesPadded.length - 3, intensityValuesPadded.length - 4, intensityValuesPadded.length - 1, intensityValuesPadded.length - 2, intensityValuesPadded.length - 3, intensityValuesPadded.length - 4 }; int indMaybePlus1 = Math.min(closestLightMzIndex + 1, intensityValuesPadded[0].length - 1); int[] tickYVals = new int[] { closestLightMzIndex, closestLightMzIndex, closestLightMzIndex, closestLightMzIndex, indMaybePlus1, indMaybePlus1, indMaybePlus1, indMaybePlus1, closestLightMzIndex, closestLightMzIndex, closestLightMzIndex, closestLightMzIndex, indMaybePlus1, indMaybePlus1, indMaybePlus1, indMaybePlus1 }; for (int i = 0; i < tickXVals.length; i++) { intensityValuesPadded[tickXVals[i]][tickYVals[i]] = intensityForTickMark; }//from ww w . j av a 2 s. co m }
From source file:com.opengamma.analytics.financial.interestrate.swaption.method.SwaptionPhysicalFixedIborLMMDDMethod.java
/** * Computes the present value curve sensitivity of the Physical delivery swaption. * @param swaption The swaption./*from w ww. j av a2 s . c o m*/ * @param lmmBundle The LMM parameters and the curves. * @return The present value. */ public InterestRateCurveSensitivity presentValueCurveSensitivity(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"); } } } 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]; } double[] cfaMod = new double[nbCF + 1]; 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 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 = -cfaMod[0]; double bM = (b0 + bK) / 2.0d; 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 meanReversionImpact = (Math.exp(2.0d * amr * timeToExpiry) - 1.0d) / (2.0d * amr); 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); double[] blkAdjoint = blackFunction.getPriceAdjoint(option, dataBlack); // Backward sweep double pvBar = 1.0; double impliedBlackVolBar = dfLMM[0] * blkAdjoint[2] * (swaption.isLong() ? 1.0 : -1.0) * pvBar; double normSigmaMBar = meanReversionImpact / (2.0 * impliedBlackVol) * impliedBlackVolBar; double[] sigmaMBar = new double[nbFactor]; for (int loopfact = 0; loopfact < nbFactor; loopfact++) { sigmaMBar[loopfact] = 2 * sigmaM[loopfact] * normSigmaMBar; } 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]; } } 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]; } } 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]; } } 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]; } 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]; } double sumNumBar = 1.0 / sumDen * xBarBar; double sumDenBar = -sumNum / (sumDen * sumDen) * xBarBar; 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; } 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]; } 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]; } } 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; 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; } 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]; } 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; 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]; } 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; double[] cfaBar = new double[nbCF]; cfaBar[0] = cfaModBar[0]; System.arraycopy(cfaModBar, 2, cfaBar, 1, nbCF - 1); double[] cfaInitBar = new double[nbCFInit]; for (int loopcf = 0; loopcf < nbCFInit; loopcf++) { cfaInitBar[loopcf] = cfaBar[indCFDate[loopcf] - indStart]; } final List<DoublesPair> listDfSensi = new ArrayList<DoublesPair>(); for (int loopcf = 0; loopcf < nbCF; loopcf++) { DoublesPair dfSensi = new DoublesPair(cft[loopcf], -cft[loopcf] * dfLMM[loopcf] * dfLMMBar[loopcf]); listDfSensi.add(dfSensi); } final Map<String, List<DoublesPair>> pvsDF = new HashMap<String, List<DoublesPair>>(); pvsDF.put(cfe.getDiscountCurve(), listDfSensi); InterestRateCurveSensitivity sensitivity = new InterestRateCurveSensitivity(pvsDF); Map<Double, InterestRateCurveSensitivity> cfeCurveSensi = CFECSC.visit(swaption.getUnderlyingSwap(), lmmBundle); for (int loopcf = 0; loopcf < cfe.getNumberOfPayments(); loopcf++) { InterestRateCurveSensitivity sensiCfe = cfeCurveSensi.get(cfe.getNthPayment(loopcf).getPaymentTime()); if (!(sensiCfe == null)) { // There is some sensitivity to that cfe. sensitivity = sensitivity.plus(sensiCfe.multiply(-multFact * cfaInitBar[loopcf])); } } return sensitivity; }
From source file:org.apache.orc.impl.mask.RedactMaskFactory.java
/** * Replace each digit in value with digit. * @param value the number to mask/*from w ww . j a va2 s . co m*/ * @return the */ public double maskDouble(double value) { /* check whether unmasking range provided */ if (!unmaskIndexRanges.isEmpty()) { return maskDoubleWIthUnmasking(value); } double base; // It seems better to mask 0 to 9.99999 rather than 9.99999e-308. if (value == 0 || DIGIT_REPLACEMENT == 0) { return DIGIT_REPLACEMENT * 1.11111; } else if (value > 0) { base = 1.11111; } else { base = -1.11111; value = -value; } int posn = Arrays.binarySearch(DOUBLE_POWER_10, value); if (posn < -DOUBLE_POWER_10.length - 2) { posn = DOUBLE_POWER_10.length - 1; } else if (posn == -1) { posn = 0; } else if (posn < 0) { posn = -posn - 2; } return DIGIT_REPLACEMENT * base * DOUBLE_POWER_10[posn]; }
From source file:com.opengamma.analytics.math.surface.InterpolatedFromCurvesDoublesSurface.java
/** * {@inheritDoc}//from w w w. j a va2 s. com */ @Override public Double getZValue(final Double x, final Double y) { Validate.notNull(x, "x"); Validate.notNull(y, "y"); final double[] z = new double[_nCurves]; int i = 0; if (_xzCurves) { final int index = Arrays.binarySearch(_points, y); if (index < 0) { for (final Curve<Double, Double> curve : _curves) { z[i++] = curve.getYValue(x); } return InterpolatedDoublesCurve.fromSorted(_points, z, _interpolator).getYValue(y); } return _curves[index].getYValue(x); } final int index = Arrays.binarySearch(_points, x); if (index < 0) { for (final Curve<Double, Double> curve : _curves) { z[i++] = curve.getYValue(y); } return InterpolatedDoublesCurve.fromSorted(_points, z, _interpolator).getYValue(x); } return _curves[index].getYValue(y); }
From source file:edu.umn.cs.spatialHadoop.nasa.StockQuadTree.java
/** * Perform a selection query that retrieves all points in the given range. * The range is specified in the two-dimensional array positions. * @param in/*from w w w. java 2s . c o m*/ * @param query_mbr * @param output * @return number of matched records * @throws IOException */ public static int selectionQuery(FSDataInputStream in, Rectangle query_mbr, ResultCollector<PointValue> output) throws IOException { long treeStartPosition = in.getPos(); int numOfResults = 0; int resolution = in.readInt(); short fillValue = in.readShort(); int cardinality = in.readInt(); long[] timestamps = new long[cardinality]; for (int i = 0; i < cardinality; i++) timestamps[i] = in.readLong(); Vector<Integer> selectedStarts = new Vector<Integer>(); Vector<Integer> selectedEnds = new Vector<Integer>(); StockQuadTree stockQuadTree = getOrCreateStockQuadTree(resolution); // Nodes to be searched. Contains node positions in the array of nodes Stack<Integer> nodes_2b_searched = new Stack<Integer>(); nodes_2b_searched.add(0); // Root node (ID=1) Rectangle node_mbr = new Rectangle(); while (!nodes_2b_searched.isEmpty()) { int node_pos = nodes_2b_searched.pop(); stockQuadTree.getNodeMBR(node_pos, node_mbr); if (query_mbr.contains(node_mbr)) { // Add this node to the selection list and stop this branch if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == stockQuadTree.nodesStartPosition[node_pos]) { // Merge with an adjacent range selectedEnds.set(selectedEnds.size() - 1, stockQuadTree.nodesEndPosition[node_pos]); } else { // add a new range selectedStarts.add(stockQuadTree.nodesStartPosition[node_pos]); selectedEnds.add(stockQuadTree.nodesEndPosition[node_pos]); } numOfResults += stockQuadTree.nodesEndPosition[node_pos] - stockQuadTree.nodesStartPosition[node_pos]; } else if (query_mbr.intersects(node_mbr)) { int first_child_id = stockQuadTree.nodesID[node_pos] * 4 + 0; int first_child_pos = Arrays.binarySearch(stockQuadTree.nodesID, first_child_id); if (first_child_pos < 0) { // No children. Hit a leaf node // Scan and add matching points only java.awt.Point record_coords = new Point(); for (int record_pos = stockQuadTree.nodesStartPosition[node_pos]; record_pos < stockQuadTree.nodesEndPosition[node_pos]; record_pos++) { stockQuadTree.getRecordCoords(record_pos, record_coords); if (query_mbr.contains(record_coords)) { // matched a record. if (!selectedEnds.isEmpty() && selectedEnds.lastElement() == record_pos) { // Merge with an adjacent range selectedEnds.set(selectedEnds.size() - 1, record_pos + 1); } else { // Add a new range of unit width selectedStarts.add(record_pos); selectedEnds.add(record_pos + 1); } numOfResults++; } } } else { // Non-leaf node. Add all children to the list of nodes to search // Add in reverse order to the stack so that results come in sorted order nodes_2b_searched.add(first_child_pos + 3); nodes_2b_searched.add(first_child_pos + 2); nodes_2b_searched.add(first_child_pos + 1); nodes_2b_searched.add(first_child_pos + 0); } } } if (output != null) { PointValue returnValue = new PointValue(); long dataStartPosition = treeStartPosition + getValuesStartOffset(cardinality); // Return all values in the selected ranges for (int iRange = 0; iRange < selectedStarts.size(); iRange++) { int treeStart = selectedStarts.get(iRange); int treeEnd = selectedEnds.get(iRange); long startPosition = dataStartPosition + selectedStarts.get(iRange) * cardinality * 2; in.seek(startPosition); for (int treePos = treeStart; treePos < treeEnd; treePos++) { // Retrieve the coords for the point at treePos stockQuadTree.getRecordCoords(treePos, returnValue); // Read all entries at current position for (int iValue = 0; iValue < cardinality; iValue++) { short value = in.readShort(); if (value != fillValue) { returnValue.value = value; returnValue.timestamp = timestamps[iValue]; output.collect(returnValue); } } } } } return numOfResults; }
From source file:com.opengamma.analytics.financial.interestrate.swaption.provider.SwaptionPhysicalFixedIborLMMDDMethod.java
/** * Computes the present value sensitivity to the displaced diffusion (shift) parameters. * @param swaption The (physical delivery) swaption. * @param lmmData The LMM and multi-curves provider. * @return The sensitivity.//from w w w . ja va 2s .c o m */ public double[] presentValueDDSensitivity(final SwaptionPhysicalFixedIbor swaption, 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]; } // TODO: 4. cfe modification (for roller coasters) final double[] cfaMod = new double[nbCF + 1]; final double cfaMod0 = cfa[0]; cfaMod[0] = cfaMod0; // modified strike cfaMod[1] = 0.0; 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 = -cfaMod0; 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.0 / 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.0 / 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] * 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 / 2.0 / tau[loopcf + 1] * 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]; } } final double[] aLMMBar = new double[nbCF - 1]; for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { aLMMBar[loopcf] = 1.0 / (liborLMM[loopcf] + 1 / deltaLMM[loopcf]) * rate0RatioBar[loopcf]; } for (int loopcf = 0; loopcf < nbCF - 1; loopcf++) { aLMMBar[loopcf] += 1.0 / (liborM[loopcf] + 1 / deltaLMM[loopcf]) * rateMRatioBar[loopcf]; } final double[] displacementBar = new double[volLMM.length]; System.arraycopy(aLMMBar, 0, displacementBar, indStart, nbCF - 1); return displacementBar; }