Fourie analysis for stock time series data②

spectrum analysis for stock 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 want to learn more basic mathmatic theory about technical analysis about stock data.
  • ・You want to know how often the stock data peak comes.
  • ・You cannot understand well about spectrum analysis even you learn it at university.

This blog is for you who have above problems. Spectrum analysis will be one solution. for example by using spectrum analysis you can understand stock data fluctuation how often and how large the trend peak will be available.

Time series data consist of below three basic pattern.

basic three pattern of time series data

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

The spectrum analysis is focussing on periodic fluctuation. this analysis is based on basic engineering theory used wide range of engineering field. so this blog is good for engineering student too.

let’s check this out🙂

Spectrum analysis

How often and how large are the stock moving?
By following below two steps we’ll see the answer.

we’ll use the matlab for showing the example.
If your PC didn’t install matlab yet, don’t worry. They have 30 day’s free trial for you.
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!😉

  1. what is spectrum analysis?
    The image is shown like below.

    The holizontal axis is friquency which you can find how often the peak will come. and the vertical axis is Gain which you can find how large the peak is.
    This graph is called Periodogrum.

    One caution, we cannot find the phase infomation which is about when the peak is comming from the periodogrum.
    If you want to know it you can use Fourie analysis of time series data as shown previous blog

    Again, this periodigrum merit is you can find the period between the peak and how big the peak is at a grance.


  2. How to do it?

    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.

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


   %download the stock data from yahoo finance.
   data = hist_stock_data('09112011','09112021','2502.T');


   y=data.Close;
   t=data.Date;

   %if the missing data available interpolate it and store the result inti YY.
   [YY,TF] = fillmissing(y,'linear','SamplePoints',t);

   %define one year as 245 business day.
   Fs=245;

   %conduct the fourie transformation and output the power spectrum density and frequency.
   [pxx,f] = periodogram(YY,rectwin(length(y)),length(y),Fs);

   %create graph
   plot(f,10*log10(pxx))
   xlabel('Cycles/Year')
   ylabel('dB / (Cycles/Year)')
   title('Periodogram')
   xlim([0 10])

you can get any stocks periodogram by changing the ticker symbole of the first line of above sample code.

next time

I’ll write about correlation corfficient next time.

references

below books are good for entry level worker or student who want to know the basic theory more.


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



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


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



#movie
Below YouTube is also good for beginners.

thank you for reading😊

Horoki🐶