-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExp002.jl
More file actions
48 lines (39 loc) · 1.1 KB
/
Exp002.jl
File metadata and controls
48 lines (39 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
module Exp002
export RV, sampleRV, sampleRVCond, sampleRVn
using Distributions
type RV
funct :: Function
parents::Array{RV,1}
end
function sampleRV(r::RV, d=Dict())
# generate a single sample but recycle already sampled values
key = hash(r)
if !haskey(d, key)
vals = map((s)->sampleRV(s,d), r.parents)
d[key] = r.funct(vals...)
end
return d[key]
end
function sampleRV(rs::Array{RV,1}, d=Dict())
# sample simultaneously from several random variables
return [sampleRV(r,d) for r in rs]
end
function sampleRVCond(r::RV, cond::RV, maxiter=10000)
for i in 1:maxiter
d = Dict() # each attempt gets a empty dictionary
val = sampleRV(r, d)
if sampleRV(cond, d) # check condition
return val
end
end
error("couldn't fulfill condition after ", maxiter, " attempts")
end
function sampleRVn(r::RV, cond::RV, n :: Int)
# samples random varibles r given condition cond n times
results = Array(Real, n)
for i in 1 : n
results[i] = sampleRVCond(r,cond)
end
results
end
end