savepoint after refactor

This commit is contained in:
NunoSempere 2024-02-25 18:35:59 -03:00
parent 9f31a9161a
commit 1fceb128bb

View File

@ -8,15 +8,19 @@ import (
) )
type src = *rand.Rand type src = *rand.Rand
type IntProbability { type IntProbability struct {
N int64 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 { var probabilities IntProbabilities
mapping := make(map[int64]float64)
sum := 0.0 sum := 0.0
// Consider zero case separately // Consider zero case separately
@ -31,15 +35,15 @@ func generatePeopleKnownDistribution(r src) map[int64]float64 {
for i := 1; i < 8; i++ { for i := 1; i < 8; i++ {
num = num * base num = num * base
p := r.Float64() p := r.Float64()
mapping[int64(num)] = p probabilities = append(probabilities, IntProbability{N: int64(num), p: p})
sum += p sum += p
} }
for key, value := range mapping { for i := range probabilities {
mapping[key] = value / sum probabilities[i].p = probabilities[i].p / sum
} }
return mapping return probabilities
} }
func chooseWrapper(n int64, k int64) int64 { func chooseWrapper(n int64, k int64) int64 {
@ -80,13 +84,13 @@ func getMatchesDrawGivenNPeopleKnown(n int64, r src) int64 {
3: 9.5% | 14 3: 9.5% | 14
*/ */
func drawFromDistributionWithReplacement(d pplKnownDistrib, r src) int64 { func drawFromDistributionWithReplacement(d IntProbabilities, r src) int64 {
pp := r.Float64() pp := r.Float64()
sum := 0.0 sum := 0.0
for i, p := range d { for i := range d {
sum += p sum += d[i].p
if pp <= sum { 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) 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 := make(map[int64]int64)
count[0] = 0 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 var sum int64 = 0
n := 1000 n := 1000
for i := 0; i < n; i++ { for i := 0; i < n; i++ {
@ -141,9 +145,10 @@ func main() {
var r = rand.New(rand.NewPCG(uint64(1), uint64(2))) var r = rand.New(rand.NewPCG(uint64(1), uint64(2)))
var distribs []IntProbabilitiesWeights
sum := int64(0) sum := int64(0)
distribs := [](int64, pplKnownDistrib){} for i := 0; i < 100; i++ {
for i := 0; i < 1000; i++ {
people_known_distribution := generatePeopleKnownDistribution(r) people_known_distribution := generatePeopleKnownDistribution(r)
// fmt.Println(people_known_distribution) // fmt.Println(people_known_distribution)
@ -152,7 +157,7 @@ func main() {
if result > 0 { if result > 0 {
fmt.Println(people_known_distribution) fmt.Println(people_known_distribution)
fmt.Println(result) fmt.Println(result)
distribs.append() distribs = append(distribs, IntProbabilitiesWeights{IntProb: people_known_distribution, w: result})
} }
sum += result sum += result
// fmt.Println(result) // fmt.Println(result)