py4sci

Previous topic

Discretization of a continuous score

Next topic

PCA of olympic decathlon data

This Page

Features for time seriesΒΆ

This example shows how to access some fairly simple features of time series that may be useful for modeling time series. They are the ACF (auto-correlation function) and a version of the Discrete Wavelet Transform.

These topics are not going to be covered in detail, but having access to some functions may be useful, nevertheless.

The data is in the package fEcofin and the wavelet tools are in the wavelet package

    library(fExtremes)

    ## Loading required package: timeDate

    ## Warning: package 'timeDate' was built under R version 2.15.1

    ## Loading required package: timeSeries

    ## Warning: package 'timeSeries' was built under R version 2.15.1

    ## Loading required package: fBasics

    ## Warning: package 'fBasics' was built under R version 2.15.1

    ## Loading required package: MASS

    ## Attaching package: 'fBasics'

    ## The following object(s) are masked from 'package:base':
    ##
    ## norm

    ## Loading required package: fGarch

    ## Warning: package 'fGarch' was built under R version 2.15.1

    ## Loading required package: fTrading

    ## Rmetrics Package fTrading (2100.76) loaded.

    ## Attaching package: 'fTrading'

    ## The following object(s) are masked from 'package:timeSeries':
    ##
    ## rollMax, rollMean, rollMin

    ## Warning: replacing previous import 'rollMax' when loading 'fTrading'

    ## Warning: replacing previous import 'rollMean' when loading 'fTrading'

    ## Warning: replacing previous import 'rollMin' when loading 'fTrading'

    ## Rmetrics Package fExtremes (2100.77) loaded.

    data(bmwRet)
    plot(bmwRet$BMW.RET, type = "l")

_images/timeseries_fig_00.png

The auto-correlation (ACF), as a function of lag, is a measure of the correlation the series and a lagged copy of itself. Since these are returns, they are already differences and we do not see much autocorrelation.

    plot(acf(bmwRet$BMW.RET))

_images/timeseries_fig_01.png

The ACF is related to the so-called power spectrum of the time-series which is a function of the (discrete) Fourier transform of the time series (i.e. the FFT).

The DWT computes something like a localized version of the FFT at different scales. It can sometimes reveal different patterns at different scales of the data.

    library(wavelets)

    ## Warning: package 'wavelets' was built under R version 2.15.1

    ## Attaching package: 'wavelets'

    ## The following object(s) are masked from 'package:timeSeries':
    ##
    ## align

    ## The following object(s) are masked from 'package:timeDate':
    ##
    ## align

    plot(dwt(bmwRet$BMW.RET, n.level = 3))

_images/timeseries_fig_02.png