MATLAB Answers

Locks
0

using vectors as inputs for a function

Asked by Locks
on 9 May 2013
Hi,
I have this function here:
function HestonCallPriceF
x0 = [1.1768 0.082259 0.83746 -0.54589]; %parameters: psi m xi rho
PC = 1; F = 100; K = 100; T = 0.2; r = 0.04; V0 = 0.02;
CallPriceF(PC, F, K, T, r, V0, x0)
%Delta
epsD = abs(F)*eps^(1/6);
Delta=(CallPriceF(PC, F+epsD, K, T, r, V0, x0)-CallPriceF(PC, F-epsD, K, T, r, V0, x0))/(2*epsD)
%Vega
epsV = abs(V0)*eps^(1/6);
Vega=(CallPriceF(PC, F, K, T, r, V0+epsV, x0)-CallPriceF(PC, F-epsD, K, T, r, V0-epsV, x0))/(2*epsV)
Which is working fine for those inputs:
PC = 1; F = 100; K = 100; T = 0.2; r = 0.04; V0 = 0.02;
Which I have used above. Indest of having this inputs, I would like the function to compute me the delta and vega for each row of a matrix and therefore I tried to replace those iputs:
PC = 1; F = 100; K = 100; T = 0.2; r = 0.04; V0 = 0.02;
with this here:
PC=data9(1,3); F=data9(1,1); K=data9(1,2); T=data9(1,5); r=data9(1,7); V0=(data9(1,8)/100)^2;
For some reason this is not working.
I would like to store the deltas and vegas in two additional vectors which I then add to the existing matrix called data9
is there a way to do this or where is the mistake I am making?

  1 Comment

why not have a loop that goes through each element of your input vectors one at a time and calculates the delta vega with the output then being placed in an output vector?

Sign in to comment.

1 Answer

Answer by Matt Tearle
on 9 May 2013
Edited by Matt Tearle
on 9 May 2013
 Accepted Answer

"this is not working" What is the error message you're getting?
Is there any reason to have HestonCallPriceF be a function? It has no inputs or outputs. Why not just comment out the function declaration line and run it as a script? Then you can at least see what's going on a bit easier, without having to go fully into debug mode.
In fact, the problem may well be that it's a function -- I don't see anywhere that data9 is defined within the function. If data9 is a variable in your base workspace and you're expecting it to be available within HestonCallPriceF, then that's your problem right there.

  3 Comments

I guess the problem is excatly what you've mentionned in the last sentence. data9 is a matrix in my workspace and I woudl like to run the function based on that.
I tried to amend the function to get it running, but this here is not working as well:
function data10=HestonCallPriceF(data9)
data10= zeros(size(data9,1),10);
data10(:,1:9)=data9;
The error message I get is the following:
Error using HestonCallPriceF (line 4)
Not enough input arguments.
Line 4 is the following:
data10= zeros(size(data9,1),10);
%x0 = [1.1768 0.082259 0.83746 -0.54589]; %parameters: psi m xi rho
%PC = 1; F = 100; K = 100; T = 0.2; r = 0.04; V0 = 0.02;
%PC = data9(1,3); F = 100; K = 100; T = 0.2; r = 0.04; V0 = 0.02;
PC=data10(:,3); F=data10(:,1); K=data10(:,2);T=data10(:,5);r=data10(:,7);V0=data10(:,8);
%PC=data9(1,3); F=data9(1,1); K=data9(1,2); T=data9(1,5); r=data9(1,7); V0=(data9(1,8)/100)^2;
CallPriceF(PC, F, K, T, r, V0, x0)
%Delta
epsD = abs(F)*eps^(1/6);
Delta=(CallPriceF(PC, F+epsD, K, T, r, V0, x0)-CallPriceF(PC, F-epsD, K, T, r, V0, x0))/(2*epsD)
%Vega
epsV = abs(V0)*eps^(1/6);
Vega=(CallPriceF(PC, F, K, T, r, V0+epsV, x0)-CallPriceF(PC, F-epsD, K, T, r, V0-epsV, x0))/(2*epsV)
data10(:,10)=Delta
It sounds like you're calling HestonCallPriceF without any inputs. (MATLAB doesn't care about this until you try to use an input that you haven't provided. In this case, that happens on line 4, where you reference size(data9,1).) If you have data9 in the base workspace, you need to call HestonCallPriceF with that as an input explicitly:
>> HestonCallPriceF(data9)
it's working now, thanks for your help!!

Sign in to comment.