This question is followed up from this Question. I would like to thank Dr. Hintze and I_Mariusz for the comments and help. I am pretty new to mathematica ( I just learned it 4 days ago) so I would like to ask if there is a way to accelerate the following codes :
Clear["Global`*"]
eq = (2*S0/(y*sigma^2))^
nu*(Gamma[nu + (2*mu)/sigma^2]/Gamma[2*nu + (2*mu)/sigma^2])*
Hypergeometric1F1[nu, 2*nu + (2*mu)/sigma^2, -2*S0/(y*sigma^2)];
int = Integrate[eq, {y, K, Infinity}, GenerateConditions -> False]
int2 = int /. nu -> (-vu/2 + Sqrt[vu^2 + 8*alpha/sigma^2]/2) /.
vu -> (2*mu/sigma^2 - 1)
mu = 15/100;
sigma = 5/100;
S0 = 100;
K = 95;
T = 1;
F[alpha_] = int2/alpha;
A = 18.4;
n = 40;
m = 50;
S = ConstantArray[0, m + 1];
B = 1;
For[k = 0, k <= m, k++,
S[[k + 1]] = Exp[A/2]/(2*B)*Re[F[A/(2*B)]];
For[j = 1, j <= n + k, j++,
S[[k + 1]] =
S[[k + 1]] + Exp[A/2]/B*(-1)^j*Re[F[(A + 2*j*Pi*I)/(2*B)]];
] ]
f = 0
For[k = 0, k <= m, k++,
f = f + Binomial[m, k]*S[[k + 1]]*2^(-m);
]
f
Currently, it takes me hours to run this program. Thank you so much for your time. I truly appreciate it.
F[alpha_] = int2/alpha
toF[alpha_] := int2/alpha
this will run quite quickly. Using=
in a function assignment doesn't actually assign it as a pattern (see the documentation on Set (=) versus SetDelayed (:=)). $\endgroup$int2
an explicit function ofalpha
. The other thing I'd suggest is to convert yourFor
loop to aTable[ Sum[] ]
structure. $\endgroup$F
for the same argument. DefineF
so it remembers:F[alpha_] := F[alpha] = int2[alpha]/alpha;
( or restructure your loop so you aren't repeating calculations , that innerFor
loop just adds one new term for eachk
.) $\endgroup$