2024-02-25 13:35:47 +00:00
|
|
|
package main
|
|
|
|
|
2024-02-25 21:15:29 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"git.nunosempere.com/NunoSempere/probppl/choose"
|
|
|
|
"math"
|
|
|
|
rand "math/rand/v2"
|
|
|
|
)
|
2024-02-25 13:35:47 +00:00
|
|
|
|
2024-02-25 13:49:43 +00:00
|
|
|
type src = *rand.Rand
|
2024-02-25 21:55:05 +00:00
|
|
|
type IntProb struct {
|
2024-02-25 21:25:58 +00:00
|
|
|
N int64
|
2024-02-25 21:35:59 +00:00
|
|
|
p float64
|
|
|
|
}
|
2024-02-25 21:55:05 +00:00
|
|
|
type IntProbs = []IntProb
|
|
|
|
type IntProbsWeights struct {
|
|
|
|
IntProbs IntProbs
|
|
|
|
w int64
|
2024-02-25 21:25:58 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 21:55:05 +00:00
|
|
|
func generatePeopleKnownDistribution(r src) IntProbs {
|
2024-02-25 13:49:43 +00:00
|
|
|
|
2024-02-25 21:55:05 +00:00
|
|
|
var probabilities IntProbs
|
2024-02-25 19:32:12 +00:00
|
|
|
sum := 0.0
|
|
|
|
|
2024-02-25 21:40:42 +00:00
|
|
|
for i := 16; i <= 2048; i *= 2 {
|
2024-02-25 19:32:12 +00:00
|
|
|
p := r.Float64()
|
2024-02-25 21:55:05 +00:00
|
|
|
probabilities = append(probabilities, IntProb{N: int64(i), p: p})
|
2024-02-25 19:32:12 +00:00
|
|
|
sum += p
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:35:59 +00:00
|
|
|
for i := range probabilities {
|
|
|
|
probabilities[i].p = probabilities[i].p / sum
|
2024-02-25 13:49:43 +00:00
|
|
|
}
|
2024-02-25 19:32:12 +00:00
|
|
|
|
2024-02-25 21:35:59 +00:00
|
|
|
return probabilities
|
2024-02-25 13:49:43 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 20:31:22 +00:00
|
|
|
func chooseWrapper(n int64, k int64) int64 {
|
|
|
|
if n < k {
|
|
|
|
return 0
|
|
|
|
} else {
|
|
|
|
return choose.Choose(n, k)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProbabilityOfKBirthdayMatchesGivenNPeopleKnown(n int64, k int64) float64 {
|
|
|
|
return float64(chooseWrapper(n, k)) * math.Pow(1.0/365.0, float64(k)) * math.Pow(1.0-(1.0/365.0), float64(n-k))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getMatchesDrawGivenNPeopleKnown(n int64, r src) int64 {
|
|
|
|
p0 := getProbabilityOfKBirthdayMatchesGivenNPeopleKnown(n, 0)
|
|
|
|
p1 := getProbabilityOfKBirthdayMatchesGivenNPeopleKnown(n, 1)
|
|
|
|
p2 := getProbabilityOfKBirthdayMatchesGivenNPeopleKnown(n, 2)
|
|
|
|
|
|
|
|
p := r.Float64()
|
|
|
|
if p < p0 {
|
|
|
|
return 0
|
|
|
|
} else if p < (p0 + p1) {
|
|
|
|
return 1
|
|
|
|
} else if p < (p0 + p1 + p2) {
|
|
|
|
return 2
|
|
|
|
} else {
|
|
|
|
return 3 // stands for 'greater than 3'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
Draw 148 times
|
|
|
|
How many people do you know that were born in the same day of the year as you?
|
|
|
|
0: 46.6% | 69
|
|
|
|
1: 31.1% | 46
|
|
|
|
2: 12.8% | 19
|
|
|
|
≥3: 9.5% | 14
|
|
|
|
*/
|
|
|
|
|
2024-02-25 21:55:05 +00:00
|
|
|
func drawFromDistributionWithReplacement(d IntProbs, r src) int64 {
|
2024-02-25 20:31:22 +00:00
|
|
|
pp := r.Float64()
|
|
|
|
sum := 0.0
|
2024-02-25 21:35:59 +00:00
|
|
|
for i := range d {
|
|
|
|
sum += d[i].p
|
2024-02-25 21:15:29 +00:00
|
|
|
if pp <= sum {
|
2024-02-25 21:35:59 +00:00
|
|
|
return int64(d[i].N) // this introduces some non-determinism, as order of maps in go isn't guaranteed
|
2024-02-25 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
fmt.Printf("%f, %f\n", sum, pp)
|
|
|
|
fmt.Println(d)
|
|
|
|
panic("Probabilities should sum up to 1")
|
|
|
|
}
|
|
|
|
|
2024-02-25 20:47:45 +00:00
|
|
|
func aboutEq(a int64, b int64) bool {
|
|
|
|
h := int64(3)
|
|
|
|
return ((-h) <= (a - b)) && ((a - b) <= h)
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:55:05 +00:00
|
|
|
func draw148PplFromDistributionAndCheck(d IntProbs, r src, show bool) int64 {
|
2024-02-25 20:31:22 +00:00
|
|
|
|
|
|
|
count := make(map[int64]int64)
|
|
|
|
count[0] = 0
|
|
|
|
count[1] = 0
|
|
|
|
count[2] = 0
|
|
|
|
count[3] = 0
|
|
|
|
for i := 0; i < 148; i++ {
|
|
|
|
person_i_ppl_known := drawFromDistributionWithReplacement(d, r)
|
|
|
|
person_i_num_birthday_matches := getMatchesDrawGivenNPeopleKnown(person_i_ppl_known, r)
|
|
|
|
count[person_i_num_birthday_matches]++
|
|
|
|
}
|
2024-02-25 20:47:45 +00:00
|
|
|
// if (count[0] == 69) && (count[1] == 46) && (count[2] == 19) && (count[3] == 14) {
|
2024-02-25 21:15:29 +00:00
|
|
|
if show {
|
|
|
|
// fmt.Println(count)
|
|
|
|
}
|
2024-02-25 20:47:45 +00:00
|
|
|
if aboutEq(count[0], 69) && aboutEq(count[1], 46) && aboutEq(count[2], 19) && aboutEq(count[3], 14) {
|
2024-02-25 20:31:22 +00:00
|
|
|
return 1
|
|
|
|
} else {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-25 21:55:05 +00:00
|
|
|
func getUnnormalizedBayesianUpdateForDistribution(d IntProbs, r src) int64 {
|
2024-02-25 20:31:22 +00:00
|
|
|
var sum int64 = 0
|
2024-02-25 21:55:05 +00:00
|
|
|
n := 10_000
|
2024-02-25 20:31:22 +00:00
|
|
|
for i := 0; i < n; i++ {
|
2024-02-25 20:47:45 +00:00
|
|
|
/* if i%1000 == 0 {
|
2024-02-25 20:33:05 +00:00
|
|
|
fmt.Println(i)
|
2024-02-25 20:47:45 +00:00
|
|
|
} */
|
2024-02-25 21:15:29 +00:00
|
|
|
draw_result := draw148PplFromDistributionAndCheck(d, r, i == 0)
|
2024-02-25 20:47:45 +00:00
|
|
|
// fmt.Println(draw_result)
|
|
|
|
sum += draw_result
|
2024-02-25 20:31:22 +00:00
|
|
|
}
|
2024-02-25 20:47:45 +00:00
|
|
|
return sum // float64(sum) / float64(n)
|
2024-02-25 20:31:22 +00:00
|
|
|
}
|
|
|
|
|
2024-02-25 13:35:47 +00:00
|
|
|
func main() {
|
|
|
|
|
2024-02-25 13:49:43 +00:00
|
|
|
var r = rand.New(rand.NewPCG(uint64(1), uint64(2)))
|
2024-02-25 20:47:45 +00:00
|
|
|
|
2024-02-25 22:07:20 +00:00
|
|
|
n_dists := 10
|
2024-02-25 22:03:25 +00:00
|
|
|
var dists = make([]IntProbsWeights, n_dists)
|
2024-02-25 21:35:59 +00:00
|
|
|
|
2024-02-25 22:07:20 +00:00
|
|
|
for i := 0; i < n_dists; i++ {
|
2024-02-25 20:47:45 +00:00
|
|
|
people_known_distribution := generatePeopleKnownDistribution(r)
|
|
|
|
result := getUnnormalizedBayesianUpdateForDistribution(people_known_distribution, r)
|
2024-02-25 21:57:46 +00:00
|
|
|
if i%10 == 0 {
|
2024-02-25 22:07:20 +00:00
|
|
|
fmt.Printf("%d/%d\n", i, n_dists)
|
2024-02-25 21:57:46 +00:00
|
|
|
}
|
2024-02-25 21:15:29 +00:00
|
|
|
if result > 0 {
|
2024-02-25 22:03:25 +00:00
|
|
|
dists[i] = IntProbsWeights{IntProbs: people_known_distribution, w: result}
|
2024-02-25 21:15:29 +00:00
|
|
|
}
|
2024-02-25 20:47:45 +00:00
|
|
|
}
|
2024-02-25 13:49:43 +00:00
|
|
|
|
2024-02-25 21:40:42 +00:00
|
|
|
// Now calculate the posterior
|
2024-02-25 22:07:20 +00:00
|
|
|
sum_weights := int64(0)
|
|
|
|
for _, dist := range dists {
|
|
|
|
sum_weights += dist.w
|
|
|
|
}
|
2024-02-25 21:55:05 +00:00
|
|
|
|
|
|
|
for i := int64(16); i <= 2048; i *= 2 {
|
|
|
|
p := 0.0
|
|
|
|
for _, dist := range dists {
|
|
|
|
for _, int_prob := range dist.IntProbs {
|
|
|
|
if int_prob.N == i {
|
|
|
|
p += float64(dist.w) * int_prob.p
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
p = p / float64(sum_weights)
|
|
|
|
fmt.Printf("%d: %f\n", i, p)
|
|
|
|
}
|
2024-02-25 13:35:47 +00:00
|
|
|
}
|