@@ -32,17 +32,18 @@ function Probabilities(x::AbstractVector{<:Integer})
32
32
return Probabilities (x ./ s, true )
33
33
end
34
34
35
-
36
35
# extend base Vector interface:
37
- for f in (:length , :size , :eachindex , :eltype ,
36
+ for f in (:length , :size , :eachindex , :eltype , :parent ,
38
37
:lastindex , :firstindex , :vec , :getindex , :iterate )
39
38
@eval Base.$ (f)(d:: Probabilities , args... ) = $ (f)(d. p, args... )
40
39
end
41
40
Base. IteratorSize (:: Probabilities ) = Base. HasLength ()
41
+ # Special extension due to the rules of the API
42
42
@inline Base. sum (:: Probabilities{T} ) where T = one (T)
43
43
44
44
"""
45
- ProbabilitiesEstimator
45
+ ProbabilitiesEstimator
46
+
46
47
The supertype for all probabilities estimators.
47
48
48
49
In Entropies.jl, probability distributions are estimated from data by defining a set of
@@ -66,6 +67,11 @@ across experimental realizations, by using the outcome as a dictionary key and t
66
67
probability as the value for that key (or, alternatively, the key remains the outcome
67
68
and one has a vector of probabilities, one for each experimental realization).
68
69
70
+ We have made the design decision that all probabilities estimators have a well defined
71
+ outcome space when instantiated. For some estimators this means that the input data
72
+ `x` must be provided both when instantiating the estimator, but also when computing
73
+ the probabilities.
74
+
69
75
All currently implemented probability estimators are:
70
76
71
77
- [`CountOccurrences`](@ref).
@@ -110,13 +116,14 @@ function probabilities(est::ProbabilitiesEstimator, x)
110
116
end
111
117
112
118
"""
113
- probabilities_and_outcomes(x, est) → (probs, Ω::Vector )
119
+ probabilities_and_outcomes(est, x )
114
120
115
- Return `probs, Ω`, where `probs = probabilities(x, est)` and
116
- `Ω[i]` is the outcome with probability `probs[i]`.
117
- The element type of `Ω` depends on the estimator.
121
+ Return `probs, outs`, where `probs = probabilities(x, est)` and
122
+ `outs[i]` is the outcome with probability `probs[i]`.
123
+ The element type of `outs` depends on the estimator.
124
+ `outs` is a subset of the [`outcome_space`](@ref) of `est`.
118
125
119
- See also [`outcomes`](@ref), [`total_outcomes`](@ref), and [`outcome_space`](@ref) .
126
+ See also [`outcomes`](@ref), [`total_outcomes`](@ref).
120
127
"""
121
128
function probabilities_and_outcomes (est:: ProbabilitiesEstimator , x)
122
129
error (" `probabilities_and_outcomes` not implemented for estimator $(typeof (est)) ." )
@@ -136,73 +143,42 @@ function probabilities! end
136
143
# Outcome space
137
144
# ##########################################################################################
138
145
"""
139
- outcome_space([x,] est::ProbabilitiesEstimator) → Ω
146
+ outcome_space(est::ProbabilitiesEstimator) → Ω
140
147
141
- Return a container (typically `Vector`) containing all _possible_ outcomes of `est`,
142
- i.e., the outcome space `Ω`.
143
- Only possible for estimators that implement [`total_outcomes`](@ref),
144
- and similarly, for some estimators `x` is not needed. The _values_ of `x` are never needed;
145
- but some times the type and dimensional layout of `x` is.
148
+ Return a container containing all _possible_ outcomes of `est`.
146
149
"""
147
- function outcome_space (x, est:: ProbabilitiesEstimator )
148
- outcome_space (est)
149
- end
150
150
function outcome_space (est:: ProbabilitiesEstimator )
151
- error (
152
- " `outcome_space(est)` not known/implemented for estimator $(typeof (est)) ." *
153
- " Try providing some input data, e.g. `outcomes_space(x, est)`." *
154
- " In some cases, this gives the dimensional layout/type information needed " *
155
- " to define the outcome space."
156
- )
151
+ error (" `outcome_space` not implemented for estimator $(typeof (est)) ." )
157
152
end
158
153
159
154
"""
160
- total_outcomes([x::Array_or_Dataset,] est::ProbabilitiesEstimator) → Int
161
-
162
- Return the size/cardinality of the outcome space ``\\ Omega`` defined by the probabilities
163
- estimator `est` imposed on the input data `x`.
155
+ total_outcomes(est::ProbabilitiesEstimator)
164
156
165
- For some estimators, the total number of outcomes is independent of `x`, in which case
166
- the input `x` is ignored and the method `total_outcomes(est)` is called. If the total
167
- number of states cannot be known a priori, an error is thrown. Primarily used in
168
- [`entropy_normalized`](@ref).
169
-
170
- ## Examples
171
-
172
- ```jldoctest setup = :(using Entropies)
173
- julia> est = SymbolicPermutation(m = 4);
174
-
175
- julia> total_outcomes(rand(42), est) # same as `factorial(m)` for any `x`
176
- 24
177
- ```
157
+ Return the length (cardinality) of the outcome space ``\\ Omega`` of `est`.
178
158
"""
179
- function total_outcomes (x, est:: ProbabilitiesEstimator )
180
- return length (outcome_space (x, est))
181
- end
159
+ total_outcomes (est:: ProbabilitiesEstimator ) = length (outcome_space (est))
182
160
183
161
"""
184
- missing_outcomes(x, est::ProbabilitiesEstimator) → n_missing::Int
162
+ missing_outcomes(est::ProbabilitiesEstimator, x ) → n_missing::Int
185
163
186
164
Estimate a probability distribution for `x` using the given estimator, then count the number
187
165
of missing (i.e. zero-probability) outcomes.
188
166
189
- Works for estimators that implement [`total_outcomes`](@ref).
190
-
191
167
See also: [`MissingDispersionPatterns`](@ref).
192
168
"""
193
- function missing_outcomes (x :: Array_or_Dataset , est :: ProbabilitiesEstimator )
169
+ function missing_outcomes (est :: ProbabilitiesEstimator , x :: Array_or_Dataset )
194
170
probs = probabilities (x, est)
195
- L = total_outcomes (x, est)
171
+ L = total_outcomes (est)
196
172
O = count (! iszero, probs)
197
173
return L - O
198
174
end
199
175
200
176
"""
201
- outcomes(x, est::ProbabilitiesEstimator)
177
+ outcomes(est::ProbabilitiesEstimator, x )
202
178
Return all (unique) outcomes contained in `x` according to the given estimator.
203
179
Equivalent with `probabilities_and_outcomes(x, est)[2]`, but for some estimators
204
180
it may be explicitly extended for better performance.
205
181
"""
206
- function outcomes (x, est:: ProbabilitiesEstimator )
182
+ function outcomes (est:: ProbabilitiesEstimator , x )
207
183
return probabilities_and_outcomes (est, x)[2 ]
208
184
end
0 commit comments