Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

single precision support added for LeastSquares calculation #857

Merged
merged 4 commits into from
Jan 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion math/src/main/scala/breeze/stats/regression/Lasso.scala
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ private case class LassoCalculator(
r2
}

private def estimateOneColumn(column: Int): LeastSquaresRegressionResult = {
private def estimateOneColumn(column: Int): LeastSquaresRegressionResult[Double] = {
/*
* Goal of this routine is to use the specified column to explain as much of the residual
* as possible, after using the already specified values in other columns.
Expand Down
172 changes: 114 additions & 58 deletions math/src/main/scala/breeze/stats/regression/LeastSquares.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,102 +4,158 @@ import breeze.generic.UFunc
import breeze.linalg._
import org.netlib.util.intW
import dev.ludovic.netlib.lapack.LAPACK.{getInstance => lapack}

import java.util.Arrays
import scala.reflect.ClassTag

private object leastSquaresImplementation {
def doLeastSquares(
data: DenseMatrix[Double],
outputs: DenseVector[Double],
workArray: Array[Double]): LeastSquaresRegressionResult = {
def doLeastSquares[T: ClassTag](
data: DenseMatrix[T],
outputs: DenseVector[T],
workArray: Array[T])
(implicit num: Numeric[T]): LeastSquaresRegressionResult[T] = {
import num._
require(data.rows == outputs.size)
require(data.rows > data.cols + 1)
require(workArray.length >= 2 * data.rows * data.cols)

val info = new intW(0)
lapack.dgels(
"N",
data.rows,
data.cols,
1,
data.data,
data.rows,
outputs.data,
data.rows,
workArray,
workArray.length,
info)
if (info.`val` < 0) {
throw new ArithmeticException("Least squares did not converge.")
}
lapack match {
case lapack_double if num == implicitly[Numeric[Double]] =>
lapack_double.dgels(
"N",
data.rows,
data.cols,
1,
data.data.asInstanceOf[Array[Double]],
data.rows,
outputs.data.asInstanceOf[Array[Double]],
data.rows,
workArray.asInstanceOf[Array[Double]],
workArray.length,
info)

if (info.`val` < 0) {
throw new ArithmeticException("Least squares did not converge.")
}

val coefficients = new DenseVector[Double](Arrays.copyOf(outputs.data.asInstanceOf[Array[Double]], data.cols))
var r2 = 0.toDouble
for (i <- 0 until (data.rows - data.cols)) {
r2 = r2 + math.pow(outputs.data(data.cols + i).toDouble, num.fromInt(2).toDouble)
}
LeastSquaresRegressionResult(coefficients.asInstanceOf[DenseVector[T]], r2.asInstanceOf[T])


val coefficients = new DenseVector[Double](Arrays.copyOf(outputs.data, data.cols))
var r2 = 0.0
for (i <- 0 until (data.rows - data.cols)) {
r2 = r2 + math.pow(outputs.data(data.cols + i), 2)
case lapack_float if num == implicitly[Numeric[Float]] =>
lapack_float.sgels(
"N",
data.rows,
data.cols,
1,
data.data.asInstanceOf[Array[Float]],
data.rows,
outputs.data.asInstanceOf[Array[Float]],
data.rows,
workArray.asInstanceOf[Array[Float]],
workArray.length,
info)

if (info.`val` < 0) {
throw new ArithmeticException("Least squares did not converge.")
}

val coefficients = new DenseVector[Float](Arrays.copyOf(outputs.data.asInstanceOf[Array[Float]], data.cols))
var r2 = 0.toDouble
for (i <- 0 until (data.rows - data.cols)) {
r2 = r2 + math.pow(outputs.data(data.cols + i).toDouble, num.fromInt(2).toDouble)
}
LeastSquaresRegressionResult(coefficients.asInstanceOf[DenseVector[T]], r2.toFloat.asInstanceOf[T])
case _ =>
throw new UnsupportedOperationException("Unsupported numeric type. Only Float and Double are supported")
}
LeastSquaresRegressionResult(coefficients, r2)

}
}

case class LeastSquaresRegressionResult(coefficients: DenseVector[Double], rSquared: Double)
extends RegressionResult[DenseVector[Double], Double] {
def apply(x: DenseVector[Double]): Double = coefficients.dot(x)
case class LeastSquaresRegressionResult[T](coefficients: DenseVector[T], rSquared: T)
extends RegressionResult[DenseVector[T], T] {
def apply(x: DenseVector[T]): T =

( coefficients.asInstanceOf[DenseVector[Double]] .dot( x.asInstanceOf[DenseVector[Double]] )).asInstanceOf[T]

def apply(X: DenseMatrix[Double]): DenseVector[Double] = X * coefficients
def apply(X: DenseMatrix[T]): DenseVector[T] =
( X .asInstanceOf[DenseMatrix[Double]] * (coefficients.asInstanceOf[DenseVector[Double]])).asInstanceOf[DenseVector[T]]
}


object leastSquares extends UFunc {
implicit val matrixVectorWithWorkArray
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] =
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] {
implicit def matrixVectorWithWorkArray[T: ClassTag](
implicit num: Numeric[T]): Impl3[DenseMatrix[T], DenseVector[T], Array[T], LeastSquaresRegressionResult[T]] =
new Impl3[DenseMatrix[T], DenseVector[T], Array[T], LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[Double],
outputs: DenseVector[Double],
workArray: Array[Double]): LeastSquaresRegressionResult =
data: DenseMatrix[T],
outputs: DenseVector[T],
workArray: Array[T]): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(data.copy, outputs.copy, workArray)
}

implicit val matrixVectorSpecifiedWork
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] =
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] {
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double], workSize: Int): LeastSquaresRegressionResult =
leastSquaresImplementation.doLeastSquares(data.copy, outputs.copy, new Array[Double](workSize))
implicit def matrixVectorSpecifiedWork[T: ClassTag](
implicit num: Numeric[T]): Impl3[DenseMatrix[T], DenseVector[T], Int, LeastSquaresRegressionResult[T]] =
new Impl3[DenseMatrix[T], DenseVector[T], Int, LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[T],
outputs: DenseVector[T],
workSize: Int): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(data.copy, outputs.copy, new Array[T](workSize))
}

implicit val matrixVector: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] =
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] {
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double]): LeastSquaresRegressionResult =
implicit def matrixVector[T: ClassTag](
implicit num: Numeric[T]): Impl2[DenseMatrix[T], DenseVector[T], LeastSquaresRegressionResult[T]] =
new Impl2[DenseMatrix[T], DenseVector[T], LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[T],
outputs: DenseVector[T]): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(
data.copy,
outputs.copy,
new Array[Double](math.max(1, data.rows * data.cols * 2)))
new Array[T](math.max(1, data.rows * data.cols * 2)))
}
}


object leastSquaresDestructive extends UFunc {
implicit val matrixVectorWithWorkArray
: Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] =
new Impl3[DenseMatrix[Double], DenseVector[Double], Array[Double], LeastSquaresRegressionResult] {

implicit def matrixVectorWithWorkArray[T: ClassTag](
implicit num: Numeric[T]): Impl3[DenseMatrix[T], DenseVector[T], Array[T], LeastSquaresRegressionResult[T]] =
new Impl3[DenseMatrix[T], DenseVector[T], Array[T], LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[Double],
outputs: DenseVector[Double],
workArray: Array[Double]): LeastSquaresRegressionResult =
data: DenseMatrix[T],
outputs: DenseVector[T],
workArray: Array[T]): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(data, outputs, workArray)
}

implicit val matrixVectorSpecifiedWork
: Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] =
new Impl3[DenseMatrix[Double], DenseVector[Double], Int, LeastSquaresRegressionResult] {
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double], workSize: Int): LeastSquaresRegressionResult =
leastSquaresImplementation.doLeastSquares(data, outputs, new Array[Double](workSize))
implicit def matrixVectorSpecifiedWork[T: ClassTag](
implicit num: Numeric[T]): Impl3[DenseMatrix[T], DenseVector[T], Int, LeastSquaresRegressionResult[T]] =
new Impl3[DenseMatrix[T], DenseVector[T], Int, LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[T],
outputs: DenseVector[T],
workSize: Int): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(data, outputs, new Array[T](workSize))
}

implicit val matrixVector: Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] =
new Impl2[DenseMatrix[Double], DenseVector[Double], LeastSquaresRegressionResult] {
def apply(data: DenseMatrix[Double], outputs: DenseVector[Double]): LeastSquaresRegressionResult =
implicit def matrixVector[T: ClassTag](
implicit num: Numeric[T]): Impl2[DenseMatrix[T], DenseVector[T], LeastSquaresRegressionResult[T]] =
new Impl2[DenseMatrix[T], DenseVector[T], LeastSquaresRegressionResult[T]] {
def apply(
data: DenseMatrix[T],
outputs: DenseVector[T]): LeastSquaresRegressionResult[T] =
leastSquaresImplementation.doLeastSquares(
data,
outputs,
new Array[Double](math.max(1, data.rows * data.cols * 2)))
new Array[T](math.max(1, data.rows * data.cols * 2)))
}

}