Changeset 158

Show
Ignore:
Timestamp:
04/25/08 00:24:02 (9 months ago)
Author:
edsuom
Message:

Got log-volatility Metropolis-within-Gibbs simulation working!!!

Files:

Legend:

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

    r157 r158  
    9595double norm, normp, L, Lp, dL; 
    9696 
     97// Seed the PRNG on the first run only 
     98static int seeded = 0; 
     99if (!seeded) { 
     100  int i; 
     101  int j = 0; 
     102  unsigned short seed[3]; 
     103  for(i=0;i<3;i++) { 
     104    do { 
     105      j++; 
     106    } while (j < (i+1)*10000); 
     107    seed[i] = clock()*clock() % 65537; 
     108  } 
     109  seed48(seed); 
     110  seeded = 1; 
     111} 
    97112 
    98113// for each multivariate sample... 
     
    113128    for(k2=0; k2<M; k2++) { 
    114129      // Generate a uniform, symmetric, random walk proposal 
    115       normp = norm + (double)wiggle * (drand48() - 0.5); 
     130      normp = norm + (double) wiggle * (drand48() - 0.5); 
    116131      // Do the ratio in log space 
    117132      Lp = -0.5 * pow(normp, 2); 
  • projects/AsynCluster/trunk/svpmc/model.py

    r157 r158  
    128128                paramContainer.pLogLikelihood = -s.inf 
    129129            up = pack.Unpacker(result) 
    130             paramContainer.pLogLikelihood = up() 
    131             paramContainer.latent = up() 
     130            paramContainer.L = up() 
     131            paramContainer.z = up() 
    132132 
    133133        def gotLikelihood(result): 
    134             paramContainer.pLogLikelihood = result[0] 
    135             paramContainer.latent = result[1] 
     134            paramContainer.L = result[0] 
     135            paramContainer.z = result[1] 
    136136         
    137137        if (remote or self.remoteMode) and not local: 
     
    150150    Construct me with a 2-D array of time series values, one time series per 
    151151    row. 
    152  
     152     
    153153    Then you can call me with a C{p x 1} drift vector I{a} and a C{p x p} 
    154154    VAR(1) cross-correlation matrix I{b}, to get a C{p x n} vector of residuals 
     
    191191    _logU_index = -1 
    192192 
    193     keyAttrs = {'tsList':None, 'Mv':100, 'Mz':100} 
     193    keyAttrs = {'tsList':None, 'Mv':50, 'Mz':50} 
    194194    paramNames = params.PARAM_NAMES 
    195195 
     
    250250        return cv 
    251251 
    252     def decaySamples(self, x, tol): 
    253         """ 
    254         Returns the number of samples needed for the effect of an impulse to 
    255         the VAR(1) process having the supplied lag-1 correlation matrix I{x} to 
    256         decay below I{tol}. 
    257         """ 
    258         return s.ceil(s.log(tol) / s.log(x.diagonal())).max() 
     252    def decaySamples(self, tol): 
     253        """ 
     254        Returns as an int the number of samples needed for the effect of an 
     255        impulse to the log-volatility VAR(1) process to decay below I{tol}. 
     256        """ 
     257        if 'ds' not in self.cache: 
     258            p = self.e.shape[0] 
     259            # The impulse 
     260            x = s.column_stack([s.ones(p), s.zeros((p, self.n))]) 
     261            # Simulate the impulse response with a VAR object 
     262            self.cache['ds'] = VAR(x).forward(x, s.zeros(p), self.e).max(0) 
     263        I = s.less(self.cache['ds'], tol).nonzero()[0] 
     264        if len(I): 
     265            return I.min() 
     266        return self.n 
    259267     
    260268    def logUniform(self): 
     
    321329        return L0, L, h[:,1:] 
    322330 
    323     def hybridGibbs(self, z, x, sigma, N=100): 
     331    def hybridGibbs(self, z, x, sigma, tol=1E-4): 
    324332        """ 
    325333        Does one iteration of a hybrid Gibbs sampler for the log-volatilities, 
     
    328336        Returns the likelihood of the innovations in I{x} given the simulated 
    329337        log-volatilities. Updates the IID normal variates in place. 
     338 
     339        The method will account for the effect of log-volatility proposals on 
     340        downstream samples. You can specify the maximum fractional effect to 
     341        account for with I{tol}. Smaller tolerances require more post-sample 
     342        log-volatility recomputations and hence more CPU time. 
    330343        """ 
    331344        L_total = 0 
    332345        acceptances = 0 
    333346        rv = self.rv 
     347        N = self.decaySamples(tol) 
    334348        # Initialize volatility shocks from cross-correlation of the IID 
    335349        # variates... 
     
    369383        Call this method with a parameter object that defines: 
    370384 
    371             1. a set of values for my parameters, and 
    372  
    373             2. a latent parameter consisting of an array of IID normal 
    374               variates to be used as a starting point for another simulation 
    375               draw of log volatilities, and 
    376  
    377             3. a 1-D array of log-likelihoods for each observation time given 
    378                the IID variates after cross-correlation and VAR(1). 
     385            1. A set of values for my parameters. 
     386 
     387            2. A latent parameter I{z} consisting of an array of IID normal 
     388               variates to be used as a starting point for another simulation 
     389               draw of log volatilities. 
     390 
     391            3. The log-likelihood I{L} of my observations given the parameters 
     392               and after this method has done a simulation draw of 
     393               log-volatilities conditional on those parameter values and 
     394               starting with the IID variates in I{z}. 
    379395 
    380396        A new 2-D array of log-volatilities will be drawn from a hybrid Gibbs 
     
    400416        self.setParamSequence(paramContainer.paramSequence()) 
    401417        # ...including the latent parameter of IID normal variates 
    402         z = paramContainer.latent.copy() 
     418        z = paramContainer.z.copy() 
    403419        # Innovations as residuals of the reversed VAR(1) 
    404420        x = self.var.reverse(self.a, self.b) 
  • projects/AsynCluster/trunk/svpmc/params.py

    r151 r158  
    239239    """ 
    240240    """ 
    241     keyAttrs = {'latent':[], 'pLogLikelihood':None, 'pLogProposal':None} 
     241    keyAttrs = {'z':[], 'L':None} 
    242242    paramNames = PARAM_NAMES 
    243243 
    244244    def __add__(self, other): 
    245         newVersion = self.__class__(latent=self.latent
     245        newVersion = self.__class__(z=self.z
    246246        newVersion.setParamSequence([ 
    247247            getattr(self, name) + getattr(other, name) 
  • projects/AsynCluster/trunk/svpmc/test/test_model.py

    r157 r158  
    288288        if VERBOSE: 
    289289            self.fig.add_subplot(111).plot(z1, z2) 
     290 
     291    def test_decaySamples(self): 
     292        x = s.zeros((5, 1000)) 
     293        x[2,0] = 1.0 # The impulse 
     294        var = model.VAR(x) 
     295        a = s.zeros(5) 
     296        modelObj = model.Model(tsList=util.TS_LIST) 
     297        for j in xrange(20): 
     298            # Keep all coefficients small enough to ensure stability 
     299            b = modelObj.e = 0.3*s.rand(5,5) 
     300            y = var.forward(x, a, b) 
     301            for tol in s.logspace(-5, -1, 10): 
     302                N = modelObj.decaySamples(tol) 
     303                print tol, N 
     304                failed = abs(y[:,N].max()) > tol 
     305                if (N > 100 or failed) and VERBOSE: 
     306                    N_plot = min([3*N, 30]) 
     307                    sp = self.fig.add_subplot(111) 
     308                    sp.semilogy(y[:,:N_plot].transpose()) 
     309                    sp.axvline(N) 
     310                    sp.axhline(tol) 
     311                    self.failIf( 
     312                        failed, 
     313                        "Estimate of %d samples " % N +\ 
     314                        "to decay within %f tolerance was insufficient" % tol) 
    290315         
    291316 
     
    507532 
    508533 
    509 class Test_Model_hybridGibbs(Model_BC_Mixin, Multivariate_BaseCase): 
    510     N = 10000 
    511     corr = 0.0 
    512     params = { 
    513         'a': [0], 
    514         'b': [[0]], 
    515         'c': [], 
    516         'd': [0.01], 
    517         'e': [[0.0]], 
    518         'f': [], 
    519         'g': [corr], 
    520         } 
    521  
    522  
    523534class Test_Model_likelihood(Model_BC_Mixin, Multivariate_BaseCase): 
    524     N = 500 
     535    N = 3000 
    525536    corr = 0.0 
    526537    params = {