Derivative of a conditional expectation function obtained by simulation
A MATLAB Automatic Differentiation Tool
Brought to you by:
anilvrao2,
weinstein87
Hi I am trying to use ADiGator to calculate the derivative of a conditional expectation obtained by Montecarlo Simulation. I have the following function:
function y = BSsimulation(x,k,N)
h=1/k(2);
dW=randn(k(2),N);
S=xones(1,N);
S(2:k(2)+1,1:N)=zeros(k(2),N);
for i=1:k(2)
S(i+1,:)=S(i,:).(1+k(3)h+sqrt(h)k(4)dW(i,:));
end
y= mean(exp(-k(3)k(5))*max(0,S(k(2)+1,:)-k(1)));
end
I am trying to run the program as follows:
x = adigatorCreateDerivInput([1 1],'x')
k = adigatorCreateAuxInput([1 5],[K,n,r,sigma,T])
adigator('BSsimulation',{x,k,N},'myderiv')
x =80
y = myderiv(x,k,N)
I see this error after (adigator(.)):
Warning: Removed 'C:\Users\jorge.sabat\Documents\MATLAB\adigator\adigatorTempDir' from the MATLAB path for this MATLAB session.
See 'doc path' for more information.
Error in BSsimulation (line 15)
dW=randn(k(2),N);
Error in adigator (line 146)
eval(TestEvalStr);
If you have any suggestion would be incredible usefull for my research.
Best regards,
Jorge
Sorry, just a little thing. For some reason the function above missed the multiplication signs (*).
function y = BSsimulation(x,k,N)
h=1/k(2);
dW=randn(k(2),N);
S=xones(1,N);
S(2:k(2)+1,1:N)=zeros(k(2),N);
for i=1:k(2)
S(i+1,:)=S(i,:).(1+k(3)h+sqrt(h)k(4)dW(i,:));
end
y= mean(exp(-k(3)k(5))*max(0,S(k(2)+1,:)-k(1)));
end
Jorge,
Three "~" in a row will let you display code more cleanly here.
As for your problem, you should make "dw" an auxiliary input to the function - randn() is not overloaded and will cause issue otherwise.
Additionally, it seems that it isn't treating "known" auxiliary inputs exactly correct, assuming "k" input is fixed, you can just use:
also, I think the mean function isn't coded up in adigator so you probably want to change that to sum()/length().
Matt