/
univariates.jl
403 lines (310 loc) · 11.1 KB
/
univariates.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
#### Domain && Support
immutable RealInterval
lb::Float64
ub::Float64
RealInterval(lb::Real, ub::Real) = new(Float64(lb), Float64(ub))
end
minimum(r::RealInterval) = r.lb
maximum(r::RealInterval) = r.ub
in(x::Real, r::RealInterval) = (r.lb <= Float64(x) <= r.ub)
isbounded{D<:UnivariateDistribution}(d::Union{D,Type{D}}) = isupperbounded(d) && islowerbounded(d)
islowerbounded{D<:UnivariateDistribution}(d::Union{D,Type{D}}) = minimum(d) > -Inf
isupperbounded{D<:UnivariateDistribution}(d::Union{D,Type{D}}) = maximum(d) < +Inf
hasfinitesupport{D<:DiscreteUnivariateDistribution}(d::Union{D,Type{D}}) = isbounded(d)
hasfinitesupport{D<:ContinuousUnivariateDistribution}(d::Union{D,Type{D}}) = false
function insupport!{D<:UnivariateDistribution}(r::AbstractArray, d::Union{D,Type{D}}, X::AbstractArray)
length(r) == length(X) ||
throw(DimensionMismatch("Inconsistent array dimensions."))
for i in 1 : length(X)
@inbounds r[i] = insupport(d, X[i])
end
return r
end
insupport{D<:UnivariateDistribution}(d::Union{D,Type{D}}, X::AbstractArray) =
insupport!(BitArray(size(X)), d, X)
insupport{D<:ContinuousUnivariateDistribution}(d::Union{D,Type{D}},x::Real) = minimum(d) <= x <= maximum(d)
insupport{D<:DiscreteUnivariateDistribution}(d::Union{D,Type{D}},x::Real) = isinteger(x) && minimum(d) <= x <= maximum(d)
support{D<:ContinuousUnivariateDistribution}(d::Union{D,Type{D}}) = RealInterval(minimum(d), maximum(d))
support{D<:DiscreteUnivariateDistribution}(d::Union{D,Type{D}}) = round(Int, minimum(d)):round(Int, maximum(d))
# Type used for dispatch on finite support
# T = true or false
immutable FiniteSupport{T} end
## macros to declare support
macro distr_support(D, lb, ub)
D_has_constantbounds = (isa(ub, Number) || ub == :Inf) &&
(isa(lb, Number) || lb == :(-Inf))
paramdecl = D_has_constantbounds ? :(d::Union{$D, Type{$D}}) : :(d::$D)
# overall
esc(quote
minimum($(paramdecl)) = $lb
maximum($(paramdecl)) = $ub
end)
end
##### generic methods (fallback) #####
## sampling
rand(d::UnivariateDistribution) = quantile(d, rand())
rand!(d::UnivariateDistribution, A::AbstractArray) = _rand!(sampler(d), A)
rand(d::UnivariateDistribution, n::Int) = _rand!(sampler(d), Array(eltype(d), n))
rand(d::UnivariateDistribution, shp::Dims) = _rand!(sampler(d), Array(eltype(d), shp))
## statistics
std(d::UnivariateDistribution) = sqrt(var(d))
median(d::UnivariateDistribution) = quantile(d, 0.5)
modes(d::UnivariateDistribution) = [mode(d)]
entropy(d::UnivariateDistribution, b::Real) = entropy(d) / log(b)
isplatykurtic(d::UnivariateDistribution) = kurtosis(d) > 0.0
isleptokurtic(d::UnivariateDistribution) = kurtosis(d) < 0.0
ismesokurtic(d::UnivariateDistribution) = kurtosis(d) == 0.0
function kurtosis(d::Distribution, correction::Bool)
if correction
return kurtosis(d)
else
return kurtosis(d) + 3.0
end
end
excess(d::Distribution) = kurtosis(d)
excess_kurtosis(d::Distribution) = kurtosis(d)
proper_kurtosis(d::Distribution) = kurtosis(d, false)
#### pdf, cdf, and friends
# pdf
pdf(d::DiscreteUnivariateDistribution, x::Int) = throw(MethodError(pdf, (d, x)))
pdf(d::DiscreteUnivariateDistribution, x::Integer) = pdf(d, round(Int, x))
pdf(d::DiscreteUnivariateDistribution, x::Real) = isinteger(x) ? pdf(d, round(Int, x)) : 0.0
pdf(d::ContinuousUnivariateDistribution, x::Real) = throw(MethodError(pdf, (d, x)))
# logpdf
logpdf(d::DiscreteUnivariateDistribution, x::Int) = log(pdf(d, x))
logpdf(d::DiscreteUnivariateDistribution, x::Integer) = logpdf(d, round( Int, x))
logpdf(d::DiscreteUnivariateDistribution, x::Real) = isinteger(x) ? logpdf(d, round(Int, x)) : -Inf
logpdf(d::ContinuousUnivariateDistribution, x::Real) = log(pdf(d, x))
# cdf
cdf(d::DiscreteUnivariateDistribution, x::Int) = cdf(d, x, FiniteSupport{hasfinitesupport(d)})
# Discrete univariate with infinite support
function cdf(d::DiscreteUnivariateDistribution, x::Int, ::Type{FiniteSupport{false}})
c = 0.0
for y = minimum(d):x
c += pdf(d, y)
end
return c
end
# Discrete univariate with finite support
function cdf(d::DiscreteUnivariateDistribution, x::Int, ::Type{FiniteSupport{true}})
# calculate from left if x < (min + max)/2
# (same as infinite support version)
x <= div(minimum(d) + maximum(d),2) && return cdf(d, x, FiniteSupport{false})
# otherwise, calculate from the right
c = 1.0
for y = x+1:maximum(d)
c -= pdf(d, y)
end
return c
end
cdf(d::DiscreteUnivariateDistribution, x::Real) = cdf(d, floor(Int,x))
cdf(d::ContinuousUnivariateDistribution, x::Real) = throw(MethodError(cdf, (d, x)))
# ccdf
ccdf(d::DiscreteUnivariateDistribution, x::Int) = 1.0 - cdf(d, x)
ccdf(d::DiscreteUnivariateDistribution, x::Real) = ccdf(d, floor(Int,x))
ccdf(d::ContinuousUnivariateDistribution, x::Real) = 1.0 - cdf(d, x)
# logcdf
logcdf(d::DiscreteUnivariateDistribution, x::Int) = log(cdf(d, x))
logcdf(d::DiscreteUnivariateDistribution, x::Real) = logcdf(d, floor(Int,x))
logcdf(d::ContinuousUnivariateDistribution, x::Real) = log(cdf(d, x))
# logccdf
logccdf(d::DiscreteUnivariateDistribution, x::Int) = log(ccdf(d, x))
logccdf(d::DiscreteUnivariateDistribution, x::Real) = logccdf(d, floor(Int,x))
logccdf(d::ContinuousUnivariateDistribution, x::Real) = log(ccdf(d, x))
# quantile
quantile(d::UnivariateDistribution, p::Real) = throw(MethodError(quantile, (d, p)))
# cquantile
cquantile(d::UnivariateDistribution, p::Real) = quantile(d, 1.0 - p)
# invlogcdf
invlogcdf(d::UnivariateDistribution, lp::Real) = quantile(d, exp(lp))
# invlogccdf
invlogccdf(d::UnivariateDistribution, lp::Real) = quantile(d, -expm1(lp))
# gradlogpdf
gradlogpdf(d::ContinuousUnivariateDistribution, x::Real) = throw(MethodError(gradlogpdf, (d, x)))
# vectorized versions
for fun in [:pdf, :logpdf,
:cdf, :logcdf,
:ccdf, :logccdf,
:invlogcdf, :invlogccdf,
:quantile, :cquantile]
_fun! = Symbol('_', fun, '!')
fun! = Symbol(fun, '!')
@eval begin
function ($_fun!)(r::AbstractArray, d::UnivariateDistribution, X::AbstractArray)
for i in 1 : length(X)
r[i] = ($fun)(d, X[i])
end
return r
end
function ($fun!)(r::AbstractArray, d::UnivariateDistribution, X::AbstractArray)
length(r) == length(X) ||
throw(ArgumentError("Inconsistent array dimensions."))
$(_fun!)(r, d, X)
end
($fun)(d::UnivariateDistribution, X::AbstractArray) =
$(_fun!)(Array(Float64, size(X)), d, X)
end
end
function _pdf_fill_outside!(r::AbstractArray, d::DiscreteUnivariateDistribution, X::UnitRange)
vl = vfirst = first(X)
vr = vlast = last(X)
n = vlast - vfirst + 1
if islowerbounded(d)
lb = minimum(d)
if vl < lb
vl = lb
end
end
if isupperbounded(d)
ub = maximum(d)
if vr > ub
vr = ub
end
end
# fill left part
if vl > vfirst
for i = 1:(vl - vfirst)
r[i] = 0.0
end
end
# fill central part: with non-zero pdf
fm1 = vfirst - 1
for v = vl:vr
r[v - fm1] = pdf(d, v)
end
# fill right part
if vr < vlast
for i = (vr-vfirst+2):n
r[i] = 0.0
end
end
return vl, vr, vfirst, vlast
end
function _pdf!(r::AbstractArray, d::DiscreteUnivariateDistribution, X::UnitRange)
vl,vr, vfirst, vlast = _pdf_fill_outside!(r, d, X)
# fill central part: with non-zero pdf
fm1 = vfirst - 1
for v = vl:vr
r[v - fm1] = pdf(d, v)
end
return r
end
abstract RecursiveProbabilityEvaluator
function _pdf!(r::AbstractArray, d::DiscreteUnivariateDistribution, X::UnitRange, rpe::RecursiveProbabilityEvaluator)
vl,vr, vfirst, vlast = _pdf_fill_outside!(r, d, X)
# fill central part: with non-zero pdf
if vl <= vr
fm1 = vfirst - 1
r[vl - fm1] = pv = pdf(d, vl)
for v = (vl+1):vr
r[v - fm1] = pv = nextpdf(rpe, pv, v)
end
end
return r
end
pdf(d::DiscreteUnivariateDistribution) = isbounded(d) ? pdf(d, minimum(d):maximum(d)) :
error("pdf(d) is not allowed when d is unbounded.")
## loglikelihood
function _loglikelihood(d::UnivariateDistribution, X::AbstractArray)
ll = 0.0
for i in 1:length(X)
@inbounds ll += logpdf(d, X[i])
end
return ll
end
loglikelihood(d::UnivariateDistribution, X::AbstractArray) =
_loglikelihood(d, X)
### macros to use StatsFuns for method implementation
macro _delegate_statsfuns(D, fpre, psyms...)
dt = eval(D)
T = dt <: DiscreteUnivariateDistribution ? :Int : :Real
# function names from StatsFuns
fpdf = Symbol(fpre, "pdf")
flogpdf = Symbol(fpre, "logpdf")
fcdf = Symbol(fpre, "cdf")
fccdf = Symbol(fpre, "ccdf")
flogcdf = Symbol(fpre, "logcdf")
flogccdf = Symbol(fpre, "logccdf")
finvcdf = Symbol(fpre, "invcdf")
finvccdf = Symbol(fpre, "invccdf")
finvlogcdf = Symbol(fpre, "invlogcdf")
finvlogccdf = Symbol(fpre, "invlogccdf")
# parameter fields
pargs = [Expr(:(.), :d, Expr(:quote, s)) for s in psyms]
esc(quote
pdf(d::$D, x::$T) = $(fpdf)($(pargs...), x)
logpdf(d::$D, x::$T) = $(flogpdf)($(pargs...), x)
cdf(d::$D, x::$T) = $(fcdf)($(pargs...), x)
ccdf(d::$D, x::$T) = $(fccdf)($(pargs...), x)
logcdf(d::$D, x::$T) = $(flogcdf)($(pargs...), x)
logccdf(d::$D, x::$T) = $(flogccdf)($(pargs...), x)
quantile(d::$D, q::Float64) = convert($T, $(finvcdf)($(pargs...), q))
cquantile(d::$D, q::Float64) = convert($T, $(finvccdf)($(pargs...), q))
invlogcdf(d::$D, lq::Float64) = convert($T, $(finvlogcdf)($(pargs...), lq))
invlogccdf(d::$D, lq::Float64) = convert($T, $(finvlogccdf)($(pargs...), lq))
end)
end
##### specific distributions #####
const discrete_distributions = [
"bernoulli",
"betabinomial",
"binomial",
"categorical",
"discreteuniform",
"geometric",
"hypergeometric",
"negativebinomial",
"noncentralhypergeometric",
"poisson",
"skellam",
"poissonbinomial"
]
const continuous_distributions = [
"arcsine",
"beta",
"betaprime",
"biweight",
"cauchy",
"chisq", # Chi depends on Chisq
"chi",
"cosine",
"epanechnikov",
"exponential",
"fdist",
"frechet",
"gamma", "erlang",
"generalizedpareto",
"generalizedextremevalue",
"gumbel",
"inversegamma",
"inversegaussian",
"kolmogorov",
"ksdist",
"ksonesided",
"laplace",
"levy",
"logistic",
"noncentralbeta",
"noncentralchisq",
"noncentralf",
"noncentralt",
"normal",
"normalcanon",
"normalinversegaussian",
"lognormal", # LogNormal depends on Normal
"pareto",
"rayleigh",
"symtriangular",
"tdist",
"triangular",
"triweight",
"uniform",
"vonmises",
"weibull"
]
for dname in discrete_distributions
include(joinpath("univariate", "discrete", "$(dname).jl"))
end
for dname in continuous_distributions
include(joinpath("univariate", "continuous", "$(dname).jl"))
end