Changeset 196

Show
Ignore:
Timestamp:
05/24/08 00:05:18 (7 months ago)
Author:
edsuom
Message:

Huge progress, got PMC running with noticably less variance in population likelihoods

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • projects/AsynCluster/trunk/doc/svpmc/example/svpmc.conf

    r175 r196  
    2525 
    2626 
    27 Jumps 
     27Variable    Value           Description 
    2828------------------------------------------------------------------------------- 
    29 0.25, 0.05, 0.01, 0.002 
    30  
     29D           6               Number of jump deviations in the D-kernel sim 
     30Df          0.2             Fractional value of each dev from prev (or 1.0) 
     31wiggle      0.5             Range +/- of uniform random walks on z for h sim 
     32N1          30              Proposals on z for each importance-sampling of h 
     33N2          20              Hybrid-Gibbs iterations for h sim 
    3134 
    3235 
  • projects/AsynCluster/trunk/svpmc/model.c

    r192 r196  
    323323 
    324324// The number of time series making up a single multivariate sample 
    325 const int Nt = Nz[0]; 
     325const int Nt = Nx[0]; 
    326326// The number of multivariate samples 
    327 const int Ns = Nz[1]; 
     327const int Ns = Nx[1]; 
    328328 
    329329int ki, k0, k1, k2, k3; 
     
    371371 
    372372  // The "previous" log-volatility of the first time-series sample is set to 
    373   // the log-volatility offset. 
    374   // 
    375   // TODO: Treat it as another latent parameter and simulate its value 
     373  // the log-volatility offset plus a simulated, latent-parameter value. 
     374  // TODO 
    376375  for(k0=0; k0<np; k0++) { 
    377376    for(k1=0; k1<Nt; k1++) 
  • projects/AsynCluster/trunk/svpmc/model.py

    r195 r196  
    6464    ########################################################################### 
    6565    """ 
    66     def __init__(self, projectManager, y): 
    67         self.modelObj = Model(paramNames=projectManager.paramNames, y=y) 
     66    def __init__(self, projectManager, y, **kw): 
     67        kw['y'] = y 
     68        kw['paramNames'] = projectManager.paramNames 
     69        self.modelObj = Model(**kw) 
    6870        self.queue = projectManager.queue 
    6971 
     
    307309        sc[:,1] = s.log(s.sqrt(2*s.pi) * sc[:,0]) 
    308310 
     311        print self.wiggle, self.N1, self.N2, self.Ne 
    309312        # Run the hybrid Gibbs rounds, returning the likelihood from the last 
    310313        # one along with the state of the IID variate vector at that point. 
  • projects/AsynCluster/trunk/svpmc/params.py

    r195 r196  
    268268    the only pertinent parameter is the constant, unvarying I{loc}. 
    269269 
    270     @ivar V: A 1-D array  of all the jump deviations that are used in 
    271       computing probability densities of jumps 
    272  
    273270    """ 
    274271    keyAttrs = {'V':None} 
     
    322319 
    323320    def _newDraw(self): 
     321        D = len(self.V) 
    324322        rJumps = self.rdsObj.rvs(self.N_draw) 
    325         V_inv = 1.0 / self.V 
    326         pJumps = self.rdsObj.pdf( 
    327             s.kron(rJumps, V_inv).reshape(self.N_draw, len(V_inv))) 
    328         pJumps *= V_inv 
     323        # Right side 
     324        V = self.V[::-1] 
     325        pJumpsL = self.rdsObj.pdf( 
     326            s.kron(rJumps, V).reshape(self.N_draw, D)) 
     327        # Left side 
     328        V = 1.0/V[:0:-1] 
     329        pJumpsR = self.rdsObj.pdf( 
     330            s.kron(rJumps, V).reshape(self.N_draw, D-1)) 
     331        pJumps = s.column_stack([pJumpsL, pJumpsR]) 
    329332        return {'k':-1, 'N':self.N_draw, 'R':rJumps, 'P':pJumps} 
    330          
     333     
    331334    def _rJumpDraw(self): 
    332335        if 'draw' not in self.cache: 
     
    376379        each possible jump deviation in the I{p} array of the result. 
    377380        """ 
     381        D = len(self.V) 
    378382        for k in xrange(self.N_attempts): 
    379             jumpValue, pV = self._rJumpDraw() 
     383            jumpValue, P = self._rJumpDraw() 
    380384            jumpValue *= self.V[vIndex] 
     385            pV = P[D-vIndex-1:2*D-vIndex-1] / self.V 
    381386            x = startValue + jumpValue 
    382387            px = self.pdf(x) 
     
    384389                break 
    385390        else: 
     391            print "Oops" 
    386392            # Could't come up with a jump from this point, fall back to an 
    387393            # independent draw from the prior distribution 
    388394            x = self.rvs() 
    389395            px = self.pdf(x) 
    390             pV = px * s.ones_like(self.V
     396            pV = px * s.ones(D
    391397        return self.Jump(x, px, pV) 
    392398 
     
    402408 
    403409    @ivar paramNames: A list of the parameter names in sequence. 
    404  
    405     @ivar V: A 1-D array  of all the jump deviations that are used in 
    406       computing probability densities of jumps made via my prior objects. 
    407410     
    408411    """ 
    409412    typeChecking = True 
    410413     
    411     def __init__(self, p, n, V): 
     414    def __init__(self, p, n): 
    412415        self.p = p 
    413416        self.n = n 
    414         self.V = V 
    415417     
    416418    def priorArray(self, paramName, *shape): 
     
    455457        I{vWeights} array. 
    456458        """ 
    457         if vIndex < 0 or vIndex >= len(self.V): 
    458             raise ValueError("Invalid jump deviation index %d" % vIndex) 
    459         if getattr(vWeights, 'shape', 0) != self.V.shape: 
    460             raise ValueError( 
    461                 "Must supply a mixture weight array with one element per "+\ 
    462                 "jump deviation") 
    463459        if self.typeChecking and \ 
    464460               not isinstance(paramContainer, ParameterContainer): 
     
    479475            jumpsFA = priorsFA.call('rJump', startValues, vIndex) 
    480476            Lp += s.log(jumpsFA.px).sum() 
    481             for k in xrange(len(self.V)): 
     477            for k in xrange(len(vWeights)): 
    482478                pJumps[k] *= jumpsFA.pV.call('item', k).prod() 
    483479            setattr(newVersion, name, jumpsFA.x) 
  • projects/AsynCluster/trunk/svpmc/project.py

    r193 r196  
    6767      sequences used in the project. 
    6868 
    69     @ivar V: A 1-D array of the jump deviations used by the D-kernel PMC 
     69    @ivar D: The number of jump deviations used by the D-kernel PMC 
    7070      inference engine that will be running the project. 
     71 
     72    @ivar Df: The value of a given jump deviation as a fraction of the previous 
     73      one (the first value is 1.0) 
    7174 
    7275    @ivar priors: An instance of L{params.PriorContainer} constructed in 
     
    8790        self.iteration = 0 
    8891        #--- Use the NullQueue for debugging, thread queue normally ----------- 
    89         self.queue = NullQueue() 
    90         #self.queue = asynqueue.ThreadQueue(1) 
     92        #self.queue = NullQueue() 
     93        self.queue = asynqueue.ThreadQueue(1) 
    9194        #---------------------------------------------------------------------- 
    9295        specDir = os.path.dirname(specFile) 
    9396        self.tables = self._parseSpec(specFile) 
    94         # Jump Deviations 
    95         self.V = s.array([ 
    96             float(x) for x in self.tables['jumps'][0][0].split(',')]) 
     97        # Set some simulation parameters 
     98        for name, value, null in self.tables['variable']: 
     99            if "." in value: 
     100                value = float(value) 
     101            else: 
     102                value = int(value) 
     103            setattr(self, name, value) 
     104        V = [1.0] 
     105        for k in xrange(self.D-1): 
     106            V.append(self.Df * V[-1]) 
     107        self.V = s.array(V) 
    97108        # Observations 
    98109        tsData, seriesTitles = self._setupTimeSeries(specDir) 
     
    100111        # Parameters 
    101112        paramTitles, dimensions = self._setupParams() 
    102         self.mgr = model.ModelManager(self, tsData) 
     113        self.mgr = model.ModelManager( 
     114            self, tsData, wiggle=self.wiggle, N1=self.N1, N2=self.N2) 
    103115        self._setupCDF( 
    104116            os.path.join(specDir, ncFileName), 
     
    206218        titles, dimensions = [], [] 
    207219        xcorrs = self.xcorrs = int(0.5*(self.p**2 + self.p)) 
    208         self.priors = params.PriorContainer(self.p, self.n, self.V
     220        self.priors = params.PriorContainer(self.p, self.n
    209221        for name, dims, title in self.tables['parameter']: 
    210222            shape = parseDims(dims) 
  • projects/AsynCluster/trunk/svpmc/test/test_params.py

    r194 r196  
    221221 
    222222class Test_Prior(util.TestCase): 
     223    def setUp(self): 
     224        self.V = s.array([1.0, 0.1, 0.01]) 
     225        self.p = params.Prior(dname='norm', loc=1, scale=1, D=3, Df=0.1) 
     226        self.p.N_draw = 10 
     227        self.pdf = self.p.rdsObj.pdf 
     228 
    223229    def test_uniformPrior(self): 
    224230        p = params.Prior(dname='uniform', loc=1, scale=2) 
     
    229235        self.failUnless(s.equal(s.array([p.pdf(x) for x in X]), 0.5).all()) 
    230236 
     237    def _checkDraw(self, rv, pV): 
     238        pdf = self.p.rdsObj.pdf 
     239        for k, v in enumerate(self.V): 
     240            self.failUnlessAlmostEqual(pdf(rv/v)/v, pV[k], 5) 
     241 
     242    def test_newDraw(self): 
     243        draw = self.p._newDraw() 
     244        k = draw['k'] 
     245        while k < draw['N']: 
     246            self._checkDraw(draw['R'][k], draw['P'][k]) 
     247            k += 1 
     248 
     249    def test_rJumpDraw(self): 
     250        for k in xrange(103): 
     251            rv, pV = self.p._rJumpDraw() 
     252            self._checkDraw(rv, pV) 
     253 
     254    def test_rJump(self): 
     255        for startValue in s.linspace(-1, +3, 10): 
     256            for vIndex, v in enumerate(self.V): 
     257                jumpObj = self.p.rJump(startValue, vIndex) 
     258                print "\n", vIndex, v 
     259                jumpValue = jumpObj.x - startValue 
     260                rv = jumpValue / v 
     261                print jumpValue, rv, jumpObj.pV[vIndex] 
     262                self.failUnlessAlmostEqual(self.pdf(rv)/v, jumpObj.pV[vIndex]) 
     263                print "OK" 
     264                 
    231265 
    232266class Test_PriorContainer(util.TestCase):