Build Your Own Probability Monads
randomhacks.net
randomhacks.net
(Also, weird reading a comment I made on that blog 8 years ago!)
I also just finished reading "The Happstack Book: Modern, Type-Safe Web Development in Haskell" which I highly recommend taking a look at: http://happstack.com/docs/crashcourse/index.html
Also, I'm curious: who are the top-notch probabilists at MSR?
How is this represented internally? What is the time and space complexity? Is there even a way to practically do this?
val d: Podel[Vector[Int]] = Bernoulli(p = 0.5).repeat(128).map(_.toVector)
println(d.probability(bits => bits(0) == 1))
println(d.map(bits => bits.count(_ == 1)).hist(sampleSize = 10000, optLabel = Some("Number of 1s in 128 bits")))
output: /*
0.5054Number of 1s in 128 bits
42 0.01%
45 0.09%
48 0.43%
51 1.53% #
54 4.49% ####
57 9.84% #########
60 16.21% ################
63 20.46% ####################
66 19.62% ###################
69 14.76% ##############
72 7.86% #######
75 3.49% ###
78 0.89%
81 0.23%
84 0.07%
87 0.01%
90 0.01%
*/ {
val X: Podel[Int] = Bernoulli(p = 0.5)
val Y: Podel[Int] = X.map(-1 * _)
val Z: Podel[Int] = X + Y
val d: Podel[Int] = for {
x <- X
y <- Y
z <- Z
} yield {
z
}
d.hist(sampleSize = 10000, optLabel = Some("Uncorrelated X, Y"))
}
{
val X: Bernoulli = Bernoulli(p = 0.5)
val d: Podel[Int] = for {
x <- X
y = - 1 * x
z = x + y
} yield {
z
}
d.hist(sampleSize = 10000, optLabel = Some("y = -x"))
}
/*
Uncorrelated X, Y-1 25.52% #########################
0 49.60% #################################################
1 24.88% ########################
y = -x
0 100.00% ####################################################################################################
*/
So far, the weakness I find with probability monad, and simulation, is lack of precision. This makes them useless when the probability you want to find is precise such as 5.324e-11. Only formulas can give you the answer.