0

The following data was imported by left clicking the file on the folder pane to bring up the import window and imported as a cell array. Each column is going to be one of my variables (K = 1st column etc).

StrikePrice  UnderlyingPrice mT       Rf     DividendRate Volatility
47           45              4        0.02   0.5          0.2
50           55              20       0.03   0.1          0.35

And I am using a function first written by Mark Hoyle (2016) that prices American Calls

function LSMAmCallContDiv(S0,K,D,r,sigma,T,NSteps,NSims)

To fill in the first row of my data for this function;

function LSMAmCallContDiv(45, 47, 0.5, 0.02, 0.2, 4, 500, 100)

Is there anyway I can do this function without manually having to change the values for the second row in my cell array? (I'm dealing with a lot of rows in reality). This was something I achieved when pricing puts in RStudio with the following code however I am a complete beginner to MatLab.

jpmitmput30results = apply(jpmitmput30full, 1, function(x) AmerPutLSM(Spot = x['UnderlyingPrice'], sigma = x['Volatility'], 
                                                                  n=500, m=100, Strike = x['StrikePrice'], 
                                                                  r = x['Rf'], dr = x['DividendRate'], 
                                                                  mT = x['mT']))
5
  • Sorry as I said I'm new to matlab and am used to R terms. I imported the data ('test.csv') which produced a 303x6 cell in my workspace. What I called a data frame above is a small version of what can be found in that data Commented Jun 2, 2021 at 19:34
  • The data was imported by left clicking the file on the folder pane to bring up the import window and imported as a cell array. Yes each column is going to be one of my variables (K = 1st column etc). I thought that each column was a variable was clear from the example above but I guess not. Commented Jun 2, 2021 at 19:47
  • This function LSMAmCallContDiv(S0,K,D,r,sigma,T,NSteps,NSims) has no output arguments. What does it do? Does it write to the console? Or to a file? You'd have to adapt the function to output a variable, to make it useful. Commented Jun 2, 2021 at 19:55
  • I've found your function here: biopen.bi.no/bi-xmlui/bitstream/handle/11250/2578853/… It's modified from here: mathworks.com/matlabcentral/fileexchange/… -- Indeed, this function prints to the console. Commented Jun 2, 2021 at 19:58
  • Yes it output's both 'Price' and 'StdErr' to the command window. Commented Jun 2, 2021 at 19:59

2 Answers 2

1

Given you have a cell array, I presume it looks like this:

data = {47   45    4   0.02   0.5   0.2
        50   55   20   0.03   0.1   0.35};

To get one value out, you can index as data{row,column}, for example data{1,3} returns 4.

Now all you need is a loop to repeatedly call your function with the right value in the right order:

for ii=1:size(data,1)
   LSMAmCallContDiv(data{ii,2},data{ii,1},data{ii,5},data{ii,4},data{ii,6},data{ii,3},500,100)
end

Since the function has no output arguments, we cannot collect its results in an array. You will have to copy-paste them from the terminal window. If you decide to modify the function to return values, then you can collect them. First modify the first line of the function to read:

function [Price,StdErr] = LSMAmCallContDiv(S0,K,D,r,sigma,T,NSteps,NSims)

and then in your own code:

Price = zeros(size(data,1),1);
StdErr = zeros(size(data,1),1);
for ii=1:size(data,1)
   [Price(ii),StdErr(ii)] = LSMAmCallContDiv(data{ii,2},data{ii,1},data{ii,5},data{ii,4},data{ii,6},data{ii,3},500,100)
end
Sign up to request clarification or add additional context in comments.

Comments

0

I'm not sure about that function in particular, but most functions can take vectorized input, it's a really useful feature. That is to say, where functions in other languages take single value inputs, matlab thinks of everything as arrays automatically, so you can pass vectors to functions instead, and it calls the function on each row in the input.

For instance,

frame = [47, 45, 4, 0.02, 0.5, 0.2; 50, 55, 20, 0.03, 0.1, 0.35];
out = LSMAmCallContDiv(frame(:,2), frame(:,1), frame(:,5), frame(:,4), frame(:,6), frame(:,3), 500, 100);

should give you a column vector with all the outputs you want. To be clear, the : in (:,2) refers to every row, and the 2 refers to the second column. So, frame(:,2) refers to the entire second column.

Now, this might not be the case for this function, it might not be able to take vectorized input, (I cannot find any documentation on it,) in which case, you might have to take a more programmatical approach.

i = 1;
while i <= height(frame)
    out(i) = LSMAmCallContDiv(frame(i,2), frame(i,1), frame(i,5), frame(i,4), frame(i,6), frame(i,3), 500, 100);
    i = i+1;
end

This is a more standard approach that one might see in any other language, (albeit less efficient in matlab), but it is certainly a good way to do it.

2 Comments

Hi Logan, thanks for your time trying to help. I have tried both of these methods and get the same error message "Too many output arguments." As I've said I'm new to matlab and not sure why I'd be getting this error. I have verified the function works by passing LSMAmCallContDiv(130,115,2,2,.2,3/365,500,100) and getting a result.
for is much more convenient here than while: for i=1:size(frame,1). (BTW: height is a method for the table class, and is not defined for your example frame array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.