Skip to content

Commit 2385e36

Browse files
committed
assign intervals
1 parent 20ec853 commit 2385e36

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

online/src/main/scala/ai/chronon/online/stats/AssignIntervals.scala

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,21 +22,21 @@ object AssignIntervals {
2222
val arr = Array.fill(bLen - 1)(0.0)
2323

2424
// discard before b(0)
25-
while(i < pLen && ptiles(i) < breaks(0)) {
25+
while (i < pLen && ptiles(i) < breaks(0)) {
2626
i += 1
2727
}
2828

29-
while(bIndex < bLen) {
29+
while (bIndex < bLen) {
3030
var result = 0.0
3131
val b = breaks(bIndex)
3232

33-
while(i < pLen && ptiles(i) < b) {
33+
while (i < pLen && ptiles(i) < b) {
3434

3535
// fraction of the equally spaced interval between p(i-1) to p(i) to assign to this break
3636
val fraction =
37-
if(i == 0) 0.0
38-
else if (ptiles(i) == ptiles(i-1)) 1.0
39-
else (ptiles(i) - prev) / (ptiles(i) - ptiles(i-1))
37+
if (i == 0) 0.0
38+
else if (ptiles(i) == ptiles(i - 1)) 1.0
39+
else (ptiles(i) - prev) / (ptiles(i) - ptiles(i - 1))
4040

4141
result += fraction
4242
prev = ptiles(i)
@@ -46,7 +46,7 @@ object AssignIntervals {
4646
// residual fraction to assign
4747
val fraction =
4848
if (i <= 0 || i >= pLen) 0.0
49-
else (b - prev) / (ptiles(i) - ptiles(i-1))
49+
else (b - prev) / (ptiles(i) - ptiles(i - 1))
5050

5151
result += fraction
5252
prev = b

online/src/main/scala/ai/chronon/online/stats/DistanceMetrics.scala

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
11
package ai.chronon.online.stats
2-
3-
import ai.chronon.api.ColorPrinter.ColorString
42
import ai.chronon.api.Window
53

64
import scala.math._
75

8-
96
object DistanceMetrics {
107

118
// TODO move this to unit test
@@ -22,19 +19,18 @@ object DistanceMetrics {
2219
val hd = hellingerDistance(A, B)
2320
println(f"The Hellinger Distance between distributions A and B is: $hd%.5f")
2421

25-
2622
// format: off
2723
// aligned vertically for easier reasoning
2824
val ptiles = Array( 1, 4, 6,6,6, 8, 9 )
2925
val breaks = Array(0, 1, 2, 3, 5, 6, 7, 8, 9, 10)
3026
// format: on
3127

3228
//val interval = 0.25
33-
val expected = Array(0.0, 1.0/3.0 , 1.0/3.0, (1.0)/(3.0) + (1.0)/(2.0), (1.0)/(2.0), 2.5, 0.5, 1, 0)
29+
val expected = Array(0.0, 1.0 / 3.0, 1.0 / 3.0, (1.0) / (3.0) + (1.0) / (2.0), (1.0) / (2.0), 2.5, 0.5, 1, 0)
3430

3531
val result = AssignIntervals.on(ptiles = ptiles.map(_.toDouble), breaks = breaks.map(_.toDouble))
3632

37-
expected.zip(result).foreach{case (e, r) => println(s"exp: $e res: $r")}
33+
expected.zip(result).foreach { case (e, r) => println(s"exp: $e res: $r") }
3834

3935
}
4036

@@ -43,15 +39,13 @@ object DistanceMetrics {
4339

4440
case class Comparison[T](previous: T, current: T, timeDelta: Window)
4541

46-
47-
def functionBuilder[T](binningFunc: Comparison[T] => Distributions, distanceFunc: Distributions => Double): Comparison[T] => Double = {
48-
c =>
49-
val dists = binningFunc(c)
50-
val distance = distanceFunc(dists)
51-
distance
42+
def functionBuilder[T](binningFunc: Comparison[T] => Distributions,
43+
distanceFunc: Distributions => Double): Comparison[T] => Double = { c =>
44+
val dists = binningFunc(c)
45+
val distance = distanceFunc(dists)
46+
distance
5247
}
5348

54-
5549
def hellingerDistance(p: Array[Double], q: Array[Double]): Double = {
5650
val (pProbs, qProbs) = computePDFs(p, q)
5751

@@ -110,14 +104,14 @@ object DistanceMetrics {
110104
val interval: Double = 1.toDouble / (n - 1.0)
111105

112106
def mass(i: Int, eh: Int): Mass = {
113-
def indexMass(i: Int): Double = interval / (percentiles(i) - percentiles(i-1))
107+
def indexMass(i: Int): Double = interval / (percentiles(i) - percentiles(i - 1))
114108
val isPointMass = eh > 1
115109
val m = (i, eh) match {
116-
case (0, _) => 0.0 // before range
117-
case (x, 0) if x>=n => 0.0 // after range
118-
case (_, e) if e > 1 => (e - 1) * interval // point mass
119-
case (x, 1) if x==n => indexMass(n-1) // exactly at end of range
120-
case (x, _) => indexMass(x) // somewhere in between
110+
case (0, _) => 0.0 // before range
111+
case (x, 0) if x >= n => 0.0 // after range
112+
case (_, e) if e > 1 => (e - 1) * interval // point mass
113+
case (x, 1) if x == n => indexMass(n - 1) // exactly at end of range
114+
case (x, _) => indexMass(x) // somewhere in between
121115
}
122116
Mass(m, isPointMass)
123117
}
@@ -137,8 +131,8 @@ object DistanceMetrics {
137131
require(p.length == q.length, s"Inputs are of different length ${p.length}, ${q.length}")
138132
var i = 0
139133
var result = 0.0
140-
while(i < p.length) {
141-
val inc = if (p(i)> 0 && q(i) > 0) p(i) * math.log(p(i) / q(i)) else 0
134+
while (i < p.length) {
135+
val inc = if (p(i) > 0 && q(i) > 0) p(i) * math.log(p(i) / q(i)) else 0
142136
result += inc
143137
i += 1
144138
}

0 commit comments

Comments
 (0)