for loop is endless
5 views (last 30 days)
Show older comments
Hello all,
I am trying to create a for loop for 10,000 iterations. I start by creating an empty vector which I then plug in into my for loop. The only problem is my for loop is not stopping and I have to force stop the script to run. Secondly, it seems that my forloop is just overwriting each iteration's computation and not appending it to the next row.
Below is an extract of my code
ST1 = 1:1:10000
%ST1 = zeros()
%Step5: Declaring empty ST2 row vector matrix for 20,000 simulations
ST2 = zeros(1,20000);
%Step5: Simulation of n = 10000 neutral paths
for n = 1:10000
ST1 = sadj*exp(r-0.5*(vol^2))*T+vol*sqrt(T)*randn(n);
end
%Step6: Compute the discounted cash flow for each path
CST1 = [1:10000]
for n = 1:10000
CST1 = exp(-r*T)*max(ST1(n)-K)
end
2 Comments
Accepted Answer
Jan
on 22 May 2022
ST1 = zeros(1, 10000); % Pre-allocate
for k = 1:10000
ST1(k) = sadj * exp(r-0.5*(vol^2)) * T + vol * sqrt(T) * randn(k);
% ^^^
end
You need to use and index, if you do not want to overwrite ST1 in each iteration.
Pre-allocation improves the processing speed, because a growing array is very expensive: In each iteration a new array must be created and the contents of the former array is copied.
The most expenvie parts of the calculations are the same in each iteration. so move it before the loop:
ST1 = zeros(1, 10000); % Pre-allocate
C = sadj * exp(r-0.5*(vol^2)) * T + vol * sqrt(T); % Do it once only
for k = 1:10000
ST1(k) = C * randn(k);
end
5 Comments
Jan
on 23 May 2022
tic
dr = 0.02; % I've insterted semicolons after each line...
vol = 0.3;
S = 160;
r = 0.04;
T = 3;
%Step1: Compute K
K = 1.1 * (S-(dr*S)*exp(-r)); % exp(-r*1) -> exp(-r)
%Step2: Compute Dividend Value
dv = dr * S;
%Step3: Compute adjusted S
sadj = S - dv*exp(-r*T);
% Why: ST1 = 1:1:10000 ???
ST1 = zeros(1, 10000); % Proper pre-allocation
ST2 = zeros(1, 20000);
%Step5: Simulation of n = 10000 neutral paths
C = sadj*exp(r-0.5*(vol^2))*T+vol*sqrt(T); % Expensive calculation out of the loop!
for kk = 1:10000
ST1(kk) = C * randn(kk);
% Failing! ^^^^^^^^^ This is a [kk x kk] matrix.
% What do you want to achieve? Maybe:
% ST1(kk) = C + randn;
% ST1(kk) = C + randn * kk;
% ???
end
%Step6: Compute the discounted cash flow for each path
% Why CST1 = [1:10000] ???
CST = zeros(1, 10000);
C2 = exp(-r*T); % Expensive calculation out of the loop!
for kk = 1:10000
CST1(kk) = C2 * max(ST1(kk)-K);
% But what does this mean: max(ST1(kk)-K). The argument of max() is a
% scalar, so the output of max() equals the argument. Strange.
end
%Estimation of the expected value and variance
a = sum(ST1);
CST1_BAR = a/10000;
CST1_VAR = var(CST1);
toc
Your original code did not run "forever", but it was very slow only:
for n = 1:10000
ST1 = randn(n);
end
Remember that randn(n) is a [n x n] matrix. So if running from 1 to 1e4 you create sum((1:1e4).^2) random numbers. This is 333.4e9 values, which use 2.6 Terabyte of RAM. Although the output is overwritten in each iteration, this takes a lot of time.
You can use the debugger to examine, what's going on. Set a breakpoint in the code and run it line by line. Observe the values of the currently changed variables either in the command window or in the workspace browser. Then you will see immediately, that this randn(n) does not do, what you expect.
Neither this randn(n) nor the max(ST1(n)-K) in the 2nd loop seems to be correct.
By the way, "n" is an unusual idea for a loop index. Therefore I've changed it to k, later to kk to avoid a confusion with the variable K. In C the index "i" is the standard choice, but this is nit recommended in Matlab to avoid confusions with the imaginary unit 1i.
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!