params['t_date'] = t_date sql = SQL.get_expiration_dates_and_vols % params data = execute_query_DF(conn, sql) if data.numrows() < 4: continue print t_date expiration_dates = np.array([int(i) for i in data('expiration_date')]) days = (expiration_dates - int(t_date)) / seconds_per_day times_to_expiration = days / 365. vol = data('vol') from alpha import alpha_theo_vol lb = [.3, .2, 1] ub = [5, 3, 10] coeffs = nerdy.wls_fit(alpha_theo_vol,[1,.7,5], times_to_expiration, vol, lb=lb,ub=ub) alpha_vols = alpha_theo_vol(coeffs, alpha_days/365.) if GRAPH is True: sleep(.1) pyplot.title(epoch2datetime(int(t_date)).date()) term_vols.set_xdata(days) term_vols.set_ydata(vol) alpha_curve.set_xdata(alpha_days) alpha_curve.set_ydata(alpha_vols) pyplot.draw() if DEBUG is False: columns = ('symbol', 't_date', 'pivot_vol', 'mean_vol', 'alpha', 'expirations') results = (symbol, int(t_date),
fit = np.zeros(z.shape) for c in range(0,degree+1): fit = fit + x[c] * (z **c) error_unweighted = np.sum((delta_wt_avg_implied_vols-fit) ** 2) error = np.sum(weights*(delta_wt_avg_implied_vols -fit)**4) return error import openopt nlp = openopt.NLP(penalty, np.zeros(4)) r=nlp.solve('ralg') coeffs2=r.xf coeffs2=list(coeffs2) print strikes def three(c, x): return c[3] * x**3 + c[2] * x**2 + c[1] * x + c[0] weights3 = np.exp(-abs(z ** 2)) coeffs3 = nerdy.wls_fit(three, [0]*4, z, delta_wt_avg_implied_vols, weights=weights3) coeffs3 = list(coeffs3) coeffs2.reverse(); coeffs3.reverse() for coeff_list in [coeffs, coeffs2, coeffs3]: while not len(coeff_list) >= 5: coeff_list.insert(0,0) except Exception, e: fail += 1 print "Exception:", e ipshell("WTF") raise Exception, e else: if DEBUG is True: print print "z:" print z