The goal is to discretize the following Matlab Code
function P1=StopLossPut(So, K, mu, sigma, r, T, Paths)
[NRepl, NSteps] = size(Paths);
NSteps = NSteps - 1;
Cost = zeros(NRepl, 1);
dt = T / NSteps;
DiscountFactors = exp(-r * (0:1:NSteps) * dt);
for k = 1:NRepl
CashFlows = zeros(NSteps + 1, 1);
if (Paths(k, 1) <= K)
Covered = 1;
CashFlows(1) = Paths(k, 1);
else
Covered = 0;
end
for t = 2:NSteps + 1
if (Covered == 0) && (Paths(k, t) <= K)
Covered = 1;
CashFlows(t) = Paths(k, t);
elseif (Covered == 1) && (Paths(k, t) > K)
Covered = 0;
CashFlows(t) = -Paths(k, t);
end
end
if Paths(k, NSteps + 1) <= K
CashFlows(NSteps + 1) = +CashFlows(NSteps + 1) - K;
end
Cost(k) = -dot(DiscountFactors, CashFlows);
end
P1 = mean(Cost);
end
Here is what I did
function P2=StopLossVPut(So, K, mu, sigma, r, T, Paths)
[NRepl, NSteps] = size(Paths);
NSteps = NSteps - 1;
Cost = zeros(NRepl, 1);
dt = T / NSteps;
CashFlows = zeros (NRepl, NSteps + 1);
DiscountFactors = exp(-r*(0:1:NSteps)*dt);
OldPrice = [zeros(NRepl,1), Paths(:,1:NSteps)];
UpTimes = find(OldPrice <= K & Paths > K);
DownTimes = find(OldPrice > K & Paths <= K);
CashFlows(UpTimes) = -Paths(UpTimes);
CashFlows(DownTimes) = Paths(DownTimes);
ExPaths = find(Paths(:, NSteps + 1) <= K);
CashFlows(ExPaths, NSteps + 1) = CashFlows(ExPaths, NSteps + 1) - K;
Cost = -CashFlows * DiscountFactors';
P2 = mean(Cost);
end
However, I am getting different results any guess where the mistake is?
I was expecting to receive the same results as the second code is the discretization of one another
and at the same time both codes should be close to the theoretical put price