Kalman Filtering for Stocks Price Prediction and Control

: Stocks price analysis has been a critical area of research as the stock market is a very fluctuating market. Stocks price is affected by demand and supply dynamics making it difficult to forecast the price of a stock at a particular instant. The entire idea of predicting stocks price is to gain significant profits but predicting how the stock market will perform is a difficult task to carry out. In an attempt to do this, we construct a dynamical system for the stock’s price and simulate it using the Kalman filter. The dynamic tracking features of the filter here enable us to track the price of the Boeing stock. The stock price variation appears to be a maneuvering system from which we derive the state space model. Further, the robustness of the model is investigated by examining observability and controllability in the state space and proving that the system can be stabilized through state feedback. Finally, the forecasting result of 252 stock closing prices from January 01, 2021, to January 01, 2022, is provided by Kalman predictor and Python simulation. The evaluation of the prediction is done using absolute and relative error which gives relatively small values and thus makes the filter accurate for prediction.


Introduction
A number of papers on the use of the Kalman filter have been published in recent years contributing many variations and solutions to specific problems especially related to robotic systems.This filter has many applications for example in the military, biomedical and automotive industry.Therefore, it is not limited only to the field of engineering but also works in computational finance (Urrea and Agramonte, 2021).
Recently, financial time series has become an important topic in quantitative finance.Accurate analysis and forecasting of a range of financial changes can provide operators and investors with reliable management and decision-making (Sezer et al., 2020).It consists of a collection of values known as a time series.Research and analysis of these categories will help investors make appropriate investment decisions and a suitable method should be found to predict and control the stock price.Filtering is an iterative process that helps infer a model's parameter when the latter relies upon a large quantity of observable and unobservable data (Javaheri et al., 2003).The problem of estimating these unobserved latent variables from observed market data often arises in mathematical finance (Date and Ponomareva, 2011).In the year 1960, American researcher Rudolf Kalman and Ruslan Bucy introduced the concept of state variables and state space of dynamical systems Meinhold and Singpurwalla (1983).At that time they proposed the state space method and developed the concept of the Kalman filter, and for decades its generalizations have been a key tool in econometrics and engineering for estimating unobserved variables from observed variables and now their use has become commonplace in finance.The Kalman filter is an estimator of the conditional moments of a Gaussian linear system and an optimal recursive algorithm that has real-time implementation in a computer (Kleeman, 1996).It is also used in the calibration of time series model predictor variables and data smoothing applications.It is suitable for dealing with multivariable systems timevarying systems and non-stationary stochastic processes.Antoulas (2013).The stock market fluctuates greatly and changes with time and the Kalman filter has good realtime dynamic tracking characteristics.The stock price analysis here is done using the Kalman filtering theory (Hagiwara, 2021).Yan and Guosheng (2015) established a state space model for Changbaishan (603099) stocks variation using a maneuvering system.They did the forecasting of 27 stocks using kalman predictor and MATLAB.They found out that the relative error of the stock Changbaishan's (603099) closing price is maintained under 4%.Its average relative error is 2.42%, but its maximum relative error attains 9.84%.This phenomenon is mainly due to abnormal fluctuations in the stock price at its bottom and it declines to 6.68%, therefore the abnormal changes of the stock price is increased by the prediction method (Yan and Guosheng, 2015).As a limit, we do not have information on observability of different model states.
To describe the application of a linear Kalman filter, (Rhudy et al., 2017) proposed a problem in which a simple object is in free fall and has no resistive force acting on it.The purpose of the filter here was to determine the objects position based on the uncertain information about its initial position and the position measurements provided by the laser range finder.Using, particle kinematics, they expected the acceleration of the object equals the acceleration due to gravity.Here the position of an object is modeled as a maneuvering system.
Relying on these results, we establish a state space model, study the observability, controllability and stability of our model.

Preliminaries
In this segment, we recall basic notions, fundamental definitions, and important theorems.In general, filters are typically used to estimate the state of a dynamic system from partial observations usually associated with some noise.
Typically, we dispose of a sequence Y0, Y1,…, Yn of observations.Each observation is linked to the unknown state Xn through a mapping of the form: where, γn ∈ is some noise.State space model can be represented in a similar way.

State Space Model (SSM)
Definition 1.1 SSM refers to a class of probabilistic graphical models that describe probabilistic relationships between latent state variables and observed measurements.The state or the measurement can be either continuous or discrete.This is a time series model where the time series, Yt is interpreted as a result of a noisy observation of a stochastic process Xt.The SSM belongs to the realm of Bayesian inference and is successfully applied in many fields.The state process Xt is assumed to be a Markov process; Xt depends solely on Xt-1 and Yt depends only on Xt: A well-studied SSM is the Kalman Filter, for which the above processes are linear and Gaussian.

Proposition 1.2
The sequence (Zt = (Xt,Yt)) is a gaussian random process in  m+d .In particular, at the instant t, the random vector Zt is Gaussian with mean and convariance matrices: Proof See proposition 3.1 in the book by Gland (2016).

Kalman Filter
The Kalman filter belongs to a family of filters called Bayesian filters.The Kalman filter uses an estimate of the previous state and current measurements or observations to compute an estimate of the current state.This means that it doesn't need historical values like a batch estimator and hence it is a recursive estimator.
Kalman filters are based on discrete linear dynamical systems in the time domain.They are designed on Markov chains and constructed on linear operators that are perturbed by some Gaussian noise.The state of the system is an n × 1 real vector, where n is the dimension of the system (Chopparapu et al., 2017).Gu (2012), the authors show that there is a duality between the Kalman filter equation and the hidden Markov model equation.The hidden Markov model can represent an arbitrary distribution for the next value of the state variables, in contrast to the Gaussian noise model that is used for the Kalman filter.In order to use the Kalman filter to estimate the internal state of a process given only a sequence of noisy observations, one must model the process in accordance with the framework of the Kalman filter.In the Kalman filter, the following linear model with n th -order multiple inputs is assumed for the evolution of the true state at time t +1: where: At ∈  n×n = The state transition matrix Bt ∈  n×m = The control input matrix applied to the control vector ut εt = The noise process assumed to follow a multivariate normal distribution with zero mean and covariance matrix Qt.That is: Here xt is an n × 1 real vector.An observation (measurement) yt of state xt is made at time t and we consider the following measurement equation: where, Ct ∈  p×n is the observation matrix and ηt is the observation noise assumed to be Gaussian with zero mean and covariance Rt, that is: The initial state x0 and the state noise vectors ε1,…, εt and measurement noise vectors η1,…, ηt are assumed independent.
The state of the filter is given by the following variables: (5) Measurement Update The combination of Eqs.3-4 give rise to our state space model: Discrete-time state-space is the solution to the linear time-invariant discrete-time system: and assuming A and B are t-independent we have: Cayley Hamilton let A be an n × n matrix with entries in a ring (commutative) with identity and let: be the characteristics polynomial of A, then A satisfies the characteristics polynomial in Eq. 9, i.e.:

Proof
We have: and so, the adjoint matrix is of the form: where, pij are polynomials in λ of degree at most n-1 for 1 ≤ i, j ≤ n.
Therefore, the adjoint matrix may be written as: for some n × n matrices B0 + B1,…, Bn-1 and from ( * ) we have: Now, equating coefficients of like powers of λ in ( * * ) we have: Multiplying these equations on the left by I, A,…, and A n , respectively we get: and adding, the sum on the left telescopes to the zero matrices 0, while the sum on the right is just p(A), so that: Complete state controllability: The state Eq. 8 (or the pair (A, B)) is said to completely state controllable or simply state controllable if, for any initial state x0 and final state xN, there exists an input sequence ut, t = 0, 1, 2,…, N-1, which transfers x0 to xN for some finite N.
Althougth the controllability theory was proposed in Kalman (1960), a test called Kalman's test is used to determine whether or not a system is controllable.
Theorem 1.6.(Kalman's Condition for Controllability) Suppose we have a linear time-invariant system with n th -order multiple inputs such that the state equation is as in Eq. 8.This system is stable if and only if the rank of the controllability matrix (also called the composite matrix), Qc is equal to n where, Qc is given by: 21 : : : So, in Kalman's test, the determinant of Qc is calculated.The value of the determinant of Qc shows whether the system is controllable or not.
If the value is non-zero it indicates a completely controllable system.

Proof. (⇒)
Let p(z) = z n + pn-1Z n−1 + ••• + p0.By the Cayley Hamilton theorem, this implies that the matrix A n is a linear combination of the matrices I, A,…, A n−1 and so A n + k (k ≥ 0) is also a linear combination and hence: where Im (B, AB, …, A n-1 B) is the image of the partitioned matrix Im (B, AB, …, A n-1 B) and this shows Θ is controllable.
The converse part can be seen in the book by Heij et al. (2007).

Definition 1.7
A realization, Θ = (A, B, C) is called reachable if originating from a point x0, all other states can be attained with an appropriate input sequence ut in a definite time interval.So, a controllable realization is reachable.
Let's consider the set R t(Θ) of states that can be reached at time t with an appropriate input sequence u0,…,ut-1 i.e., R t(Θ) = {x ∈ R n | there exist u such that x = x(t; 0, ui.)}.

Definition 1.8
Observability the state model in Eq. 8 (or the pair (A, C)) is said to be observable if any initial state x0 can be uniquely determined from the knowledge of yt and input sequence ut, for t = 0,1,2,…, N -1, where N is some finite time.

Theorem 1.9. (Kalman's Rank Condition for Observability)
The dynamics equation given by Eq. 8 is completely observable if and only if the rank of the composite matrix, Qo is n.where:   1 : : : In Kalman's test, we need to calculate the determinant of the matrix.A non-zero determinant indicates we have an observable system.

Proof
See theorem 3.3 in the book by Gu (2012).

Application
Here, we model the variation of Boeing stock prices for a sampling period of one year using a maneuvering system.Based on the highly fluctuating nature of the stock market and the dynamic tracking features of the Kalman filter, the stock price is viewed as a maneuvering system, that is: Let the state vector, xt+1|t contain the stock price and its price change, it can be modeled as: These linear equations are transformed to:

Process Noise
The process noise covariance matrix Q or error in the state process can be written as (according to prop 1.2): where, σx and σẋ are the standard deviations of the stock price and its variation, respectively.
We define the standard deviation of the stock price as the standard deviation of acceleration x  multiplied by 2 2 t  .Therefore, by multiplying the standard deviation of the acceleration by 2 2 t  we'll have the standard deviation of the stock price.
Similarly, if we multiply the standard deviation of the acceleration by delta ∆t, we'll get the standard deviation of the price change.So, we can write the process covariance noise Q as follows:

Measurement Noise
The covariance of the measurement noise R is a scalar and is equal to the variance of the measurement noise.It can be written as:

Controllability
Now from: The controllability matrix is given by: Also, the rank of Qc = 2 = n thus the SSM is controllable.

Observability
Verifying now the Observability, we extract from Eq. 16, the transformation matrix: The observability matrix is given by: 11 01 Also, the rank is equal to 2 and the state space model is completely observable.

Stability Analysis Definition 3.1. (State Stability)
Consider the system given by 8, we say this system is stable in the sense of state stability if, for every time t, we have that ||xt|| ≤ M for some constant M ∈ .
It is worth noting that M may not depend on the time t and that this definition still holds if we add some disturbance to the system.

Definition 3.2 (Asymptotic Stability)
The system in 8 is asymptotically stable if lim 0 t t x   for any solution with ||x0|| ≤ M.

Definition 3.3
The spectrum of A is the set of eigenvalues of A: Let's recall that any eigenvalue can be written as λ = ζ + jω, if ω = 0 the eigenvalue is real and we denote it by (λ) Proposition 3.4 The system in Eq. 8 is stable if and only if A has all its eigenvalues in the open unit disc.i.e., if λ is an eigenvalue of A, A is stable if and only if:

Feedback Stabilization
The concept of controllability ensures that we can fully control the state of a system using a control signal ut, thereby allowing the system to go from any initial state to a final state in a given time interval.It is commonly called "point-to-point control".Even though point-to-point control is how controllability is defined, we are interested in another control problem in some real-world situations: Stabilization.
Without control input (ut = 0) in Eq. 8, the eigenvalue of A, λ = 1 with a multiplicity of 2 which is internally unstable.Now for u  0, given the matrices A ∈  n×n , B ∈  n×m , does there exist a matrix K ∈  k×n such that the system: Stabilization (regulation) the task of designing the control input ut such that the controlled system becomes stable.The most common approach is through a state feedback mechanism.
In this configuration the input is a fixed (matrix) gain multiplied by a state i.e., we feedback the state xt: By direct substitution, we readily see that: So the stability of the close loop system is a function of A -BK.

Now and taking
Now, det(sI −(A−BK)) = 0 and so: We want the closed-loop system to be asymptotically stable and therefore the roots of this polynomial have to be in the left half plane.Assume we want closed loop Eigenvalue: s 1 = −1, s 2 = −2 which gives the desired characteristic polynomial: Now comparing this with Eq. 20 we have: The above results show that our system can be stabilized through state feedback.

Numerical Simulation of Kalman Filter
We use 252 stock closing price historical data of Boeing Stock from January 1 st, 2021 to January 1 st, 2022 as the experimental data.
Let: and the Kalman predictor is implemented using Python simulation.The plots below show a simulation of the filtered value against the observed and actual data.
Figure 1 presents the general state feedback mechanism Fig. 2 shows a graph of filtered estimates, true value of stocks price and the observation at a particular instant.It can be seen that the estimates are close to the true values and hence captures well the exact value.
Figure 3 here shows the variation of the filtered estimates in a stock's price which provides information about the volatility of the stock's price.This reveals that the Kalman filter can efficiently capture the volatility of the stock's price.

Evaluation
Evaluation metrics explain the performance of a model.An important aspect of evaluation metrics is their capability to discriminate among model results.For our evaluation here we shall consider two metric, absolute and relative errors which are given by the formula: From Table 1, we observe that the relative error of the Boeing stock closing price is kept below 5%, the average relative error is 2.43% but the maximum relative error is 12.8%.This result can be improved by adding the dimensionality of the state space model.It can be done by adding the number of tracking features.
Kalman filters are powerful tools for analyzing and modeling time series data that can handle nonstationary models with exogenous control inputs as well as compute difficult probability functions.
Informally, we can evaluate the goodness of our model by evaluation the gaussianity of the residue (zt).One important assumption made in the development of Eq. 6 is that the residual is gaussian and Fig. 4 shows that our modeling obeys this assumption.The histogram's empirical data distribution should mirror the normal distribution and have a bell-shaped form.
A Quantile-Quantile plot (QQ plot) in Fig. 5 is used to assess the normality of the residues in our application.It represents the probability plot of the standardized data against the standard normal distribution.Here the correlation between our data and normal quantiles measures how well the data are modeled by a normal distribution.For normal data the points plotted in the QQ plot should fall approximately on a straight line, indicating high positive correlation.

Conclusion
In this research work, we study the variation of the price process assumed to be a maneuvering system as a result of the continuous fluctuation in the market.We model the price change with a dynamical system and derive the state space model for the stock price from the system dynamic.We realize through Kalman's conditions that our model is both controllable and observable.In an attempt to verify if the system will return back to an equilibrium point when disturbed, we verify the stability of the SSM and realize that it is not stable (asymptotically), but can be stabilized through state feedback.We then simulate the SSM via the Kalman filter in Python.The simulation result of 252 historical data indicates that the filtering method is effective through the various error metrics Fig. 2. For better accuracy achievement, the features of the state transition matrix can be added thereby adding the dimension of the SSM.As a proposition for further research, since the noise here is embedded in the acceleration, we can add some additional Gaussian noise to the SSM in equation 12. Also, we could infinitely increase the number of assets and develop a big data model for stock price prediction.

P
 the error covariance matrix at time t given some observations up to and including time t Filtering is done in two stages: Time update or prediction and measurement update or filtering.
estimate of the state at time t +1 considering some observations up to and including time t  P  the error covariance matrix (a measure of the estimated accuracy of the state estimate) at time t +1 given some observations up to and including time t   

Table 1 :
Absolute and relative errors for some estimated values