def calc_ema_imacd_spectra_mse(Neff_imacd, Neff_ema, Nwindow): "Mean-Squared Error\ 1) generates h_ema and h_imacd given the input parameters;\ 2) takes the amplitude of the FFT for each response\ 3) computes the cumulative sum of each gain spectrum\ 4) computes and retuns the MSE of the two cumulative gain spectra" h_ema = ema(Neff_ema, Nwindow) h_imacd = int_macd_poly(Neff_imacd, Nwindow) h_ema_fft = np.fft.fft(h_ema) h_imacd_fft = np.fft.fft(h_imacd) h_ema_fft_abs = np.absolute(h_ema_fft) h_imacd_fft_abs = np.absolute(h_imacd_fft) h_ema_fft_abs_sum = np.cumsum(h_ema_fft_abs) h_imacd_fft_abs_sum = np.cumsum(h_imacd_fft_abs) mse = np.sum(np.square(h_ema_fft_abs_sum - h_imacd_fft_abs_sum)) / Nwindow return mse
from scipy import optimize from ema import ema from int_macd_poly import int_macd_poly from calc_ema_imacd_spectra_mse import calc_ema_imacd_spectra_mse Nwindow = 1024 Neff_ema = 16 Neff_imacd = 16 f, axarr = plt.subplots(2, 2) # a) Indicative responses h_ema = ema(Neff_ema, Nwindow) h_imacd = int_macd_poly(Neff_imacd, Nwindow) axarr[0, 0].plot(h_ema[:150]) axarr[0, 0].plot(h_imacd[:150]) axarr[0, 0].set_title('Ema and integrated macd poly.') # b) Spectra h_ema_fft = np.fft.fft(h_ema) h_imacd_fft = np.fft.fft(h_imacd) h_ema_fft_abs = np.absolute(h_ema_fft) h_imacd_fft_abs = np.absolute(h_imacd_fft) axarr[0, 1].plot(h_ema_fft_abs) axarr[0, 1].plot(h_imacd_fft_abs, 'r--')
y_fde = apply_ema_poly1_filter(prices, Neff) h_ema_poly1 = ema_poly1(Neff, Nwindow) candidate = np.convolve(prices - prices[0], h_ema_poly1) + prices[0] y_convolution = candidate[:len(prices)] axarr[1, 1].set_title("Ema poly1") axarr[1, 1].plot(y_fde) axarr[1, 1].plot(y_convolution, 'o', markerfacecolor='none') # e) Integrated Macd-Poly Neff = 32 lag = 3 y_fde = apply_integrated_macd_poly_filter(prices, Neff) h_int_macd_poly = int_macd_poly(Neff, Nwindow) candidate = np.convolve(prices - prices[0], h_int_macd_poly) + prices[0] y_convolution = candidate[:len(prices)] axarr[2, 0].set_title("Integrated macd poly") axarr[2, 0].plot(y_fde) axarr[2, 0].plot(y_convolution, 'o', markerfacecolor='none') # f) Macd Neff_pos = 16 Neff_neg = 32 lag = 2 y_fde = apply_macd_filter(prices, Neff_pos, Neff_neg) h_macd = macd(Neff_pos, Neff_neg, Nwindow)
axarr[1, 1].set_title("Ema poly1") axarr[1, 1].plot(impulse_response_fde) axarr[1, 1].plot(impulse_response_direct, 'o', markerfacecolor='none') # e) Integrated Macd-Poly Neff = 32 Neff_modified = 32 lag = 3 impulse = np.zeros(Nwindow) impulse[lag] = 1 candidate = apply_integrated_macd_poly_filter(impulse, Neff) impulse_response_fde = candidate[lag:] impulse_response_direct = int_macd_poly(Neff_modified, Nwindow) axarr[2, 0].set_title("Integrated Macd-Poly") axarr[2, 0].plot(impulse_response_fde) axarr[2, 0].plot(impulse_response_direct, 'o', markerfacecolor='none') # f) Macd Neff_pos = 16 Neff_neg = 32 lag = 2 impulse = np.zeros(Nwindow) impulse[lag] = 1 candidate = apply_macd_filter(impulse, Neff_pos, Neff_neg)