Page 1 of 1
how to get monthly correlation coefficient by daily data
Posted: Wed Jun 29, 2011 3:25 am
by cklint
Following examples are my daily interest rates from 2000-01-02 to 2010-01-31.
I like to obtain correlation coefficient between two variable within each month. How should I do? Do I have to make program?
Thanks in advance.
----------------------------------
data(yyyy-mm-dd) us interest rate uk interest rate
2000-01-02 5.56 6.75
2000-01-02 5.89 6.89
'
'
'
2000-01-31 7.56 9.22
2000-02-01 7.42 8.99
'
'
2000-02-28 8.56 10.35
2000-03-01 8.99 10.44
'
'
2010-01-01 3.25 4.22
'
'
'
2010-01-31 3.00 4.00
Re: how to get monthly correlation coefficient by daily data
Posted: Wed Jun 29, 2011 7:49 am
by EViews Gareth
Yes, you'll have to make a program that loops through the months, calculating and storing the coefficient each time.
Re: how to get monthly correlation coefficient by daily data
Posted: Thu Jun 30, 2011 9:54 am
by EViews Glenn
Not quite true. I think we can do better. You probably should still make a program, but I think we can avoid the loops.
The following set of commands computes the by-month correlation for the two series and matches them back to the original month in the workfile.
Code: Select all
' set sample to all (I'm assuming that's what you want)
smpl @all
' squared deviations from monthly mean
series xdev = x - @meansby(x, @year, @month)
series ydev = y - @meansby(y, @year, @month)
series xydev = xdev * ydev
' covariances
series xvar = @varsby(x, @year, @month)
series yvar = @varsby(y, @year, @month)
series xycov = @meansby(xydev, @year, @month)
' correlations
series xycorr = xycov / (@sqrt(xvar)*@sqrt(yvar))
Note that each observation in the workfile has the corresponding value of the correlation in XYCORR. If you want to get just the set of monthy correlations without repeats, we'll have to subset the series XYCORR. The following code gets the first observation of our correlations in each month and exports to a vector:
Code: Select all
series first = (@date = @minsby(@date, @year, @month))
smpl if first = 1
stom(xycorr, corrvec)
My quick check indicates that this works, but you should do some double checking to make sure that it works for you.
Re: how to get monthly correlation coefficient by daily data
Posted: Thu Jul 07, 2011 7:31 am
by cklint
Thank you very much. It's great. I think that the function of aggregation should be somewhat extended in user-friendly ways.
Re: how to get monthly correlation coefficient by daily data
Posted: Fri Mar 27, 2015 3:20 am
by econworker
Would I ask what is the difference of this approach with rolling correlation by movecor command?
Re: how to get monthly correlation coefficient by daily data
Posted: Fri Mar 27, 2015 7:01 am
by EViews Gareth
The movcor command requires a fixed window length. Months do not have a fixed number of days.
Re: how to get monthly correlation coefficient by daily data
Posted: Mon Apr 13, 2015 7:38 am
by econworker
The movcor command requires a fixed window length. Months do not have a fixed number of days.
Correct, months dont have fixed number of days, but then is it possible to generate a monthly roling correlation from weekly data (number of weeks are not fixed), that the correlation values of each week dont be fixed for the Whole month?
Thank you
Re: how to get monthly correlation coefficient by daily data
Posted: Mon Apr 13, 2015 8:07 am
by EViews Gareth
I don't understand your question.