|
136 | 136 |
|
137 | 137 | # TODO: It would be useful to define == as well.
|
138 | 138 |
|
139 |
| -# TODO: Now that `UnivariateFinite` is any finite measure, we can |
140 |
| -# replace the following nonsense with an overloading of `+`. I think |
141 |
| -# it is only used in MLJEnsembles.jl - but need to check. I believe |
142 |
| -# this is a private method we can easily remove |
143 |
| - |
144 |
| -function average(dvec::AbstractVector{UnivariateFinite{S,V,R,P}}; |
145 |
| - weights=nothing) where {S,V,R,P} |
146 |
| - |
147 |
| - n = length(dvec) |
148 |
| - |
149 |
| - Dist.@check_args(UnivariateFinite, weights == nothing || n==length(weights)) |
150 |
| - |
151 |
| - # check all distributions have consistent pool: |
152 |
| - first_index = first(dvec).decoder.classes |
153 |
| - for d in dvec |
154 |
| - d.decoder.classes == first_index || |
155 |
| - error("Averaging UnivariateFinite distributions with incompatible"* |
156 |
| - " pools. ") |
157 |
| - end |
158 |
| - |
159 |
| - # get all refs: |
160 |
| - refs = reduce(union, [keys(d.prob_given_ref) for d in dvec]) |> collect |
161 |
| - |
162 |
| - # initialize the prob dictionary for the distribution sum: |
163 |
| - prob_given_ref = LittleDict{R,P}([refs...], zeros(P, length(refs))) |
164 |
| - |
165 |
| - # make vector of all the distributions dicts padded to have same common keys: |
166 |
| - prob_given_ref_vec = map(dvec) do d |
167 |
| - merge(prob_given_ref, d.prob_given_ref) |
168 |
| - end |
169 |
| - |
170 |
| - # sum up: |
171 |
| - if weights == nothing |
172 |
| - scale = 1/n |
173 |
| - for x in refs |
174 |
| - for k in 1:n |
175 |
| - prob_given_ref[x] += scale*prob_given_ref_vec[k][x] |
176 |
| - end |
177 |
| - end |
178 |
| - else |
179 |
| - scale = 1/sum(weights) |
180 |
| - for x in refs |
181 |
| - for k in 1:n |
182 |
| - prob_given_ref[x] += |
183 |
| - weights[k]*prob_given_ref_vec[k][x]*scale |
184 |
| - end |
185 |
| - end |
186 |
| - end |
187 |
| - d1 = first(dvec) |
188 |
| - return UnivariateFinite(sample_scitype(d1), d1.decoder, prob_given_ref) |
189 |
| -end |
190 |
| - |
191 | 139 | """
|
192 | 140 | Dist.pdf(d::UnivariateFinite, x)
|
193 | 141 |
|
|
0 commit comments