elm - 1 year ago 71
Scala Question

# Scala simple histogram

For a given

`Array[Double]`
, for instance

``````val a = Array.tabulate(100){ _ => Random.nextDouble * 10 }
``````

what is a simple approach to calculate a histogram with
`n`
bins ?

A very similar preparation of values as in @om-nom-nom 's answer, yet the histogram method quite small by using `partition`,

``````case class Distribution(nBins: Int, data: List[Double]) {
require(data.length > nBins)

val Epsilon = 0.000001
val (max,min) = (data.max,data.min)
val binWidth = (max - min) / nBins + Epsilon
val bounds = (1 to nBins).map { x => min + binWidth * x }.toList

def histo(bounds: List[Double], data: List[Double]): List[List[Double]] =
bounds match {
case h :: Nil => List(data)
case h :: t   => val (l,r) = data.partition( _ < h) ; l :: histo(t,r)
}

val histogram = histo(bounds, data)
}
``````

Then for

``````val data = Array.tabulate(100){ _ => scala.util.Random.nextDouble * 10 }
val h = Distribution(5, data.toList).histogram
``````

and so

``````val tabulated = h.map {_.size}
``````
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download