Coding Cross Correlation Function in Python

Discussion in 'Python' started by DarthXander, Dec 2, 2009.

  1. DarthXander

    DarthXander Guest

    I have two data sets which I wish to perform the discrete correlation
    function on and then plot the results for many values of t to see what
    if any time lag exists between the data.
    Thus far my code is;

    import csv
    import pylab
    from pylab import *
    from numpy import *
    from numpy import array

    HSBC=csv.reader(open("HSBC data.csv"))
    Barclays=csv.reader(open("Barclays data.csv"))

    x=[]
    a=[]
    y=[]
    b=[]
    g=[]
    h=[]
    d=[]

    for Date, Close in HSBC:
    x.append(Date)
    a.append(float(Close))

    for Date, Close in Barclays:
    y.append(Date)
    b.append(float(Close))

    for index in range(len(a)):
    g.append(a[index]-mean(a))

    for index in range(len(b)):
    h.append(b[index]-mean(b))

    r=std(a)
    s=std(b)

    So I have all the necessary components for the DCF.

    However I'm not faced with the challenge of performing the DCF for t
    in the range of potentially 0-700 or so.
    Currently I could do it individually for each value of tau ie;

    t1=[]
    for index in range(len(g)-1):
    j=(g[index]*h[index+1])/(r*s)
    t1.append(j)

    d.append(mean(t1))

    However to do this 700 times seems ridiculous. How would I get python
    to perform this for me for t in a range of roughly 0-700?

    Thanks
    Alex
     
    DarthXander, Dec 2, 2009
    #1
    1. Advertising

  2. DarthXander

    sturlamolden Guest

    On 2 Des, 18:50, DarthXander <> wrote:

    > However to do this 700 times seems ridiculous. How would I get python
    > to perform this for me for t in a range of roughly 0-700?


    For two 1D ndarrays, the cross-correlation is

    from numpy.fft import rfft, irfft
    from numpy import fliplr

    xcorr = lambda x,y : irfft(rfft(x)*rfft(fliplr(y)))

    Normalize as you wish, and preferably pad with zeros before invoking
    xcorr.
     
    sturlamolden, Dec 2, 2009
    #2
    1. Advertising

  3. DarthXander

    DarthXander Guest

    On Dec 2, 7:12 pm, sturlamolden <> wrote:
    > For two 1D ndarrays, the cross-correlation is
    >
    > from numpy.fft import rfft, irfft
    > from numpy import fliplr
    >
    > xcorr = lambda x,y : irfft(rfft(x)*rfft(fliplr(y)))
    >
    > Normalize as you wish, and preferably pad with zeros before invoking
    > xcorr.


    Thanks, though I'd like to do this longer hand than the built in
    functions! Great to approximate it though.
     
    DarthXander, Dec 2, 2009
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jarson
    Replies:
    0
    Views:
    416
    Jarson
    Apr 28, 2004
  2. robert

    numpy/scipy: correlation

    robert, Nov 11, 2006, in forum: Python
    Replies:
    18
    Views:
    6,988
    robert
    Nov 16, 2006
  3. Gary Wessle

    code to find correlation

    Gary Wessle, Aug 26, 2006, in forum: C++
    Replies:
    1
    Views:
    346
    Victor Bazarov
    Aug 27, 2006
  4. Godai

    Help! Image Correlation

    Godai, Nov 19, 2007, in forum: C Programming
    Replies:
    2
    Views:
    410
    Godai
    Nov 19, 2007
  5. Replies:
    1
    Views:
    253
    Dennis Lee Bieber
    Feb 20, 2008
Loading...

Share This Page