DATA Step, Macro, Functions and more

Rolling pairwise correlations

Reply
Occasional Contributor mgm
Occasional Contributor
Posts: 10

Rolling pairwise correlations

[ Edited ]

I'm looking to calculate rolling window pairwise correlations, something in SAS that replicates the following python function. Please let me know if you need data and more examples ? 

 

Proc IML  doesn't do the trick either : 

proc iml;
	use work.ret_trans;
	read all var{date} into myDates;		
	read all var _NUM_ into X[colname=NumerNames];
do x=18 to nrow(myDates);
		dt=myDates[x];
		lagdt = intnx('month',dt,-18,'end');
		read all var _NUM_ where(date <= dt & date > lagdt ) into trailing18;
		
		idx = setdif(1:ncol(trailing18), 1); 
		
	pc = corr(trailing18);
	
	if x=18 then create work.PairWise from pc ;
		else edit work.PairWise ;
		append from pc;
	
  end;
quit;

 

pandas.rolling_corr pandas.rolling_corr(arg1, arg2=None, window=None, min_periods=None, freq=None, center=False, pairwise=None, how=None)

Moving sample correlation.

 

 

Parameters: Returns:

arg1 : Series, DataFrame, or ndarray

arg2 : Series, DataFrame, or ndarray, optional

if not supplied then will default to arg1 and produce pairwise output

window : int

Size of the moving window. This is the number of observations used for calculating the statistic.

min_periods : int, default None

Minimum number of observations in window required to have a value (otherwise result is NA).

freq : string or DateOffset object, optional (default None)

Frequency to conform the data to before computing the statistic. Specified as a frequency string or DateOffset object.

center : boolean, default False

Set the labels at the center of the window.

how : string, default ‘None’

Method for down- or re-sampling

pairwise : bool, default False

If False then only matching columns between arg1 and arg2 will be used and the output will be a DataFrame. If True then all pairwise combinations will be calculated and the output will be a Panel in the case of DataFrame inputs. In the case of missing elements, only complete pairwise observations will be used.

y : type depends on inputs

DataFrame / DataFrame -> DataFrame (matches on columns) or Panel (pairwise) DataFrame / Series -> Computes result for each column Series / Series -> Series

 

SAS Super FREQ
Posts: 3,475

Re: Rolling pairwise correlations

If you have SAS/ETS software, you can use PROC EXPAND. See the second example in Vora (2008):

http://www2.sas.com/proceedings/forum2008/093-2008.pdf

 

A DATA step solution is provided in the same paper.

 

Ask a Question
Discussion stats
  • 1 reply
  • 218 views
  • 2 likes
  • 2 in conversation