Wavelet analysis

Time series information is not always sufficient to get insight into the data. Sometimes the frequency content of the data also contains important information about the data. In the time domain, Fourier transformation (FT) captures the frequency-amplitude of the data but it does not show when in time this frequency has happened. In the case of stationary data, all frequency components exist at any point in time but this is not true for non-stationary data. So, FT does not fit for non-stationary data. Wavelet transformation (WT) has the capacity to provide time and frequency information simultaneously in the form of time-frequency. WT is important to analyze financial time series as most of the financial time series are non-stationary. In the remainder of this chapter, wavelet analysis (WT), I will help you understand how to solve non-stationary data in R using wavelets analysis. Stock price/index data requires certain techniques or transformations to obtain further information about the series which raw data does not show. The daily closing price for the Dow Jones Industrial Average (DJIA) and S&P500 index from January 1, 2010 to December 31, 2015 has been used for illustration purposes. I am going to use the wavelets package for this:

  1. Before starting to work with wavelets transformation, you have to install the package named Wavelets:
            >  install.packages('wavelets') 
    
  2. Once you install it or you already have this package in your machine then you just have to load it into the workspace:
            > library(wavelets) 
    
  3. To get a first impression of the data, we plot the dji and snp time series and their reture:
            >   par(mfrow=c(2,1)) 
            >   plot(dji,type="l") 
            >   plot(ret_dji,type="l") 
    

The first line is used to divide the plot into a two-by-one matrix, so the plot can have two plots in one figure, and the next two commands plot the Dow Jones price and its return series, which can be seen in Figure 3.13:

Wavelet analysis

Figure 3.13: Price and return series for Dow Jones Index (DJI)

The dji and snp time series are non-stationary. We use head and tail to look at the first and last part of the time series:

>head (dji) 
           DJI.Close 
2010-01-04 10583.96 
2010-01-05 10572.02 
2010-01-06 10573.68 
2010-01-07 10606.86 
2010-01-08 10618.19 
2010-01-11 10663.99 
 
>tail (dji) 
           DJI.Close 
2015-12-23 17602.61 
2015-12-24 17552.17 
2015-12-28 17528.27 
2015-12-29 17720.98 
2015-12-30 17603.87 
2015-12-31 17425.03 

Now we apply discrete wavelets transformation (DWT) on the dji data and decompose it using various filters. It requires data in time series, matrix, or data frame format. We look at the dji variable format which is xts and zoo object. So we need to convert it into an acceptable format:

dji<- as.ts (dji)  

Now it is ready to be used in discrete wavelets transformation's R function. We also need to provide other parameters, such as the type of filter you will be using and the number of levels you want your data to be decomposed into:

model<- dwt (dji, filter="la8", n.levels=3) 

It saves the output in the variable called model. You can write model on the command prompt and it will display the output in the command prompt:

>model 

It generates output which consists of various information matrices such as wavelet coefficients, scaling coefficients, type of filter used, and number of levels used. You can extract any individual information as well. To extract wavelet coefficients, you have to write the following command on the command prompt:

>model 

It generates output which consists of various information matrices such as wavelet coefficients, scaling coefficients, type of filter used, and number of levels used. You can extract any individual information as well. To extract wavelet coefficients, you have to write the following command on the command prompt:

>model@W          # to extract wavelets coefficients 
>model@V          # to extract scaling coefficients 

These commands generate a relative list of wavelets and scaling coefficients. To get an individual component of wavelets, you have to mention the following:

> model@W$W1      # to extract first level of wavelet coefficients 
> model@V$V1      # to extract first level of scaling coefficients 

We can also use the plot command to visualize data series, wavelets, and scaling coefficients:

> plot (model)  

Figure 3.14 will plot the price and its various level coefficients and help us to visualize and understand the data clearly:

Wavelet analysis

Figure 3.14: Plots for time series, wavelets, and scaling coefficients

You can also use the discrete wavelet transformation function for the haar filter:

model<- dwt (dji, filter="haar", n.levels=3) 
> plot (model)  

It will plot the data series, wavelets, and scaling coefficients using the haar filter.

To compute inverse discrete wavelet transformation, you have to use a wavelet object, as defined using discrete wavelet transformation. The variable model is a wavelet object using the haar filter:

imodel<- idwt(model, fast=TRUE) 

Sometimes it is necessary to know the class of the R objects, for example, model and imodel.

We can use the following commands for this:

> class(model) 
[1] "dwt" 
attr(,"package") 
[1] "wavelets"   
>  class(imodel) 
[1] "ts" 

The variable imodel is created using inverse wavelet transformation and it generates an original time series object.

Multiresolution analysis (MRA) is another widely useful wavelet method for time series analysis. Financial markets generate large quantities of data, which is analyzed to generate algorithmic trading signals. Wavelet multi-resolution analysis is increasingly being applied to these datasets because it enables traders to focus on a particular time scale where trading patterns are considered important. The la8 filter is used in the following example and the haar filter also can replaced for la8:

> model <- mra(dji, filter="la8", n.levels=3) 

For the analysis of market data, maximal overlap discrete wavelet transform (MODWT) is preferred.

As an example, I considered the case of Dow Jones Index time series, dji, as input to the modwt function:

> model <- modwt(dji, filter="la8", n.levels=5) 

The preceding function decomposes the time series in detailed wavelets and scaled coefficients, which can be seen in Figure 3.15. The plot.modwt() function can be used to plot this modwt output:

>plot.modwt(model) 

Wavelet analysis

Figure 3.15: Plot of Maximal Overlap Discrete wavelets transform

A few jumps in the time series can be seen as jumps in smaller coefficients such as W1 and W2, and smooth coefficients such as W6 show movement about some mean for the time period. Wavelets and scaled coefficients in Figure 3.15 clearly show price data at different time scales.

Wavelet analysis provides an important tool in quantitative finance, with applications ranging from short-term prediction and the calculation of variance in relation to specific time scales.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset