From f3264ece337194134fdce7c7194749f57cb45eb6 Mon Sep 17 00:00:00 2001 From: Trent Hauck Date: Sat, 29 Sep 2012 09:39:04 -0500 Subject: [PATCH 1/5] added j-lagged autocovariance function --- pandas/core/series.py | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 7400aa5bde2e7..16ce1aba543da 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1513,6 +1513,38 @@ def autocorr(self): """ return self.corr(self.shift(1)) + def autocov(self, j=1, unbiased=False): + """ + j-lag autocovariance + + Parameters + ---------- + j: int, default 1 + Periods to lag the covariance calculation by + unbiased : boolean, default False + If true return an unbiased estimator of the autocovariance + + Returns + ------- + autocov : float + """ + n = len(self) + + if abs(j) >= n: + raise Exception('lag parameter must be within the bounds of n') + if n == 0: + return np.nan + + this = self - self.mean() + shifted = this.shift(-j) + + if unbiased: + d = n - j + else: + d = n + + return float(np.correlate(self[:n - j], shifted[:n - j]) / d) + def clip(self, lower=None, upper=None, out=None): """ Trim values at input threshold(s) From a8894297eb0bf317525cb34e8eba5661bd52785c Mon Sep 17 00:00:00 2001 From: Trent Hauck Date: Sat, 29 Sep 2012 10:32:29 -0500 Subject: [PATCH 2/5] Added autocov() tests --- pandas/core/series.py | 4 +--- pandas/tests/test_series.py | 12 ++++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index 16ce1aba543da..aa9409f9162f5 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1530,9 +1530,7 @@ def autocov(self, j=1, unbiased=False): """ n = len(self) - if abs(j) >= n: - raise Exception('lag parameter must be within the bounds of n') - if n == 0: + if abs(j) >= n or n == 0 return np.nan this = self - self.mean() diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 9474f6061f936..47416bf376e29 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1841,6 +1841,18 @@ def test_cov(self): cp[:] = np.nan self.assert_(isnull(cp.cov(cp))) + def test_autocov(self): + + ts = Series([1, 2, 3] * 1000) + + #too big of lag + self.assert_(np.isnan(ts.autocov(j=len(ts) + 1))) + + lag = 1 + n = len(ts) + self.assertAlmostEqual(ts.var(), ts.autocov() / ts.autocorr()) + + def test_copy(self): ts = self.ts.copy() From 73abf6f109a71e9689fa23ce7403106318967af8 Mon Sep 17 00:00:00 2001 From: Trent Hauck Date: Sat, 29 Sep 2012 12:47:05 -0500 Subject: [PATCH 3/5] Tests pass for `autocov` --- pandas/core/series.py | 5 +++-- pandas/tests/test_series.py | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/pandas/core/series.py b/pandas/core/series.py index aa9409f9162f5..6919680e3c39d 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1530,7 +1530,7 @@ def autocov(self, j=1, unbiased=False): """ n = len(self) - if abs(j) >= n or n == 0 + if abs(j) >= n or n == 0: return np.nan this = self - self.mean() @@ -1541,7 +1541,8 @@ def autocov(self, j=1, unbiased=False): else: d = n - return float(np.correlate(self[:n - j], shifted[:n - j]) / d) + return float(np.correlate(this[:n - j].values, + shifted[:n - j].values)) / d def clip(self, lower=None, upper=None, out=None): """ diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index 47416bf376e29..c69c731254361 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1843,14 +1843,16 @@ def test_cov(self): def test_autocov(self): - ts = Series([1, 2, 3] * 1000) + ts = Series([1, 2, 3] * 2) #too big of lag self.assert_(np.isnan(ts.autocov(j=len(ts) + 1))) lag = 1 n = len(ts) - self.assertAlmostEqual(ts.var(), ts.autocov() / ts.autocorr()) + self.assertAlmostEqual(ts.autocov(j=0), 2.0/3) + self.assertAlmostEqual(ts.autocov(j=1), -1.0/6) + self.assertAlmostEqual(ts.autocov(j=2), -1.0/3) def test_copy(self): From 3fe107e9754ce5cab0b8af6360b739a7586bfe52 Mon Sep 17 00:00:00 2001 From: Trent Hauck Date: Sat, 29 Sep 2012 12:51:48 -0500 Subject: [PATCH 4/5] cleaned up test --- pandas/tests/test_series.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/pandas/tests/test_series.py b/pandas/tests/test_series.py index c69c731254361..1826916a985ba 100644 --- a/pandas/tests/test_series.py +++ b/pandas/tests/test_series.py @@ -1848,8 +1848,7 @@ def test_autocov(self): #too big of lag self.assert_(np.isnan(ts.autocov(j=len(ts) + 1))) - lag = 1 - n = len(ts) + #test calculations self.assertAlmostEqual(ts.autocov(j=0), 2.0/3) self.assertAlmostEqual(ts.autocov(j=1), -1.0/6) self.assertAlmostEqual(ts.autocov(j=2), -1.0/3) From 6769d6a6d66a2ec09ffc37ae36e3738b9b49db69 Mon Sep 17 00:00:00 2001 From: Trent Hauck Date: Sat, 29 Sep 2012 14:06:55 -0500 Subject: [PATCH 5/5] added see also for acovf from statsmodels --- pandas/core/series.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/pandas/core/series.py b/pandas/core/series.py index 6919680e3c39d..ac8fada304cf9 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -1524,6 +1524,11 @@ def autocov(self, j=1, unbiased=False): unbiased : boolean, default False If true return an unbiased estimator of the autocovariance + See Also + -------- + statsmodels.tsa.statstools.acovf for autocovariance function, which + returns an array of lagged autocovariances + Returns ------- autocov : float