fslaborg / FSharp.Stats

statistical testing, linear algebra, machine learning, fitting and signal processing in F#
https://fslab.org/FSharp.Stats/
Other
210 stars 56 forks source link

[Feature Request] Median should give a more helpful error for empty non-float sequences #339

Open nhirschey opened 1 month ago

nhirschey commented 1 month ago

Is your feature request related to a problem? Please describe. The median of an empty float sequence is nan. The median of an empty integer or decimal sequence causes an error with an unhelpful error message.

The error message for an empty non-float sequence should make it clear it's an FSharp.Stats problem to make it easier to debug.

#r "nuget: FSharp.Stats, 0.5.1-preview.2"

open FSharp.Stats

let ys: float [] = [||]
median ys

> val ys: float array = [||]
> val it: float = nan

let xs: decimal [] = [||]
median xs
> System.DivideByZeroException: Attempted to divide by zero.
   at System.Decimal.DecCalc.VarDecDiv(DecCalc& d1, DecCalc& d2)
   at System.Decimal.op_Division(Decimal d1, Decimal d2)

Describe the solution you'd like An error message such as "FSharp.Stats median is undefined for an empty decimal sequence"