@@ -396,29 +396,32 @@ end
396396
397397# based on linear interpolation
398398function pdf (o:: KHist , x:: Number )
399- if x ≤ minimum (o. ex)
400- return 0.0
401- elseif x ≥ maximum (o. ex)
402- return 0.0
403- else
404- i = searchsortedfirst (o. bins, KHistBin (x, 0 )) - 1
399+ a, b = extrema (o. ex)
400+ if a < x < b
401+ i = searchsortedfirst (o. bins, KHistBin (x, 0 ))
405402 x1, y1 = xy (o. bins[i - 1 ])
406403 x2, y2 = xy (o. bins[i])
407404 return smooth (y1, y2, (x - x1) / (x2 - x1)) / area (o)
405+ else
406+ x == a && return o. bins[1 ]. count / area (o)
407+ x == b && return o. bins[end ]. count / area (o)
408+ return 0.0
408409 end
409410end
410411
411412function cdf (o:: KHist , x:: Number )
412- if x ≤ minimum (o. ex)
413- return 0.0
414- elseif x ≥ maximum (o. ex)
415- return 1.0
413+ a, b = extrema (o. ex)
414+ if x < a
415+ return 0.0
416+ elseif x == a
417+ return o. bins[1 ]. count / area (o)
418+ elseif x ≥ b
419+ return 1.0
416420 else
417- i = searchsortedfirst (o. bins, KHistBin (x, 0 )) - 1
418- x1, y1 = o. bins[i - 1 ]. loc, o. bins[i- 1 ]. count
419- x2, y2 = o. bins[i]. loc, o. bins[i]. count
420- w = x - x1
421+ i = searchsortedfirst (o. bins, KHistBin (x, 0 ))
422+ x1, y1 = xy (o. bins[i - 1 ])
423+ x2, y2 = xy (o. bins[i])
421424 h = smooth (y1, y2, (x2 - x) / (x2 - x1))
422- return (area (o, i- 2 ) + w * h) / area (o)
425+ return (area (o, i - 2 ) + (x - x1) * h) / area (o)
423426 end
424427end
0 commit comments