become a bit confused about FilledSamples and mixtures

This commit is contained in:
NunoSempere 2024-12-24 16:11:37 +01:00
parent 7d8a582bd5
commit e473223bbd
2 changed files with 21 additions and 0 deletions

View File

@ -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" +

View File

@ -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()
}