-
Notifications
You must be signed in to change notification settings - Fork 0
/
NnwEstimatorKwavg-test.lua
63 lines (50 loc) · 1.33 KB
/
NnwEstimatorKwavg-test.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
-- NnwEstimatorKwavg-test.lua
-- unit tests
require 'all'
tests = {}
tester = Tester()
function makeExample()
local nsamples = 10
local ndims = 3
local xs = torch.Tensor(nsamples, ndims)
local ys = torch.Tensor(nsamples)
for i = 1, nsamples do
for d = 1, ndims do
xs[i][d] = i
ys[i] = i * 10
end
end
return nsamples, ndims, xs, ys
end -- makeExample
function tests.one()
--if true then return end
local v = makeVerbose(false, 'tests.one')
local nSamples, nDims, xs, ys = makeExample()
local query = torch.Tensor(nDims):fill(3)
v('xs', xs)
v('ys', ys)
v('query', query)
local function test(k, expected)
v('k', k)
v('expected', expected)
local knn = NnwEstimatorKwavg(xs, ys, 'epanechnikov quadratic')
local ok, estimate = knn:estimate(query, k)
local tol = 1e-3
tester:assert(ok)
v('estimate', estimate)
tester:asserteqWithin(expected, estimate, tol)
end
-- see lab book for 2012-10-18 for calculations
--test(1, 30, false)
test(3, 30)
test(5, 30)
test(6, 30)
test(7, 32.7275)
--test(8, 45)
--test(9, 50)
--test(10, 55)
end -- one
-- run unit tests
print('*********************************************************************')
tester:add(tests)
tester:run(true) -- true ==> verbose