Changeset 185

Show
Ignore:
Timestamp:
05/19/08 15:06:13 (7 months ago)
Author:
edsuom
Message:

Unit testing support C code for model

Files:

Legend:

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

    r184 r185  
    156156double normLogDensity(double x) 
    157157{ 
    158   static double logC = -log(2 * 3.1415926535897931); 
     158  static double logC = -log(2 * M_PI); 
    159159  return -0.5*x*x + logC; 
    160160} 
     
    251251  int k; 
    252252  double L; 
    253   double Lmin = +1E20;   // Anything will be less positive 
    254   double Lmax = -1E20;   // Anything will be more positive 
     253  double Lmin = +HUGE_VAL;   // Anything will be less positive 
     254  double Lmax = -HUGE_VAL;   // Anything will be more positive 
    255255  for(k=0; k<n; k++) { 
    256256    L = sp[k][1]->Lx; 
     
    271271// the log-probability of the proposal's having been selected. 
    272272struct selection \ 
    273 importanceSample(struct sample *sp[][2], double Lz[], double Dp[], int n) 
    274 { 
    275   int k0, kpMax; 
     273importanceSample(struct sample *sp[][2], double Lz[], int n) 
     274{ 
     275  int k, kpMax; 
    276276  double val; 
    277277  double pSum = 0; 
    278   double pMax = -1E20;  // Anything will be more positive 
     278  double pMax = -HUGE_VAL;  // Anything will be more positive 
     279  double Dp[n]; 
    279280  struct selection result; 
    280281 
    281282  // Compute linear probability density for each proposal 
    282   for(k0=0; k0<n; k0++) { 
    283     val = exp(sp[k0][1]->Lx + Lz[k0]); 
    284     Dp[k0] = val; 
     283  for(k=0; k<n; k++) { 
     284    val = exp(sp[k][1]->Lx + Lz[k]); 
     285    Dp[k] = val; 
    285286    pSum += val; 
    286287    if(val > pMax) { 
    287288      pMax = val; 
    288       kpMax = k0
     289      kpMax = k
    289290    } 
    290291  } 
  • projects/AsynCluster/trunk/svpmc/test/test_model.py

    r184 r185  
    376376        H2(k1, k0) = PA1(S.h, k1); 
    377377    } 
     378    free(S.z); 
     379    free(S.h); 
     380    free(S.x0); 
     381    free(S.x1); 
    378382    """ 
    379383     
     
    455459 
    456460class Test_Model_logLikelihood(BaseCase): 
    457     N = 100 
    458      
    459461    code = """ 
    460462    int k0, k1; 
     
    470472    S.x0 = (double *) malloc(sizeof(double) * Nx[0]); 
    471473    S.x1 = (double *) malloc(sizeof(double) * Nx[0]); 
     474 
     475    for(k1=0; k1<Nx[0]; k1++) { 
     476        PA1(S.z, k1) = Z1(k1); 
     477        PA1(S.h, k1) = H1(k1); 
     478    } 
    472479     
    473480    for(k0=0; k0<Nx[1]; k0++) { 
    474       for(k1=0; k1<Nx[0]; k1++) { 
     481      for(k1=0; k1<Nx[0]; k1++) 
    475482        xk[k1] = X2(k1, k0); 
    476         PA1(S.z, k1) = Z2(k1, k0); 
    477         PA1(S.h, k1) = H2(k1, k0); 
    478       } 
    479483      // This is what's being tested 
    480484      logLikelihood(&S, &P, xk); 
    481485    } 
     486    free(S.z); 
     487    free(S.h); 
     488    free(S.x0); 
     489    free(S.x1); 
    482490    return_val = S.Lx; 
    483491    """ 
     
    501509 
    502510    def test_Lx_accumulates(self): 
    503         # TODO 
    504         Ns = 2 
     511        Ns = 100 
    505512        Lx = self._logLikelihood(s.ones((1,Ns)), 0) 
    506513        distObj = stats.norm(0, 1) 
    507         self.failUnlessAlmostEqual(Lx, s.log(Ns*distObj.pdf(1))) 
     514        self.failUnlessAlmostEqual(Lx, Ns*s.log(distObj.pdf(1))) 
    508515             
     516    def test_Lx_multivariate(self): 
     517        Nt = 8 
     518        Ns = 100 
     519        Lx = self._logLikelihood(s.ones((Nt,Ns)), 0) 
     520        distObj = stats.norm(0, 1) 
     521        self.failUnlessAlmostEqual(Lx, Nt*Ns*s.log(distObj.pdf(1))) 
     522 
     523 
     524class Test_Model_spArray(BaseCase): 
     525    code = """ 
     526    int k; 
     527    const int n = NL[0]; 
     528    struct selection spSelection; 
     529    struct sample sp[n][2]; 
     530    double Lz[n]; 
     531    py::tuple result(2); 
     532     
     533    for(k=0; k<n; k++) { 
     534      Lz[k] = Z1(k); 
     535      sp[k][1].Lx = L1(k); 
     536    } 
     537     
     538    return_val = S.Lx; 
     539    """ 
     540 
    509541 
    510542class Test_Model_likelihood(BaseCase):