Skip to content

Instantly share code, notes, and snippets.

@pmembrey
Created April 2, 2012 00:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmembrey/2279694 to your computer and use it in GitHub Desktop.
Save pmembrey/2279694 to your computer and use it in GitHub Desktop.
Black Scholes in Erlang
% Simple Black Scholes implementation for Erlang
% Author: Peter Membrey
% Date: 5th March 2012
% Based on Python version by Andy Smith at:
% http://www.espenhaug.com/black_scholes.html
%
-module(blackscholes).
-compile(export_all).
calcCall(S,X,T,R,V) ->
D1 = (math:log(S/X)+(R+V*V/2.0)*T)/(V*math:sqrt(T)),
D2 = D1-V*math:sqrt(T),
S*normalDistribution(D1)-X*math:exp(-R*T)*normalDistribution(D2).
calcPut(S,X,T,R,V) ->
D1 = (math:log(S/X)+(R+V*V/2.0)*T)/(V*math:sqrt(T)),
D2 = D1-V*math:sqrt(T),
X*math:exp(-R*T)*normalDistribution(-D2)-S*normalDistribution(-D1).
normalDistribution(X) ->
A1 = 0.31938153,
A2 = -0.356563782,
A3 = 1.781477937,
A4 = -1.821255978,
A5 = 1.330274429,
L = abs(X),
K = 1.0 / (1.0 + 0.2316419 * L),
W = 1.0 - 1.0 / math:sqrt(2*math:pi())* math:exp(-L*L/2.0) * (A1*K + A2*K*K + A3*math:pow(K,3) + A4*math:pow(K,4) + A5*math:pow(K,5)),
case X < 0 of
true -> 1.0-W;
false -> W
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment