Package com.opengamma.analytics.financial.greeks

Examples of com.opengamma.analytics.financial.greeks.GreekResultCollection


        final double t1 = definition.getTimeToExpiry(date);
        final double sigma = data.getVolatility(t1, k1);
        final double r = data.getInterestRate(t1);
        final double b = data.getCostOfCarry();
        final OptionDefinition callDefinition = underlying.isCall() ? underlying : new EuropeanVanillaOptionDefinition(k2, underlying.getExpiry(), true);
        final GreekResultCollection result = BSM.getGreeks(callDefinition, data, REQUIRED_GREEKS);
        final double callBSM = result.get(Greek.FAIR_PRICE);
        final double callDelta = result.get(Greek.DELTA);
        final double underlyingSigma = sigma * Math.abs(callDelta) * s / callBSM;
        final double d1 = getD1(callBSM, k1, t1, underlyingSigma, b);
        final double d2 = getD2(d1, underlyingSigma, t1);
        final int sign = definition.isCall() ? 1 : -1;
        if (underlying.isCall()) {
View Full Code Here


  public GreekResultCollection getGreeks(final T definition, final U data, final Set<Greek> requiredGreeks) {
    Validate.notNull(definition);
    Validate.notNull(data);
    Validate.notNull(requiredGreeks);
    final Function1D<U, Double> pricingFunction = getPricingFunction(definition);
    final GreekResultCollection results = new GreekResultCollection();
    final GreekVisitor<Double> visitor = getGreekVisitor(pricingFunction, data, definition);
    for (final Greek greek : requiredGreeks) {
      final Double result = greek.accept(visitor);
      results.put(greek, result);
    }
    return results;
  }
View Full Code Here

      public Double evaluate(final T t) {
        return treeFunction.evaluate(t).getNode(0, 0).second;
      }

    };
    final GreekResultCollection results = new GreekResultCollection();
    final GreekVisitor<Double> visitor = new FiniteDifferenceGreekVisitor<>(function, data, definition);
    for (final Greek greek : requiredGreeks) {
      final Double result = greek.accept(visitor);
      results.put(greek, result);
    }
    return results;
  }
