Module: Eps::Statistics

Defined in:
lib/eps/statistics.rb

Class Method Summary collapse

Class Method Details

.normal_cdf(x, mean, std_dev) ⇒ Object



3
4
5
# File 'lib/eps/statistics.rb', line 3

def self.normal_cdf(x, mean, std_dev)
  0.5 * (1.0 + Math.erf((x - mean) / (std_dev * Math.sqrt(2))))
end

.students_t_cdf(x, n) ⇒ Object

Hill, G. W. (1970). Algorithm 395: Student’s t-distribution. Communications of the ACM, 13(10), 617-619.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/eps/statistics.rb', line 10

def self.students_t_cdf(x, n)
  start, sign = x < 0 ? [0, 1] : [1, -1]

  z = 1.0
  t = x * x
  y = t / n.to_f
  b = 1.0 + y

  if n > n.floor || (n >= 20.0 && t < n) || n > 200.0
    # asymptotic series for large or noninteger n
    if y > 10e-6
      y = Math.log(b)
    end
    a = n - 0.5
    b = 48.0 * a * a
    y *= a
    y = (((((-0.4 * y - 3.3) * y - 24.0) * y - 85.5) / (0.8 * y * y + 100.0 + b) + y + 3.0) / b + 1.0) * Math.sqrt(y)
    return start + sign * normal_cdf(-y, 0.0, 1.0)
  end

  if n < 20 && t < 4.0
    # nested summation of cosine series
    y = Math.sqrt(y)
    a = y
    if n == 1
      a = 0.0
    end

    # loop
    if n > 1
      n -= 2
      while n > 1
        a = (n - 1) / (b * n) * a + y
        n -= 2
      end
    end
    a = n == 0 ? a / Math.sqrt(b) : (Math.atan(y) + a / b) * (2.0 / Math::PI)
    return start + sign * (z - a) / 2.0
  end

  # tail series expanation for large t-values
  a = Math.sqrt(b)
  y = a * n
  j = 0
  while a != z
    j += 2
    z = a
    y = y * (j - 1) / (b * j)
    a += y / (n + j)
  end
  z = 0.0
  y = 0.0
  a = -a

  # loop (without n + 2 and n - 2)
  while n > 1
    a = (n - 1) / (b * n) * a + y
    n -= 2
  end
  a = n == 0 ? a / Math.sqrt(b) : (Math.atan(y) + a / b) * (2.0 / Math::PI)
  start + sign * (z - a) / 2.0
end