# turn infinite loop into finite loop

I am trying to make a table where I can save the stock value with the iterative part, where I should have 100 sequence with 843 entry in each sequence. The loop doesn't run correctly in the sense that it just says "running", so I am assuming it is running infinitely. I am referring to the for loop at the end, I have updated this post to include the background information I started with as seen in the code. My main goal is to get the matrix to iterate with respect to x values. For example matrix 1 should respond to the first row of x and matrix 2 should correspond with second row of x.

dt = 0.0001;
dxt = r*dWt;
r = .005;
t = .0833;
sigma = .2183;
S0 = 201.05;
k = 212.50;

X = Table[0.0158, 100, 834];
For[i = 1, i <= 100, i++,
For[j = 2, j <= 834, j++,
X[[i, j]] =
X[[i, j - 1]] + r*RandomVariate[NormalDistribution[0, (dt)^1/2]];
];
];

\[Mu] = dt/2 dS;
\[Lambda] = (dt*sigma^2)/(2*sigma^2);
s = Range[0, 500, 5];(*Stock price*)
V = Table[0, 101];
(* value of option at particular time *)
For[i = 1,
i <= 101, i++,
If[s[[i]] - k > 0,
V[[i]] = s[[i]] - k,
V[[i]] = 0
];
];

For[p = 1, p < Length[X], p++,
For[q = 2, q <= 834, q++,
btcs[p_, q_] =
SparseArray[{{m_, m_} ->
1 + 2*\[Lambda]*Indexed[s, m]^2 +
Indexed[X, {p, q}]*dt, {m_, l_} /;
l - m == 1 -> -\[Lambda]*Indexed[s, m]^2 - \[Mu]*
Indexed[X, {p, q}]*Indexed[s, m], {m_, l_} /;
m - l == 1 -> -\[Lambda]*Indexed[s, m]^2 + \[Mu]*
Indexed[X, {p, q}]*Indexed[s, m]}, {101, 101}];
A = btcs[p_, q_]];];
Print[A]


But my code is infinitely running and not giving me an output.

• The posted code doesn't do anything. X is undefined so Length[X] is 0 and the outer For test fails immediately. – Bob Hanlon Dec 22 '19 at 19:36
• I had X from my previous part, where X = Table[0.0158, 100, 834]. – Naljor Dec 22 '19 at 19:42
• I am trying to iterate where the loop will run through my R1C1, R1C2, R3C3...all the way up to R3C834 and then carry on with R2Ca and so on... that's is represented as 'p' and 'q'. my 'm' and 'l' are just indices for specific position within matrix. – Naljor Dec 22 '19 at 19:44
• Then edit your question to include all required information. – Bob Hanlon Dec 22 '19 at 19:44

Clear["Global*"]

nrows = 100;
ncols = 834;


Note that dWt is undefined

dt = 0.0001;
dxt = r*dWt;
r = .005;
t = .0833;
sigma = .2183;
S0 = 201.05;
k = 212.50;

X = ConstantArray[0.0158, {nrows, ncols}];

SeedRandom[1234]
(* required to make results reproducible *)


I assumed that (dt)^1/2 was intended to be dt^(1/2), i.e., Sqrt[dt]

Table[X[[i, j]] = X[[i, j - 1]] +
r*RandomVariate[NormalDistribution[0, Sqrt[dt]]],
{i, nrows}, {j, 2, ncols}];


Note that dS is undefined

μ = dt/2 dS;
λ = (dt*sigma^2)/(2*sigma^2);
s = Range[0, 500, 5];
(*Stock price*)


Definition of V can be simplified to

V = Clip[s - k, {0, 500}];


Population of the sparse array is slow but does complete

(For[p = 1, p < nrows, p++,
For[q = 2, q <= ncols, q++,
btcs[p_, q_] =
SparseArray[{{m_, m_} ->
1 + 2*λ*Indexed[s, m]^2 + Indexed[X, {p, q}]*dt, {m_, l_} /;
l - m == 1 -> -λ*Indexed[s, m]^2 - μ*
Indexed[X, {p, q}]*Indexed[s, m], {m_, l_} /;
m - l == 1 -> -λ*Indexed[s, m]^2 + μ*
Indexed[X, {p, q}]*Indexed[s, m]}, {nrows + 1, nrows + 1}];
A = btcs[p_, q_]];];) // AbsoluteTiming

(* {286.008, Null} *)


Looking at the second row (Note again that dS was not defined):

Normal[A][[2]]

(* {-0.00125 + 4.60593*10^-6 dS, 1.0025, -0.00125 -
4.60593*10^-6 dS, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, \
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0} *)

% // Length

(* 101 *)
`