[PYTHON] Prediction model construction ①

Create a demand forecasting algo little by little while playing with mathematical formulas. While touching various mathematics and derailing, I aim for something that will eventually become a dissertation.

Sine curve estimation with Taylor expansion

The Teylor expansion is expressed by the following formula.

f\left(x\right)=\sum_{n=0}\frac{f^{\left(n\right)}\left(a\right)}{n!}\left(x-a\right)^{n}

Assume $ x = a + 1 $ in the above equation. Then, it becomes as follows.


f\left(a+1\right)=\sum_{n=0}^{\infty}\frac{f^{\left(n\right)}\left(a\right)}{n!}\left(a+1-a\right)^{n}\\=f\left(a\right)+\sum_{n=1}^{\infty}\frac{f^{\left(n\right)}\left(a\right)}{n!}

If you give $ a $ to this function $ f $, you get $ f $ of $ a + 1 $. In other words, once you have the value of the previous time, it is like predicting the value of the next time. Give a sine curve as a trial. It becomes the following formula.

\sin\left(a+1\right)=\sin\left(a\right)+\cos\left(a\right)-\frac{\sin\left(a\right)}{2!}-\frac{\cos\left(a\right)}{3!}+\frac{\sin\left(a\right)}{4!}+\cdots

Let's plot with python.

from numpy import *
from pylab import *
%matplotlib inline

x = arange(100) / 10 * pi
y = sin(x) #Current


n = 1
z =0 #Forecast
for i in range(10):
    if ((i + 2) // 2) % 2:
        op = 1
    else:
        op = -1
    
    n *= i if  i else 1
    if i % 2:
        z += op * cos(x) / n
    else:
        z += op * sin(x) / n
  
plot(x, y,"r",label="now");plot(x,z,"b", label="pred");legend();show()

Unknown.png

The future (blue: pred) at the present (red: now) Since I am predicting, it seems that blue is slightly shifted to the left from red.

This phase shift should be 1, so let's shift the blue by 1.

x2 = arange(100) / 10 * pi + 1 #Shift the phase by 1
plot(x, y,"r",label="now");plot(x2,z,"b", label="pred");legend();show()

Unknown.png

It overlapped exactly. I can predict. With arbitrary precision (but only continuously differentiable) as long as the ** functional form ** can be defined It looks like you can predict it. But neither financial data nor purchasing data has a functional system in advance. (I'm glad if I got it), You need to create some kind of predictive model (mathematical formula).

Recommended Posts

Prediction model construction ①
real-time-Personal-estimation (new model construction)
Language prediction model by TensorFlow
[Kaggle] Baseline model construction, Pipeline processing
Time series analysis 4 Construction of SARIMA model
Temperature prediction model running on edgetpu (coral)