Compare commits
No commits in common. "5c51b6a0a26f30488c51ac4deb36e6a9c0f0c2b1" and "1b552ffc9ac4596a3c102188b6e987902a70c430" have entirely different histories.
5c51b6a0a2
...
1b552ffc9a
7
makefile
7
makefile
|
@ -1,9 +1,2 @@
|
||||||
dev:
|
dev:
|
||||||
go run probppl.go
|
go run probppl.go
|
||||||
|
|
||||||
prod:
|
|
||||||
go build -o probppl
|
|
||||||
./probppl
|
|
||||||
|
|
||||||
record:
|
|
||||||
make prod > record.txt
|
|
||||||
|
|
108
probppl.go
108
probppl.go
|
@ -5,36 +5,36 @@ import (
|
||||||
"git.nunosempere.com/NunoSempere/probppl/choose"
|
"git.nunosempere.com/NunoSempere/probppl/choose"
|
||||||
"math"
|
"math"
|
||||||
rand "math/rand/v2"
|
rand "math/rand/v2"
|
||||||
"sync"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type src = *rand.Rand
|
type src = *rand.Rand
|
||||||
type IntProb struct {
|
type pplKnownDistrib = map[int64]float64
|
||||||
N int64
|
|
||||||
p float64
|
|
||||||
}
|
|
||||||
type IntProbs = []IntProb
|
|
||||||
type IntProbsWeights struct {
|
|
||||||
IntProbs IntProbs
|
|
||||||
w int64
|
|
||||||
}
|
|
||||||
|
|
||||||
func generatePeopleKnownDistribution(r src) IntProbs {
|
func generatePeopleKnownDistribution(r src) map[int64]float64 {
|
||||||
|
mapping := make(map[int64]float64)
|
||||||
var probabilities IntProbs
|
|
||||||
sum := 0.0
|
sum := 0.0
|
||||||
|
|
||||||
for i := 16; i <= 2048; i *= 2 {
|
// Consider zero case separately
|
||||||
|
/*p0 := r.Float64()
|
||||||
|
mapping[0.0] = p0
|
||||||
|
sum += p0
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Consider successive exponents of 1.5
|
||||||
|
num := 16.0
|
||||||
|
base := 2.0
|
||||||
|
for i := 1; i < 8; i++ {
|
||||||
|
num = num * base
|
||||||
p := r.Float64()
|
p := r.Float64()
|
||||||
probabilities = append(probabilities, IntProb{N: int64(i), p: p})
|
mapping[int64(num)] = p
|
||||||
sum += p
|
sum += p
|
||||||
}
|
}
|
||||||
|
|
||||||
for i := range probabilities {
|
for key, value := range mapping {
|
||||||
probabilities[i].p = probabilities[i].p / sum
|
mapping[key] = value / sum
|
||||||
}
|
}
|
||||||
|
|
||||||
return probabilities
|
return mapping
|
||||||
}
|
}
|
||||||
|
|
||||||
func chooseWrapper(n int64, k int64) int64 {
|
func chooseWrapper(n int64, k int64) int64 {
|
||||||
|
@ -75,13 +75,13 @@ func getMatchesDrawGivenNPeopleKnown(n int64, r src) int64 {
|
||||||
≥3: 9.5% | 14
|
≥3: 9.5% | 14
|
||||||
*/
|
*/
|
||||||
|
|
||||||
func drawFromDistributionWithReplacement(d IntProbs, r src) int64 {
|
func drawFromDistributionWithReplacement(d pplKnownDistrib, r src) int64 {
|
||||||
pp := r.Float64()
|
pp := r.Float64()
|
||||||
sum := 0.0
|
sum := 0.0
|
||||||
for i := range d {
|
for i, p := range d {
|
||||||
sum += d[i].p
|
sum += p
|
||||||
if pp <= sum {
|
if pp <= sum {
|
||||||
return int64(d[i].N) // this introduces some non-determinism, as order of maps in go isn't guaranteed
|
return int64(i)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -95,7 +95,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 IntProbs, r src, show bool) int64 {
|
func draw148PplFromDistributionAndCheck(d pplKnownDistrib, r src, show bool) int64 {
|
||||||
|
|
||||||
count := make(map[int64]int64)
|
count := make(map[int64]int64)
|
||||||
count[0] = 0
|
count[0] = 0
|
||||||
|
@ -118,9 +118,9 @@ func draw148PplFromDistributionAndCheck(d IntProbs, r src, show bool) int64 {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func getUnnormalizedBayesianUpdateForDistribution(d IntProbs, r src) int64 {
|
func getUnnormalizedBayesianUpdateForDistribution(d pplKnownDistrib, r src) int64 {
|
||||||
var sum int64 = 0
|
var sum int64 = 0
|
||||||
n := 30_000
|
n := 100
|
||||||
for i := 0; i < n; i++ {
|
for i := 0; i < n; i++ {
|
||||||
/* if i%1000 == 0 {
|
/* if i%1000 == 0 {
|
||||||
fmt.Println(i)
|
fmt.Println(i)
|
||||||
|
@ -134,52 +134,22 @@ func getUnnormalizedBayesianUpdateForDistribution(d IntProbs, r src) int64 {
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
n_dists := 30_000
|
var r = rand.New(rand.NewPCG(uint64(1), uint64(2)))
|
||||||
var dists = make([]IntProbsWeights, n_dists)
|
|
||||||
|
|
||||||
// Prepare for concurrency
|
sum := int64(0)
|
||||||
num_threads := 32
|
for i := 0; i < 1000; i++ {
|
||||||
var wg sync.WaitGroup
|
|
||||||
wg.Add(num_threads)
|
|
||||||
|
|
||||||
for i := range num_threads {
|
people_known_distribution := generatePeopleKnownDistribution(r)
|
||||||
go func() {
|
// fmt.Println(people_known_distribution)
|
||||||
defer wg.Done()
|
result := getUnnormalizedBayesianUpdateForDistribution(people_known_distribution, r)
|
||||||
var r = rand.New(rand.NewPCG(uint64(i), uint64(i+1)))
|
fmt.Println(i)
|
||||||
for j := i * (n_dists / num_threads); j < (i+1)*(n_dists/num_threads); j++ {
|
if result > 0 {
|
||||||
people_known_distribution := generatePeopleKnownDistribution(r)
|
fmt.Println(people_known_distribution)
|
||||||
result := getUnnormalizedBayesianUpdateForDistribution(people_known_distribution, r)
|
fmt.Println(result)
|
||||||
/*
|
|
||||||
if i%10 == 0 {
|
|
||||||
fmt.Printf("%d/%d\n", i, n_dists)
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
if result > 0 {
|
|
||||||
dists[j] = IntProbsWeights{IntProbs: people_known_distribution, w: result}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}()
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
wg.Wait()
|
|
||||||
// Now calculate the posterior
|
|
||||||
sum_weights := int64(0)
|
|
||||||
for _, dist := range dists {
|
|
||||||
sum_weights += dist.w
|
|
||||||
}
|
|
||||||
|
|
||||||
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)
|
sum += result
|
||||||
fmt.Printf("%d: %f\n", i, p)
|
// fmt.Println(result)
|
||||||
}
|
}
|
||||||
|
fmt.Println(sum)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user