-
-
Notifications
You must be signed in to change notification settings - Fork 298
/
discrete_rv.jl
66 lines (45 loc) · 1.53 KB
/
discrete_rv.jl
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#=
Generates an array of draws from a discrete random variable with a
specified vector of probabilities.
@author : Spencer Lyon <spencer.lyon@nyu.edu>
@date: 2014-07-10
References
----------
http://quant-econ.net/jl/finite_markov.html?highlight=discrete_rv
TODO: as of 07/10/2014 it is not possible to define the property
interface we see in the python version. Once issue 1974 from
the main Julia repository is resolved, we can revisit this and
implement that feature.
=#
"""
Generates an array of draws from a discrete random variable with
vector of probabilities given by q.
##### Fields
- `q::Vector{T<:Real}`: A vector of non-negative probabilities that sum to 1
- `Q::Vector{T<:Real}`: The cumulative sum of q
"""
type DiscreteRV{T<:Real}
q::Vector{T}
Q::Vector{T}
DiscreteRV(x::Vector{T}) = new(x, cumsum(x))
end
# outer constructor so people don't have to put {T} themselves
DiscreteRV{T<:Real}(x::Vector{T}) = DiscreteRV{T}(x)
"""
Make a single draw from the discrete distribution
##### Arguments
- `d::DiscreteRV`: The `DiscreteRV` type represetning the distribution
##### Returns
- `out::Int`: One draw from the discrete distribution
"""
draw(d::DiscreteRV) = searchsortedfirst(d.Q, rand())
"""
Make multiple draws from the discrete distribution represented by a
`DiscreteRV` instance
##### Arguments
- `d::DiscreteRV`: The `DiscreteRV` type representing the distribution
- `k::Int`:
##### Returns
- `out::Vector{Int}`: `k` draws from `d`
"""
draw{T}(d::DiscreteRV{T}, k::Int) = Int[draw(d) for i=1:k]