-
-
Notifications
You must be signed in to change notification settings - Fork 304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add code for a multivariate discrete random variable #204
Open
cc7768
wants to merge
1
commit into
master
Choose a base branch
from
MVDRV
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -78,3 +78,69 @@ function Base.rand!(out::AbstractArray{T}, d::DiscreteRV) where T<:Integer | |
end | ||
|
||
@deprecate draw Base.rand | ||
|
||
|
||
struct MVDiscreteRV{TV1<:AbstractArray,TV2<:AbstractVector,K,TI<:Integer} | ||
q::TV1 | ||
Q::TV2 | ||
dims::NTuple{K,TI} | ||
|
||
function MVDiscreteRV{TV1,TV2,K,TI}(q::TV1, Q::TV2, dims::NTuple{K,TI}) where {TV1,TV2,K,TI} | ||
abs(sum(q) - 1.0) > 1e-10 && error("q should sum to 1") | ||
abs(Q[end] - 1.0) > 1e-10 && error("Q[end] should be 1") | ||
length(Q) != prod(dims) && error("Number of elements is inconsistent") | ||
|
||
new{TV1,TV2,K,TI}(q, Q, dims) | ||
end | ||
end | ||
|
||
|
||
function MVDiscreteRV(q::TV1) where TV1<:AbstractArray | ||
Q = cumsum(vec(q)) | ||
dims = size(q) | ||
|
||
return MVDiscreteRV{typeof(q),typeof(Q),length(dims),eltype(dims)}(q, Q, dims) | ||
end | ||
|
||
|
||
""" | ||
Make a single draw from the multivariate discrete distribution. | ||
|
||
##### Arguments | ||
|
||
- `d::MVDiscreteRV`: The `MVDiscreteRV` type represetning the distribution | ||
|
||
##### Returns | ||
|
||
- `out::NTuple{Int}`: One draw from the discrete distribution | ||
""" | ||
function Base.rand(d::MVDiscreteRV) | ||
x = rand() | ||
i = searchsortedfirst(d.Q, x) | ||
|
||
return ind2sub(d.dims, i) | ||
end | ||
|
||
""" | ||
Make multiple draws from the discrete distribution represented by a | ||
`MVDiscreteRV` instance | ||
|
||
##### Arguments | ||
|
||
- `d::MVDiscreteRV`: The `DiscreteRV` type representing the distribution | ||
- `k::Int` | ||
|
||
##### Returns | ||
|
||
- `out::Vector{NTuple{Int}}`: `k` draws from `d` | ||
""" | ||
Base.rand(d::MVDiscreteRV{T1,T2,K,TI}, k::V) where {T1,T2,K,TI,V} = | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is |
||
NTuple{K,TI}[rand(d) for i in 1:k] | ||
|
||
function Base.rand!(out::AbstractArray{NTuple{K,TI}}, d::MVDiscreteRV) where {K,TI} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will you please add a tests for this method? |
||
@inbounds for I in eachindex(out) | ||
out[I] = rand(d) | ||
end | ||
|
||
return out | ||
end |
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it makes sense to have the only inner constructor take just
q
. We wouldn't have to do thelength(Q) == prod(dims)
checkWe don't really want people to create these in any other way