2023-11-02 23:24:36 +00:00
|
|
|
p_a = 0.8
|
|
|
|
p_b = 0.5
|
|
|
|
p_c = p_a * p_b
|
|
|
|
|
|
|
|
weights[0] = 1 - p_c
|
|
|
|
weights[1] = p_c / 2
|
|
|
|
weights[2] = p_c / 4
|
|
|
|
weights[3] = p_c / 4
|
|
|
|
|
|
|
|
/* We'll have to define the mixture manually */
|
|
|
|
define mixture(){
|
|
|
|
p = sample_unit_uniform()
|
|
|
|
if(p <= weights[0]){
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
if(p <= (weights[0] + weights[1])){
|
|
|
|
return 1
|
|
|
|
}
|
|
|
|
if(p<= (weights[0] + weights[1] + weights[2])){
|
|
|
|
return sample_to(1, 3)
|
|
|
|
}
|
|
|
|
return sample_to(2, 10)
|
|
|
|
}
|
|
|
|
|
|
|
|
/* n_samples = 1000000 */
|
2023-11-02 23:34:48 +00:00
|
|
|
n_samples = 1000000
|
2023-11-02 23:24:36 +00:00
|
|
|
sum=0
|
|
|
|
for(i=0; i < n_samples; i++){
|
|
|
|
/* samples[i] = mixture() */
|
|
|
|
sum += mixture()
|
|
|
|
}
|
|
|
|
sum/n_samples
|
|
|
|
|
|
|
|
halt
|