From e473223bbdbaf44edf3cc180f16dda2ebadd7f4e Mon Sep 17 00:00:00 2001 From: NunoSempere Date: Tue, 24 Dec 2024 16:11:37 +0100 Subject: [PATCH] become a bit confused about FilledSamples and mixtures --- fermi.go | 17 +++++++++++++++++ sample/sample.go | 4 ++++ 2 files changed, 21 insertions(+) diff --git a/fermi.go b/fermi.go index 29575f1..4149240 100644 --- a/fermi.go +++ b/fermi.go @@ -21,6 +21,7 @@ type Stack struct { type Dist interface { Samples() []float64 + Sampler(sample.State) float64 } type Scalar float64 @@ -48,21 +49,37 @@ func (p Scalar) Samples() []float64 { } return xs } +func (p Scalar) Sampler(r sample.State) float64 { + return float64(p) +} func (ln Lognormal) Samples() []float64 { sampler := func(r sample.State) float64 { return sample.Sample_to(ln.low, ln.high, r) } // Can't do parallel because then I'd have to await throughout the code return sample.Sample_serially(sampler, N_SAMPLES) } +func (ln Lognormal) Sampler(r sample.State) float64 { + return sample.Sample_to(ln.low, ln.high, r) +} func (beta Beta) Samples() []float64 { sampler := func(r sample.State) float64 { return sample.Sample_beta(beta.a, beta.b, r) } return sample.Sample_serially(sampler, N_SAMPLES) } +func (beta Beta) Sampler(r sample.State) float64 { + return sample.Sample_beta(beta.a, beta.b, r) +} func (fs FilledSamples) Samples() []float64 { return fs.xs } +func (fs FilledSamples) Sampler(r sample.State) float64 { + // This is a bit subtle, because sampling from a FilledSamples item iteratively converges + // to something different than the initial distribution + n := len(fs.xs) + i := sample.Sample_int(n, r) + return fs.xs[i] +} /* Constants */ const HELP_MSG = "1. Grammar:\n" + diff --git a/sample/sample.go b/sample/sample.go index abb3055..c6cd913 100644 --- a/sample/sample.go +++ b/sample/sample.go @@ -16,6 +16,10 @@ type func64 = func(State) float64 var global_state = rand.New(rand.NewPCG(uint64(1), uint64(2))) +func Sample_int(n int, r State) int { + return r.IntN(n) +} + func Sample_unit_uniform(r State) float64 { return r.Float64() }