Skip to content

Instantly share code, notes, and snippets.

@netsatsawat
Created July 21, 2020 11:53
Show Gist options
  • Select an option

  • Save netsatsawat/4bd191ebc887cd057c9ed75e7a524428 to your computer and use it in GitHub Desktop.

Select an option

Save netsatsawat/4bd191ebc887cd057c9ed75e7a524428 to your computer and use it in GitHub Desktop.
Partial example of utilizing adaptive filter as real-time model prediction
s_future = ts['Close'].values.flatten()[1000: ]
filter_min_error = pa_list[np.argmin(error_list)]
print(f'Selected the filter with mu of {filter_min_error.mu}')
print(f'with avg eror of {error_list[np.argmin(error_list)]}')
x_future = pa.input_from_history(s_future, n)
d_future = np.zeros(len(x_future))
N_future = len(x_future)
for i, k in enumerate(range((n-1), N_future)):
d_future[i] = s_future[k+1]
actual_list = np.zeros(N_future)
predict_list = np.zeros(N_future)
# Simulate real-time prediction and update the filter
for k in range(N_future):
x_input = x_future[k]
y = filter_min_error.predict(x_input)
d_actual = d_future[k]
filter_min_error.adapt(d_actual, x_input)
actual_list[k] = d_actual
predict_list[k] = y
## show results
avg_error = np.round(np.mean(10*np.log10((actual_list -predict_list)**2)), 4)
plt.figure(figsize=(15,9))
plt.subplot(211)
plt.title("Adaptive Filter on future data (simulated)")
plt.xlabel("Samples [k]")
plt.plot(actual_list[n: (len(actual_list)-n)], "b", label="actual")
plt.plot(predict_list[n: (len(predict_list)-n)], "g", label="predict")
plt.legend()
plt.subplot(212)
plt.title(f'Filter error (avg: {avg_error})')
plt.xlabel("Samples [k]")
plt.plot(10*np.log10((actual_list[n: (len(actual_list)-n)] - \
predict_list[n: (len(predict_list)-n)])**2),
"r", label="Error")
plt.legend()
plt.tight_layout()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment