Menu

Derivative of a conditional expectation function obtained by simulation

Help
2016-12-05
2016-12-06
  • Jorge Sabat

    Jorge Sabat - 2016-12-05

    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.

    In adigator>filekeeping at 722
    In adigator at 104
    Warning: Error in initial test evaluation of user file on given input info
    In adigator at 148
    Error using randn
    Size inputs must be integers.

    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

     
  • Jorge Sabat

    Jorge Sabat - 2016-12-05

    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

     
  • Matthew J. Weinstein

    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:

    gx = adigatorCreateDerivInput([1 1],'x')
    gdW = adigatorCreateAuxInput([n N])
    k  = [K,n,r,sigma,T];
    adigator('BSsimulation',{gx,k,N,gdW},'myderiv')
    
    x.f = 80;
    x.dx = 1;
    y = myderiv(x,k,N,dW);
    
    dydx = y.dx;
    

    also, I think the mean function isn't coded up in adigator so you probably want to change that to sum()/length().

    Matt

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.