Thursday, 5 December 2019

quantopian curve fit gaussian + polynomial

import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

def expfit(x, a, b, c, d, e, f, g):
    return  a*np.exp(-(b*x*x+c*x+d))+e*x*x+f*x+g

x = np.linspace(1,1+len(price),len(price))

params, params_covariance = curve_fit(expfit, x, price.values )

print params

y = expfit(x, *params)

f = pd.Series(y, index=price.index, name='exp fit')

pd.concat([price, f], axis=1).plot()

[  1.87976840e+03  -2.61667985e-06   6.40016840e-03   3.83225576e+00
  -1.06443204e-05   7.86290764e-02  -1.36885462e+01]

[  1.15483490e+01   1.10677384e-04  -1.16953358e-02  -1.34140290e+00
  -7.36267261e-06   3.04019894e-04   5.55989990e+01]

[  1.58855939e+02   9.57979344e-05  -1.48746514e-02   1.72812129e+00
  -4.63387042e-05   1.52436240e-01   3.93288443e+01]

def expfit(x, a, b, c, d, e, f, g, h):
    return  a*np.exp(-(b*x*x+c*x+d))+e*x*x+f*x+g+h*x*x*x

[  7.19650057e+02   2.88317903e-06   2.24186888e-03   7.27803145e-01
  -5.10066383e-04   8.24549494e-01  -2.58031805e+02   9.90353284e-08]

gaussian + 3rd order polynomial

[  9.86790120e-03   9.34621548e-05  -9.37662256e-03  -8.68029637e+00
  -5.06336033e-05   5.08207847e-02   4.03453234e+01   1.05790726e-08]


reference:
http://chuanshuoge2.blogspot.com/2019/12/quantopian-curve-fit-gaussian-linear.html

No comments:

Post a Comment