View Full Code Here

    ArgumentChecker.isTrue(volatility > 0., "volatility should be positive");
    ArgumentChecker.isTrue(Doubles.isFinite(volatility), "volatility should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(interestRate), "interestRate should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(dividend), "dividend should be finite");

    final GreekResultCollection collection = new GreekResultCollection();
    final LatticeSpecification modLattice = (lattice instanceof TimeVaryingLatticeSpecification) ? new TrigeorgisLatticeSpecification() : lattice;

    final int nSteps = function.getNumberOfSteps();
    final double strike = function.getStrike();
    final double timeToExpiry = function.getTimeToExpiry();

    final double dt = timeToExpiry / nSteps;
    final double discount = Math.exp(-interestRate * dt);
    final double[] params = modLattice.getParameters(spot, strike, timeToExpiry, volatility, interestRate - dividend, nSteps, dt);
    final double upFactor = params[0];
    final double downFactor = params[1];
    final double upProbability = params[2];
    final double downProbability = params[3];
    final double upOverDown = upFactor / downFactor;
    ArgumentChecker.isTrue(upProbability > 0., "upProbability should be greater than 0.");
    ArgumentChecker.isTrue(upProbability < 1., "upProbability should be smaller than 1.");

    final double assetPrice = spot * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiry(assetPrice, upOverDown);
    final double[] res = new double[4];

    final double[] pForDelta = new double[] {spot * downFactor, spot * upFactor };
    final double[] pForGamma = new double[] {pForDelta[0] * downFactor, pForDelta[0] * upFactor, pForDelta[1] * upFactor };

    for (int i = nSteps - 1; i > -1; --i) {
      values = function.getNextOptionValues(discount, upProbability, downProbability, values, spot, 0., downFactor, upOverDown, i);
      if (i == 2) {
        res[2] = 2. * ((values[2] - values[1]) / (pForGamma[2] - pForGamma[1]) - (values[1] - values[0]) / (pForGamma[1] - pForGamma[0])) / (pForGamma[2] - pForGamma[0]);
        res[3] = values[1];
      }
      if (i == 1) {
        res[1] = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
      }
    }
    res[0] = values[0];
    res[3] = modLattice.getTheta(spot, volatility, interestRate, dividend, dt, res);

    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

      ArgumentChecker.isTrue(Doubles.isFinite(volatility[i]), "volatility should be finite");
      ArgumentChecker.isTrue(Doubles.isFinite(interestRate[i]), "interestRate should be finite");
      ArgumentChecker.isTrue(Doubles.isFinite(dividend[i]), "dividend should be finite");
    }

    final GreekResultCollection collection = new GreekResultCollection();

    final double[] nu = vLattice.getShiftedDrift(volatility, interestRate, dividend);
    final double spaceStep = vLattice.getSpaceStep(timeToExpiry, volatility, nSteps, nu);
    final double upFactor = Math.exp(spaceStep);
    final double downFactor = Math.exp(-spaceStep);
    final double upOverDown = Math.exp(2. * spaceStep);

    final double[] upProbability = new double[nSteps];
    final double[] downProbability = new double[nSteps];
    final double[] df = new double[nSteps];
    final double[] dt = new double[2];
    for (int i = 0; i < nSteps; ++i) {
      final double[] params = vLattice.getParameters(volatility[i], nu[i], spaceStep);
      upProbability[i] = params[1];
      downProbability[i] = 1. - params[1];
      df[i] = Math.exp(-interestRate[i] * params[0]);
      if (i == 0) {
        dt[0] = params[0];
      }
      if (i == 2) {
        dt[1] = params[1];
      }
      ArgumentChecker.isTrue(upProbability[i] > 0., "upProbability should be greater than 0.");
      ArgumentChecker.isTrue(upProbability[i] < 1., "upProbability should be smaller than 1.");
    }

    final double assetPrice = spot * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiry(assetPrice, upOverDown);
    final double[] res = new double[4];

    final double[] pForDelta = new double[] {spot * downFactor, spot * upFactor };
    final double[] pForGamma = new double[] {pForDelta[0] * downFactor, pForDelta[0] * upFactor, pForDelta[1] * upFactor };

    for (int i = nSteps - 1; i > -1; --i) {
      values = function.getNextOptionValues(df[i], upProbability[i], downProbability[i], values, spot, 0., downFactor, upOverDown, i);
      if (i == 2) {
        res[2] = 2. * ((values[2] - values[1]) / (pForGamma[2] - pForGamma[1]) - (values[1] - values[0]) / (pForGamma[1] - pForGamma[0])) / (pForGamma[2] - pForGamma[0]);
        res[3] = values[1];
      }
      if (i == 1) {
        res[1] = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
      }
    }
    res[0] = values[0];
    res[3] = vLattice.getTheta(dt[0], dt[1], res);

    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

    ArgumentChecker.isTrue(Doubles.isFinite(spot), "Spot should be finite");
    ArgumentChecker.isTrue(volatility > 0., "volatility should be positive");
    ArgumentChecker.isTrue(Doubles.isFinite(volatility), "volatility should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(interestRate), "interestRate should be finite");

    final GreekResultCollection collection = new GreekResultCollection();
    final LatticeSpecification modLattice = (lattice instanceof TimeVaryingLatticeSpecification) ? new TrigeorgisLatticeSpecification() : lattice;

    final int nSteps = function.getNumberOfSteps();
    final double strike = function.getStrike();
    final double timeToExpiry = function.getTimeToExpiry();

    final double dt = timeToExpiry / nSteps;
    ArgumentChecker.isTrue(dividend.checkTimeSteps(dt), "Number of steps is too small");
    ArgumentChecker.isTrue(dividend.checkDividendBeforeExpiry(timeToExpiry), "Dividend is paid after expiry");

    final double discount = Math.exp(-interestRate * dt);
    final double[] params = modLattice.getParameters(spot, strike, timeToExpiry, volatility, interestRate, nSteps, dt);
    final double upFactor = params[0];
    final double downFactor = params[1];
    final double upProbability = params[2];
    final double downProbability = params[3];
    final double upOverDown = upFactor / downFactor;
    ArgumentChecker.isTrue(upProbability > 0., "upProbability should be greater than 0.");
    ArgumentChecker.isTrue(upProbability < 1., "upProbability should be smaller than 1.");

    final int[] divSteps = dividend.getDividendSteps(dt);

    double assetPriceBase = dividend.spotModifier(spot, interestRate);
    final double assetPriceTerminal = assetPriceBase * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiry(assetPriceTerminal, upOverDown);

    int counter = 0;
    final int nDivs = dividend.getNumberOfDividends();
    final double[] res = new double[4];

    if (dividend instanceof ProportionalDividendFunctionProvider) {
      for (int i = nSteps - 1; i > -1; --i) {
        for (int k = nDivs - 1 - counter; k > -1; --k) {
          if (i == divSteps[k]) {
            assetPriceBase = dividend.dividendCorrections(assetPriceBase, 0., 0., k);
            ++counter;
          }
        }
        values = function.getNextOptionValues(discount, upProbability, downProbability, values, assetPriceBase, 0., downFactor, upOverDown, i);
        if (i == 2) {
          final double[] pForGamma = dividend.getAssetPricesForGamma(spot, interestRate, divSteps, upFactor, downFactor, 0.);
          res[2] = 2. * ((values[2] - values[1]) / (pForGamma[2] - pForGamma[1]) - (values[1] - values[0]) / (pForGamma[1] - pForGamma[0])) / (pForGamma[2] - pForGamma[0]);
          res[3] = values[1];
        }
        if (i == 1) {
          final double[] pForDelta = dividend.getAssetPricesForDelta(spot, interestRate, divSteps, upFactor, downFactor, 0.);
          res[1] = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
        }
      }
    } else {
      double sumDiscountDiv = 0.;
      for (int i = nSteps - 1; i > -1; --i) {
        sumDiscountDiv *= Math.exp(-interestRate * dt);
        for (int k = nDivs - 1 - counter; k > -1; --k) {
          if (i == divSteps[k]) {
            sumDiscountDiv = dividend.dividendCorrections(sumDiscountDiv, interestRate, dt * i, k);
            ++counter;
          }
        }
        values = function.getNextOptionValues(discount, upProbability, downProbability, values, assetPriceBase, sumDiscountDiv, downFactor, upOverDown, i);
        if (i == 2) {
          final double[] pForGamma = dividend.getAssetPricesForGamma(assetPriceBase, interestRate, divSteps, upFactor, downFactor, sumDiscountDiv);
          res[2] = 2. * ((values[2] - values[1]) / (pForGamma[2] - pForGamma[1]) - (values[1] - values[0]) / (pForGamma[1] - pForGamma[0])) / (pForGamma[2] - pForGamma[0]);
          res[3] = values[1];
        }
        if (i == 1) {
          final double[] pForDelta = dividend.getAssetPricesForDelta(assetPriceBase, interestRate, divSteps, upFactor, downFactor, sumDiscountDiv);
          res[1] = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
        }
      }
    }

    res[0] = values[0];
    res[3] = modLattice.getTheta(spot, volatility, interestRate, 0., dt, res);
    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

  }

  @Override
  public GreekResultCollection getGreeks(final OptionDefinition definition, final T data, final Set<Greek> requiredGreeks) {
    final Function1D<T, RecombiningBinomialTree<DoublesPair>> treeFunction = getTreeGeneratingFunction(definition);
    final GreekResultCollection results = new GreekResultCollection();
    final GreekVisitor<Double> visitor = getGreekVisitor(treeFunction, data, definition);
    for (final Greek greek : requiredGreeks) {
      final Double result = greek.accept(visitor);
      results.put(greek, result);
    }
    return results;
  }
