Skip to content

Instantly share code, notes, and snippets.

@OpenGamma-Blog
Last active December 24, 2015 12:19
Show Gist options
  • Save OpenGamma-Blog/6796814 to your computer and use it in GitHub Desktop.
Save OpenGamma-Blog/6796814 to your computer and use it in GitHub Desktop.
import static com.lambder.deriva.Deriva.*;
public class Formulas {
public static Expression black(final boolean isCall) {
// Logistic aproximation of Cumulated Standard Normal Distribution
// 1/( e^(-0.07056 * x^3 - 1.5976*x) + 1)
Expression N = div(1.0,
add(
exp(
sub(
mul(
-0.07056,
pow('x', 3)),
mul(-1.5976, 'x'))),
1.0));
// ( F/K+T*σ^2/2 ) / σ*sqrt(T)
Expression d1 = div(
add(
div('F', 'K'),
mul(div(sq("sigma"), 2.0), 'T')),
mul("sigma", sqrt('T')));
// ( F/K-T*σ^2/2 ) / σ*sqrt(T)
Expression d2 = div(
sub(
div('F', 'K'),
mul(div(sq("sigma"), 2.0), 'T')),
mul("sigma", sqrt('T')));
// e^(-r*T) * ( F*N(d1)-K*N(d2) )
Expression call = mul(
exp(neg(mul('r', 'T'))),
sub(
mul('F', N.bind('x', d1)),
mul('K', N.bind('x', d2))));
// e^(-r*T) * ( F*N(-d2)-K*N(-d1) )
Expression put = mul(
exp(neg(mul('r', 'T'))),
sub(
mul('F', N.bind('x', neg(d2))),
mul('K', N.bind('x', neg(d1)))));
return isCall ? call : put;
}
// usage
public static void main(String[] args) {
// lets fix timeToExpiry to 0.523 and get only strike, forward and lognormalVol sensitivities
Expression blackModel = black(true).bind('T', 0.523);
Function1 fun = d(blackModel, 'F', 'K', 'r').function('F', 'K', 'r');
fun.execute(12.3, 14.3, 0.03);
fun.execute(12.3, 11.0, 0.03);
fun.execute(12.3, 11.0, 0.02);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment