Implementation of Kalman filter for forecasting stock price trends and their impressions. The conclusion is that it may be available for the short term. It runs on google colab.
Originally used for controlling robots and rockets, it is used to estimate the state of the system using inaccurate observations. This time, I'm using the simplest one.
We think that stock price fluctuations follow a certain probability distribution, and we make predictions based on that, so please use it as one of the indicators.
Also, this time we make an unrealistic assumption because we assume that the potential way stock prices fluctuate and how they actually change follows a normal distribution.
Therefore, while it works well for stock prices that are moving steadily to some extent, it seems that it cannot keep up with sudden changes.
It's annoying why the formula is so, so if you want to see it, please refer to [wiki](https://ja.wikipedia.org/wiki/Kalman filter). In terms of implementation, there is no problem if you define the following functions. Takes a sequence as an argument and returns a sequence of the same length.
kalman.py
import numpy as np
def kalman(s,x,P):
res_x=[]#Estimated position at that time
res_v=[]#Estimated speed at that time
for i in range(len(s)):
Z=np.array([s[i]])
y=Z-H@x
S=H@[email protected]+R
[email protected]@np.linalg.inv(S)
x=x+K@y
P=(I-K@H)@P
#predicion
x=(F@x)+u
P=F@[email protected]
res_x.append(x[0,0])
return res_x
x=np.array([[0.], [0.]])#The first is the position and the second is the speed
P=np.array([[1000., 0.], [0., 1000.]])#Covariance matrix
u=np.array([[0.], [0.]])#External force but not this time
F=np.array([[1., 1.], [0, 1.]])#Definition of the relationship between position and velocity
H=np.array([[1., 0.]])
R=np.array([[1.]])
I=np.array([[1., 0.], [0., 1.]])
t=[1,2,4,3,7,4,6,5.2]#Sequence to put
print(kalman(t,x,P))
The result is as follows.
res.txt
[0.999000999000999,2.9980029930179533,5.332389258923225,4.500249550392171,7.299929903069252,6.400310235003376, 7.000316132748338, 6.886095984845674]
Since there is no particular regularity in the given sequence this time, it cannot be predicted correctly, but if random numbers are generated according to the probability distribution, it will be estimated with considerable accuracy.
I'm writing this article on August 14th, and I wonder if the stock price will cool down in Corona and hit a double bottom, and it's about to rise. It's a time like that. Assuming that the value obtained by the Kalman filter is the original stock price, the stock price is likely to return to it.
Here are some of the brands that I have applied that are characteristic. One of them is ENEOS Holdings (5020.T).
At first glance, I don't know what it is, but the time to start applying it to the filter is different. Around December 2018, the entire stock market plummeted due to the influence of the Trump administration, and around February to March 2020, stock prices plummeted due to the influence of Corona. These events have good outliers, so you can see that the Kalman filter estimates are suddenly off. So, by applying the Kalman filter again after each plunge event, you can see that a relatively good estimate can be made.
If there are no big events, I think it can be used as an index (especially when buying). Personally, I think that it will rise to the middle of the Kalman filter value at a certain point and the stock price, so I also buy other indicators. However, when the stock price changes significantly, it will be bad if you do not shift the initial value to be applied.
I think it can be used in day trading. Rather, the price movement of day trading seems to be closer to the normal distribution, so ... I would appreciate it if you could let me know if you have any opinions.
Also, this Kalman filter can be used by robots as it is.
Recommended Posts