Thursday 5 December 2019

quantopian curve fit gaussian

start = '2008-01-01'
end = '2018-01-01'
price = get_pricing('XOM', fields='price', start_date=start, end_date=end)
price.plot()
import numpy as np
import matplotlib.pyplot as plt
from scipy.optimize import curve_fit
import pandas as pd

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

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()

[ -4.26776636e-01   3.41090632e-06  -3.42580712e-03  -3.33515628e+00
   8.08866791e+01   1.00000000e+00]
reference:
https://en.wikipedia.org/wiki/Gaussian_function
http://chuanshuoge2.blogspot.com/2019/12/python-curve-fitting.html
http://chuanshuoge2.blogspot.com/2019/12/quantopian-curve-fitting-log.html

No comments:

Post a Comment