• API
• FAQ
• Tools
• Archive
SHARE
TWEET Untitled a guest Sep 19th, 2019 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. ```
2. override def cumulativeProbability(x: Double): Double = {
3.     // An approximation to the true CDF since numerical integration diverges.
4.     // Source and notation: https://www.sciencedirect.com/science/article/pii/S209012321000069X.
5.     val standardizedX = (x - location) / scale
6.
7.     if (standardizedX < -3) 0.0
8.     else if (-3 <= standardizedX && standardizedX < -1) H1(standardizedX)
9.     else if (-1 <= standardizedX && standardizedX < 1) H2(standardizedX)
10.     else if (1 <= standardizedX && standardizedX < 3) H3(standardizedX)
11.     else 1.0
12.   }
13.
14. private def H1(x: Double): Double = {
15.     val first = (3 * SHAPESQ + 9) * gI(-3 / shape, x)
16.     val second = shape * (3 + 2 / 3 * SHAPESQ) * math.exp(-9 / (2 * SHAPESQ))
17.     val third = -shape * (9 + 2 / 3 * SHAPESQ + 3 * shape * x + SHAPESQ * x * x / 3) * math.exp(-x * x / 2)
18.     (first + second + third) / (8 * SQRT2PI)
19.   }
20.
21.   private def H2(x: Double): Double = {
22.     val first = (3 * SHAPESQ + 9) * gI(-3 / shape, -1 / shape)
23.     val second = 8 * gI(-1 / shape, x)
24.     val third = shape * (3 + 2 / 3 * SHAPESQ) * math.exp(-9 / (2 * SHAPESQ))
25.     val fourth = -shape * (1 + 2 * SHAPESQ) * math.exp(-1 / (2 * SHAPESQ))
26.     val fifth = 2 / 3 * shape * (-9 + 2 * SHAPESQ + SHAPESQ * x * x) * math.exp(-x * x / 2)
27.     (first + second + third + fourth + fifth) / (8 * SQRT2PI)
28.   }
29.
30.   private def H3(x: Double): Double = {
31.     val first = (3 * SHAPESQ + 9) * gI(-3 / shape, -1 / shape)
32.     val second = 8 * gI(-1 / shape, 1 / shape)
33.     val third = (7 - 3 * SHAPESQ) * gI(1 / shape, x)
34.     val fourth = shape * (3 + 2 / 3 * SHAPESQ) * math.exp(-9 / (2 * SHAPESQ))
35.     val fifth = -shape / 3 * (27 - 9 * shape * x + SHAPESQ * x * x + 2 * SHAPESQ) * math.exp(-x * x / 2)
36.     (first + second + third + fourth + fifth) / (8 * SQRT2PI)
37.   }
38.
39.   // Integrate exp(-x^2 / 2) from a to b.
40.   private def gI(a: Double, b: Double): Double = SQRT2PI / 2 * Erf.erf(a / SQRT2PI, b / SQRT2PI)
41. ```
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top