Changeset 191

Show
Ignore:
Timestamp:
05/22/08 17:21:43 (7 months ago)
Author:
edsuom
Message:

Figured out N1=10, N2=2, Ne=3 sim params

Files:

Legend:

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

    r190 r191  
    110110  // of h to this point 
    111111  double Lx; 
    112   // Change in Lx from this time-series sample to the previous one 
    113   double L_diff; 
    114112  // Multivariate normal proposal, which is just z for time-series samples 
    115113  // after the first in each log-volatility computation 
     
    227225   
    228226  // Now the log-likelihoood. For each time series... 
    229   S->L_diff = 0; 
    230227  for(k=0; k<N; k++) { 
    231228    // Offset the decorrelated innovation by the mean, which is the correlation 
     
    240237    val = SPA1(S, x0, k) / SPP2(P, sc, k, 0); 
    241238    // Fast squaring instead of pow() 
    242     S->L_diff -= 0.5 * val*val + SPP2(P, sc, k, 1); 
     239    S->Lx -= 0.5 * val*val + SPP2(P, sc, k, 1); 
    243240    // Since the innovation has been inverse-scaled by the square root of the 
    244241    // proposed volatility, the probability density needs to be scaled as 
    245242    // well. In log space, this is a subtraction of half the log-volatility. 
    246     S->L_diff -= 0.5 * SPA1(S, h, k); 
    247   } 
    248   S->Lx += S->L_diff; 
     243    S->Lx -= 0.5 * SPA1(S, h, k); 
     244  } 
    249245} 
    250246 
     
    329325 
    330326int ki, k0, k1, k2, k3; 
    331 double val, L_diff
     327double val
    332328double Lx = 0; 
    333329double Lh = 0; 
     
    450446      Z2(k1, k0) = zp[spSelection.kp]; 
    451447   
    452     //--- DEBUG ----------------------------------------------------------------- 
    453     //printf("\n%d %d\n---------------------------\n",k0, spSelection.kp); 
    454     //for(k1=0; k1<n; k1++) 
    455     //  printf("%d %f %f\n", k1, Lxk[k1], sp[k1].Lx); 
    456     //--------------------------------------------------------------------------- 
    457    
    458448    // Set the initial log-volatility of the proposals for the next time-series 
    459449    // sample to the log-volatility of the selected proposal for this time-series 
  • projects/AsynCluster/trunk/svpmc/test/test_model.py

    r190 r191  
    611611        return iv, h, x 
    612612 
     613    def _inlineVars(self, wiggle, eValue, N1, N2=1, Ne=3): 
     614        return { 
     615            'h':s.empty(1), 
     616            'sc':s.array([1, s.log(s.sqrt(2*s.pi))]), 
     617            'w':wiggle, 
     618            'np':N1, 
     619            'ni':N2, 
     620            'ne':Ne, 
     621            'd':s.array([0.0]), 
     622            'e':s.array([[float(eValue)]]), 
     623            'g':s.array([0.0]), 
     624            'rz':s.array([[1.0]]), 
     625            'ri':s.array([[1.0]]) 
     626            } 
     627     
    613628    def _modelAndParamFactory(self, wiggle, N1, N2, **kw): 
    614629        """ 
     
    642657            sp.grid(True) 
    643658     
    644     def _likelihood(self, eValue, wiggle, Ns, Nr, N1, N2, Ne, params): 
     659    def _likelihood(self, Ns, Nr, inlineVars): 
    645660        """ 
    646661        Runs C code for L{model.Model.likelihood} independent of the method 
     
    648663        """ 
    649664        z = s.zeros((1, Ns)) 
    650         params['z'] = z 
     665        inlineVars['z'] = z 
    651666        modelObj = model.Model() 
     667        eValue = inlineVars['e'][0][0] 
    652668        for j in xrange(Nr): 
    653669            iv, hReal, x = self._simData(Ns, eValue) 
    654             params['x'] = s.row_stack([x]) 
    655             modelObj.inlineDirect(modelObj.code['likelihood'], **params) 
     670            inlineVars['x'] = s.row_stack([x]) 
     671            modelObj.inlineDirect(modelObj.code['likelihood'], **inlineVars) 
    656672            hSim = self._z_to_h(z[0,:], eValue, 1.0) 
    657673            yield hReal, hSim 
     
    661677        eValue = 0.90 
    662678        Ns, N1, N2 = 10, 5, 20 
    663         x = 2*s.ones(Ns) 
    664         Lx, Lh, zSim = self._likelihood(x, eValue, wiggle, N1, N2) 
    665         print "\nk   Lx      Lh       Simulated Volatility Shocks" 
     679        inlineVars = self._inlineVars(wiggle, eValue, N1, N2) 
     680        inlineVars.update({'x':2*s.ones(Ns), 'z':s.zeros((1, Ns))}) 
     681        modelObj = model.Model() 
     682        print "\n  k      Lx       Lh     Simulated Volatility Shocks" 
    666683        print "-"*100 
    667         for k, z in enumerate(zSim): 
    668             zLine = "   ".join(["%+4.2f" % xx for xx in z]) 
    669             print "%3d: %+5.1f  %+6.1f%s%s" % (k, Lx[k], Lh[k], " "*5, zLine) 
    670  
    671     def test_highCorrelation(self): 
    672         # Empirically, it looks like Ne=3 is best, just slightly better than 
    673         # Ne=2 and Ne=4. Beyond Ne=4 things decline significantly. 
    674         Nr = 2000 
    675         wiggle = 3.0 
    676         eValue = 0.95 
    677         Ns, N1, N2 = 1000, 10, 1 
     684        for k in xrange(N2): 
     685            Lx, Lh = modelObj.inlineDirect( 
     686                modelObj.code['likelihood'], **inlineVars) 
     687            zLine = "   ".join(["%+4.2f" % xx for xx in inlineVars['z'][0,:]]) 
     688            print "%3d: %+6.1f  %+7.1f%s%s" % (k, Lx, Lh, " "*5, zLine) 
     689 
     690    def _simRounds(self, Nr, Ns, inlineVars, name, values): 
    678691        fig = self.fig 
    679         Ne_values = (2,3,4,5,6,) 
    680         params = { 
    681             'h':s.empty(1), 
    682             'sc':s.array([1, s.log(s.sqrt(2*s.pi))]), 
    683             'w':wiggle, 
    684             'np':N1, 
    685             'ni':N2, 
    686             'd':s.array([0.0]), 
    687             'e':s.array([[float(eValue)]]), 
    688             'g':s.array([0.0]), 
    689             'rz':s.array([[1.0]]), 
    690             'ri':s.array([[1.0]]) 
    691             } 
    692         for k, Ne in enumerate(Ne_values): 
    693             params['ne'] = Ne 
     692        bins = s.linspace(0.65, 0.95, 20) 
     693        for k, value in enumerate(values): 
     694            inlineVars[name] = value 
    694695            correlations = [] 
    695             for hReal, hSim in self._likelihood( 
    696                 eValue, wiggle, Ns, Nr, N1, N2, Ne, params): 
     696            for hReal, hSim in self._likelihood(Ns, Nr, inlineVars): 
    697697                corrInfo = stats.spearmanr(hReal, hSim) 
    698698                if VERBOSE: 
    699699                    print corrInfo[0] 
    700700                correlations.append(corrInfo[0]) 
    701             sp = fig.add_subplot(100*len(Ne_values)+11+k) 
    702             sp.hist(s.array(correlations)) 
    703             sp.set_title("Ne=%d" % Ne) 
     701            sp = fig.add_subplot(100*len(values)+11+k) 
     702            sp.hist(s.array(correlations), bins) 
     703            sp.set_title("%s=%d" % (name, value)) 
     704 
     705    def test_optimize_Ne(self): 
     706        # Empirically, it looks like Ne=3 is best, just slightly better than 
     707        # Ne=2 and Ne=4. Beyond Ne=4 things decline significantly. 
     708        Nr = 1000 
     709        wiggle = 3.0 
     710        eValue = 0.95 
     711        Ns, N1, N2 = 1000, 10, 2 
     712        inlineVars = self._inlineVars(wiggle, eValue, N1, N2) 
     713        self._simRounds(Nr, Ns, inlineVars, 'ne', (2,3,4,5,6)) 
    704714        #self._check(x, iv[1,:], zSim[-1,:], eValue) 
     715 
     716    def test_optimize_N1(self): 
     717        # Empirically, it looks like np=10 is best. Going to np=20 barely gives 
     718        # any improvement, and obviously doubles the amount of computation 
     719        # time. Dropping to np=5 is significantly worse. 
     720        Nr = 1000 
     721        wiggle = 3.0 
     722        eValue = 0.95 
     723        Ns, N2 = 1000, 2 
     724        inlineVars = self._inlineVars(wiggle, eValue, 1, N2) 
     725        self._simRounds(Nr, Ns, inlineVars, 'np', (2,5,10,20)) 
     726        #self._check(x, iv[1,:], zSim[-1,:], eValue) 
     727 
     728    def test_optimize_N2(self): 
     729        # Empirically, it looks like ni=2 is best. It's noticeably better than 
     730        # ni=1 (though not dramatically so), but going to anything larger 
     731        # doesn't show any improvement. 
     732        Nr = 1000 
     733        wiggle = 2.0 
     734        eValue = 0.95 
     735        Ns, N1 = 1000, 10 
     736        inlineVars = self._inlineVars(wiggle, eValue, N1, 1) 
     737        self._simRounds(Nr, Ns, inlineVars, 'ni', (1,2,3,4)) 
     738        #self._check(x, iv[1,:], zSim[-1,:], eValue) 
    705739     
    706740    def test_highCorrelation_c(self): 
    707         Ne = 2 
    708         wiggle = 3.0 
    709         eValue = 0.99 
    710         Ns, N1, N2 = 200, 20, 1 
    711         for hReal, hSim in self._likelihood(eValue, wiggle, Ns, 1, N1, N2, Ne): 
     741        Ne = 3 
     742        wiggle = 2.0 
     743        eValue = 0.97 
     744        Ns, N1, N2 = 200, 10, 2 
     745        inlineVars = self._inlineVars(wiggle, eValue, N1, N2, Ne) 
     746        for hReal, hSim in self._likelihood(Ns, 1, inlineVars): 
    712747            self._plot((hReal, hSim)) 
    713748