Changeset 148

Show
Ignore:
Timestamp:
04/16/08 20:05:10 (8 months ago)
Author:
edsuom
Message:

Refined testing of Model's multivariate stuff; working on pInnovation

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/AsynCluster/trunk/svpmc/model.py

    r147 r148  
    391391        currently in my random walker C{nw}. 
    392392        """ 
     393         
    393394 
    394395 
     
    426427        h = self.draw_h(v, wiggle) 
    427428        # Compute the conditional densities 
    428          
     429        p = self.pInnovations(self.decorrelate(x, h)) 
     430        # Return the log-likelihood and the underlying volatility shocks 
     431        return s.exp(p).sum(), self.nw.x 
  • projects/AsynCluster/trunk/svpmc/test/test_model.py

    r147 r148  
    131131 
    132132 
    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)]) 
     133class 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 
     170class Test_VAR(Multivariate_BaseCase): 
     171    def setUp(self): 
    137172        self.var = model.VAR(self.y) 
    138173 
     
    155190        v = s.randn(2, n) 
    156191        y = self.var.forward(v, a, b) 
     192        # Uncorrelated 
     193        self._check_uncorr(y[0,:], y[1,:]) 
    157194        # IIR filtering 
    158195        fig = self.fig 
     
    165202            sp = fig.add_subplot(221 + 2*k + 1) 
    166203            sp.plot(y[k,:]) 
    167         # Uncorrelated 
    168         r = stats.spearmanr(y[0,:], y[1,:])[0] 
    169         self.failUnless(abs(r) < 0.1, "Doesn't appear uncorrelated") 
    170204 
    171205    def test_forward_xcorr(self): 
     
    213247 
    214248 
    215 class Test_Model_draw_h(util.TestCase): 
     249class Test_Model_draw_h(Multivariate_BaseCase): 
    216250    def setUp(self): 
    217251        self.model = model.Model(tsList=util.TS_LIST) 
     
    238272        for j in xrange(20): 
    239273            h = self.model.draw_h(0.5) 
     274        # Uncorrelated 
     275        self._check_uncorr(h[0,:], h[1,:]) 
    240276        # IIR filtering 
    241277        fig = self.fig 
     
    248284            sp = fig.add_subplot(221 + 2*k + 1) 
    249285            sp.plot(h[k,:]) 
    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 
    271287    def test_xcorr_var(self): 
    272288        self.model.d = s.array([0.0, 0.0]) 
     
    282298 
    283299 
    284 class Test_Model_decorrelate(util.TestCase): 
     300class Test_Model_decorrelate(Multivariate_BaseCase): 
    285301    def setUp(self): 
    286302        self.model = model.Model() 
    287303 
    288304    def test_basic(self): 
    289         n = 1000 
    290         for p in xrange(2, 6): 
    291             for correlation in s.linspace(0.1, 0.9, 10): 
     305        n = 10000 
     306        for p in xrange(2, 4): 
     307            # Tweak the model object to make it testable for this p value 
     308            self.model.tsList = [util.TS_LIST[0]] * p 
     309            for correlation in s.linspace(0.1, 0.9, 5): 
    292310                # Generate some multivariate normal test data 
    293311                r = s.maximum(s.identity(p), correlation*s.ones((p,p))) 
     312                print "\n\n", p 
     313                print r 
    294314                x = random.multivariate_normal(s.zeros(p), r, n).transpose() 
    295                 # Tweak the model object to make it testable for this p value 
    296                 self.model.tsList = [util.TS_LIST[0]] * p 
     315                # Set the model's innovation shock correlations parameter 
    297316                self.model.c = correlation * s.ones( 
    298317                    sum([p-k-1 for k in xrange(p)])) 
     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() 
    299345                # Get the decorrelated values 
    300346                y = self.model.decorrelate(x) 
     
    304350                        if j == k: 
    305351                            continue 
    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 
     355class 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") 
  • projects/AsynCluster/trunk/svpmc/test/test_sample.py

    r147 r148  
    168168            self.check(y1) 
    169169            self.check(y2) 
    170             corrInfo = stats.pearsonr(y1, y2) 
     170            p = stats.pearsonr(y1, y2)[1] 
    171171            self.failUnless( 
    172                 abs(corrInfo[0]) < 0.1
    173                 "Expected correlation of 0, not %f" % corrInfo[0]
     172                p > 0.05
     173                "'Uncorrelated' hypothesis fails with significance p=%f" % p
    174174            if VERBOSE: 
    175175                self.fig.add_subplot(111).plot(y1, y2)