Skip to content

Instantly share code, notes, and snippets.

@slyphon
Created September 18, 2018 20:21
Show Gist options
  • Save slyphon/b04ae5f76de1befd46c5715cac49f270 to your computer and use it in GitHub Desktop.
Save slyphon/b04ae5f76de1befd46c5715cac49f270 to your computer and use it in GitHub Desktop.
fstatistic in breeze/scala
import breeze.linalg._
import breeze.stats._
import breeze.numerics._
def fStatistic(test: DenseVector[Double], control: DenseMatrix[Double]): Double = {
val mtest = test.toDenseMatrix
val allData = DenseMatrix.vertcat(mtest, control)
val grandMean = mean(allData)
val testMean = mean(test)
// sum of squares between canary mean and grandMean
val sumSqBetwn: Double =
sum(pow((testMean -:- grandMean), 2.0) *:* allData.rows.toDouble)
// sum of squares total
val sumSqTotal: Double = sum(pow(allData -:- grandMean, 2.0))
// sum of squares error
val sumSqError: Double = sumSqTotal - sumSqBetwn
// mean square between
val meanSqBtwn: Double = sumSqBetwn / (allData.cols - 1)
// mean square within
val meanSqWithin: Double = sumSqError / ((allData.rows * allData.cols) - mtest.cols).toDouble
meanSqBtwn / meanSqWithin
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment