Package it.unimi.dsi.fastutil.doubles

Examples of it.unimi.dsi.fastutil.doubles.DoubleArrayList


        _timeSteps, _spaceSteps, _timeGridBunching, _spaceGridBunching, 1.0);

    final double[] timeNodes = pdeRes.getGrid().getTimeNodes();
    final double[] spaceNodes = pdeRes.getGrid().getSpaceNodes();
    final int n = pdeRes.getNumberSpaceNodes();
    final DoubleArrayList strikes = new DoubleArrayList();
    final DoubleArrayList impliedVolatilities = new DoubleArrayList();
    final DoubleArrayList prices = new DoubleArrayList();
    final DoubleArrayList blackPrices = new DoubleArrayList();
    final DoubleArrayList absoluteDomesticPrice = new DoubleArrayList();
    //final DoubleArrayList absoluteForeignPrice = new DoubleArrayList();
    final DoubleArrayList bsDelta = new DoubleArrayList();
    final DoubleArrayList bsDualDelta = new DoubleArrayList();
    final DoubleArrayList bsGamma = new DoubleArrayList();
    final DoubleArrayList bsDualGamma = new DoubleArrayList();
    final DoubleArrayList bsVega = new DoubleArrayList();
    final DoubleArrayList bsVanna = new DoubleArrayList();
    final DoubleArrayList bsVomma = new DoubleArrayList();
    final DoubleArrayList modelDelta = new DoubleArrayList();
    final DoubleArrayList modelDualDelta = new DoubleArrayList();
    final DoubleArrayList modelGamma = new DoubleArrayList();
    final DoubleArrayList modelDualGamma = new DoubleArrayList();
    final DoubleArrayList modelVega = new DoubleArrayList();
    final DoubleArrayList modelVanna = new DoubleArrayList();
    final DoubleArrayList modelVomma = new DoubleArrayList();
    for (int i = 0; i < n - 1; i++) {
      final double moneyness = pdeRes.getSpaceValue(i);
      final double k = moneyness * forward;
      final double mPrice = pdeRes.getFunctionValue(i);
      double impVol = 0;
      try {
        impVol = getBSImpliedVol(mPrice, moneyness, expiry, isCall);
      } catch (final Exception e) {
        continue;
      }

      final int timeIndex = SurfaceArrayUtils.getLowerBoundIndex(timeNodes, expiry);
      final int spaceIndex = SurfaceArrayUtils.getLowerBoundIndex(spaceNodes, moneyness);
      final double value1 = forward * pdeRes.getFunctionValue(spaceIndex, timeIndex);
      final double value2 = forward * pdeRes.getFunctionValue(spaceIndex + 1, timeIndex);
      final double m1 = pdeRes.getSpaceValue(spaceIndex);
      final double m2 = pdeRes.getSpaceValue(spaceIndex + 1);
      //review R White 9/3/2012 This is pointless as moneyness == m1 (it is just the value at space index i) - what we should be doing
      //is finding the price (and all other greeks) for a given moneyness (corresponding to an option) that is not on the grid
      //So price and blackPrice should be the same (within the round trip to implied volatility)
      final double price = ((m2 - moneyness) * value1 + (moneyness - m1) * value2) / (m2 - m1);
      final double blackPrice = BlackFormulaRepository.price(forward, k, expiry, impVol, isCall);
      strikes.add(k);
      impliedVolatilities.add(impVol);
      prices.add(price);
      blackPrices.add(blackPrice);
      bsDelta.add(getBSDelta(forward, k, expiry, impVol, isCall));
      bsDualDelta.add(getBSDualDelta(forward, k, expiry, impVol, isCall));
      bsGamma.add(getBSGamma(forward, k, expiry, impVol));
      bsDualGamma.add(getBSDualGamma(forward, k, expiry, impVol));
      bsVega.add(getBSVega(forward, k, expiry, impVol));
      bsVanna.add(getBSVanna(forward, k, expiry, impVol));
      bsVomma.add(getBSVomma(forward, k, expiry, impVol));
      absoluteDomesticPrice.add(Math.PI); //DEBUG - just trying to get a number through the system

      final double modelDD = getModelDualDelta(pdeRes, i);
      modelDualDelta.add(modelDD);
      final double fixedSurfaceDelta = getFixedSurfaceDelta(mPrice, moneyness, modelDD);
      final double surfaceDelta = getSurfaceDelta(pdeResForwardUp, pdeResForwardDown, forward, forwardShift, i);
      final double modelD = getModelDelta(fixedSurfaceDelta, forward, surfaceDelta);
      modelDelta.add(modelD);

      final double modelDG = getModelDualGamma(pdeRes, i, forward);
      modelDualGamma.add(modelDG);
      final double fixedSurfaceGamma = getFixedSurfaceGamma(moneyness, modelDG);
      final double dSurfaceDMoneyness = getDSurfaceDMoneyness(pdeResForwardUp, pdeResForwardDown, forward, forwardShift, i);
      final double surfaceGamma = getSurfaceGamma(pdeResForwardUp, pdeResForwardDown, pdeRes, forward, forwardShift, i);
      modelGamma.add(getModelGamma(fixedSurfaceGamma, surfaceDelta, moneyness, dSurfaceDMoneyness, surfaceGamma));

      modelVega.add(getModelVega(pdeResVolUp, pdeResVolDown, volShift, i));
      final double xVanna = getXVanna(volShift, pdeResVolUp, pdeResVolDown, i, moneyness);
      final double surfaceVanna = getSurfaceVanna(pdeResForwardUpVolUp, pdeResForwardUpVolDown, pdeResForwardDownVolUp, pdeResForwardDownVolDown, volShift, forwardShift, i);
      modelVanna.add(getModelVanna(xVanna, surfaceVanna));
      modelVomma.add(getModelVomma(pdeRes, pdeResVolUp, pdeResVolDown, volShift, i));
    }
    final PDEResultCollection result = new PDEResultCollection(strikes.toDoubleArray());
    result.put(PDEResultCollection.GRID_PRICE, prices.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_PRICE, blackPrices.toDoubleArray());
    result.put(PDEResultCollection.GRID_IMPLIED_VOL, impliedVolatilities.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_DELTA, bsDelta.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_DUAL_DELTA, bsDualDelta.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_GAMMA, bsGamma.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_DUAL_GAMMA, bsDualGamma.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_VEGA, bsVega.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_VANNA, bsVanna.toDoubleArray());
    result.put(PDEResultCollection.GRID_BLACK_VOMMA, bsVomma.toDoubleArray());
    result.put(PDEResultCollection.GRID_DELTA, modelDelta.toDoubleArray());
    result.put(PDEResultCollection.GRID_DUAL_DELTA, modelDualDelta.toDoubleArray());
    result.put(PDEResultCollection.GRID_GAMMA, modelGamma.toDoubleArray());
    result.put(PDEResultCollection.GRID_DUAL_GAMMA, modelDualGamma.toDoubleArray());
    result.put(PDEResultCollection.GRID_VEGA, modelVega.toDoubleArray());
    result.put(PDEResultCollection.GRID_VANNA, modelVanna.toDoubleArray());
    result.put(PDEResultCollection.GRID_VOMMA, modelVomma.toDoubleArray());
    result.put(PDEResultCollection.GRID_DOMESTIC_PV_QUOTE, absoluteDomesticPrice.toDoubleArray());
    return result;
  }
