 # How to get variance of a vector in a ScalaTask

#1

Hello,
the problem here is more about scala than openmole, I need to calculate the mean and variance of a vector. Here is a working code for this :

``````val meanAndVar =
doubleValue = doubleValue.map(_ - meanValue)
doubleValue = doubleValue.map(math.pow(_, 2))
val varValue = doubleValue.sum/doubleValue.size
""") set (
inputs += VectorValue,
outputs += (meanValue, varValue)
)
``````

As you can see it’s not very good, I’m doing three iterations over the doubleValue vector and I’m certain I can do it in only one (using an accumulator or something like this ?) but scala and functional programming/lambda calculus are really not my area of expertise.
Does someone know where to find such a code or how to achieve this task in a better way?

#2

Here is a solution from Marwan Ghanem to solve the problem :

``````val meanAndVar =
val varValue = ((doubleValue.map(x => math.pow(x - meanValue,2))).reduceLeft(_+_))/doubleValue.size
""") set (
inputs += VectorValue,
outputs += (meanValue, varValue)
)
``````

#3

Great that you found a solution, but don’t bother too much with this kind micro optimisation, if you computation task (model) takes more than 10 times this operation (which is certain if you are using an external task).

In OpenMOLE we provide also a function to compute that directly:

``````ScalaTask("""
val meanValue = doubleValue.average
val varValue = doubleValue.meanSquaredError""")
``````

should work.