Wednesday, April 28, 2010

Wavelet Spectrogram Non-Stationary Financial Time Series analysis using R (TTR/Quantmod/dPlR) with USDEUR

I've been doing some research lately regarding types of spectral imaging and decomposition techniques that apply to non-stationary signals. As mentioned earlier, one of the major problems with the simple fourier analysis is that the basis functions extend to infinity in both directions and the signals are assumed to be stationary. Although, I won't expand too much right now, one of the advantages of wavelets is that they use local small windowed basis functions, allowing them to capture not only non-stationary signals, but signals that are aperiodic: two large advantages over fourier based methods when dealing with financial time series.

I put together a few small examples to understand how to visually understand a spectrogram.

Fig 1. Simple 58 day cycle captured with 11 octaves and 2048 (2^11) data points

As in earlier tutorial based posts, we use a simple 58 day cycle to show the basic time series sine based waveform. Now the plot on the bottom is known as a spectrogram. The type of wavelet operation for this spectrogram is known as a continuous wave Morlet transform. The package is dpLR (The Dendrochronology Program Library) put together by Andy Bunn . The package was designed to analyze tree rings. Notice that there are a multitude of tools utilizing this type of technology, ranging from MRIs, to climatology, to speech processing. It is, IMO, the modern day version of dft type spectral tools (however, for non-stationary and aperiodic signals). Now looking on the spectrogram plot, please keep in mind the units are Days not Years (I need to see how to alter that, hopefully Dr. Bunn is listening=).

The time scales represents linear time, or a window of 2048 days that was sampled. We could have used any time series, but it needs to be length=2^N; if not, there is a function to pad the rest of the data with zeros to make up that length. The vertical scale is a log scale that shows what are called 'octaves'. Borrowing from musical vernacular, we can think of them of scales which double in magnitude for every prior scale and represent localized frequency energy information at such scales. The colors represent the heat or power of the signal in regions of interest. Due to some issues with this transform, we ignore uncertain information outside of the dark parabolic region (cone of influence). It is clear that the highest power is the dark red region right at around 58 days. What is important here is not so much to understand the exact value of the cycle, but the persistence in the dominant cycle (s). We notice the cycle persists throughout the entire spectrogram Time Series length (much as we would expect from the 2D time series plot).

What happens if we use different frequencies that change over time? Here we notice a clear advantage over fourier based methods. A fourier based decomposition would be able to locate the dominant tones, however, because it uses infinite bases, the reconstructed signal would not capture the isolation of different frequencies.

Fig 2. Composite Stationary Time Series comprised of 3 dominant tones

Notice, that we can clearly see the regions of dominant tones by following the chart and looking for the most concentrated power (red) regions, which are around 48, 253, and 532 day cycles. We also notice that the power density can be viewed in terms of time context, our eyes simply follow along in time and observe strong regions of signal energy concentration.

Ok, but what about if the signal itself is non-stationary?

Fig 3. Composite signal added to exponential curve to make signal non-stationary

Notice, that even though we now have a non-stationary signal, the regions of underlying cyclic component stability are still detectable by eye!

Lastly, a financial time series of USDEUR was captured via TTR/Quantmod packages.

Fig 4. USDEUR time series spectrogram

Notice even with the non-stationary financial signal, there is a very clear dominant cycle pattern that is persistent at roughly 255 days (anyone familiar with trading recognizes that as the approximate number of trading days per year).

Keep in mind that there are also aliases (and spreading) present in sampling methods which may look like periodic signals, but are merely digital artifacts of the underlying sampled signal. We also see the very short term noise present in the bottom lower scales.

Another interesting application of this is that it may not only be used as a modern tool to augment non-stationary decomposition, but for those familiar with pattern based techniques, it (and the periodogram counterpart) is often used in pattern recognition and markov type modeling.

That's all for now. Hopefully, you have gained some appreciation for wavelet based spectral techniques vs. Fourier spectral based analysis.

I have been debating whether to break up the post, but because I was added to the R bloggers thread, I wanted the post to be complete for local readers.

That's it for now.


  1. Nice post. I just wanted to make you aware of other packages in R that perform wavelet analysis. For the continuous wavelet transform (CWT), I would recommend Rwave which is an implementation of Swave from Practical Time-Frequency Analysis: Gabor and Wavelet Transforms with an Implementation in S, by Rene Carmona, Wen L. Hwang and Bruno Torresani, Academic Press, 1998. For the discrete wavelet transform (DWT), I would recommend waveslim which was used to analyze all the data from An Introduction to Wavelets and Other Filtering Methods in Finance and Economics" by Gencay, Selcuk and Whitcher, Academic Press, 2001.

  2. Thanks for the feedback and recs. I'll take a look at the other packages.

  3. Nice...

    Its always so much more fun to follow along with actual code and data...

    Keep up the nice work!!!


    -Digital Dude-

    "I can never stand still. I must explore and experiment. I am never satisfied with my work. I resent the limitations of my own imagination." -Walt Disney-

  4. Thanks DD,

    I may not always include scripts or code as
    1) Most of the work can be repeated from past posts and is superfluous (all signal synthesis was covered in past, quantmod/ttr examples can be lifted from prior posts). Sort of forces readers to do some research and really learn it, however, if anyone is stuck just drop a comment and I'll do my best to help.
    2) I often sort of write scripts on the fly, unedited, and post as I move along, so that they may not come out very clean or organized.

  5. Dear IT

    Can you explain how to get the pretty picture (spectogram) "by hand"? I'm using Matlab (for many years) instead of R, and when I use the built-in function, it spits out some picture that doesn't make any sense.
    I don't need the actual code, but a few hints what to do, to get the same/similar picture as you did.
    Thanks in advance.



  6. Hi anton,

    First: thanks for addressing which post. Blogger doesn't tell me the specific post for which a comment addresses, so sometimes I have to go digging through to figure it out.

    Regarding the hand explanation, it's too complicated to explain in a single comment.
    However, Matlab (like most spectral software tools) spits out the y axis in the frequency domain, which is what I'm guessing you are encountering.

    What I have (days) on the y-axis is simply the inverse of the frequency measurement (freq=1/Time). You could try to alter the axis range in the matlab code to reflect that, or simply manually invert the frequency scale levels to get to days. One reason I liked this package it that it did the work for me=)

    Hope that helps,

  7. Hello

    The y axis is the period
    What's the meaning of the x axis?


  8. Hello

    What's the meaning of the x axis?

  9. Hello,
    Both axis are related to time. The x-axis is local time, the y-axis is periodicity strength corresponding to the localized time.