0

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?

halfer
  • 19,824
  • 17
  • 99
  • 186
Jamil
  • 61
  • 7

3 Answers3

1
def prediction(Input: DenseMatrix[Double], outcomes: DenseMatrix[Double]) = 
  (AAprediction: DenseVector[Double]) => {

This is declaration of prediction. It's a method that takes two arguments and returns a function. Quickly looking at the code it looks like it's the function has type DenseVector[Double] => DenseVector[Double], a more precise declaration would be:

def prediction(Input: DenseMatrix[Double], 
   outcomes: DenseMatrix[Double]): DenseVector[Double] => DenseVector[Double]

Essentially what it doing in prediction(ep,out) is constructing a function. A much simpler example:

scala> def addConst(x:Int):Int => Int = y => x + y 
addConst: (x: Int)Int => Int

scala> addConst(10)
res1: Int => Int = <function1>

In this case, again, we built a function. To use this function we could either call res1(5) or

scala> addConst(10)(5)
res2: Int = 15
pedrofurla
  • 12,763
  • 1
  • 38
  • 49
  • I think the problem is that AAprediction does not get filled inside the loop, or there is no output. – Jamil Apr 14 '17 at 22:59
0

The basic function in scala is defined like this:

def f(x:Int):Int = {
  x
}

Where you define the input type Int, return type Int and the function body.

What you have is (for the prediction method):

def f(x:Int) = (y:Int) => y + x

where f returns another function Int => Int. In your case the prediction function returns a function DenseVector[Double] => DenseVector[Double] that's why nothing gets executed.

jamborta
  • 5,130
  • 6
  • 35
  • 55
  • By doing so I get few issues with my code, perhaps that means my code is not correct as well. Thank you for helping me. – Jamil Apr 13 '17 at 15:19
0

I will simplify part, which you did wrong or misunderstood, but I believe it will be enough to understand what went wrong and you will know how to fix it.

You defined prediction function which looks like this:

def prediction(addTo: Int, multiplyBy: Int) = (number: Int) => { (number + addTo) * multiplyBy }

and then you used 2 required parameters, and now if, you would substitute this variables, it would look like this: prediction(2, 3)

number => (number + 2) * 3 <- and this is a function, isn't it?

To calculate the final value, you need to use 3rd argument which is inside an anonymous function.

So in our example prediction(2, 3)(1) would give us an actual output 9. Or you can define def addTwoMultiplyByThree = prediction(2, 3) and use it with multiple other values.

Rumid
  • 1,627
  • 2
  • 21
  • 39