| 133 | | class Test_VAR(util.TestCase): |
|---|
| 134 | | def setUp(self): |
|---|
| 135 | | self.y = s.row_stack([ |
|---|
| 136 | | util.TS_LIST[k].intersect(util.TS_LIST[1-k])() for k in (0,1)]) |
|---|
| | 133 | class Multivariate_BaseCase(util.TestCase): |
|---|
| | 134 | y = s.row_stack([ |
|---|
| | 135 | util.TS_LIST[k].intersect(util.TS_LIST[1-k])() for k in (0,1)]) |
|---|
| | 136 | |
|---|
| | 137 | def _check_uncorr(self, x, y, plot=True): |
|---|
| | 138 | corrInfo = stats.spearmanr(x, y) |
|---|
| | 139 | failed = abs(corrInfo[0]) > 0.1 and corrInfo[1] < 0.05 |
|---|
| | 140 | regressInfo = stats.linregress(x, y) |
|---|
| | 141 | if (plot and VERBOSE) or failed: |
|---|
| | 142 | m, b = regressInfo[:2] |
|---|
| | 143 | sp = self.fig.add_subplot(111) |
|---|
| | 144 | sp.plot(x, y, 'o-', x, m*x+b, '-') |
|---|
| | 145 | sp.grid(True) |
|---|
| | 146 | sp.set_title("r=%f, p=%f" % corrInfo) |
|---|
| | 147 | if failed: |
|---|
| | 148 | self.fail( |
|---|
| | 149 | "Correlation is %f; " % corrInfo[0] +\ |
|---|
| | 150 | "'Uncorrelated' hypothesis fails with p=%f" % corrInfo[1]) |
|---|
| | 151 | |
|---|
| | 152 | def _check_xcorr(self, dPeak): |
|---|
| | 153 | n = self.model.n |
|---|
| | 154 | for j in xrange(20): |
|---|
| | 155 | h = self.model.draw_h(0.5) |
|---|
| | 156 | # Correlation |
|---|
| | 157 | nd = 6 |
|---|
| | 158 | d = s.arange(-nd, nd+1) |
|---|
| | 159 | r = s.array( |
|---|
| | 160 | [s.correlate(h[0,nd+lag:n-nd+lag], h[1,nd:n-nd])[0] for lag in d]) |
|---|
| | 161 | lagMetric = r[nd+dPeak] / \ |
|---|
| | 162 | max(s.concatenate([r[:nd+dPeak], r[nd+dPeak:]])) |
|---|
| | 163 | self.failUnless(lagMetric < 10) |
|---|
| | 164 | if VERBOSE: |
|---|
| | 165 | sp = self.fig.add_subplot(111) |
|---|
| | 166 | sp.plot(d, r, 'o-') |
|---|
| | 167 | sp.grid(True) |
|---|
| | 168 | |
|---|
| | 169 | |
|---|
| | 170 | class Test_VAR(Multivariate_BaseCase): |
|---|
| | 171 | def setUp(self): |
|---|
| 250 | | # Uncorrelated |
|---|
| 251 | | r = stats.spearmanr(h[0,:], h[1,:])[0] |
|---|
| 252 | | self.failUnless(abs(r) < 0.1, "Doesn't appear uncorrelated") |
|---|
| 253 | | |
|---|
| 254 | | def _check_xcorr(self, dPeak): |
|---|
| 255 | | n = self.model.n |
|---|
| 256 | | for j in xrange(20): |
|---|
| 257 | | h = self.model.draw_h(0.5) |
|---|
| 258 | | # Correlation |
|---|
| 259 | | nd = 6 |
|---|
| 260 | | d = s.arange(-nd, nd+1) |
|---|
| 261 | | r = s.array( |
|---|
| 262 | | [s.correlate(h[0,nd+lag:n-nd+lag], h[1,nd:n-nd])[0] for lag in d]) |
|---|
| 263 | | lagMetric = r[nd+dPeak] / \ |
|---|
| 264 | | max(s.concatenate([r[:nd+dPeak], r[nd+dPeak:]])) |
|---|
| 265 | | self.failUnless(lagMetric < 10) |
|---|
| 266 | | if VERBOSE: |
|---|
| 267 | | sp = self.fig.add_subplot(111) |
|---|
| 268 | | sp.plot(d, r, 'o-') |
|---|
| 269 | | sp.grid(True) |
|---|
| 270 | | |
|---|
| | 286 | |
|---|
| | 318 | # Get the decorrelated values from the model |
|---|
| | 319 | y = self.model.decorrelate(x) |
|---|
| | 320 | # Check for non-correlation |
|---|
| | 321 | for j in xrange(p): |
|---|
| | 322 | for k in xrange(p): |
|---|
| | 323 | if j == k: |
|---|
| | 324 | continue |
|---|
| | 325 | self._check_uncorr(y[j,:], y[k,:], plot=False) |
|---|
| | 326 | |
|---|
| | 327 | def test_complex(self): |
|---|
| | 328 | n = 10000 |
|---|
| | 329 | for p in xrange(2, 7): |
|---|
| | 330 | # Stuff specific to this number of time series |
|---|
| | 331 | N_correlations = sum([p-k-1 for k in xrange(p)]) |
|---|
| | 332 | cMax = 0.90 / s.sqrt(p/1.5) |
|---|
| | 333 | # Tweak the model object to make it testable for this p value |
|---|
| | 334 | self.model.tsList = [util.TS_LIST[0]] * p |
|---|
| | 335 | for i in xrange(10): |
|---|
| | 336 | # Randomly generate a set of cross-correlations and set the |
|---|
| | 337 | # model's innovation shock correlations parameter |
|---|
| | 338 | self.model.c = 0.05 + cMax * s.rand(N_correlations) |
|---|
| | 339 | # Generate some multivariate normal test data having the |
|---|
| | 340 | # generated correlations |
|---|
| | 341 | r = self.model.nw.covarMatrix(self.model.c) |
|---|
| | 342 | print "\n\n", p |
|---|
| | 343 | print r |
|---|
| | 344 | x = random.multivariate_normal(s.zeros(p), r, n).transpose() |
|---|
| 306 | | corrInfo = stats.spearmanr(y[j,:], y[k,:]) |
|---|
| 307 | | self.failUnless( |
|---|
| 308 | | abs(corrInfo[0]) < 0.1, |
|---|
| 309 | | "Doesn't appear uncorrelated") |
|---|
| | 352 | self._check_uncorr(y[j,:], y[k,:], plot=False) |
|---|
| | 353 | |
|---|
| | 354 | |
|---|
| | 355 | class Test_Model_other(Multivariate_BaseCase): |
|---|
| | 356 | def setUp(self): |
|---|
| | 357 | self.model = model.Model() |
|---|
| | 358 | |
|---|
| | 359 | def test_pInnovations(self): |
|---|
| | 360 | self.fail("TODO") |
|---|
| | 361 | |
|---|
| | 362 | def test_likelihood(self): |
|---|
| | 363 | self.fail("TODO") |
|---|