View Full Code Here


    return f;
  }

  @Override
  public double[] getInterestRateParameterSensitivity(final double time) {
    final DoubleArrayList result = new DoubleArrayList();
    double[] temp;
    temp = _curves[0].getInterestRateParameterSensitivity(time);
    for (final double element : temp) {
      result.add(element);
    }
    for (int loopcurve = 1; loopcurve < _curves.length; loopcurve++) {
      temp = _curves[loopcurve].getInterestRateParameterSensitivity(time);
      for (final double element : temp) {
        result.add(element);
      }
    }
    return result.toDoubleArray();
  }
View Full Code Here

  @Override
  public VaRCalculationResult evaluate(final EmpiricalDistributionVaRParameters parameters, final DoubleTimeSeries<?>... data) {
    ArgumentChecker.notNull(parameters, "parameters");
    ArgumentChecker.notNull(data, "data");
    final double var = _varCalculator.evaluate(parameters, data).getVaRValue();
    final DoubleArrayList excesses = new DoubleArrayList();
    for (final double portfolioReturn : data[0].valuesArrayFast()) {
      if (portfolioReturn < -var) {
        excesses.add(portfolioReturn);
      }
    }
    if (excesses.isEmpty()) {
      return new VaRCalculationResult(var, null);
    }
    return new VaRCalculationResult(-_meanCalculator.evaluate(excesses.toDoubleArray()), null);
  }
