ad icon indicating copy to clipboard operation
ad copied to clipboard

Speed

Open joshimar12 opened this issue 8 years ago • 0 comments

I really like this package but unless I am misusing it I think it suffers terribly from lack of speed. I am currently trying to calculate the greeks of a portfolio of 4 call options by two methods, namely AD and finite differences. Even when using the gradient function from ad the finite differences method still outperforms the ad implementation in terms of speed by quite a bit. I am still relatively new to python, so I may just be implementing it in a way so that it is slower. I have attached my code below, any advice would be appreciated.

from ad import adnumber from ad.admath import * import numpy as np import time

time_start = time.clock() def AD_vega_singlesim(M,S,v,T,r,delta,K,S1,v1,T1,r1,K1,S2,v2,T2,r2,K2,S3,v3,T3,r3,K3): sensi1 = np.zeros(4) sensi = np.zeros(4) sensi2 = np.zeros(4) sensi3 = np.zeros(4) for j in range(1, int(M)): z = np.random.normal(0,1) price = ((S_exp((r - delta - .5_(v2))T + ( v * z_sqrt(T))) - K)* exp(-r_T))/M price1 = ((S1.exp((r1 - delta - .5*(v12))T1 + ( v1 * z_sqrt(T1))) - K1)* exp(-r1_T1))/M price2 = ((S2_exp((r2 - delta - .5(v2__2))T2 + ( v2 * z_sqrt(T2))) - K2) exp(-r2_T2))/M price3 = ((S3_exp((r3 - delta - .5_(v3__2))T3 + ( v3 * z_sqrt(T3))) - K3) exp(-r3*T3))/M

    if price>0:
        sensi += price.gradient((r,v,T,S))
    if price1>0:
        sensi1 +=  price1.gradient((r1,v1,T1,S1))
    if price2>0:
        sensi2 +=  price2.gradient((r2,v2,T2,S2))
    if price3>0:
        sensi3 +=  price3.gradient((r3,v3,T3,S3))
return sensi,sensi1,sensi2,sensi3

print AD_vega_singlesim(1000,adnumber(100),adnumber(.2),adnumber(1.0),adnumber(.05),.01,100.0,adnumber(110),adnumber(.25),adnumber(2),adnumber(.06),125,adnumber(125),adnumber(.31),adnumber(2.5),adnumber(.07),122, adnumber(150),adnumber(.13),adnumber(1.5),adnumber(.05),143) time_elapsed = (time.clock() - time_start) print time_elapsed

joshimar12 avatar Jul 05 '16 22:07 joshimar12