Skip to content

Instantly share code, notes, and snippets.

@iwein
Created November 22, 2011 21:01
Show Gist options
  • Save iwein/1386948 to your computer and use it in GitHub Desktop.
Save iwein/1386948 to your computer and use it in GitHub Desktop.
LinearFit full code
object LinearFit {
def through (points : (Double,Double)*) : (Double, Double) = {
val (xTotal,yTotal) = ((0.0,0.0) /: points) { case ((xA, yA), (x, y)) => (xA + x, yA + y) }
val (xAverage, yAverage) = (xTotal / points.size , yTotal / points.size)
val (squareSumXX, squareSumXY) = ((0.0,0.0) /: points) {
case ((ssxx, ssxy), (x, y)) => (ssxx + pow(x - xAverage, 2),
ssxy + (x - xAverage) * (y - yAverage))
}
val b = (squareSumXY / squareSumXX)
(yAverage - b * xAverage, b)
}
def through (points : Map[Double,Double]) : (Double, Double) = {
through(points.toSeq:_*)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment