Bookmark and Share

Notice: On March 31, it was announced that Statalist is moving from an email list to a forum. The old list will shut down at the end of May, and its replacement, statalist.org is already up and running.


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Rolling correlation coefficients with panel data


From   Richard Herron <richard.c.herron@gmail.com>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Rolling correlation coefficients with panel data
Date   Mon, 3 Sep 2012 17:22:48 -0400

The process is much faster if you calculate rho "manually" using Cov
and Var created with summations.

I use the following .ado file. HTH.

*! 0.1 Richard Herron 10/15/2011
program rolling_rho
    version 11.2
    syntax varlist(numeric), window(real)

    * get dependent and indpendent vars from varlist
    tempvar x y x2 y2 xy xs ys xys x2s y2s covxy varx vary
    tokenize "`varlist'"
    generate `y' = `1'
    generate `x' = l.`y'
    local w = `window'

    * generate products
    generate `xy' = `x'*`y'
    generate `x2' = `x'*`x'
    generate `y2' = `y'*`y'

    * generate cumulative sums
    generate `xs' = sum(`x')
    generate `ys' = sum(`y')
    generate `xys' = sum(`xy')
    generate `x2s' = sum(`x2')
    generate `y2s' = sum(`y2')

    * generate variances and covariances
    generate `covxy' = (s`w'.`xys' - s`w'.`xs'*s`w'.`ys'/`w')/`w'
    generate `varx' = (s`w'.`x2s' - s`w'.`xs'*s`w'.`xs'/`w')/`w'
    generate `vary' = (s`w'.`y2s' - s`w'.`ys'*s`w'.`ys'/`w')/`w'

    * generate rho
    generate rho = `covxy'/sqrt(`varx')/sqrt(`vary')

end




On Sat, Sep 1, 2012 at 5:45 PM, Alexandre Athanassiadis
<msf11aa3@mail.wbs.ac.uk> wrote:
> Dear All,
>
>
> I hope you are well.
>
> In the context of my research, I am using a rather large dataset (730
> corporates, their daily stock price for the 2003-2011 period and the
> risk free rate for the same period). Among other things, I am trying
> to compute the rolling correlation coefficients of the stock prices
> with the risk free rate.
>
> I have declared the data to be 'panel' without any difficulties:
> - egen id=group(tic)
> - xtset id Date, daily
>
> and I am using the following line to compute the correlation coefficients:
> - rolling, window(20) clear; corr price rate
>
> Unfortunately it take ages to get the job done! Any ideas on how I
> could improve my method?
>
> Thank you very much for your help,
>
> Regards,
>
> Alexandre
> *
> *   For searches and help try:
> *   http://www.stata.com/help.cgi?search
> *   http://www.stata.com/support/statalist/faq
> *   http://www.ats.ucla.edu/stat/stata/
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/


© Copyright 1996–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   Site index