BlackScholesFormulaRepository { public static double strikeForDelta(final double spot, final double spotDelta, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall) { ArgumentChecker.isTrue(spot > 0.0, "non-positive/NaN spot; have {}", spot); ArgumentChecker.isTrue(timeToExpiry > 0.0, "non-positive/NaN timeToExpiry; have {}", timeToExpiry); ArgumentChecker.isTrue(lognormalVol > 0.0, "non-positive/NaN lognormalVol; have {}", lognormalVol); ArgumentChecker.isFalse(Double.isNaN(interestRate), "interestRate is NaN"); ArgumentChecker.isFalse(Double.isNaN(costOfCarry), "costOfCarry is NaN"); ArgumentChecker.isFalse(Double.isInfinite(spot), "spot is infinite"); ArgumentChecker.isFalse(Double.isInfinite(spotDelta), "spotDelta is infinite"); ArgumentChecker.isFalse(Double.isInfinite(timeToExpiry), "timeToExpiry is infinite"); ArgumentChecker.isFalse(Double.isInfinite(lognormalVol), "lognormalVol is infinite"); ArgumentChecker.isFalse(Double.isInfinite(interestRate), "interestRate is infinite"); ArgumentChecker.isFalse(Double.isInfinite(costOfCarry), "costOfCarry is infinite"); final double rescaledDelta = spotDelta * Math.exp((-costOfCarry + interestRate) * timeToExpiry); Validate.isTrue((isCall && rescaledDelta > 0. && rescaledDelta < 1.) || (!isCall && spotDelta < 0. && rescaledDelta > -1.), "delta/Math.exp((costOfCarry - interestRate) * timeToExpiry) out of range, ", rescaledDelta); final double sigmaRootT = lognormalVol * Math.sqrt(timeToExpiry); final double rescaledSpot = spot * Math.exp(costOfCarry * timeToExpiry); final int sign = isCall ? 1 : -1; final double d1 = sign * NORMAL.getInverseCDF(sign * rescaledDelta); return rescaledSpot * Math.exp(-d1 * sigmaRootT + 0.5 * sigmaRootT * sigmaRootT); }  @ExternalFunction static double price(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall); @ExternalFunction static double delta(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall); static double strikeForDelta(final double spot, final double spotDelta, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry,
      final boolean isCall); @ExternalFunction static double dualDelta(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry,
      final boolean isCall); @ExternalFunction static double gamma(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double dualGamma(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double crossGamma(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double theta(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall); static double charm(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry, final boolean isCall); static double dualCharm(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry,
      final boolean isCall); @ExternalFunction static double vega(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double vanna(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double dualVanna(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double vomma(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double vegaBleed(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry); @ExternalFunction static double rho(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry,
      final boolean isCall); @ExternalFunction static double carryRho(final double spot, final double strike, final double timeToExpiry, final double lognormalVol, final double interestRate, final double costOfCarry,
      final boolean isCall);  }
@Test(expectedExceptions = IllegalArgumentException.class) public void negativeVolErrorstrikeForDeltaTest() { BlackScholesFormulaRepository.strikeForDelta(SPOT, 0.1, TIME_TO_EXPIRY, -0.5, INTEREST_RATES[1], COST_OF_CARRY, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void negativeSpotErrorstrikeForDeltaTest() { BlackScholesFormulaRepository.strikeForDelta(-SPOT, 0.1, TIME_TO_EXPIRY, VOLS[1], INTEREST_RATES[1], COST_OF_CARRY, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void negativeStrikeErrorstrikeForDeltaTest() { BlackScholesFormulaRepository.strikeForDelta(SPOT, 0.1, -TIME_TO_EXPIRY, VOLS[1], INTEREST_RATES[1], COST_OF_CARRY, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rangeErrorstrikeForDelta1Test() { BlackScholesFormulaRepository.strikeForDelta(SPOT, 100., TIME_TO_EXPIRY, VOLS[1], INTEREST_RATES[1], COST_OF_CARRY, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rangeErrorstrikeForDelta2Test() { BlackScholesFormulaRepository.strikeForDelta(SPOT, -100., TIME_TO_EXPIRY, VOLS[1], NAN, COST_OF_CARRY, true); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rangeErrorstrikeForDelta3Test() { BlackScholesFormulaRepository.strikeForDelta(SPOT, 100., TIME_TO_EXPIRY, VOLS[1], INTEREST_RATES[1], COST_OF_CARRY, false); }
@Test(expectedExceptions = IllegalArgumentException.class) public void rangeErrorstrikeForDelta4Test() { BlackScholesFormulaRepository.strikeForDelta(SPOT, -100., TIME_TO_EXPIRY, VOLS[1], NAN, COST_OF_CARRY, false); }