View Full Code Here

    ArgumentChecker.isTrue(volatility > 0., "volatility should be positive");
    ArgumentChecker.isTrue(Doubles.isFinite(volatility), "volatility should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(interestRate), "interestRate should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(dividend), "dividend should be finite");

    final GreekResultCollection collection = new GreekResultCollection();
    final LatticeSpecification modLattice = (lattice instanceof TimeVaryingLatticeSpecification) ? new TrigeorgisLatticeSpecification() : lattice;

    final int nSteps = function.getNumberOfSteps();
    final double strike = function.getStrike();
    final double timeToExpiry = function.getTimeToExpiry();

    final double dt = timeToExpiry / nSteps;
    final double discount = Math.exp(-interestRate * dt);
    final double[] params = modLattice.getParametersTrinomial(spot, strike, timeToExpiry, volatility, interestRate - dividend, nSteps, dt);
    final double upFactor = params[0];
    final double middleFactor = params[1];
    final double downFactor = params[2];
    final double upProbability = params[3];
    final double middleProbability = params[4];
    final double downProbability = params[5];
    final double middleOverDown = middleFactor / downFactor;
    ArgumentChecker.isTrue(upProbability > 0., "upProbability should be greater than 0.");
    ArgumentChecker.isTrue(upProbability < 1., "upProbability should be smaller than 1.");
    ArgumentChecker.isTrue(middleProbability > 0., "middleProbability should be greater than 0.");
    ArgumentChecker.isTrue(middleProbability < 1., "middleProbability should be smaller than 1.");
    ArgumentChecker.isTrue(downProbability > 0., "downProbability should be greater than 0.");

    final double assetPrice = spot * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiryTrinomial(assetPrice, middleOverDown);
    final double[] res = new double[4];

    final double[] pForDelta = new double[] {spot * downFactor, spot * middleFactor, spot * upFactor };
    final double[] pForGamma = new double[] {pForDelta[0] * downFactor, pForDelta[0] * middleFactor, pForDelta[1] * middleFactor, pForDelta[2] * middleFactor, pForDelta[2] * upFactor };

    for (int i = nSteps - 1; i > -1; --i) {
      values = function.getNextOptionValues(discount, upProbability, middleProbability, downProbability, values, spot, 0., downFactor, middleOverDown, i);
      if (i == 2) {
        final double delta1 = (values[4] - values[3]) / (pForGamma[4] - pForGamma[3]);
        final double delta2 = (values[3] - values[2]) / (pForGamma[3] - pForGamma[2]);
        final double delta3 = (values[2] - values[1]) / (pForGamma[2] - pForGamma[1]);
        final double delta4 = (values[1] - values[0]) / (pForGamma[1] - pForGamma[0]);
        final double gamma1 = 2. * (delta1 - delta2) / (pForGamma[4] - pForGamma[2]);
        final double gamma2 = 2. * (delta2 - delta3) / (pForGamma[3] - pForGamma[1]);
        final double gamma3 = 2. * (delta3 - delta4) / (pForGamma[2] - pForGamma[0]);
        res[2] = (gamma1 + gamma2 + gamma3) / 3.;
        res[3] = values[2];
      }
      if (i == 1) {
        final double delta1 = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
        final double delta2 = (values[2] - values[1]) / (pForDelta[2] - pForDelta[1]);
        res[1] = 0.5 * (delta1 + delta2);
      }
    }
    res[0] = values[0];
    res[3] = modLattice.getTheta(spot, volatility, interestRate, dividend, dt, res);

    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

    ArgumentChecker.notNull(dividend, "dividend");

    ArgumentChecker.isTrue(spot > 0., "Spot should be positive");
    ArgumentChecker.isTrue(Doubles.isFinite(spot), "Spot should be finite");

    final GreekResultCollection collection = new GreekResultCollection();
    final TimeVaryingLatticeSpecification vLattice = new TimeVaryingLatticeSpecification();
    final int nSteps = function.getNumberOfSteps();
    final double timeToExpiry = function.getTimeToExpiry();

    ArgumentChecker.isTrue(nSteps == interestRate.length, "Wrong interestRate length");
    ArgumentChecker.isTrue(nSteps == volatility.length, "Wrong volatility length");
    ArgumentChecker.isTrue(nSteps == dividend.length, "Wrong dividend length");

    for (int i = 0; i < nSteps; ++i) {
      ArgumentChecker.isTrue(volatility[i] > 0., "volatility should be positive");
      ArgumentChecker.isTrue(Doubles.isFinite(volatility[i]), "volatility should be finite");
      ArgumentChecker.isTrue(Doubles.isFinite(interestRate[i]), "interestRate should be finite");
      ArgumentChecker.isTrue(Doubles.isFinite(dividend[i]), "dividend should be finite");
    }

    final double[] nu = vLattice.getShiftedDrift(volatility, interestRate, dividend);
    final double dt = timeToExpiry / nSteps;
    final double spaceStep = vLattice.getSpaceStepTrinomial(volatility, nu, dt);
    final double downFactor = Math.exp(-spaceStep);
    final double middleOverDown = Math.exp(spaceStep);

    final double[] upProbability = new double[nSteps];
    final double[] middleProbability = new double[nSteps];
    final double[] downProbability = new double[nSteps];
    final double[] df = new double[nSteps];
    for (int i = 0; i < nSteps; ++i) {
      final double[] params = vLattice.getParametersTrinomial(volatility[i], nu[i], dt, spaceStep);
      upProbability[i] = params[0];
      middleProbability[i] = params[1];
      downProbability[i] = params[2];
      df[i] = Math.exp(-interestRate[i] * dt);
      ArgumentChecker.isTrue(upProbability[i] > 0., "upProbability should be greater than 0.");
      ArgumentChecker.isTrue(upProbability[i] < 1., "upProbability should be smaller than 1.");
      ArgumentChecker.isTrue(middleProbability[i] > 0., "middleProbability should be greater than 0.");
      ArgumentChecker.isTrue(middleProbability[i] < 1., "middleProbability should be smaller than 1.");
      ArgumentChecker.isTrue(downProbability[i] > 0., "downProbability should be greater than 0.");
    }

    final double assetPrice = spot * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiryTrinomial(assetPrice, middleOverDown);
    final double[] res = new double[4];

    final double[] pForDelta = new double[] {spot * downFactor, spot, spot * middleOverDown };
    final double[] pForGamma = new double[] {pForDelta[0] * downFactor, pForDelta[0], spot, pForDelta[2], pForDelta[2] * middleOverDown };

    for (int i = nSteps - 1; i > -1; --i) {
      values = function.getNextOptionValues(df[i], upProbability[i], middleProbability[i], downProbability[i], values, spot, 0., downFactor, middleOverDown, i);
      if (i == 2) {
        final double delta1 = (values[4] - values[3]) / (pForGamma[4] - pForGamma[3]);
        final double delta2 = (values[3] - values[2]) / (pForGamma[3] - pForGamma[2]);
        final double delta3 = (values[2] - values[1]) / (pForGamma[2] - pForGamma[1]);
        final double delta4 = (values[1] - values[0]) / (pForGamma[1] - pForGamma[0]);
        final double gamma1 = 2. * (delta1 - delta2) / (pForGamma[4] - pForGamma[2]);
        final double gamma2 = 2. * (delta2 - delta3) / (pForGamma[3] - pForGamma[1]);
        final double gamma3 = 2. * (delta3 - delta4) / (pForGamma[2] - pForGamma[0]);
        res[2] = (gamma1 + gamma2 + gamma3) / 3.;
        res[3] = values[2];
      }
      if (i == 1) {
        final double delta1 = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
        final double delta2 = (values[2] - values[1]) / (pForDelta[2] - pForDelta[1]);
        res[1] = 0.5 * (delta1 + delta2);
      }
    }
    res[0] = values[0];
    res[3] = vLattice.getTheta(spot, 0., 0., 0., dt, res);

    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

    ArgumentChecker.isTrue(Doubles.isFinite(spot), "Spot should be finite");
    ArgumentChecker.isTrue(volatility > 0., "volatility should be positive");
    ArgumentChecker.isTrue(Doubles.isFinite(volatility), "volatility should be finite");
    ArgumentChecker.isTrue(Doubles.isFinite(interestRate), "interestRate should be finite");

    final GreekResultCollection collection = new GreekResultCollection();
    final LatticeSpecification modLattice = (lattice instanceof TimeVaryingLatticeSpecification) ? new TrigeorgisLatticeSpecification() : lattice;

    final int nSteps = function.getNumberOfSteps();
    final double strike = function.getStrike();
    final double timeToExpiry = function.getTimeToExpiry();

    final double dt = timeToExpiry / nSteps;
    ArgumentChecker.isTrue(dividend.checkTimeSteps(dt), "Number of steps is too small");
    ArgumentChecker.isTrue(dividend.checkDividendBeforeExpiry(timeToExpiry), "Dividend is paid after expiry");

    final double discount = Math.exp(-interestRate * dt);
    final double[] params = modLattice.getParametersTrinomial(spot, strike, timeToExpiry, volatility, interestRate, nSteps, dt);
    final double upFactor = params[0];
    final double middleFactor = params[1];
    final double downFactor = params[2];
    final double upProbability = params[3];
    final double middleProbability = params[4];
    final double downProbability = params[5];
    final double middleOverDown = middleFactor / downFactor;
    ArgumentChecker.isTrue(upProbability > 0., "upProbability should be greater than 0.");
    ArgumentChecker.isTrue(upProbability < 1., "upProbability should be smaller than 1.");
    ArgumentChecker.isTrue(middleProbability > 0., "middleProbability should be greater than 0.");
    ArgumentChecker.isTrue(middleProbability < 1., "middleProbability should be smaller than 1.");
    ArgumentChecker.isTrue(downProbability > 0., "downProbability should be greater than 0.");

    final int[] divSteps = dividend.getDividendSteps(dt);

    double assetPriceBase = dividend.spotModifier(spot, interestRate);
    final double assetPriceTerminal = assetPriceBase * Math.pow(downFactor, nSteps);
    double[] values = function.getPayoffAtExpiryTrinomial(assetPriceTerminal, middleOverDown);

    int counter = 0;
    final int nDivs = dividend.getNumberOfDividends();
    final double[] res = new double[4];

    if (dividend instanceof ProportionalDividendFunctionProvider) {
      for (int i = nSteps - 1; i > -1; --i) {
        for (int k = nDivs - 1 - counter; k > -1; --k) {
          if (i == divSteps[k]) {
            assetPriceBase = dividend.dividendCorrections(assetPriceBase, 0., 0., k);
            ++counter;
          }
        }
        values = function.getNextOptionValues(discount, upProbability, middleProbability, downProbability, values, assetPriceBase, 0., downFactor, middleOverDown, i);
        if (i == 2) {
          final double[] pForGamma = dividend.getAssetPricesForGamma(spot, interestRate, divSteps, upFactor, middleFactor, downFactor, 0.);
          final double delta1 = (values[4] - values[3]) / (pForGamma[4] - pForGamma[3]);
          final double delta2 = (values[3] - values[2]) / (pForGamma[3] - pForGamma[2]);
          final double delta3 = (values[2] - values[1]) / (pForGamma[2] - pForGamma[1]);
          final double delta4 = (values[1] - values[0]) / (pForGamma[1] - pForGamma[0]);
          final double gamma1 = 2. * (delta1 - delta2) / (pForGamma[4] - pForGamma[2]);
          final double gamma2 = 2. * (delta2 - delta3) / (pForGamma[3] - pForGamma[1]);
          final double gamma3 = 2. * (delta3 - delta4) / (pForGamma[2] - pForGamma[0]);
          res[2] = (gamma1 + gamma2 + gamma3) / 3.;
          res[3] = values[2];
        }
        if (i == 1) {
          final double[] pForDelta = dividend.getAssetPricesForDelta(spot, interestRate, divSteps, upFactor, middleFactor, downFactor, 0.);
          final double delta1 = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
          final double delta2 = (values[2] - values[1]) / (pForDelta[2] - pForDelta[1]);
          res[1] = 0.5 * (delta1 + delta2);
        }
      }
    } else {
      double sumDiscountDiv = 0.;
      for (int i = nSteps - 1; i > -1; --i) {
        sumDiscountDiv *= Math.exp(-interestRate * dt);
        for (int k = nDivs - 1 - counter; k > -1; --k) {
          if (i == divSteps[k]) {
            sumDiscountDiv = dividend.dividendCorrections(sumDiscountDiv, interestRate, dt * i, k);
            ++counter;
          }
        }
        values = function.getNextOptionValues(discount, upProbability, middleProbability, downProbability, values, assetPriceBase, sumDiscountDiv, downFactor, middleOverDown, i);
        if (i == 2) {
          final double[] pForGamma = dividend.getAssetPricesForGamma(assetPriceBase, interestRate, divSteps, upFactor, middleFactor, downFactor, sumDiscountDiv);
          final double delta1 = (values[4] - values[3]) / (pForGamma[4] - pForGamma[3]);
          final double delta2 = (values[3] - values[2]) / (pForGamma[3] - pForGamma[2]);
          final double delta3 = (values[2] - values[1]) / (pForGamma[2] - pForGamma[1]);
          final double delta4 = (values[1] - values[0]) / (pForGamma[1] - pForGamma[0]);
          final double gamma1 = 2. * (delta1 - delta2) / (pForGamma[4] - pForGamma[2]);
          final double gamma2 = 2. * (delta2 - delta3) / (pForGamma[3] - pForGamma[1]);
          final double gamma3 = 2. * (delta3 - delta4) / (pForGamma[2] - pForGamma[0]);
          res[2] = (gamma1 + gamma2 + gamma3) / 3.;
          res[3] = values[2];
        }
        if (i == 1) {
          final double[] pForDelta = dividend.getAssetPricesForDelta(assetPriceBase, interestRate, divSteps, upFactor, middleFactor, downFactor, sumDiscountDiv);
          final double delta1 = (values[1] - values[0]) / (pForDelta[1] - pForDelta[0]);
          final double delta2 = (values[2] - values[1]) / (pForDelta[2] - pForDelta[1]);
          res[1] = 0.5 * (delta1 + delta2);
        }
      }
    }
    res[0] = values[0];
    res[3] = modLattice.getTheta(spot, volatility, interestRate, 0., dt, res);
    collection.put(Greek.FAIR_PRICE, res[0]);
    collection.put(Greek.DELTA, res[1]);
    collection.put(Greek.GAMMA, res[2]);
    collection.put(Greek.THETA, res[3]);

    return collection;
  }
View Full Code Here

TOP

Related Classes of com.opengamma.analytics.financial.greeks.GreekResultCollection

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.