View Full Code Here

   * @deprecated {@link YieldCurveBundle} is deprecated
   */
  @Deprecated
  public DoubleMatrix1D calculateFromPresentValue(final Map<String, List<DoublesPair>> curveSensitivities, final YieldCurveBundle curves, final DoubleMatrix1D couponSensitivity,
      final DoubleMatrix2D jacobian) {
    final DoubleArrayList resultList = new DoubleArrayList();
    for (final String curveName : curves.getAllNames()) {
      final List<Double> pointToParameterSensitivity = _parameterSensitivityCalculator.pointToParameterSensitivity(curveSensitivities.get(curveName), curves.getCurve(curveName));
      final DoubleMatrix1D nodeSensitivity = new DoubleMatrix1D(pointToParameterSensitivity.toArray(new Double[pointToParameterSensitivity.size()]));
      final int n = nodeSensitivity.getNumberOfElements();
      final DoubleMatrix2D inverseJacobian = MATRIX_ALGEBRA.getInverse(jacobian);
      for (int i = 0; i < n; i++) {
        double sum = 0;
        for (int j = 0; j < n; j++) {
          sum += -couponSensitivity.getEntry(i) * inverseJacobian.getEntry(j, i) * nodeSensitivity.getEntry(j);
        }
        resultList.add(sum);
      }
    }
    return new DoubleMatrix1D(resultList.toDoubleArray());
  }
View Full Code Here

   * @return The instrument quote / rate sensitivity
   * @deprecated {@link YieldCurveBundle} is deprecated
   */
  @Deprecated
  public DoubleMatrix1D calculateFromParRate(final Map<String, List<DoublesPair>> curveSensitivities, final YieldCurveBundle interpolatedCurves, final DoubleMatrix2D jacobian) {
    final DoubleArrayList resultList = new DoubleArrayList();
    for (final String curveName : interpolatedCurves.getAllNames()) {
      final List<Double> pointToParameterSensitivity = _parameterSensitivityCalculator.pointToParameterSensitivity(curveSensitivities.get(curveName), interpolatedCurves.getCurve(curveName));
      final DoubleMatrix1D nodeSensitivity = new DoubleMatrix1D(pointToParameterSensitivity.toArray(new Double[pointToParameterSensitivity.size()]));
      final int n = nodeSensitivity.getNumberOfElements();
      final DoubleMatrix2D inverseJacobian = MATRIX_ALGEBRA.getInverse(jacobian);
      for (int i = 0; i < n; i++) {
        double sum = 0;
        for (int j = 0; j < n; j++) {
          sum += inverseJacobian.getEntry(j, i) * nodeSensitivity.getEntry(j);
        }
        resultList.add(sum);
      }
    }
    return new DoubleMatrix1D(resultList.toDoubleArray());
  }
View Full Code Here

   * @return The market quote sensitivity
   * @deprecated {@link YieldCurveBundle} is deprecated
   */
  @Deprecated
  public DoubleMatrix1D calculateFromSimpleInterpolatedCurve(final Map<String, List<DoublesPair>> curveSensitivities, final YieldCurveBundle interpolatedCurves) {
    final DoubleArrayList resultList = new DoubleArrayList();
    for (final String curveName : interpolatedCurves.getAllNames()) {
      final List<Double> pointToParameterSensitivity = _parameterSensitivityCalculator.pointToParameterSensitivity(curveSensitivities.get(curveName), interpolatedCurves.getCurve(curveName));
      final DoubleMatrix1D nodeSensitivity = new DoubleMatrix1D(pointToParameterSensitivity.toArray(new Double[pointToParameterSensitivity.size()]));
      final int n = nodeSensitivity.getNumberOfElements();
      for (int i = 0; i < n; i++) {
        double sum = 0;
        for (int j = 0; j < n; j++) {
          sum += nodeSensitivity.getEntry(j);
        }
        resultList.add(sum);
      }
    }
    return new DoubleMatrix1D(resultList.toDoubleArray());
  }
