-
Notifications
You must be signed in to change notification settings - Fork 1
/
statistics.lua
163 lines (108 loc) · 3.24 KB
/
statistics.lua
1
2
3
4
5
6
7
8
9
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
-- Approximations for erf(x) and erfInv(x) from
-- https://en.wikipedia.org/wiki/Error_function
local statistics = {}
local random, floor, ceil = math.random, math.floor, math.ceil
local exp, log, sqrt = math.exp, math.log, math.sqrt
local ROOT_2 = sqrt(2.0)
local A = 8 * (math.pi - 3.0) / (3.0 * math.pi * (4.0 - math.pi))
local B = 4.0 / math.pi
local C = 2.0 / (math.pi * A)
local D = 1.0 / A
local function erf(x)
if x == 0 then return 0 end
local xSq = x * x
local aXSq = A * xSq
local v = sqrt(1.0 - exp(-xSq * (B + aXSq) / (1.0 + aXSq)))
return (x > 0 and v) or -v
end
local function erf_inv(x)
if x == 0 then return 0 end
if x <= -1 or x >= 1 then return nil end
local y = log(1 - x * x)
local u = C + 0.5 * y
local v = sqrt(sqrt(u * u - D * y) - u)
return (x > 0 and v) or -v
end
local function std_normal(u)
return ROOT_2 * erf_inv(2.0 * u - 1.0)
end
local function generate_cdf(lambda_index, lambda)
local max = ceil(4 * lambda)
local pdf = exp(-lambda)
local cdf = pdf
local t = { [0] = pdf }
for i = 1, max - 1 do
pdf = pdf * lambda / i
cdf = cdf + pdf
t[i] = cdf
end
return t
end
local cdf_table = {}
for li = 1, 100 do
cdf_table[li] = generate_cdf(li, 0.25 * li)
end
local function poisson(lambda, max)
if max < 2 then
return (random() < exp(-lambda) and 0) or 1
elseif lambda >= 2 * max then
return max
end
local u = random()
local lambda_index = floor(4 * lambda + 0.5)
local cdfs = cdf_table[lambda_index]
if cdfs then
lambda = 0.25 * lambda_index
if u < cdfs[0] then return 0 end
if max > #cdfs then max = #cdfs + 1 else max = floor(max) end
if u >= cdfs[max - 1] then return max end
if max > 4 then -- Binary search
local s = 0
while s + 1 < max do
local m = floor(0.5 * (s + max))
if u < cdfs[m] then max = m else s = m end
end
else
for i = 1, max - 1 do
if u < cdfs[i] then return i end
end
end
return max
else
local x = lambda + sqrt(lambda) * std_normal(u)
return (x < 0.5 and 0) or (x >= max - 0.5 and max) or floor(x + 0.5)
end
end
-- Error and Inverse error functions
statistics.erf = erf
statistics.erf_inv = erf_inv
--- Standard normal distribution function (mean 0, standard deviation 1).
-- @return - Any real number (actually between -3.0 and 3.0).
statistics.std_normal = function()
local u = random()
if u < 0.001 then return -3.0 elseif u > 0.999 then return 3.0 end
return std_normal(u)
end
--- Standard normal distribution function (mean 0, standard deviation 1).
-- @param mu - The distribution mean.
-- @param sigma - The distribution standard deviation.
-- @return - Any real number (actually between -3*sigma and 3*sigma).
statistics.normal = function(mu, sigma)
local u = random()
if u < 0.001 then
return mu - 3.0 * sigma
elseif u > 0.999 then
return mu + 3.0 * sigma
end
return mu + sigma * std_normal(u)
end
--- Poisson distribution function.
-- @param lambda - The distribution mean and variance.
-- @param max - The distribution maximum.
-- @return - An integer between 0 and max (both inclusive).
statistics.poisson = function(lambda, max)
lambda, max = tonumber(lambda), tonumber(max)
if not lambda or not max or lambda <= 0 or max < 1 then return 0 end
return poisson(lambda, max)
end
return statistics