I am relatively new to Scala, been using R and MATLAB before. I have written the following piece of code in Scala. I've written the same code in R and MATLAB, and in both it works just fine, but due to my inexperience with Scala the code below does not work.
import breeze.linalg._
import breeze.numerics.exp
import scala.math.log
val data = breeze.stats.distributions.Uniform(0, 1)
val ep: DenseMatrix[Double] = DenseMatrix.rand(39, 3, data).t
val a = DenseVector(1.0)
val out: DenseMatrix[Double] = tile(a, 1, 39)
val fout: DenseVector[Double] = out.toDenseVector
val A: Double = 0.0
val B: Double = 1.0
val eta: Double = 2.0 / Math.pow(B - A, 2.0)
val nCol: Int = 39
val nRow: Int = 3
var gA = 0.0
var gB = 0.0
var gamma = 0.0
def SubstFunction(predictions: DenseVector[Double], expertsPrediction: DenseVector[Double]): Double = {
gA = -(1 / eta) * log(predictions dot exp(-eta * (expertsPrediction :- A)) :^ 2.0)
gB = -(1 / eta) * log(predictions dot exp(-eta * (expertsPrediction :- B)) :^ 2.0)
gamma = (0.5 * (B + A)) - ((gB - gA) / 2 * (B - A))
gamma
}
def prediction(Input: DenseMatrix[Double], outcomes: DenseVector[Double]): DenseVector[Double] = {
var weights = DenseVector(1.0,1.0,1.0)
val AAprediction = DenseVector.fill(nCol)(0.0)
//DenseVector.ones[Double](nCol).t
for (l<-0 to Input.cols) {
val normalisedWeights = weights / sum(weights)
AAprediction(l) = SubstFunction(normalisedWeights, Input(::,l))
weights = normalisedWeights :* exp(eta :* (Input(::,l) :- outcomes(l)) :^ 2.0).toDenseVector
}
AAprediction: DenseVector[Double]
}
prediction(ep,fout)
I think problem might be when prediction calls sbstFunction
in it. I am using Scala Work sheet in intelliJ. When I run the code I get no error but I don't get a numeric output instead I get:
<function1> res1: Unit = ()
Update: I have fixed the code and now I am getting the following error:
Column must be in bounds for slice!
Can someone please help me understand what I am doing wrong?