Follow is the SABR function part of my code in python:
def SABR_func( K, alp, bet, rho, nu ):
"SABR"
f = mV0*np.exp((0.02 - q)*tau)
z = nu/alp*(f * K)**(0.5*(1 - bet)) * np.log(f / K)
xz = np.log((np.sqrt(1 - 2*rho*z + z*z) + z - rho) / (1 - rho))
zdivxz = z / xz
zdivxz[np.isnan(zdivxz)] = 1.0
result = ( alp*(f*K)**(0.5* (bet-1) )*
(1 + ( ((1 - bet)*np.log(f/K))**2/24 + ((1 - bet)*np.log(f/K))**4/1920 ))**(-1.0)
* zdivxz
* ( 1 + ( ((1 - bet)*alp )**2 / (24*(f*K)**(1 - bet))
+ 0.25*alp*bet*rho*nu / ((f*K)**(0.5*(1-bet)))
+ ((2-3*rho**2)*nu**2)/24)*tau )
)
return result
And I use the function to fit the data in every time slice:
popt, pcov = curve_fit(SABR_func, cp[0, :], cp[1, :], p0 = init_guess,maxfev=10000)
#init_guess = [alp,bet,rho, nu] I have write before this
print(popt)
y1[it, :] = SABR_func(stkSet, *popt)
where:
mV0 is the Synthetic futures prices for atm options
cp[0,:] is the strike of option
cp[1,:] is the price of option
But I found that the model fitting is very poor,and be like this gif(pink points are true data)
Thanks for jChoi's help, I find I have some wrong with the f's formula,but when I using the right foluma, the result is same. Is there any other wrong I don't find ?