@@ -1045,14 +1045,14 @@ def pacf(x, nlags=None, method="ywadjusted", alpha=None):
1045
1045
@deprecate_kwarg ("unbiased" , "adjusted" )
1046
1046
def ccovf (x , y , adjusted = True , demean = True , fft = True ):
1047
1047
"""
1048
- Calculate the crosscovariance between two series.
1048
+ Calculate the cross-covariance between two series.
1049
1049
1050
1050
Parameters
1051
1051
----------
1052
1052
x, y : array_like
1053
1053
The time series data to use in the calculation.
1054
1054
adjusted : bool, optional
1055
- If True, then denominators for crosscovariance is n-k, otherwise n.
1055
+ If True, then denominators for cross-covariance are n-k, otherwise n.
1056
1056
demean : bool, optional
1057
1057
Flag indicating whether to demean x and y.
1058
1058
fft : bool, default True
@@ -1062,7 +1062,9 @@ def ccovf(x, y, adjusted=True, demean=True, fft=True):
1062
1062
Returns
1063
1063
-------
1064
1064
ndarray
1065
- The estimated crosscovariance function.
1065
+ The estimated cross-covariance function: the element at index k
1066
+ is the covariance between {x[k], x[k+1], ..., x[n]} and {y[0], y[1], ..., y[m-k]},
1067
+ where n and m are the lengths of x and y, respectively.
1066
1068
"""
1067
1069
x = array_like (x , "x" )
1068
1070
y = array_like (y , "y" )
@@ -1083,11 +1085,11 @@ def ccovf(x, y, adjusted=True, demean=True, fft=True):
1083
1085
d = n
1084
1086
1085
1087
method = "fft" if fft else "direct"
1086
- return correlate (xo , yo , "full" , method = method )[n - 1 :] / d
1088
+ return correlate (xo , yo , "full" , method = method )[n - 1 :] / d
1087
1089
1088
1090
1089
1091
@deprecate_kwarg ("unbiased" , "adjusted" )
1090
- def ccf (x , y , adjusted = True , fft = True ):
1092
+ def ccf (x , y , adjusted = True , fft = True , * , nlags = None , alpha = None ):
1091
1093
"""
1092
1094
The cross-correlation function.
1093
1095
@@ -1096,27 +1098,54 @@ def ccf(x, y, adjusted=True, fft=True):
1096
1098
x, y : array_like
1097
1099
The time series data to use in the calculation.
1098
1100
adjusted : bool
1099
- If True, then denominators for cross-correlation is n-k, otherwise n.
1101
+ If True, then denominators for cross-correlation are n-k, otherwise n.
1100
1102
fft : bool, default True
1101
1103
If True, use FFT convolution. This method should be preferred
1102
1104
for long time series.
1105
+ nlags : int, optional
1106
+ Number of lags to return cross-correlations for. If not provided,
1107
+ the number of lags equals len(x).
1108
+ alpha : float, optional
1109
+ If a number is given, the confidence intervals for the given level are
1110
+ returned. For instance if alpha=.05, 95 % confidence intervals are
1111
+ returned where the standard deviation is computed according to
1112
+ 1/sqrt(len(x)).
1103
1113
1104
1114
Returns
1105
1115
-------
1106
1116
ndarray
1107
- The cross-correlation function of x and y.
1117
+ The cross-correlation function of x and y: the element at index k
1118
+ is the correlation between {x[k], x[k+1], ..., x[n]} and {y[0], y[1], ..., y[m-k]},
1119
+ where n and m are the lengths of x and y, respectively.
1120
+ confint : ndarray, optional
1121
+ Confidence intervals for the CCF at lags 0, 1, ..., nlags-1 using the level given by
1122
+ alpha and the standard deviation calculated as 1/sqrt(len(x)) [1]. Shape (nlags, 2).
1123
+ Returned if alpha is not None.
1108
1124
1109
1125
Notes
1110
1126
-----
1111
- If adjusted is true, the denominator for the autocovariance is adjusted.
1127
+ If adjusted is True, the denominator for the cross-correlation is adjusted.
1128
+
1129
+ References
1130
+ ----------
1131
+ .. [1] Brockwell and Davis, 2016. Introduction to Time Series and
1132
+ Forecasting, 3rd edition, p. 242.
1112
1133
"""
1113
1134
x = array_like (x , "x" )
1114
1135
y = array_like (y , "y" )
1115
1136
adjusted = bool_like (adjusted , "adjusted" )
1116
1137
fft = bool_like (fft , "fft" , optional = False )
1117
1138
1118
1139
cvf = ccovf (x , y , adjusted = adjusted , demean = True , fft = fft )
1119
- return cvf / (np .std (x ) * np .std (y ))
1140
+ ret = cvf / (np .std (x ) * np .std (y ))
1141
+ ret = ret [:nlags ]
1142
+
1143
+ if alpha is not None :
1144
+ interval = stats .norm .ppf (1.0 - alpha / 2.0 ) / np .sqrt (len (x ))
1145
+ confint = ret .reshape (- 1 , 1 ) + interval * np .array ([- 1 , 1 ])
1146
+ return ret , confint
1147
+ else :
1148
+ return ret
1120
1149
1121
1150
1122
1151
# moved from sandbox.tsa.examples.try_ld_nitime, via nitime
0 commit comments