@@ -300,38 +300,54 @@ const UNIVARIATE_FINITE_DOCSTRING =
300
300
301
301
Construct a discrete univariate distribution whose finite support is
302
302
the elements of the vector `support`, and whose corresponding
303
- probabilities are elements of the vector `probs`, which must sum to
304
- one.
303
+ probabilities are elements of the vector `probs`. More generally,
304
+ construct an abstract *array* of `UnivariateFinite` distributions by
305
+ choosing `probs` to be an array of one higher dimension than the array
306
+ generated.
305
307
306
- Unless `pool` is specified, `support` must have type
308
+ Unless `pool` is specified, `support` should have type
307
309
`AbstractVector{<:CategoricalValue}` and all elements are assumed to
308
310
share the same categorical pool.
309
311
312
+ *Important.* All levels of the common pool have associated
313
+ probabilites, not just those in the specified `support`. However,
314
+ these probabilities are always zero (see example below).
315
+
316
+ If `probs` has size `(C, n1, n2, ..., nk)` then an array of size `(n1,
317
+ n2, ..., nk)` is created. In all cases elements along the first axis
318
+ always sum to one.
319
+
310
320
```
311
321
using CategoricalArrays
312
322
v = categorical([:x, :x, :y, :x, :z])
313
323
314
324
julia> UnivariateFinite(classes(v), [0.2, 0.3, 0.5])
315
- UnivariateFinite(x=>0.2, y=>0.3, z=>0.5) (Multiclass{3} samples )
325
+ UnivariateFinite{Multiclass{3}} (x=>0.2, y=>0.3, z=>0.5)
316
326
317
327
julia> d = UnivariateFinite([v[1], v[end]], [0.1, 0.9])
318
- UnivariateFinite(x=>0.1, z=>0.9) (Multiclass{3} samples)
328
+ UnivariateFiniteMulticlass{3}(x=>0.1, z=>0.9)
329
+
330
+ julia> rand(d, 3)
331
+ 3-element Array{Any,1}:
332
+ CategoricalArrays.CategoricalValue{Symbol,UInt32} :z
333
+ CategoricalArrays.CategoricalValue{Symbol,UInt32} :z
334
+ CategoricalArrays.CategoricalValue{Symbol,UInt32} :z
335
+
336
+ julia> levels(d)
337
+ 3-element Array{Symbol,1}:
338
+ :x
339
+ :y
340
+ :z
319
341
320
342
julia> pdf(d, :y)
321
343
0.0
322
-
323
344
```
324
345
325
346
Alternatively, `support` may be a list of raw (non-categorical)
326
347
elements if `pool` is:
327
348
328
- - some `v::CategoricalVector` such that `support` is a subset of
329
- `levels(v)`
330
-
331
- - some `a::CategoricalValue` such that `support` is a subset of
332
- `levels(a)`
333
-
334
- - some `CategoricalPool` object
349
+ - some `CategoricalArray`, `CategoricalValue` or `CategoricalPool`,
350
+ such that `support` is a subset of `levels(pool)`
335
351
336
352
- `missing`, in which case a new categorical pool is created which has
337
353
`support` as its only levels.
@@ -341,67 +357,47 @@ considered ordered.
341
357
342
358
```
343
359
julia> UnivariateFinite([:x, :z], [0.1, 0.9], pool=missing, ordered=true)
344
- UnivariateFinite(x=>0.1, z=>0.9) (OrderedFactor{2} samples )
360
+ UnivariateFinite{OrderedFactor{2}} (x=>0.1, z=>0.9)
345
361
346
362
julia> d = UnivariateFinite([:x, :z], [0.1, 0.9], pool=v) # v defined above
347
363
UnivariateFinite(x=>0.1, z=>0.9) (Multiclass{3} samples)
348
364
349
365
julia> pdf(d, :y) # allowed as `:y in levels(v)`
350
366
0.0
367
+
368
+ v = categorical([:x, :x, :y, :x, :z, :w])
369
+ probs = rand(3, 100)
370
+ probs = probs ./ sum(probs, dims=1)
371
+ julia> UnivariateFinite([:x, :y, :z], probs, pool=v)
372
+ 100-element UnivariateFiniteVector{Multiclass{4},Symbol,UInt32,Float64}:
373
+ UnivariateFinite{Multiclass{4}}(x=>0.194, y=>0.3, z=>0.505)
374
+ UnivariateFinite{Multiclass{4}}(x=>0.727, y=>0.234, z=>0.0391)
375
+ UnivariateFinite{Multiclass{4}}(x=>0.674, y=>0.00535, z=>0.321)
376
+ ⋮
377
+ UnivariateFinite{Multiclass{4}}(x=>0.292, y=>0.339, z=>0.369)
351
378
```
352
379
380
+ ---
381
+
353
382
UnivariateFinite(prob_given_class; pool=nothing, ordered=false)
354
383
355
384
Construct a discrete univariate distribution whose finite support is
356
385
the set of keys of the provided dictionary, `prob_given_class`, and
357
386
whose values specify the corresponding probabilities.
358
387
359
388
The type requirements on the keys of the dictionary are the same as
360
- `support` above.
389
+ the elements of `support` given above. If the values (probabilities)
390
+ are arrays instead of scalars, then an abstract array of
391
+ `UnivariateFinite` elements is created, with the same size as the
392
+ array.
361
393
362
394
"""
395
+ UNIVARIATE_FINITE_DOCSTRING
363
396
UnivariateFinite (d:: AbstractDict ; kwargs... ) =
364
397
UnivariateFinite (get_interface_mode (), d; kwargs... )
365
398
UnivariateFinite (support:: AbstractVector , probs; kwargs... ) =
366
399
UnivariateFinite (get_interface_mode (), support, probs; kwargs... )
367
400
UnivariateFinite (probs; kwargs... ) =
368
401
UnivariateFinite (get_interface_mode (), probs; kwargs... )
369
-
370
402
UnivariateFinite (:: LightInterface , a... ; kwargs... ) =
371
403
errlight (" UnivariateFinite" )
372
-
373
- const UNIVARIATE_FINITE_VECTOR_DOCSTRING =
374
- """
375
- UnivariateFiniteArray(support, probs; pool=nothing, ordered=false)
376
-
377
- Construct a performant array of `UnivariateFinite` elements.
378
-
379
- For an explanation of `support` and the keyword arguments, see
380
- [`UnivariateFinite`](@ref) . Here `probs` should be an array with
381
- `size(probs, 1) = C`, where `C = length(support)`, and its elements
382
- should sum to one along the first dimension.
383
-
384
- In the special binary case `prob` may be a vector of arbitrary `Real`
385
- elements between 0 and 1, signifying the probabilities of the first
386
- element of `support`.
387
-
388
- ```
389
- using CategoricalArrays
390
- v = categorical([:x, :x, :y, :x, :z, :w])
391
- p = rand(6, 3)
392
- p = p ./ sum(p, dims=2)
393
- UnivariateFiniteArray([v[1], v[3], v[5]], p)
394
-
395
- UnivariateFiniteArray([:x, :z, :z], pool=missing, ordered=true)
396
-
397
- ```
398
-
399
- """
400
- UnivariateFiniteArray (probs:: AbstractArray ; kwargs... ) =
401
- UnivariateFiniteArray (get_interface_mode (), probs; kwargs... )
402
- UnivariateFiniteArray (support:: AbstractArray ,
403
- probs:: AbstractArray ; kwargs... ) =
404
- UnivariateFiniteArray (get_interface_mode (), support, probs; kwargs... )
405
-
406
- UnivariateFiniteArray (:: LightInterface , a... ; kwargs... ) =
407
- errlight (" UnivariateFiniteArray" )
0 commit comments