commit 6417e0aeccb84995279fd0adb0a11feee575c18b parent 1f4eb1fec4ac4fa3f9fc9aa4518238e76de2a583 Author: NunoSempere <nuno.sempere@protonmail.com> Date: Fri, 16 Feb 2024 13:52:28 +0100 add initial go mixture implementation Diffstat:
| M | go/squiggle.go | | | 26 | +++++++++++++++++++++++++- |
1 file changed, 25 insertions(+), 1 deletion(-)
diff --git a/go/squiggle.go b/go/squiggle.go @@ -54,7 +54,31 @@ func sample_mixture(fs []func64, weights []float64) float64 { for _, weight := range weights { sum_weights += weight } - return sum_weights + + var total float64 = 0 + for i, weight := range weights { + total += weight / sum_weights + weights[i] = total + } + + var result float64 + var flag int = 0 + var p float64 = r.Float64() + + for i, weight := range weights { + if p < weight { + result = fs[i]() + flag = 1 + break + } + } + + if flag == 0 { + result = fs[len(fs)-1]() + } + return result + // return weights[0] + } func main() {