Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 2.67 KB | None | 0 0
  1. def resultToProgress(name: String, result: Array[Row], approxCols: Seq[Int]): Double = {
  2.     var retVal = Double.PositiveInfinity
  3.     val N: Int = 3 // smoothing factor
  4.  
  5.  
  6.     if(approxCols.length == 1) {
  7.  
  8.       if (!lossHistory.contains(name)) {
  9.         lossHistory.put(name, ArrayBuffer[Seq[Double]]())
  10.       }
  11.       val poolLossHist: ArrayBuffer[Seq[Double]] =
  12.         lossHistory.getOrElse(name, ArrayBuffer[Seq[Double]]())
  13.  
  14.       // if we have only one approx col
  15.       if (result.length == 1) {
  16.         // if we only have one row in the results
  17.         val estAndBounds = result(0).toSeq(approxCols(0)).asInstanceOf[GenericRowWithSchema]
  18.         val estimation = estAndBounds(0).asInstanceOf[Double]
  19.         poolLossHist.append(Seq(estimation))
  20.         if(poolLossHist.size > 1) {
  21.           val poolLossDiffHist = (1 until poolLossHist.size).map { i =>
  22.             Math.abs(poolLossHist(i)(0) - poolLossHist(i - 1)(0))
  23.           }
  24.           val max = poolLossDiffHist.max
  25.           val poolLossDiffNormalized = poolLossDiffHist.map( x => x / max)
  26.  
  27.           val lastN = poolLossDiffNormalized.slice(poolLossDiffNormalized.size -
  28.             Math.min(N, poolLossDiffNormalized.size), poolLossDiffNormalized.size)
  29.           val avg = lastN.sum / lastN.size
  30.           retVal = avg
  31.         }
  32.       } else if (!result.isEmpty) {
  33.         // if not, average all of the groups
  34.         val estimations = result.map { row =>
  35.           row.toSeq(approxCols(0)).asInstanceOf[GenericRowWithSchema](0).asInstanceOf[Double]
  36.         }
  37.         // estimations is a sequence of estimations for each group
  38.         poolLossHist.append(estimations)
  39.         if(poolLossHist.size > 1) {
  40.           // poolLossDiffHist is a sequence of differences for each group
  41.           val poolLossDiffHist = (1 until poolLossHist.size).map { i =>
  42.             poolLossHist(i).zip(poolLossHist(i-1)).map { case (x: Double, y: Double) =>
  43.                 Math.abs(x-y)
  44.             }
  45.           }
  46.           // maxs is the max diff for each group
  47.           val maxs = poolLossDiffHist.map( x => x.max )
  48.           // poolLossDiffNormalized is the normalized diff for each group
  49.           val poolLossDiffNormalized = poolLossDiffHist.zip(maxs).map{ case(group, max) =>
  50.             group.map( x => x / max )
  51.           }
  52.           // lastN is the last N historical points, each of which has the normalized diff
  53.           val lastN = poolLossDiffNormalized.slice(poolLossDiffNormalized.size -
  54.             Math.min(N, poolLossDiffNormalized.size), poolLossDiffNormalized.size)
  55.           val avgs = lastN.map { hist =>
  56.             hist.sum / hist.size
  57.           }
  58.           retVal = avgs.sum / avgs.size
  59.         }
  60.       }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement