Skip to content
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
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cc7768
Copy link
Member

@cc7768 cc7768 commented Feb 21, 2018

This code is very similar to the code that is in the QE.jl library for DiscreteRV.

We could simply treat multivariate discrete random variables as a univariate discrete random variable (and just make everything long vectors), but that requires working with conversions between linear and cartesian indexing. It also simplifies the interpretation of joint distributions.

This mostly mimics the code from DiscreteRV and adds a call to ind2sub so that it does the conversions for you.

@cc7768 cc7768 requested a review from sglyon February 21, 2018 18:08
@codecov-io
Copy link

codecov-io commented Feb 21, 2018

Codecov Report

Merging #204 into master will decrease coverage by 0.1%.
The diff coverage is 78.57%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master     #204      +/-   ##
==========================================
- Coverage   91.12%   91.01%   -0.11%     
==========================================
  Files          24       24              
  Lines        1712     1726      +14     
==========================================
+ Hits         1560     1571      +11     
- Misses        152      155       +3
Impacted Files Coverage Δ
src/discrete_rv.jl 86.95% <78.57%> (-13.05%) ⬇️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 9334727...d8798d5. Read the comment docs.

@sglyon
Copy link
Member

sglyon commented Feb 21, 2018

Just throwing this out there -- would it be better to replace the q and Q fields with an instance of DiscreteRV on vec(q)?

end


function MVDiscreteRV(q::TV1) where TV1<:AbstractArray
Copy link
Member

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 the length(Q) == prod(dims) check

We don't really want people to create these in any other way


- `out::Vector{NTuple{Int}}`: `k` draws from `d`
"""
Base.rand(d::MVDiscreteRV{T1,T2,K,TI}, k::V) where {T1,T2,K,TI,V} =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is k supposed to be? I think it should have k::Integer instead of a free type param

Base.rand(d::MVDiscreteRV{T1,T2,K,TI}, k::V) where {T1,T2,K,TI,V} =
NTuple{K,TI}[rand(d) for i in 1:k]

function Base.rand!(out::AbstractArray{NTuple{K,TI}}, d::MVDiscreteRV) where {K,TI}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please add a tests for this method?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants