- Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathstat.lua
More file actions
Latest commit
359 lines (329 loc) · 8.56 KB
/
Copy pathstat.lua
File metadata and controls
359 lines (329 loc) · 8.56 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
--------------------------------------------------------------------------------
-- Statistical functions module.
--
-- Copyright (C) 2011-2016 Stefano Peluchetti. All rights reserved.
--------------------------------------------------------------------------------
-- Variances and covariances are computed according to the unbiased version of
-- the algorithm.
-- Welford-type algorithms are used for superior numerical stability, see:
-- http://en.wikipedia.org/wiki/Algorithms_for_calculating_variance
-- http://www.johndcook.com/standard_deviation.html
-- TODO: BIC, AIC.
-- TODO: Function join(...) to join results for parallel computing.
-- TODO: Speed-up via OpenBLAS.
localffi=require"ffi"
localalg=require"sci.alg"
localsqrt, abs, max, log=math.sqrt, math.abs, math.max, math.log
localvec, mat, arrayct=alg.vec, alg.mat, alg.arrayct
localtypeof, metatype=ffi.typeof, ffi.metatype
localfunctionclear(x)
fori=1,#xdo
x[i] =0
end
end
localfunctionmean(x)
if#x<1then
error("#x >=1 required: #x="..#x)
end
localmu=0
fori=1,#xdo
mu=mu+ (x[i] -mu)/i
end
returnmu
end
localfunctionvar(x)
if#x<2then
error("#x >= 2 required: #x"..#x)
end
localmu, s2=0, 0
fori=1,#xdo
localdelta=x[i] -mu
mu=mu+delta/i
s2=s2+delta*(x[i] -mu)
end
returns2/(#x-1)
end
localfunctioncov(x, y)
localmux, muy, s2c=0, 0, 0
ifnot#x==#ythen
error("#x ~= #y: #x="..#x..", #y="..#y)
end
if#x<2then
error("#x >= 2 required: #x="..#x)
end
fori=1,#xdo
localdeltax=x[i] -mux
localdeltay=y[i] -muy
localr=1/i
mux=mux+deltax*r
muy=muy+deltay*r
s2c=s2c+deltax*(y[i] -muy)
end
returns2c/(#x-1)
end
localfunctioncor(x, y)
returncov(x, y)/sqrt(var(x)*var(y))
end
localfunctionchk_dim(self, x)
locald=self._d
ifd~=#xthen
error("argument with #="..#x.." passed to statistics of dimension="..d)
end
returnd
end
localfunctionchk_eq_square(X, Y)
localn, m=X:nrow(), X:ncol()
localny, my=Y:nrow(), Y:ncol()
ifnot (n==mandn==nyandn==my) then
error("arguments must be square matrices of equal size, passed: "..n.."x"..m
..", "..ny.."x"..my)
end
returnn, m
end
localfunctiondim(self)
returnself._d
end
localfunctionlen(self)
returnself._n
end
localfunctiontos_mean(self)
localm=vec(self._d)
self:mean(m)
return"mean:\n"..m:width()
end
localmeand_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
clear(self._mu)
end,
push=function(self, x)
locald=chk_dim(self, x)
self._n=self._n+1
fori=1,ddoself._mu[i] =self._mu[i] + (x[i] -self._mu[i])/self._nend
end,
mean=function(self, mean)
locald=chk_dim(self, mean)
ifself._n<1then
error("n >= 1 required: n="..self._n)
end
fori=1,ddomean[i] =self._mu[i] end
end,
__tostring=tos_mean,
}
meand_mt.__index=meand_mt
localmean0_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
self._mu=0
end,
push=function(self, x)
self._n=self._n+1
self._mu=self._mu+ (x-self._mu)/self._n
end,
mean=function(self)
returnself._mu
end,
__tostring=tos_mean,
}
mean0_mt.__index=mean0_mt
localmeand_ct=typeof("struct { int32_t _d, _n; $& _mu; }", arrayct)
localmean0_ct=typeof("struct { int32_t _d, _n; double _mu; }")
meand_ct=metatype(meand_ct, meand_mt)
mean0_ct=metatype(mean0_ct, mean0_mt)
localfunctiontos_var(self)
localv=mat(self._d, self._d)
self:var(v)
returntos_mean(self).."\nvar:\n"..v:width()
end
localvard_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
clear(self._mu)
clear(self._s2)
end,
push=function(self, x)
locald=chk_dim(self, x)
self._n=self._n+1
localr=1/self._n
fori=1,ddo
self._delta[i] =x[i] -self._mu[i]
self._mu[i] =self._mu[i] +self._delta[i]*r
self._s2[i] =self._s2[i] +self._delta[i]*(x[i] -self._mu[i])
end
end,
mean=meand_mt.mean,
var=function(self, var)
locald=chk_dim(self, var)
ifself._n<2then
error("n >= 2 required: n="..self._n)
end
fori=1,ddovar[i] =self._s2[i]/(self._n-1) end
end,
__tostring=tos_var,
}
vard_mt.__index=vard_mt
localvar0_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
self._mu=0
self._s2=0
end,
push=function(self, x)
self._n=self._n+1
localr=1/self._n
self._delta=x-self._mu
self._mu=self._mu+self._delta*r
self._s2=self._s2+self._delta*(x-self._mu)
end,
mean=mean0_mt.mean,
var=function(self)
ifself._n<2then
error("n >= 2 required: n="..self._n)
end
returnself._s2/(self._n-1)
end,
__tostring=tos_var,
}
var0_mt.__index=var0_mt
localvard_ct=typeof("struct { int32_t _d, _n; $& _mu; $& _delta; $& _s2; }",
arrayct, arrayct, arrayct)
localvar0_ct=typeof("struct { int32_t _d, _n; double _mu, _delta, _s2; }")
vard_ct=metatype(vard_ct, vard_mt)
var0_ct=metatype(var0_ct, var0_mt)
-- Y *can* alias X.
localfunctioncovtocor(X, Y)
localn, m=chk_eq_square(X, Y)
forr=1,ndo
forc=1,mdo
ifr~=cthen
Y[{r,c}] =X[{r,c}]/sqrt(X[{r,r}]*X[{c,c}])
end
end
end
fori=1,ndoY[{i,i}] =1end
end
localfunctiontos_cor(self)
localc, r=mat(self._d, self._d), mat(self._d, self._d)
self:cov(c)
self:cor(r)
returntos_mean(self).."\ncov:\n"..c:width().."\ncor:\n"..r:width()
end
localcovd_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
clear(self._mu)
clear(self._s2)
end,
push=function(self, x)
locald=chk_dim(self, x)
self._n=self._n+1
localr=1/self._n
fori=1,ddo
self._delta[i] =x[i] -self._mu[i]
self._mu[i] =self._mu[i] +self._delta[i]*r
end
fori=1,ddoforj=1,ddo
self._s2[{i,j}] =self._s2[{i,j}] +self._delta[i]*(x[j] -self._mu[j])
endend
end,
mean=meand_mt.mean,
var=function(self, var)
locald=chk_dim(self, var)
ifself._n<2then
error("n >= 2 required: n="..self._n)
end
fori=1,ddovar[i] =self._s2[{i,i}]/(self._n-1) end
end,
cov=function(self, cov)
localn, m=chk_eq_square(self._s2, cov)
ifself._n<2then
error("n >= 2 required: n="..self._n)
end
fori=1,ndoforj=1,mdo
cov[{i,j}] =self._s2[{i,j}]/(self._n-1)
endend
end,
cor=function(self, cor)
self:cov(cor)
covtocor(cor, cor)
end,
__tostring=tos_cor,
}
covd_mt.__index=covd_mt
localcovd_ct=typeof("struct { int32_t _d, _n; $& _mu; $& _delta; $& _s2; }",
arrayct, arrayct, arrayct)
covd_ct=metatype(covd_ct, covd_mt)
localsamples_mt= {
dim=dim,
len=len,
clear=function(self)
self._n=0
self._x= { }
end,
push=function(self, x)
locald=chk_dim(self, x)
self._n=self._n+1
self._x[n] =x:copy()
end,
samples=function(self, samples)
localn, m=samples:nrow(), samples:ncol()
ifn~=self._norm~=self._dthen
error("output matrix has wrong dimensions")
end
fori=1,ndoforj=1,mdo
samples[{i,j}] =self._x[{i,j}]
endend
end,
}
samples_mt.__index=samples_mt
localanchor=setmetatable({ }, { __mode="k" })
localfunctionolmean(dim)
ifdim==0then
returnmean0_ct(dim)
else
localmu=vec(dim)
localv=meand_ct(dim, 0, mu)
anchor[v] = { mu }
returnv
end
end
localfunctionolvar(dim)
ifdim==0then
returnvar0_ct(dim)
else
localmu, delta, s2=vec(dim), vec(dim), vec(dim)
localv=vard_ct(dim, 0, mu, delta, s2)
anchor[v] = { mu, delta, s2 }
returnv
end
end
localfunctionolcov(dim)
localmu, delta, s2=vec(dim), vec(dim), mat(dim, dim)
localv=covd_ct(dim, 0, mu, delta, s2)
anchor[v] = { mu, delta, s2 }
returnv
end
localfunctionolsamples(dim)
returnsetmetatable({ _d=dim, _n=0, _x= { } }, samples_mt)
end
return {
mean=mean,
var=var,
cov=cov,
cor=cor,
covtocor=covtocor,
olmean=olmean,
olvar=olvar,
olcov=olcov,
olsamples=olsamples,
}