final double vols[] = { 0.11 };
final double jInt[] = { 1.0, 5.0 };
final double mLJ[] = { -0.20, 0.0, 0.20 };
final double jV[] = { 0.01, 0.25 };
final DayCounter dc = new Actual360();
new Settings().setEvaluationDate(Date.todaysDate());
final Date today = new Settings().evaluationDate();
final SimpleQuote spot = new SimpleQuote(0.0);
final SimpleQuote qRate = new SimpleQuote(0.0);
final YieldTermStructure qTS = Utilities.flatRate(qRate, dc);
final SimpleQuote rRate = new SimpleQuote(0.0);
final YieldTermStructure rTS = Utilities.flatRate(rRate, dc);
final SimpleQuote vol = new SimpleQuote(0.0);
final BlackVolTermStructure volTS = Utilities.flatVol(vol, dc);
final SimpleQuote jumpIntensity = new SimpleQuote(0.0);
final SimpleQuote meanLogJump = new SimpleQuote(0.0);
final SimpleQuote jumpVol = new SimpleQuote(0.0);
final Merton76Process stochProcess = new Merton76Process(
new Handle<Quote>(spot),
new Handle<YieldTermStructure>(qTS),
new Handle<YieldTermStructure>(rTS),
new Handle<BlackVolTermStructure>(volTS),
new Handle<Quote>(jumpIntensity),
new Handle<Quote>(meanLogJump),
new Handle<Quote>(jumpVol));
// The jumpdiffusionengine greeks are very sensitive to the convergence level.
// A tolerance of 1.0e-08 is usually sufficient to get reasonable results
final PricingEngine engine = new JumpDiffusionEngine(stochProcess, 1e-08);
for (final Type type : types) {
for (final double strike : strikes) {
for (final double element : jInt) {
jumpIntensity.setValue(element);
for (final double element2 : mLJ) {
meanLogJump.setValue(element2);
for (final double element3 : jV) {
jumpVol.setValue(element3);
for (final double residualTime : residualTimes) {
final Date exDate = today.add((int) (residualTime * 360 + 0.5));
final Exercise exercise = new EuropeanExercise(exDate);
for (int kk = 0; kk < 1; kk++) {
StrikedTypePayoff payoff = null;
// option to check
if (kk == 0) {
payoff = new PlainVanillaPayoff(type, strike);
} else if (kk == 1) {
payoff = new CashOrNothingPayoff(type, strike, 100.0);
} else if (kk == 2) {
payoff = new AssetOrNothingPayoff(type, strike);
} else if (kk == 3) {
payoff = new GapPayoff(type, strike, 100.0);
}
final EuropeanOption option = new EuropeanOption(payoff, exercise);
option.setPricingEngine(engine);
for (final double u : underlyings) {
for (final double q : qRates) {
for (final double r : rRates) {
for (final double v : vols) {
spot.setValue(u);
qRate.setValue(q);
rRate.setValue(r);
vol.setValue(v);
final double value = option.NPV();
calculated.put("delta", option.delta());
calculated.put("gamma", option.gamma());
calculated.put("theta", option.theta());
calculated.put("rho", option.rho());
calculated.put("divRho", option.dividendRho());
calculated.put("vega", option.vega());
if (value > spot.value() * 1.0e-5) {
// perturb spot and get delta and gamma
final double du = u * 1.0e-5;
spot.setValue(u + du);
double value_p = option.NPV();
final double delta_p = option.delta();
spot.setValue(u - du);
double value_m = option.NPV();
final double delta_m = option.delta();
spot.setValue(u);
expected.put("delta", (value_p - value_m) / (2 * du));
expected.put("gamma", (delta_p - delta_m) / (2 * du));
// perturb rates and get rho and dividend rho
final double dr = 1.0e-5;
rRate.setValue(r + dr);
value_p = option.NPV();
rRate.setValue(r - dr);
value_m = option.NPV();
rRate.setValue(r);
expected.put("rho", (value_p - value_m) / (2 * dr));
final double dq = 1.0e-5;
qRate.setValue(q + dq);
value_p = option.NPV();
qRate.setValue(q - dq);
value_m = option.NPV();
qRate.setValue(q);
expected.put("divRho", (value_p - value_m) / (2 * dq));
// perturb volatility and get vega
final double dv = v * 1.0e-4;
vol.setValue(v + dv);
value_p = option.NPV();
vol.setValue(v - dv);
value_m = option.NPV();
vol.setValue(v);
expected.put("vega", (value_p - value_m) / (2 * dv));
final Date yesterday = today.sub(1);
final Date tomorrow = today.add(1);
final double dT = dc.yearFraction(yesterday, tomorrow);
new Settings().setEvaluationDate(yesterday);
value_m = option.NPV();
new Settings().setEvaluationDate(tomorrow);
value_p = option.NPV();
new Settings().setEvaluationDate(today);