MathGroup Archive 2004

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Compile

  • To: mathgroup at smc.vnet.net
  • Subject: [mg48314] Re: [mg48302] Compile
  • From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
  • Date: Sun, 23 May 2004 06:15:40 -0400 (EDT)
  • References: <200405220704.DAA08873@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

I wonder how you managed to get an error about 
Random[NormalDistribution[0,1]], since there  are errors in you code 
that should stop you ever getting to that point. First of all you have 
the a right square bracket in the wrong place, I mean the last one 
below:

> MCCall=Compile[{{S, K, T, r, div, sig},{M, _Integer},{ N, _Integer}}]

secondly, the syntax {S, K, T, r, div, sig} is quite wrong (look at the 
help for Compile), thirdly you need to load the 
Statistics`NormalDistribution` package, fourthly, in any case your 
function is very unlikely to compile well, if at all, so that it will 
probably be no faster than without compiling. You will do much better 
to re-write your code in a functional way, using Nest instead of For 
(For is very inefficient in Mathematica), that alone will give you a 
substantial speed up. Finally, you might want to have a look at my 
posting with the subject  "strange problems with Random" , otherwise 
you might be surprised that your answers turn out not to be very 
accurate.

Andrzej Kozlowski


On 22 May 2004, at 16:04, John wrote:

> Hi,
>
> i am new to mathematica, i have written a monte carlo simulation for an
> option, I wanted to compile the function, but i just get errors 
> concerning
> the Random[NormalDistribution[0,1]] function, could anybody help, 
> thats the
> code:
>
> MCCall=Compile[{{S, K, T, r, div, sig},{M, _Integer},{ N, _Integer}}],
> Module[{dt, nudt, sigsdt, lns, sumct, i, j, lnst, st, ct},
> dt = T/N;
> nudt = (r - div - 0.5*sig^2)*dt;
> sigsdt = sig*Sqrt[dt];
> lns = Log[S];
> sumct = 0;
> i = 1;
> j = 1;
> a = {};
> For[i = 1, i < M,
> lnst = lns;
> For[j = 1, j < N,
> lnst = lnst + nudt + sigsdt*Random[NormalDistribution[0, 1]]; j++];
> st = Exp[lnst];
> ct = Max[0, (st - K)];
> sumct = sumct + ct;
> a = Append[a, sumct/(i*Exp[-r*T])]; i++];
> sumct/(M*Exp[-r*T])]];
>
> TIA John
>


  • References:
    • Compile
      • From: "John" <john34@sepei.com>
  • Prev by Date: Re: Fractal Geometry--> Levy Flights
  • Next by Date: Re: padding numbers in front of uniquely generatd names
  • Previous by thread: Compile
  • Next by thread: Re: Compile