home | news | discuss | issues | license LURE: num |
|
---|---|
Incrementally watch a stream of numberstim@menzies.us |
|
Use this code to incrementally monitor the mean and standard deviation of a stream of numbers. Also, use this code for fast statistical tests (to check if two distributions are difference). As shown below in the Simple usage:
A shortcut:
Another shortcut:
Of course, such parametric tests assume Gaussian distributions (bell-shaped, symmetrical, single peak).
For stats tests for non-Gaussian's, see |
local the=require "config"
|
CreationCreate a new watcher. |
local function create()
return {n=0,mu=0,m2=0,sd=0,hi=-1e32,lo=1e32,w=1} end
|
UpdateUpdate a watcher |
local function update(i,x)
if x ~= the.ignore then
i.n = i.n + 1
if x < i.lo then i.lo = x end
if x > i.hi then i.hi = x end
local delta = x - i.mu
i.mu = i.mu + delta / i.n
i.m2 = i.m2 + delta * (x - i.mu)
if i.n > 1 then
i.sd = (i.m2 / (i.n - 1))^0.5 end end
return i end
local function about(i)
return {{pos=i.pos},{txt=i.txt},{n=i.n},{mu=i.mu},
{sd=i.sd},{lo=i.lo},{hi=i.hi}} end
|
Handy short cut |
local function watch()
local i = create()
return i, function (x) return update(i,x) end end
|
UpdatesUpdate a watcher
|
local function updates(t,f,all)
all = all or create()
f = f or function (z) return z end
for _,one in pairs(t) do
update(all, f(one)) end
return all end
|
Normalization |
local function norm(i,x)
if x==the.ignore then return x end
return (x - i.lo) / (i.hi - i.lo + 1e-32) end
|
Like |
local function like(i,x,_)
local var = i.sd^2
local denom = (2*math.pi*var)^.5
local num = 2.7182818^(-1*(x-i.mu)^2/(2*var))
return num/(denom + 10^-64) end
|
DistanceUsing a watcher Returns two numbers |
local function distance(i,j,k)
if j == the.ignore and k == the.ignore then
return 0,0
elseif j == the.ignore then
k = norm(i,k)
j = k < 0.5 and 1 or 0
elseif k == the.ignore then
j = norm(i,j)
k = j < 0.5 and 1 or 0
else
j,k = norm(i,j), norm(i,k)
end
return math.abs(j-k)^2,1 end
|
Map numbers to a range.
|
local function discretize(i,x)
if x==the.ignore then return x end
if not i.bins then return x end
for _,b in pairs(i.bins) do
r = b.label
if x<=b.most then break end end
return r end
|
Parametric significance tests
|
local function ttest1(df,first,last,crit)
if df <= first then
return crit[first]
elseif df >= last then
return crit[last]
else
local n1 = first
while n1 < last do
local n2=n1*2
if df >= n1 and df <= n2 then
local old,new = crit[n1],crit[n2]
return old + (new-old) * (df-n1)/(n2-n1) end
n1=n1*2 end end end
local function ttest(i,j) -- Debugged using https://goo.gl/CRl1Bz
local t = (i.mu - j.mu) /
math.sqrt(math.max(10^-64, i.sd^2/i.n + j.sd^2/j.n ))
local a = i.sd^2/i.n
local b = j.sd^2/j.n
local df = (a + b)^2 / (10^-64 + a^2/(i.n-1) + b^2/(j.n - 1))
local c = ttest1(math.floor( df + 0.5 ),
the.num.first,
the.num.last,
the.num.criticals[the.num.conf])
return math.abs(t) > c end
|
Parametric Effect Size TestFor an explanation of this code, see equations 2,3,4 and Table 9 of V.B. Kampenes et al., A systematic review of effect size in software engineering experiments, Inform. Softw. Technol. (2007), doi:10.1016/j.infsof.2007.02.015. For a discussion on why effect size is so important, see E. Kocaguneli, T. Zimmermann, C. Bird, N. Nagappan and T. Menzies, Distributed development considered harmful?, 2013 35th International Conference on Software Engineering (ICSE), San Francisco, CA, 2013, pp. 882-890. |
local function hedges(i,j) -- from https://goo.gl/w62iIL
local nom = (i.n - 1)*i.sd^2 + (j.n - 1)*j.sd^2
local denom = (i.n - 1) + (j.n - 1)
local sp = math.sqrt( nom / denom )
local g = math.abs(i.mu - j.mu) / sp
local c = 1 - 3.0 / (4*(i.n + j.n - 2) - 1) -- handle small samples
return g * c > the.num.small end -- Table9, https://goo.gl/jNNCHH says small,medium=0.38,1.0
|
Statistical differenceTwo populations are statistically similar if they differ by less than a trivially small amount; and if they are statistically significantly different. |
local function same(i,j)
return not (hedges(i,j) and ttest(i,j)) end
|
External interface |
return {create=create, watch=watch,
update=update, updates=updates,
norm=norm, about=about,
like=like,
distance=distance,
same=same, ttest=ttest, hedges=hedges}
|
LegalLURE, Copyright (c) 2017, Tim Menzies All rights reserved, BSD 3-Clause License Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|