diff --git a/probppl.go b/probppl.go index 200fd84..47e1d7b 100644 --- a/probppl.go +++ b/probppl.go @@ -8,15 +8,19 @@ import ( ) type src = *rand.Rand -type IntProbability { +type IntProbability struct { N int64 - p float64 + p float64 +} +type IntProbabilities = []IntProbability +type IntProbabilitiesWeights struct { + IntProb IntProbabilities + w int64 } -type pplKnownDistrib = map[int64]float64 +func generatePeopleKnownDistribution(r src) IntProbabilities { -func generatePeopleKnownDistribution(r src) map[int64]float64 { - mapping := make(map[int64]float64) + var probabilities IntProbabilities sum := 0.0 // Consider zero case separately @@ -31,15 +35,15 @@ func generatePeopleKnownDistribution(r src) map[int64]float64 { for i := 1; i < 8; i++ { num = num * base p := r.Float64() - mapping[int64(num)] = p + probabilities = append(probabilities, IntProbability{N: int64(num), p: p}) sum += p } - for key, value := range mapping { - mapping[key] = value / sum + for i := range probabilities { + probabilities[i].p = probabilities[i].p / sum } - return mapping + return probabilities } func chooseWrapper(n int64, k int64) int64 { @@ -80,13 +84,13 @@ func getMatchesDrawGivenNPeopleKnown(n int64, r src) int64 { ≥3: 9.5% | 14 */ -func drawFromDistributionWithReplacement(d pplKnownDistrib, r src) int64 { +func drawFromDistributionWithReplacement(d IntProbabilities, r src) int64 { pp := r.Float64() sum := 0.0 - for i, p := range d { - sum += p + for i := range d { + sum += d[i].p if pp <= sum { - return int64(i) // this introduces some non-determinism, as order of maps in go isn't guaranteed + return int64(d[i].N) // this introduces some non-determinism, as order of maps in go isn't guaranteed } } @@ -100,7 +104,7 @@ func aboutEq(a int64, b int64) bool { return ((-h) <= (a - b)) && ((a - b) <= h) } -func draw148PplFromDistributionAndCheck(d pplKnownDistrib, r src, show bool) int64 { +func draw148PplFromDistributionAndCheck(d IntProbabilities, r src, show bool) int64 { count := make(map[int64]int64) count[0] = 0 @@ -123,7 +127,7 @@ func draw148PplFromDistributionAndCheck(d pplKnownDistrib, r src, show bool) int } } -func getUnnormalizedBayesianUpdateForDistribution(d pplKnownDistrib, r src) int64 { +func getUnnormalizedBayesianUpdateForDistribution(d IntProbabilities, r src) int64 { var sum int64 = 0 n := 1000 for i := 0; i < n; i++ { @@ -141,9 +145,10 @@ func main() { var r = rand.New(rand.NewPCG(uint64(1), uint64(2))) + var distribs []IntProbabilitiesWeights + sum := int64(0) - distribs := [](int64, pplKnownDistrib){} - for i := 0; i < 1000; i++ { + for i := 0; i < 100; i++ { people_known_distribution := generatePeopleKnownDistribution(r) // fmt.Println(people_known_distribution) @@ -152,7 +157,7 @@ func main() { if result > 0 { fmt.Println(people_known_distribution) fmt.Println(result) - distribs.append() + distribs = append(distribs, IntProbabilitiesWeights{IntProb: people_known_distribution, w: result}) } sum += result // fmt.Println(result)