View Full Code Here

    }
    final ForwardCurve forwardCurve = (ForwardCurve) forwardCurveObject;

    // 3. Remove empties, convert expiries from number to years, and scale vols
    final Map<Pair<Double, Double>, Double> volValues = new HashMap<Pair<Double, Double>, Double>();
    final DoubleArrayList tList = new DoubleArrayList();
    final DoubleArrayList kList = new DoubleArrayList();
    // SurfaceInstrumentProvider just used to get expiry calculator - find a better way as this is quite ugly.
    final String surfacePrefix = surfaceName.split("\\_")[1];
    final ExchangeTradedInstrumentExpiryCalculator expiryCalculator = new BloombergCommodityFutureOptionVolatilitySurfaceInstrumentProvider(surfacePrefix, "Comdty", "", 0., "")
        .getExpiryRuleCalculator();
    for (final Number nthExpiry : rawSurface.getXs()) {
      final Double t = TimeCalculator.getTimeBetween(valDate, expiryCalculator.getExpiryDate(nthExpiry.intValue(), valDate, calendar));

      if (!isValidStrike(forwardCurve, rawSurface, t, nthExpiry)) {
        continue;
      }

      if (t > 5. / 365.) { // Bootstrapping vol surface to this data causes far more trouble than any gain. The data simply isn't reliable.
        for (final Double strike : rawSurface.getYs()) {
          final Double vol = rawSurface.getVolatility(nthExpiry, strike);
          if (vol != null) {
            tList.add(t);
            kList.add(strike);
            volValues.put(Pair.of(t, strike), vol / 100.);
          }
        }
      }
    }
    final VolatilitySurfaceData<Double, Double> stdVolSurface = new VolatilitySurfaceData<Double, Double>(rawSurface.getDefinitionName(), rawSurface.getSpecificationName(), rawSurface.getTarget(),
        tList.toArray(new Double[0]), kList.toArray(new Double[0]), volValues);

    // 4. Return
    final ValueProperties stdVolProperties = createValueProperties()
        .with(ValuePropertyNames.SURFACE, surfaceName)
        .with(InstrumentTypeProperties.PROPERTY_SURFACE_INSTRUMENT_TYPE, InstrumentTypeProperties.COMMODITY_FUTURE_OPTION)
View Full Code Here

  /** Build a volatility surface based on Expiry, T, and Strike, K. T is in measured in our standard OG-Analytic years */
  private static VolatilitySurfaceData<Double, Double> getSurfaceFromVolatilityQuote(final VolatilitySurfaceData<Number, Double> optionVolatilities, final ZonedDateTime now,
      final Calendar calendar) {
    final Map<Pair<Double, Double>, Double> volatilityValues = new HashMap<Pair<Double, Double>, Double>();
    final DoubleArrayList tList = new DoubleArrayList();
    final DoubleArrayList kList = new DoubleArrayList();
    final LocalDate today = now.toLocalDate();
    for (final Number x : optionVolatilities.getXs()) {
      final Double t = FutureOptionUtils.getIRFutureOptionTtm(x.intValue(), today, calendar);
      for (final Double y : optionVolatilities.getYs()) {
        final Double volatility = optionVolatilities.getVolatility(x, y);
        if (volatility != null) {
          tList.add(t);
          kList.add(y / 100.);
          volatilityValues.put(Pair.of(t, y / 100.), volatility / 100); // TODO Normalisation, could this be done elsewhere?
        }
      }
    }
    return new VolatilitySurfaceData<Double, Double>(optionVolatilities.getDefinitionName(), optionVolatilities.getSpecificationName(),
        optionVolatilities.getTarget(), tList.toArray(new Double[0]), kList.toArray(new Double[0]), volatilityValues);
  }
