add bc version without comments or extraneous newlines.

This commit is contained in:
NunoSempere 2023-11-02 23:40:05 +00:00
parent 90b8804884
commit 5473a6aeda
2 changed files with 59 additions and 0 deletions

View File

@ -0,0 +1,27 @@
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
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
sum=0
for(i=0; i < n_samples; i++){
sum += mixture()
}
sum/n_samples
halt

View File

@ -0,0 +1,32 @@
scale = 16
pi = 4 * atan(1)
normal90confidence=1.64485362695
define sample_unit_uniform(){
return rand()/maxrand()
}
define sample_unit_normal(){
u1=sample_unit_uniform()
u2=sample_unit_uniform()
z = sqrt(-2 * l(u1)) * sin(2 * pi * u2)
return z
}
define sample_uniform(min, max){
return (min + sample_unit_uniform()*(max-min))
}
define sample_normal(mean, sigma){
return (mean + sigma * sample_unit_normal())
}
define sample_lognormal(logmean, logstd){
return e(sample_normal(logmean, logstd))
}
define sample_normal_from_90_confidence_interval(low, high){
mean = (high + low) / 2
std = (high - low) / (2 * normal90confidence)
return sample_normal(mean, std)
}
define sample_to(low, high){
loglow = l(low)
loghigh = l(high)
return e(sample_normal_from_90_confidence_interval(loglow, loghigh))
}