time-to-botec

Benchmark sampling in different programming languages
Log | Files | Refs | README

estimate.bc (576B)


      1 p_a = 0.8
      2 p_b = 0.5
      3 p_c = p_a * p_b
      4 
      5 weights[0] = 1 - p_c
      6 weights[1] = p_c / 2
      7 weights[2] = p_c / 4
      8 weights[3] = p_c / 4
      9 
     10 /* We'll have to define the mixture manually */
     11 define mixture(){
     12   p = sample_unit_uniform()
     13   if(p <= weights[0]){
     14     return 0
     15   } 
     16   if(p <= (weights[0] + weights[1])){
     17     return 1
     18   }
     19   if(p<= (weights[0] + weights[1] + weights[2])){
     20     return sample_to(1, 3)
     21   }
     22   return sample_to(2, 10)
     23 }
     24 
     25 /* n_samples = 1000000 */
     26 n_samples = 1000000
     27 sum=0
     28 for(i=0; i < n_samples; i++){
     29   /* samples[i] = mixture() */
     30   sum += mixture()
     31 }
     32 sum/n_samples
     33 
     34 halt