Fourie analysis for stock time series data①

How to calculate fourie analysis for stock time series data

Hello this is data scientist Hiroki.

Don’t you have some trouble for future analysis of stock time data, and do you want to use the basic mathmatics for it? I also have some trouble especially when I read the technical analysis books of stock time data. I wanted to use the besic mathmatics theory for the stock time data analysis. so I decided to write this blog.

This blog is for you who have below problems.

  • ・you feel it is difficult to conduct time data analysis of stock data.
  • ・you read some thechnical analysis books about stock data but you feel it is something different what you need.
  • ・you want to use some basic mathmatic theory like fourier transformation for stock data analysis.

let’s start below 3 step basic pattern of fourier transformation.
I’ll tell you step by step, don’t worry🙂

basic three pattern of time series data

  1. Direct current component(average)
  2. Periodic fluctuation(sin wave)
  3. Irreguler fluctuation(random noise)

Any time series data consist of linear combination of above 3 pattern.

Somehow you can understand the similarity of the both data.
For the very first step, it is ok that you understand time series data consist of linear combination of direct current component and periodic fluctuation and irreguler fluctuation.

To resolve time series data into direct current component and periodic fluctuation is called Fourier series expansion.

Please check below Yobinori’s Youtube link in detail(in Japanese though).

Example

I have a Question!! for you at first. what will it be if below graph is added all?

The answer is approximation of Nikkei225 time series data by fourier series expansion until 3 items.

By resolving like this, you can get the signal size and period which you can find when signal peak will come.
ofcourse it is not enough that you cna judge your investing timing when you buy or sell the stock. But I think it will be one strong evidence of your judgement.

Code

As an example let’s look at below Matlab code.
To run this code showed in this article, you need Matlab and Curve Fitting Toolboxwhich is provided with Matlab. If your PC didn’t install matlab yet, don’t worry. They have [30 day’s free trial(https://jp.mathworks.com/campaigns/products/trials.html?prodcode=ML) for you. and Curve Fitting toolbox free trial also available. If you are not good at programming, don’t worry. You can just copy and paste of this blog code. I’ll explain how to do it step by step from now. Let’s get started!😉

At first you need to download the program file which you can download any stock data from yahoo finance. please access this weblink and download the file and save it to the same directory of workspase.

copy below code and paste it to the matlab workspase and run.
then you will get the same result as showen above example.

%to get Nikkei225 data from yahoo finance
NIKKEI = hist_stock_data('09112011','09112021','^N225');

%to store the date data into the variable named year
year=NIKKEI.Date;
L=length(year);
x=[1:L]';


%to complement the missing data and store the complemented data into the variable named YY
[YY,TF] = fillmissing(NIKKEI.Close,'linear','SamplePoints',year);


%curve fitting by 3 item fourier series expansion(your PC needs curve fitting toolbox)
f2 = fit(x,YY,'fourier3')


%to store the fourier coefficient to the variable
a0=f2.a0;
a1=f2.a1;
a2=f2.a2;
a3=f2.a3;
b1=f2.b1;
b2=f2.b2;
b3=f2.b3;
w=f2.w;

%resolve signal into linear combination of direct current component and periodic fluctuation
irreguler fluctuation
y1=a0*ones(L,1);%direct current component
y2_sin=b1*sin(x*w)+b2*sin(2*x*w)+b3*sin(3*x*w); %periodic fluctuation
y2_cos=a1*cos(x*w)+a2*cos(2*x*w)+a3*cos(3*x*w);%periodic fluctuation
y3=200*randn(L,1);%Irreguler fluctuation


y=y1+y2_sin+y2_cos+y3;


%create graph
figure
plot(year,YY);
hold on
plot(year,y);
xlabel('year')
ylabel('株価[¥]')
title('日経平均株価')
legend('日経平均','フーリエ近似')


figure
subplot(8,1,1)
    plot(year,y1)
    text(datetime('2013-01-04'),19916,'1.直流成分 y=19920')

subplot(8,1,2)
    plot(year,b1*sin(x*w))
    xlim([datetime(year(1)),datetime(year(L))])
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=-6113*sin(0.001931x)')

subplot(8,1,3)   
    plot(year,a1*cos(x*w))
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=-3199*cos(0.001931x)')

subplot(8,1,4)
    plot(year,b2*sin(2*x*w))
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=-2489*sin(2*0.001931x)')

subplot(8,1,5)   
    plot(year,a2*cos(2*x*w))
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=-3909*cos(2*0.001931x)')

subplot(8,1,6)
    plot(year,b3*sin(3*x*w))
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=76.72*sin(3*0.001931x)')

subplot(8,1,7)
    plot(year,a3*cos(3*x*w))
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'2.周期変動  y=-2765*cos(3*0.001931x)')

subplot(8,1,8)
    plot(year,y3)
    ylim([-5000 5000])
    text(datetime('2013-01-04'),3800,'3.不規則変動  y=200*randn(L,1)')

references

below books are good for entry level worker or student who want to know the basic theory about fourie transformation, written in japanese though.


マンガでわかるフーリエ解析(Amazonリンク)



Excelで学ぶ理論と技術 フーリエ変換入門(Amazonリンク)


入門はじめての時系列分析/盛岡書房




MATLAB入門(メルカリ) MATLAB入門(Amazon)

Relative movies

below movie is also very cool. please check it out!!

thank you so much for reading this blog.

Hiroki🐶