dimensions - Fix matlab code with error -
i understand there error dimensions in line dr=(r-v*v/2)*dt . have little knowledge of matlab. fix it, please. code small , simple. maybe find time look
function [optionprice] = upandoutcalloption(s,r,v,x,b,t,dt) t = 0; dr=[]; pert=[]; while (t < t) & (s < b) t = t + dt; dr = (r - v.*v./2).*dt; pert = v.*sqrt( dt ).*randn(); s = s.*exp(dr + pert); end if s<b % within barrier, price european option. optionprice = exp(-r.*t).* max(0, s - x); else % hit barrier, option withdrawn. optionprice = 0; end end call function of kind: k=1:amountofoptions [optionprices(k)] = upandoutcalloption(stockprice(k)*o,riskfreerate(k)*o,... volatility(k)*o, strike(k)*o, barrier(k)*o, timetoexpiry(k)*o, samplerate(k)*o); result(k) = mean(optionprices(k)); end
therefore, difficulties.
it's know problem within dr = (r - v.*v./2).*dt;. command has many possible problems related dimensions:
here doing element-wise multiplication (because of
.*) matrices, requires (in case of command)rhas same number of rows , columnsv(since because of element-wise,v.*v/2has same sizev).moreover, unnecessary element-wise division scalar number, means there no need have
./2in matlab.and, finally, since it's element-wise multiplication again, matrix
(r - v.*v./2)must have same number of rows , columns matrixdt.
check here more information matlab's matrix operations.
Comments
Post a Comment