Instantly share code, notes, and snippets.

Embed
What would you like to do?
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