Erlang/OTP Forums

Author Message

<  Erlang  ~  Convert binomial tree option pricer to use tail recursion?

riskyrisk
Posted: Sat Mar 21, 2009 7:53 pm Reply with quote
Joined: 11 Oct 2008 Posts: 8 Location: Netherlands
I've been thinking about the Haskell code posted here:
http://www.thulasidas.com/2009-03/a-new-kind-of-binomial-tree.htm
I've translated it to Erlang. Also added code to handle call or put and increased the speed by using log math instead of pow and multiplication. But it's slow! I think because it doesn't use tail recursion in the crucial f function.
Any ideas on how to speed it up?

Code:
-module(crr).
-import(math, [log/1, sqrt/1, exp/1, pow/2]).
-export([optionPrice/7]).

max(X, Y) when X > Y -> X;
max(_X, Y) -> Y.

optionPrice(CP, S, K, T, R, V, N) ->
     Dt = T / N,
     Df = exp(-R * Dt),
     U = exp(V * sqrt(Dt)),
     D = 1 / U,
     Pu = (exp(R * Dt) - D) / (U - D),
     Pd = 1 - Pu,
     S * f(0, 0, CP, K / S , Df, log(U), Pu, Pd, N).

s(J, U) ->
    exp(J * U).

f(N, J, CP, X, _Df, U, _Pu, _Pd, N) ->
     Sij = s(J, U),
     max(CP * (Sij - X), 0);
f(I, J, CP, X, Df, U, Pu, Pd, N) ->
     Sij = s(J, U),
     Vij = Df * (
          Pu * f(I + 1, J + 1, CP, X, Df, U, Pu, Pd, N) +
          Pd * f(I + 1, J - 1, CP, X, Df, U, Pu, Pd, N) ),
     max(CP * (Sij - X),   Vij).

riskyrisk
View user's profile Send private message

Display posts from previous:  

All times are GMT
Page 1 of 1
This forum is locked: you cannot post, reply to, or edit topics.

Jump to:  

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum