-
-
Notifications
You must be signed in to change notification settings - Fork 304
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add code for a multivariate discrete rv
- Loading branch information
Showing
3 changed files
with
128 additions
and
26 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,6 +42,7 @@ export | |
|
||
# discrete_rv | ||
DiscreteRV, | ||
MVDiscreteRV, | ||
draw, | ||
|
||
# mc_tools | ||
|
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 |
---|---|---|
@@ -1,34 +1,69 @@ | ||
@testset "Testing discrete_rv.jl" begin | ||
|
||
# set up | ||
n = 10 | ||
x = rand(n) | ||
x ./= sum(x) | ||
drv = DiscreteRV(x) | ||
|
||
# test Q sums to 1 | ||
@test drv.Q[end] ≈ 1.0 | ||
|
||
# test lln | ||
draws = rand(drv, 100_000) | ||
c = counter(draws) | ||
counts = Array{Float64}(n) | ||
for i=1:n | ||
counts[i] = c[i] | ||
end | ||
counts ./= sum(counts) | ||
@testset "Testing univariate discrete rv" begin | ||
# set up | ||
n = 10 | ||
x = rand(n) | ||
x ./= sum(x) | ||
drv = DiscreteRV(x) | ||
|
||
# test Q sums to 1 | ||
@test drv.Q[end] ≈ 1.0 | ||
|
||
# test lln | ||
draws = rand(drv, 100_000) | ||
c = counter(draws) | ||
counts = Array{Float64}(n) | ||
for i=1:n | ||
counts[i] = c[i] | ||
end | ||
counts ./= sum(counts) | ||
|
||
@test isapprox(Base.maximum(abs, counts - drv.q), 0.0; atol=1e-2) | ||
@test isapprox(Base.maximum(abs, counts - drv.q), 0.0; atol=1e-2) | ||
|
||
draws = Array{Int}(100_000) | ||
rand!(draws, drv) | ||
c = counter(draws) | ||
counts = Array{Float64}(n) | ||
for i=1:n | ||
counts[i] = c[i] | ||
draws = Array{Int}(100_000) | ||
rand!(draws, drv) | ||
c = counter(draws) | ||
counts = Array{Float64}(n) | ||
for i=1:n | ||
counts[i] = c[i] | ||
end | ||
counts ./= sum(counts) | ||
|
||
@test isapprox(Base.maximum(abs, counts - drv.q), 0.0; atol=1e-2) | ||
end | ||
counts ./= sum(counts) | ||
|
||
@test isapprox(Base.maximum(abs, counts - drv.q), 0.0; atol=1e-2) | ||
@testset "Testing multivariate discrete rv" begin | ||
# Do tests for various sizes | ||
for dims in [(5, 3), (5, 10, 3), (5, 7, 5, 10)] | ||
# How many dimensions | ||
n = length(dims) | ||
|
||
# Make some distributional matrix | ||
q = rand(dims...) | ||
q ./= sum(q) # Normalize to sum to 1 | ||
|
||
# Create mv rv | ||
rv = MVDiscreteRV(q) | ||
|
||
# Make sure it doesn't draw numbers that don't make sense... Must | ||
# be between 1 and n | ||
for i in 1:n | ||
@test rand(rv)[i] >= 1 | ||
@test rand(rv)[i] <= dims[i] | ||
end | ||
|
||
ndraws = 1_000_000 | ||
draws = rand(rv, ndraws) | ||
counter = zeros(dims...) | ||
for i in 1:ndraws | ||
draw = draws[i] | ||
counter[draw...] += 1.0 | ||
end | ||
counter ./= ndraws | ||
@test isapprox(Base.maximum(abs, counter - rv.q), 0.0; atol=1e-2) | ||
end | ||
|
||
end | ||
|
||
end # testset |