From echo cancellation to price prediction
Echo cancellation is a technique to remove acoustic echo from audio signal. The procedure is generally applicable to any signal, for instance price time-series of an asset. This post proposes that the price of an asset at any moment can be seen as the echo of its prices at some previous moments. It then explains how price prediction problem can be formulated and solved using echo cancellation.
System Identification
System identification is the process of identifying an unknown system through observing its input and its output. The unknown system in this context is assumed to be linear time-invariant and is specified by a finite impulse response (FIR). It is here used to predict an asset price by looking into its price history. The FIR takes the price history as the input and generates an estimation future price. The block diagram of the price predictor is depicted below. The system is identified through an adaptive process: it starts with an initial FIR and adapts it every time a new price tick comes.
Figure 1: System Identification
In each adaptive process the FIR is updated as little as possible such
that if the price history is applied to the new FIR, it still
generates the exact price. Let
is a vector containing
such that it is as close as possible to the previous FIR,
i.e.
subject to
Using the method of Lagrange multiplier, it can be shown that
where
This adaptive approach can be used to predict the price of an
asset. The quality of prediction can be assessed by its convergence
measured by
Relation to echo cancellation
Echo cancellation is the process of removing echo in a signal, for
example removing acoustic echo in a telephone call. A popular
technique to cancel acoustic echo is to identify echo via system
identification, the same approach which is used in this post for price
prediction. To turn our price predictor to an echo canceler, it is
only enough to fill
Normalized least mean square
A price predictor, an echo canceler, or any other unknown system can
be identified by normalized least mean square (NLMS) method. The
procedure follows the computation given by
Bigger step size causes
Python implementation
The implementation of NLMS algorithm on price data is
straightforward. It is enough to update the impulse response
iteratively according to
is the length of price history is the step size is the regularization parameter
These parameters are passed while constructing an object of
system_identifier
. This class provides system identification
functionality.
import numpy as np | |
class price_predictor: | |
def __init__(self, filter_len, alpha, delta): | |
if type(filter_len) is not int or filter_len < 1: | |
ValueError('Filter length must be a positive integer') | |
if type(alpha) is not np.float64 or alpha <= 0 or alpha >= 2: | |
ValueError('Alpha (step size) must lie in (0,2)') | |
if type(delta) is not np.float64 or delta <= 0: | |
ValueError('Delta (regularization parameter) must lie in (0,2)') | |
self._h = np.zeros(filter_len) # impulse response | |
self._p = np.zeros(filter_len) # price vector | |
self._alpha = alpha | |
self._delta = delta | |
def get_filter(self): | |
"""return filter coefficients""" | |
return self._h | |
def get_price_vec(self): | |
"""returns input vector""" | |
return self._p | |
def update_filter(self, new_price: np.float64): | |
"""update filter coefficients with new price xx and returns the | |
current estimation | |
""" | |
l = len(self._h) | |
p = self._p | |
h = self._h | |
a = self._alpha | |
d = self._delta | |
# update the tap weights | |
predicted_price = p@h | |
e = new_price - predicted_price | |
diff = a * e * p / (p@p + d) | |
h += diff | |
# store the new signal vaue | |
p[1:l] = p[0:l-1] | |
p[0] = new_price | |
return predicted_price, diff@diff |
In this class, update_filter
takes the new price value, computes the
error and updates the FIR via
This is an indicator of convergence and together with the prediction error can be used to estimate the quantity of prediction.
Figure 2: Prediction Result
To evaluate the algorithm, I use it to predict minutely price of
bitcoin from 01.09.2021 to 12.09.2021. After running the algorithm, it
takes some time for it to converge. Convergence means the estimated
FIR does not fluctuate or change rapidly and thereby is stable for
price prediction. The top diagram shows the convergence that is
estimated by computing the distance between consecutive FIR as
specified by
Conclusion
This post is proposed an algorithm for asset price prediction based on system identification problem. The algorithm adapts continuously with price change and provides indicators for prediction error and convergence. The indicators jointly can demonstrate how precise the price estimation for the asset is.