-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bfcc173
commit bcb25ca
Showing
4 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
using Revise | ||
using Dice | ||
# using Random | ||
using Distributions | ||
|
||
NSAMPLES=1000 | ||
samples = rand(Bernoulli(0.5), NSAMPLES) | ||
samples = map(x -> if x == 1 true else false end, samples) | ||
|
||
pws = Dict{}(0 => 0.4, 1 => 0.6) | ||
|
||
function dice_program(s) | ||
param = if s 2/3 else 3/4 end | ||
# param = 0.7 | ||
code = @dice begin | ||
flip1 = flip(param) | ||
# flip2 = if flip1 flip(4/7) else flip(2/3) end | ||
flip2 = s | ||
w = DistUInt{2}([flip1, flip2]) | ||
observe(w < DistUInt{2}([flip(0.5), flip(0.5)])) | ||
w | ||
end | ||
inside_expectation = expectation(code) | ||
prob_evidence = pr(allobservations(code)) | ||
# @show prob_evidence | ||
return inside_expectation, prob_evidence[true], pws[s] | ||
end | ||
|
||
|
||
mean = map(x -> dice_program(x), samples) | ||
|
||
num = sum(map(x -> x[1]*x[2]*x[3]/0.5, mean)) | ||
den = sum(map(x -> x[2]*x[3]/0.5, mean)) | ||
print(num/den) | ||
|
||
# true distribution ---- 0 -> 0.1, 1 -> 0.2, 2 -> 0.3, 3 -> 0.4 | ||
# true expectation ---- 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
using Dice | ||
# using Random | ||
using Distributions | ||
|
||
NSAMPLES=100000 | ||
samples = rand(Bernoulli(0.5), NSAMPLES) | ||
samples = map(x -> if x == 1 true else false end, samples) | ||
|
||
function dice_program(s) | ||
code = @dice begin | ||
w = DistUInt{2}([flip(0.5), s]) | ||
observe(w < DistUInt{2}([flip(0.5), flip(0.5)])) | ||
w | ||
end | ||
inside_expectation = expectation(code) | ||
prob_evidence = pr(allobservations(code)) | ||
# @show prob_evidence | ||
return inside_expectation, prob_evidence[true] | ||
end | ||
|
||
|
||
mean = map(x -> dice_program(x), samples) | ||
|
||
num = sum(map(x -> x[1]*x[2], mean)) | ||
den = sum(map(x -> x[2], mean)) | ||
print(num/den) | ||
|
||
# true distribution ---- 0 -> 0.5, 1 -> 0.33, 2 -> 0.166, 3 -> 0 | ||
# true expectation ---- 0.66 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Revise | ||
using Dice | ||
using Distributions | ||
using Plots | ||
|
||
a = bitblast(DistFix{9, 0}, Normal(0, 1), 16, -8.0, 8.0) | ||
code = @dice begin | ||
# observe(prob_equals(a.mantissa.number.bits[5:7], [false, false, false])) | ||
b = bitblast(DistFix{9, 0}, Normal(0, 1), 16, -8.0, 8.0) | ||
observe(prob_equals(a + b, DistFix{9, 0}(1.0))) | ||
a | ||
end | ||
|
||
# p = pr(a.mantissa.number.bits[5:7]) | ||
|
||
# plot(pr(a)) | ||
|
||
p = expectation(code) | ||
plot(p) |