Serving the Quantitative Finance Community

 
User avatar
crisky
Topic Author
Posts: 0
Joined: May 24th, 2003, 9:21 pm

Bond Pricing: Matlab Vs Excel speed

September 22nd, 2005, 8:55 am

Hi,just wondering if you had similar experiences with Matlab's Fixed Income Toolbox + Excel, any suggestions would be appreciated:I recently re-wrote some VBA functions as Matlab m-files, replacing VBA's PRICE() function with Matlab's equivalent function 'bndprice'.I then compiled the m-file functions into a Type Library .dll using Excel Builder and sourced the .dll from Excel.As expected, the .dll functions called from Excel were faster than the original VBA functions, where the functions involved matrix operations and did not involve the bond pricing function.But the new functions that relied on 'bndprice' were a lot slower than the original VBA functions that relied on PRICE().To check that bndprice was in fact a lot slower than PRICE() I did this:1. A test m-file called bndprice 1000 times (using same inputs) and returned the time taken to complete: 25 sec.2. A VBA macro called ActiveSheet.Calculate 1000 times on a sheet that has only one PRICE() function with fixed inputs: 3 sec(This is without setting Application.Screenupdating=False which speeds VBA up).So on an Excel sheet with lots of calculations such as DV01's, it seems best to stick to a bond pricing function other than the Matlab one!Ideally the .dll would be making use of PRICE() but this doesn't seem possible for a matlab-based library...Thanks for taking a look.
 
User avatar
monarc
Posts: 0
Joined: June 21st, 2004, 2:35 pm

Bond Pricing: Matlab Vs Excel speed

September 22nd, 2005, 1:24 pm

Did u try this:Instead of compiling into a dll and sourcing it from excel, did you directly run it from within excel...I mean by invoking the matlab command window....This way the calculation will run in the matlab command window and the output will be given to the excel after the calculation are over.....Just a thought if I understand your question correctly. I will try doing this at home if I have time later,,,,...
 
User avatar
crisky
Topic Author
Posts: 0
Joined: May 24th, 2003, 9:21 pm

Bond Pricing: Matlab Vs Excel speed

September 23rd, 2005, 12:25 am

Hi monarc-I had a similar thought- that the speed was getting lost somewhere in the function call, rather than bndprice itself being slow.So when writing the test m-file that did 1000x calcs of the bndprice, I did it in Matlab's command window without involving Excel.But the run took 25 sec Vs Excel's 3 sec.
 
User avatar
monarc
Posts: 0
Joined: June 21st, 2004, 2:35 pm

Bond Pricing: Matlab Vs Excel speed

September 23rd, 2005, 1:25 pm

crisky:Matlab was never meant to be a tool for speeding up calculations..I think if you are looking in that direction then c++ may be good....But anyways did you try converting the m file into a mex or dll file and calling it from inside VBA code of excel?? How much was the time taken then?? I am sure it wont be 25 sec...Moreover, make sure that you are NOT running any other applications on the computer...it does depend on the CPU power also.
 
User avatar
esty
Posts: 0
Joined: May 20th, 2004, 7:07 pm

Bond Pricing: Matlab Vs Excel speed

September 23rd, 2005, 10:27 pm

Also, I don't mean to insult, but is your matlab code fully vectorized? Iterative matlab code is dog slow.
 
User avatar
crisky
Topic Author
Posts: 0
Joined: May 24th, 2003, 9:21 pm

Bond Pricing: Matlab Vs Excel speed

September 25th, 2005, 2:33 am

QuoteOriginally posted by: monarccrisky:Matlab was never meant to be a tool for speeding up calculations..I think if you are looking in that direction then c++ may be good....But anyways did you try converting the m file into a mex or dll file and calling it from inside VBA code of excel?? How much was the time taken then?? I am sure it wont be 25 sec...Moreover, make sure that you are NOT running any other applications on the computer...it does depend on the CPU power also.Initially I converted the m-files into a .dll and called the function 1000 times from Excel, independently of matlab.This took about 25 sec, the same as calling it 1000 times in a matlab command window- so it seems that the inner workings of the bndprice() are slow compared to PRICE(), regardless of whether bndprice() is used through a complied .dll in Excel or in Matlab directly.
 
User avatar
crisky
Topic Author
Posts: 0
Joined: May 24th, 2003, 9:21 pm

Bond Pricing: Matlab Vs Excel speed

October 21st, 2005, 2:35 pm

I'll replicate Excel's PRICE() function in an m-file, then use it instead of Matlab's bndprice() function for the .dll. It's possible that bndprice() is slower because its designed to price a wider range of bond types.Would anyone know where to find the source code of Excel's PRICE() formula/ if it is available?Thanks!
 
User avatar
Boofta
Posts: 0
Joined: July 14th, 2002, 3:00 am

Bond Pricing: Matlab Vs Excel speed

October 22nd, 2005, 12:59 am

QuoteOriginally posted by: crisky2. A VBA macro called ActiveSheet.Calculate 1000 times on a sheet that has only one PRICE() function with fixed inputs: 3 sec(This is without setting Application.Screenupdating=False which speeds VBA up).Are you sure this is actually calling PRICE() 1000 times? If the inputs are static throughout the test, Excel's dependency tree can just assume that nothing has changed, and then not bother with calling =PRICE(), so in effect you are timing Activesheet.Calculate only.2 ways to verify:1) In the cell that has the =PRICE() formula, do Cell.Formula = Cell.Formula. This will ensure the formula is evaluated (like pressing F2)2) randomise one of the arguments in your test.
 
User avatar
crisky
Topic Author
Posts: 0
Joined: May 24th, 2003, 9:21 pm

Bond Pricing: Matlab Vs Excel speed

October 22nd, 2005, 5:44 am

that's a good point- i just checked using a randomized input but there was no change in speed, so Excel was calculating 1000x. (i initially noticed the speed difference between the excel and matlab-based functions when calculating a sheet full of bonds, where there were a couple hundred calcs).
Last edited by crisky on October 21st, 2005, 10:00 pm, edited 1 time in total.