time-to-botec

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

commit fa0065c96e4f5310987300787b666030e26046b0
parent 4544adb3d0ae0257cb9b728b4e6eca6dd2d0b79e
Author: NunoSempere <nuno.sempere@protonmail.com>
Date:   Fri, 16 Feb 2024 13:43:29 +0100

wrangle go types

Diffstat:
Mgo/notes.md | 2+-
Mgo/squiggle.go | 18++++++++++++++----
2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/go/notes.md b/go/notes.md @@ -4,4 +4,4 @@ - [x] Test with a million samples of a simple lognormal, just to get a sense of speed - [ ] Add mixture distribution - [ ] Look into go routines for filling up an array. -- [ ] Anonymous functions for nested: https://stackoverflow.com/questions/74523441/nested-functions-in-go +- [ ] Anonymous functions for nested: https://stackoverflow.com/questions/74523441/nested-functions-in-o diff --git a/go/squiggle.go b/go/squiggle.go @@ -49,8 +49,12 @@ func sample_to(low float64, high float64) float64 { type func64 func() float64 -func sample_mixture(fs [](func(float64) float64), ps []float64) float64 { - return 1.0 +func sample_mixture(fs []func64, weights []float64) float64 { + var sum_weights float64 = 0 + for i_, weight := range weights { + sum_weights += weight + } + return sum_weights } func main() { @@ -67,6 +71,12 @@ func main() { return sample_to(1, 10) } - fs := [3](func64){f1, f1, f1} - // x := sample_mixture() + f2 := func() float64 { + return sample_to(100, 1000) + } + + fs := [2](func64){f1, f2} + ws := [2](float64){0.4, 0.1} + x := sample_mixture(fs[0:], ws[0:]) + fmt.Printf("%v\n", x) }