/
discretenonparametric.jl
316 lines (256 loc) · 7.9 KB
/
discretenonparametric.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
DiscreteNonParametric(xs, ps)
A *Discrete nonparametric distribution* explicitly defines an arbitrary
probability mass function in terms of a list of real support values and their
corresponding probabilities
```julia
d = DiscreteNonParametric(xs, ps)
params(d) # Get the parameters, i.e. (xs, ps)
support(d) # Get a sorted AbstractVector describing the support (xs) of the distribution
probs(d) # Get a Vector of the probabilities (ps) associated with the support
```
External links
* [Probability mass function on Wikipedia](http://en.wikipedia.org/wiki/Probability_mass_function)
"""
struct DiscreteNonParametric{T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}} <: DiscreteUnivariateDistribution
support::Ts
p::Ps
function DiscreteNonParametric{T,P,Ts,Ps}(xs::Ts, ps::Ps; check_args::Bool=true) where {
T<:Real,P<:Real,Ts<:AbstractVector{T},Ps<:AbstractVector{P}}
check_args || return new{T,P,Ts,Ps}(xs, ps)
@check_args(
DiscreteNonParametric,
(length(xs) == length(ps), "length of support and probability vector must be equal"),
(ps, isprobvec(ps), "vector is not a probability vector"),
(xs, allunique(xs), "support must contain only unique elements"),
)
sort_order = sortperm(xs)
new{T,P,Ts,Ps}(xs[sort_order], ps[sort_order])
end
end
DiscreteNonParametric(vs::AbstractVector{T}, ps::AbstractVector{P}; check_args::Bool=true) where {
T<:Real,P<:Real} =
DiscreteNonParametric{T,P,typeof(vs),typeof(ps)}(vs, ps; check_args=check_args)
Base.eltype(::Type{<:DiscreteNonParametric{T}}) where T = T
# Conversion
convert(::Type{DiscreteNonParametric{T,P,Ts,Ps}}, d::DiscreteNonParametric) where {T,P,Ts,Ps} =
DiscreteNonParametric{T,P,Ts,Ps}(convert(Ts, support(d)), convert(Ps, probs(d)), check_args=false)
Base.convert(::Type{DiscreteNonParametric{T,P,Ts,Ps}}, d::DiscreteNonParametric{T,P,Ts,Ps}) where {T,P,Ts,Ps} = d
# Accessors
params(d::DiscreteNonParametric) = (d.support, d.p)
"""
support(d::DiscreteNonParametric)
Get a sorted AbstractVector defining the support of `d`.
"""
support(d::DiscreteNonParametric) = d.support
"""
probs(d::DiscreteNonParametric)
Get the vector of probabilities associated with the support of `d`.
"""
probs(d::DiscreteNonParametric) = d.p
function Base.isapprox(c1::DiscreteNonParametric, c2::DiscreteNonParametric; kwargs...)
support_c1 = support(c1)
support_c2 = support(c2)
return length(support_c1) == length(support_c2) &&
isapprox(support_c1, support_c2; kwargs...) &&
isapprox(probs(c1), probs(c2); kwargs...)
end
# Sampling
function rand(rng::AbstractRNG, d::DiscreteNonParametric)
x = support(d)
p = probs(d)
n = length(p)
draw = rand(rng, float(eltype(p)))
cp = p[1]
i = 1
while cp <= draw && i < n
@inbounds cp += p[i +=1]
end
return x[i]
end
sampler(d::DiscreteNonParametric) =
DiscreteNonParametricSampler(support(d), probs(d))
# Override the method in testutils.jl since it assumes
# an evenly-spaced integer support
get_evalsamples(d::DiscreteNonParametric, ::Float64) = support(d)
# Evaluation
pdf(d::DiscreteNonParametric) = copy(probs(d))
function pdf(d::DiscreteNonParametric, x::Real)
s = support(d)
idx = searchsortedfirst(s, x)
ps = probs(d)
if idx <= length(ps) && s[idx] == x
return ps[idx]
else
return zero(eltype(ps))
end
end
logpdf(d::DiscreteNonParametric, x::Real) = log(pdf(d, x))
function cdf(d::DiscreteNonParametric, x::Real)
ps = probs(d)
P = float(eltype(ps))
# trivial cases
x < minimum(d) && return zero(P)
x >= maximum(d) && return one(P)
isnan(x) && return P(NaN)
n = length(ps)
stop_idx = searchsortedlast(support(d), x)
s = zero(P)
if stop_idx < div(n, 2)
@inbounds for i in 1:stop_idx
s += ps[i]
end
else
@inbounds for i in (stop_idx + 1):n
s += ps[i]
end
s = 1 - s
end
return s
end
function ccdf(d::DiscreteNonParametric, x::Real)
ps = probs(d)
P = float(eltype(ps))
# trivial cases
x < minimum(d) && return one(P)
x >= maximum(d) && return zero(P)
isnan(x) && return P(NaN)
n = length(ps)
stop_idx = searchsortedlast(support(d), x)
s = zero(P)
if stop_idx < div(n, 2)
@inbounds for i in 1:stop_idx
s += ps[i]
end
s = 1 - s
else
@inbounds for i in (stop_idx + 1):n
s += ps[i]
end
end
return s
end
function quantile(d::DiscreteNonParametric, q::Real)
0 <= q <= 1 || throw(DomainError())
x = support(d)
p = probs(d)
k = length(x)
i = 1
cp = p[1]
while cp < q && i < k #Note: is i < k necessary?
i += 1
@inbounds cp += p[i]
end
x[i]
end
minimum(d::DiscreteNonParametric) = first(support(d))
maximum(d::DiscreteNonParametric) = last(support(d))
insupport(d::DiscreteNonParametric, x::Real) =
length(searchsorted(support(d), x)) > 0
mean(d::DiscreteNonParametric) = dot(probs(d), support(d))
function var(d::DiscreteNonParametric)
x = support(d)
p = probs(d)
return var(x, Weights(p, one(eltype(p))); corrected=false)
end
function skewness(d::DiscreteNonParametric)
x = support(d)
p = probs(d)
return skewness(x, Weights(p, one(eltype(p))))
end
function kurtosis(d::DiscreteNonParametric)
x = support(d)
p = probs(d)
return kurtosis(x, Weights(p, one(eltype(p))))
end
entropy(d::DiscreteNonParametric) = entropy(probs(d))
entropy(d::DiscreteNonParametric, b::Real) = entropy(probs(d), b)
function mode(d::DiscreteNonParametric)
x = support(d)
p = probs(d)
return mode(x, Weights(p, one(eltype(p))))
end
function modes(d::DiscreteNonParametric)
x = support(d)
p = probs(d)
return modes(x, Weights(p, one(eltype(p))))
end
function mgf(d::DiscreteNonParametric, t::Real)
x = support(d)
p = probs(d)
s = zero(Float64)
for i in 1:length(x)
s += p[i] * exp(t*x[i])
end
s
end
function cf(d::DiscreteNonParametric, t::Real)
x = support(d)
p = probs(d)
s = zero(Complex{Float64})
for i in 1:length(x)
s += p[i] * cis(t*x[i])
end
s
end
# Sufficient statistics
struct DiscreteNonParametricStats{T<:Real,W<:Real,Ts<:AbstractVector{T},
Ws<:AbstractVector{W}} <: SufficientStats
support::Ts
freq::Ws
end
function suffstats(::Type{<:DiscreteNonParametric}, x::AbstractArray{T}) where {T<:Real}
N = length(x)
N == 0 && return DiscreteNonParametricStats(T[], Float64[])
n = 1
vs = Vector{T}(undef,N)
ps = zeros(Float64, N)
x = sort(vec(x))
vs[1] = x[1]
ps[1] += 1.
xprev = x[1]
@inbounds for i = 2:N
xi = x[i]
if xi != xprev
n += 1
vs[n] = xi
end
ps[n] += 1.
xprev = xi
end
resize!(vs, n)
resize!(ps, n)
DiscreteNonParametricStats(vs, ps)
end
function suffstats(::Type{<:DiscreteNonParametric}, x::AbstractArray{T},
w::AbstractArray{W}; check_args::Bool=true) where {T<:Real,W<:Real}
@check_args DiscreteNonParametric (length(x) == length(w))
N = length(x)
N == 0 && return DiscreteNonParametricStats(T[], W[])
n = 1
vs = Vector{T}(undef, N)
ps = zeros(W, N)
xorder = sortperm(vec(x))
x = vec(x)[xorder]
w = vec(w)[xorder]
vs[1] = x[1]
ps[1] += w[1]
xprev = x[1]
@inbounds for i = 2:N
xi = x[i]
wi = w[i]
if xi != xprev
n += 1
vs[n] = xi
end
ps[n] += wi
xprev = xi
end
resize!(vs, n)
resize!(ps, n)
DiscreteNonParametricStats(vs, ps)
end
# # Model fitting
fit_mle(::Type{<:DiscreteNonParametric},
ss::DiscreteNonParametricStats{T,W,Ts,Ws}) where {T,W,Ts,Ws} =
DiscreteNonParametric{T,W,Ts,Ws}(ss.support, normalize!(copy(ss.freq), 1), check_args=false)