@@ -150,10 +150,10 @@ function Base.show(stream::IO, d::UnivariateFinite)
150
150
# instantiation of d
151
151
x1 = d. decoder (first (raw))
152
152
p1 = d. prob_given_class[first (raw)]
153
- str = " UnivariateFinite($x1 =>$p1 "
153
+ str = " UnivariateFinite($x1 =>$( round (p1, sigdigits = 3 )) "
154
154
pairs = (d. decoder (r)=> d. prob_given_class[r] for r in raw[2 : end ])
155
155
for pair in pairs
156
- str *= " , $(pair[1 ]) =>$(pair[2 ]) "
156
+ str *= " , $(pair[1 ]) =>$(round ( pair[2 ], sigdigits = 3 ) ) "
157
157
end
158
158
str *= " )"
159
159
print (stream, str)
@@ -199,18 +199,14 @@ function average(dvec::AbstractVector{UnivariateFinite{L,U,T}};
199
199
end
200
200
201
201
# get all refs:
202
- refs = reduce (union, [keys (d. prob_given_class) for d in dvec])
203
-
204
- # pad each individual dicts so they have common keys:
205
- z = LittleDict {U,T} ([x => zero (T) for x in refs]. .. )
206
- prob_given_class_vec = map (dvec) do d
207
- merge (z, d. prob_given_class)
208
- end
202
+ refs = Tuple (reduce (union, [keys (d. prob_given_class) for d in dvec]))
209
203
210
204
# initialize the prob dictionary for the distribution sum:
211
- prob_given_class = LittleDict {U,T} ()
212
- for x in refs
213
- prob_given_class[x] = zero (T)
205
+ prob_given_class = LittleDict {U,T} (refs, zeros (T, length (refs)))
206
+
207
+ # make vector of all the distributions dicts padded to have same common keys:
208
+ prob_given_class_vec = map (dvec) do d
209
+ merge (prob_given_class, d. prob_given_class)
214
210
end
215
211
216
212
# sum up:
@@ -232,15 +228,10 @@ function average(dvec::AbstractVector{UnivariateFinite{L,U,T}};
232
228
end
233
229
234
230
return UnivariateFinite (first (dvec). decoder, prob_given_class)
235
-
236
231
end
237
232
238
233
function _pdf (d:: UnivariateFinite{L,U,T} , ref) where {L,U,T}
239
- if haskey (d. prob_given_class, ref)
240
- return d. prob_given_class[ref]
241
- else
242
- return zero (T)
243
- end
234
+ return get (d. prob_given_class, ref, zero (T))
244
235
end
245
236
246
237
Distributions. pdf (d:: UnivariateFinite{L,U,T} ,
@@ -336,7 +327,9 @@ function Distributions.fit(d::Type{<:UnivariateFinite},
336
327
isempty (vpure) && error (" No non-missing data to fit. " )
337
328
N = length (vpure)
338
329
count_given_class = Dist. countmap (vpure)
339
- prob_given_class = LittleDict ([x=> c/ N for (x, c) in count_given_class])
330
+ classes = Tuple (keys (count_given_class))
331
+ probs = values (count_given_class)./ N
332
+ prob_given_class = LittleDict (classes, probs)
340
333
return UnivariateFinite (prob_given_class)
341
334
end
342
335
0 commit comments