View Full Code Here

    }
    if (specification.getSurfaceInstrumentProvider() instanceof CallPutSurfaceInstrumentProvider) {
      callAboveStrike = ((CallPutSurfaceInstrumentProvider<?, ?>) specification.getSurfaceInstrumentProvider()).useCallAboveStrike();
    }
    final Map<Pair<Double, Double>, Double> volatilityValues = new HashMap<Pair<Double, Double>, Double>();
    final DoubleArrayList txList = new DoubleArrayList();
    final DoubleArrayList kList = new DoubleArrayList();
    final LocalDate today = now.toLocalDate();
    for (final Number x : optionPrices.getXs()) { // Loop over option expiries
      final LocalDate expiry = expiryRule.getExpiryDate(x.intValue(), today, calendar);
      final Double optionTtm = TimeCalculator.getTimeBetween(today, expiry);
      // Get the corresponding future, which may not share the same expiries as the option itself
      final Double[] futureExpiries = futurePrices.getXData();
      final int nFutures = futureExpiries.length;
      if (nFutures == 0) {
        s_logger.info("No future prices found for surface : " + specification.getName());
        return null;
      }
      Double underlyingExpiry;
      int i = 0;
      do {
        underlyingExpiry = futureExpiries[i++];
      } while (underlyingExpiry < optionTtm && i < nFutures);

      if (underlyingExpiry < optionTtm) {
        s_logger.info("Requesting an option price where the underlying future price isn't available. "
            + "Either there are too many expiries in VolatilitySurfaceDefinition or too few in the corresponding FuturePriceCurveDefinition");

      } else {
        final Double forward = futurePrices.getYValue(underlyingExpiry);
        // Loop over strikes
        for (final Double y : optionPrices.getYs()) {
          final Double price = optionPrices.getVolatility(x, y);
          if (price != null) {
            try {

              // Compute the Black volatility implied from the option price
              final double volatility = getVolatility(surfaceQuoteType, y / 100.0, price, forward, optionTtm, callAboveStrike / 100.);
              if (!CompareUtils.closeEquals(volatility, 0.0)) {
                txList.add(optionTtm);
                kList.add(y / 100.0);
                volatilityValues.put(Pair.of(optionTtm, y / 100.), volatility);
              }
            } catch (final MathException e) {
              s_logger.info("Could not imply volatility for ({}, {}); error was {}", new Object[] {x, y, e.getMessage() });
            } catch (final IllegalArgumentException e) {
              s_logger.info("Could not imply volatility for ({}, {}); error was {}", new Object[] {x, y, e.getMessage() });
            }
          }
        }
      }
    }
    return new VolatilitySurfaceData<Double, Double>(optionPrices.getDefinitionName(), optionPrices.getSpecificationName(),
        optionPrices.getTarget(), txList.toArray(new Double[0]), kList.toArray(new Double[0]), volatilityValues);
  }
View Full Code Here

  private static VolatilitySurfaceData<Double, Double> getSurfaceFromVolatilityQuote(final VolatilitySurfaceData<Number, Double> optionVolatilities, final ZonedDateTime now,
      final Calendar calendar) {
    final BondFutureOptionExpiryCalculator expiryCalculator = BondFutureOptionExpiryCalculator.getInstance();
    final Map<Pair<Double, Double>, Double> volatilityValues = new HashMap<Pair<Double, Double>, Double>();
    final DoubleArrayList tList = new DoubleArrayList();
    final DoubleArrayList kList = new DoubleArrayList();
    final LocalDate today = now.toLocalDate();
    for (final Number x : optionVolatilities.getXs()) {
      final Double t = TimeCalculator.getTimeBetween(today, expiryCalculator.getExpiryDate(x.intValue(), today, calendar));
      for (final Double y : optionVolatilities.getYs()) {
        final Double volatility = optionVolatilities.getVolatility(x, y);
        if (volatility != null) {
          tList.add(t);
          kList.add(y / 100.);
          volatilityValues.put(Pair.of(t, y / 100.), volatility / 100); // TODO Normalisation, could this be done elsewhere?
        }
      }
    }
    return new VolatilitySurfaceData<Double, Double>(optionVolatilities.getDefinitionName(), optionVolatilities.getSpecificationName(),
        optionVolatilities.getTarget(), tList.toArray(new Double[0]), kList.toArray(new Double[0]), volatilityValues);
  }
View Full Code Here

TOP

Related Classes of it.unimi.dsi.fastutil.doubles.DoubleArrayList

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.