I have a type alias on a SortedMap[Int, Double]
and I'd like to have an implicit that allows me to pass my SortedMap
to some of the built-in functions in breeze, specifically the breeze.stats._
functions variance
and stddev
.
Here's a working example without implicits:
package com.soquestion
import breeze.linalg._
import breeze.stats._
import scala.collection.SortedMap
import scala.language.implicitConversions
object proof {
type Series = SortedMap[Int, Double]
def example: Double = {
val s: Series = SortedMap(1 -> 9.0, 2 -> 2.0, 3 -> 5.0, 4 -> 4.0, 5 -> 12.0, 6 -> 7.0, 7 -> 8.0, 8 -> 11.0, 9 -> 9.0, 10 -> 3.0, 11 -> 7.0, 12 -> 4.0, 13 -> 12.0, 14 -> 5.0, 15 -> 4.0, 16 -> 10.0, 17 -> 9.0, 18 -> 6.0, 19 -> 9.0, 20 -> 4.0)
stddev(s.values)
}
}
run in sbt console
scala> com.soquestion.proof.example
res0: Double = 3.0607876523260447
What I'd like is to not have to specify the .values
and just call stddev(s)
and variance(s)
.
Here's what I tried
package com.soquestion
import breeze.linalg._
import breeze.stats._
import scala.collection.SortedMap
import scala.language.implicitConversions
object proof {
// Implicitly convert the SortedMap, or any map, to a DenseVector[Double]
implicit def series2DenseVector(s: Traversable[(Int, Double)]): DenseVector[Double] = {
DenseVector(s.map(_._2).toArray)
}
type Series = SortedMap[Int, Double]
def example: Double = {
val s: Series = SortedMap(1 -> 9.0, 2 -> 2.0, 3 -> 5.0, 4 -> 4.0, 5 -> 12.0, 6 -> 7.0, 7 -> 8.0, 8 -> 11.0, 9 -> 9.0, 10 -> 3.0, 11 -> 7.0, 12 -> 4.0, 13 -> 12.0, 14 -> 5.0, 15 -> 4.0, 16 -> 10.0, 17 -> 9.0, 18 -> 6.0, 19 -> 9.0, 20 -> 4.0)
stddev(s) // <--- compiler error here
}
}
but I get a compiler error of
could not find implicit value for parameter impl: breeze.stats.stddev.Impl[com.soquestion.proof.Series,VR]
going through the breeze documentation I wasn't able to find a good example of what implicit I need to provide. Ideally I'd like to make one implicit that would allow me to call both stdev
and variance
without multiple implicits.
I did see the question Scala Breeze DenseVector Implicit failure, but I don't see how it would apply to this scenario.
Full formatted answer based on @dlwh's answer below in case anyone needs it in the future
package com.soquestion
import breeze.linalg.support._
import breeze.linalg.support.CanTraverseValues._
import breeze.stats._
import scala.annotation.tailrec
import scala.collection.SortedMap
import scala.language.implicitConversions
object proof {
type Series = SortedMap[Int, Double]
def example: Double = {
// ideally this implicit would go in a scope higher up so it could be
// brought in wherever it's needed, but this works for a sample
implicit object SeriesIter extends CanTraverseValues[Series, Double] {
def isTraversableAgain(from: Series) = true
def traverse(from: Series, fn: ValuesVisitor[Double]): Unit = {
@tailrec def traverser(idx: Int, t: Array[Double]): Unit = {
if (idx == 1) fn.visit(t.head)
else {
fn.visit(t.head)
traverser(idx - 1, t.tail)
}
}
val v: Array[Double] = from.values.toArray
fn.zeros(0, 0d)
traverser(v.size, v)
}
}
val s: Series = SortedMap(1 -> 9.0, 2 -> 2.0, 3 -> 5.0, 4 -> 4.0, 5 -> 12.0, 6 -> 7.0, 7 -> 8.0, 8 -> 11.0, 9 -> 9.0, 10 -> 3.0, 11 -> 7.0, 12 -> 4.0, 13 -> 12.0, 14 -> 5.0, 15 -> 4.0, 16 -> 10.0, 17 -> 9.0, 18 -> 6.0, 19 -> 9.0, 20 -> 4.0)
stddev(s)
}
}
The docs could be much better for this, and I do wish I could make the error messages more helpful.
If you look at stddev
's source you'll see that it requires an implementation of variance.Impl
, which requires a meanAndVariance.Impl
, which can be provided for any type that has a CanTraverseValues[T, Double]
implicit. By default, there's a CanTraverseValues
implicit for collections, but only on the contained type, not the values of a scala's Map
types.
Implementing CanTraverseValues and CanMapValues will enable most breeze UFuncs.
Scala generally won't "chain" implicits, which is why your proof
example didn't work.