Changeset 149

Show
Ignore:
Timestamp:
04/17/08 17:11:23 (8 months ago)
Author:
edsuom
Message:

Working on model.likelihood

Files:

Legend:

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

    r148 r149  
    281281      observation data. 
    282282 
    283     """ 
     283    @cvar M: Number of volatility draws per likelihood evaluation. 
     284 
     285    """ 
     286    _logU_index = -1 
     287 
     288    M = 100 
    284289    keyAttrs = {'tsList':None} 
    285290    paramNames = [ 
     
    382387        return self.inline('y', 'x', 'r', y=s.empty_like(x)) 
    383388 
    384     def pInnovations(self, y): 
     389    def likelihoodOfInnovations(self, y): 
    385390        """ 
    386391        Call this method with a C{[pn]} array I{y} of uncorrelated 
    387392        innovations. 
    388393 
    389         The result will be the probability density of each innovation, given my 
    390         volatility/innovation shock correlations C{g} and the volatility shocks 
    391         currently in my random walker C{nw}. 
    392         """ 
    393          
     394        The result will be the total log-likelihood of the innovations, given 
     395        my volatility/innovation shock correlations C{g} and the volatility 
     396        shocks currently in my random walker C{nw}. A constant term in the 
     397        log-likelihood is disregarded, C{sqrt(2*pi)}. 
     398        """ 
     399        mu = self.g * self.nw.x 
     400        sigma2 = 1 - self.g**2 
     401        logProbs = -(y - mu)**2  / (2*sigma2) - 0.5 * s.log(sigma2) 
     402        return logProbs.sum() 
     403 
     404    def logUniform(self, N): 
     405        """ 
     406        Returns a log-uniform variate taken from a large array that is 
     407        generated and refreshed periodically. Generating the uniform variates 
     408        and taking their log in a single large array operation is more 
     409        efficient than doing so one value at a time. 
     410        """ 
     411        logU = getattr(self, '_logU_array', []) 
     412        if self._logU_index >= len(logU): 
     413            self._logU_index = -1 
     414            # The efficient array operation 
     415            logU = self._logU_array = s.log(s.rand(10000)) 
     416        self._logU_index += 1 
     417        return logU[self._logU_index] 
    394418 
    395419 
     
    424448        # Obtain innovations as residuals of the reversed VAR(1) 
    425449        x = self.var.reverse(self.a, self.b) 
    426         # Simulate log-volatilities for a while 
    427         h = self.draw_h(v, wiggle) 
    428         # Compute the conditional densities 
    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 
     450        # MCMC simulation of log-volatilities 
     451        L = -s.inf 
     452        for j in xrange(self.M): 
     453            # Draw a proposal array of log-volatilities 
     454            h = self.draw_h(v, wiggle) 
     455            # Compute the total log-likelihood conditional on the proposed 
     456            # log-volatilities 
     457            Lp = self.likelihoodOfInnovations(self.decorrelate(x, h)) 
     458            # Metropolis-Hastings accept/reject of the proposal 
     459            if Lp > L or (Lp-L) > self.logUniform(): 
     460                L = Lp 
     461                vShocks = self.nw.x.copy() 
     462        return L, vShocks 
  • projects/AsynCluster/trunk/svpmc/sample.py

    r147 r149  
    7474    I run a 2-D array of values through a random walk within a zero-mean, unit 
    7575    variance normal distribution. 
     76 
     77    @ivar x: My current random walk values. 
     78     
    7679    """ 
    7780    m = 10 
  • projects/AsynCluster/trunk/svpmc/test/test_model.py

    r148 r149  
    357357        self.model = model.Model() 
    358358 
    359     def test_pInnovations(self): 
     359    def test_likelihoodOfInnovations(self): 
     360        self.fail("TODO") 
     361 
     362    def test_logUniform(self): 
    360363        self.fail("TODO") 
    361364