How to implement Stopping rule and other formulas

6 views (last 30 days)
Hi All,
I'm struggling with the following, I have my code and I need to take the number that is closer than zero for every column of the matrix, I tried implementing a if argument buy it didnt work, additionally I should a a stopping rule which I'm don't really seem to understand so far how it is supposed to be. My code is the following:
Xp = Dp(:,1); % Strike price put
Vp = Dp(:,2); % Daily settlement price put
F = 9866; % Futures Dec14 price
S = 9833.07; % underlying closing price
T = 0.5; % Time to maturity to Dec14
r = log(F./S)./T; % interest rate calculated using the futures price
q = 0; % No divided yield
t = 0; % Initial day
flag=0; % To calculate for a put
sg = [0.001:0.001:1]'; % Volatility range
ImpVol =zeros(length(sg),length(Vp));
for i =1:length(sg)
for j =1:length(Vp)
ImpVol(i,j) = BSM(S,Xp(j),r,q,sg(i),t,T,flag)- Vp(j);
end
end
So I need to take the closest number to 0 for every column in ImpVol and add a stopping rule, I suppose in case the interval is too long and the jump from step to step is too small.
I would really appreciate your help!!!
Regards,
Laura R
  7 Comments
Pierre Benoit
Pierre Benoit on 11 Sep 2014
Oops, my bad.
You could use another variable to store all positive values of ImpVol and NaN otherwise
dummy_ImpVol = Imp_Vol;
dummy_ImpVol(Imp_Vol<=0) = NaN;
[smallVal,indexVal] = min(dummy_ImpVol);
For your stopping rule, you could use the function error with a if.
Geoff Hayes
Geoff Hayes on 11 Sep 2014
@Laura - I typically use a stopping rule whenever I have a while loop and have some condition that may always evaluate true so I just add a second condition to ensure that the maximum number of iterations has not yet been reached
maxNumIters = 100;
atIter = 1;
someCondition = true;
while someCondition && atIter<=maxNumIters
% increment to the next iteration
atIter = atIter + 1;
% do stuff
% re-evaluate someCondition
end
if atIter>maxNumIters
% maximum number of iterations has been surpassed so write error
error('maximum number of iterations in my function have been met');
end
I'm not sure how this could be migrated to your code where you have two for loops, unless there is some code that you have not shown?

Sign in to comment.

Answers (0)

Categories

Find more on Creating and Concatenating Matrices 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!