-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNnw.lua
334 lines (292 loc) · 11.2 KB
/
Nnw.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
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
-- Nnw.lua
-- common functions for the Nearest Neighbor package
require 'affirm'
require 'makeVerbose'
require 'verify'
-- API overview
if false then
-- simple average
ok, estimate = Nnw.estimateAvg(xs, ys, nearestIndices, visible, weights, k)
-- kernel-weighted average
ok, estimate = Nnw.estimateKwavg(xs, ys, nearestIndices, visible, weights, k)
-- local linear regression
ok,estimate = Nnw.estimateLlr(xs, ys, nearestIndices, visible, weights, k)
-- euclidean distance
distances = Nnw.euclideanDistances(xs, query)
-- nearest neighbor distances and indices
sortedDistances, sortedIndices = Nnw.nearest(xs, query)
-- weights from the Epanenchnikov kernel
-- where lambda is the distance to the k-th nearest neighbor
weights = Nnw.weights(sortedDistances, lambda)
end
Nnw = {}
function Nnw.estimateAvg(xs, ys, nearestIndices, visible, k)
-- return true, average of k nearest visible neighbors
-- ignore the weights
local v, isVerbose = makeVerbose(false, 'Nnw.estimateAvg')
verify(v, isVerbose,
{{xs, 'xs', 'isAny'},
{ys, 'ys', 'isTensor1D'},
{nearestIndices, 'nearestIndices', 'isTensor1D'},
{visible, 'visible', 'isTensor1D'},
{k, 'k', 'isIntegerPositive'}})
local sum = 0
local found = 0
for nearestIndex = 1, nearestIndices:size(1) do
local obsIndex = nearestIndices[nearestIndex]
if visible[obsIndex] == 1 then
found = found + 1
sum = sum + ys[obsIndex]
v('obsIndex, y', obsIndex, ys[obsIndex])
if found == k then
break
end
end
end
if found < k then
return false, 'not able to find k neighbors'
else
local result = sum / k
v('result', result)
return true, result
end
end -- Nnw.estimateAvg
function Nnw.estimateKwavg(k, sortedNeighborIndices, visible, weights, allYs)
-- ARGS
-- k : integer > 0, number of neighbors to use
-- sortedNeighborIndices : 1D Tensor
-- use first k neighbors that are also visible
-- visible : 1D Tensor
-- visible[obsIndex] == 1 ==> use this observation
-- as a neighbor
-- weights : 1D Tensor
-- allYs : 1D Tensor
-- RETURNS
-- ok : true or false
-- estimate : number or string
local v, isVerbose = makeVerbose(false, 'Nnw.estimateKwavg')
verify(v, isVerbose,
{{k, 'k', 'isIntegerPositive'},
{sortedNeighborIndices, 'sortedNeighborIndices', 'isTensor1D'},
{visible, 'visible', 'isTensor1D'},
{weights, 'weights', 'isTensor1D'},
{allYs, 'allYs', 'isTensor1D'}})
local sumWeightedYs = 0
local sumWeights = 0
local found = 0
for i = 1, visible:size(1) do
local obsIndex = sortedNeighborIndices[i]
if visible[obsIndex] == 1 then
local weight = weights[i]
local y = allYs[obsIndex]
v('i,obsIndex,weight,y', i, obsIndex, weight, y)
sumWeights = sumWeights + weight
sumWeightedYs = sumWeightedYs + weight * y
found = found + 1
if found == k then
break
end
end
end
v('sumWeights', sumWeights)
v('sumWeightedYs', sumWeightedYs)
if sumWeights == 0 then
return false, 'all weights were zero'
elseif found < k then
return false, string.format('only %d obs in neighborhood; k = %d',
found, k)
else
local estimate = sumWeightedYs / sumWeights
v('estimate', estimate)
return true, estimate
end
end -- Nnw.estimateKwavg
function Nnw.estimateLlr(k, regularizer,
sortedNeighborIndices, visible, weights,
query, allXs, allYs)
-- ARGS
-- k : integer > 0, number of neighbors to use
-- regularizer : number >= 0, added to each weight
-- sortedNeighborIndices : 1D Tensor
-- use first k neighbors that are also visible
-- visible : 1D Tensor
-- visible[obsIndex] == 1 ==> use this observation
-- as a neighbor
-- weights : 1D Tensor
-- allXs : 2D Tensor
-- allYs : 1D Tensor
-- RETURNS
-- ok : true or false
-- estimate : number or string
local debug = 0
--local debug = 1 -- determine why inverse fails
local v, isVerbose = makeVerbose(false, 'Nnw.estimateLlr')
verify(v, isVerbose,
{{k, 'k', 'isIntegerPositive'},
{regularizer, 'regularizer', 'isNumberNonNegative'},
{sortedNeighborIndices, 'sortedNeighborIndices', 'isTensor1D'},
{visible, 'visible', 'isTensor1D'},
{weights, 'weights', 'isTensor1D'},
{query, 'query', 'isTensor1D'},
{allXs, 'allXs', 'isTensor2D'},
{allYs, 'allYs', 'isTensor1D'}})
assert(allYs:size(1) == allXs:size(1),
'allXs and allYs must have same number of observations')
local nDims = allXs:size(2)
assert(k > nDims,
string.format('undetermined since k(=%d) <= nDims (=%d)',
k, nDims))
-- FIX ME: create using k nearest visible neighbors
-- create regression matrix B
-- by prepending a 1 in the first position
local B = torch.Tensor(k, nDims + 1)
local selectedYs = torch.Tensor(k)
local wVector = torch.Tensor(k)
local found = 0
for i = 1, allXs:size(1) do
local obsIndex = sortedNeighborIndices[i]
if visible[obsIndex] == 1 then
found = found + 1
v('i,obsIndex,found', i, obsIndex, found)
B[found][1] = 1
for d = 1, nDims do
B[found][d+1] = allXs[obsIndex][d]
end
selectedYs[found] = allYs[obsIndex]
wVector[found] = weights[i] + regularizer
if found == k then
break
end
end
end
v('nObs, nDims', nObs, nDims)
v('B (regression matrix)', B)
v('selectedYs', selectedYs)
v('wVector', wVector)
-- also prepend a 1 in the first position of the query
-- to make the final multiplication work, the prepended query needs to be 2D
local extendedQuery = torch.Tensor(1, nDims + 1)
extendedQuery[1][1] = 1
for d = 1, nDims do
extendedQuery[1][d + 1] = query[d]
end
v('extendedQuery', extendedQuery)
local BT = B:t() -- transpose B
local W = DiagonalMatrix(wVector)
v('BT', BT)
v('W', W)
-- BTWB = B^T W B
local BTWB = BT * (W:mul(B))
v('BTWB', BTWB)
-- invert BTWB, catching error
local ok, BTWBInv = pcall(torch.inverse, BTWB)
if not ok then
-- if the error message is "getrf: U(i,i) is 0, U is singular"
-- then LU factorization succeeded but U is exactly 0, so that
-- division by zero will occur if U is used to solve a
-- system of equations
-- ref: http://dlib.net/dlib/matrix/lapack/getrf.h.html
if debug == 1 then
print('Llr:estimate: error in call to torch.inverse')
print('error message = ' .. BTWBInv)
error(BTWBInv)
end
return false, BTWBInv -- return the error message
end
local betas = BTWBInv * BT * W:mul(selectedYs)
local estimate1 = extendedQuery * BTWBInv * BT * W:mul(selectedYs)
v('estimate1', estimate1)
local estimate = extendedQuery * betas
v('extendedQuery', extendedQuery)
v('beta', BTWBInv * BT * W:mul(selectedYs))
v('estimate', estimate[1])
affirm.isTensor1D(estimate, 'estimate')
assert(1 == estimate:size(1))
return true, estimate[1]
end -- Nnw.estimateLlr
function Nnw.euclideanDistance(x, query)
-- return scalar Euclidean distance
local debug = 0
--debug = 1 -- zero value for lambda
local v, isVerbose = makeVerbose(false, 'Nnw:euclideanDistance')
verify(v, isVerbose,
{{x, 'x', 'isTensor1D'},
{query, 'query', 'isTensor1D'}})
assert(x:size(1) == query:size(1))
local ds = x - query
if debug == 1 then
for i = 1, x:size(1) do
print(string.format('x[%d] %f query[%d] %f ds[%d] %f',
i, x[i], i, query[i], i, ds[i]))
end
end
v('ds', ds)
local distance = math.sqrt(torch.sum(torch.cmul(ds, ds)))
v('distance', distance)
return distance
end -- euclideanDistance
function Nnw.euclideanDistances(xs, query)
-- return 1D tensor such that result[i] = EuclideanDistance(xs[i], query)
-- We require use of Euclidean distance so that this code will work.
-- It computes all the distances from the query point at once
-- using Clement Farabet's idea to speed up the computation.
local v, isVerbose = makeVerbose(false, 'Nnw:euclideanDistances')
verify(v,
isVerbose,
{{xs, 'xs', 'isTensor2D'},
{query, 'query', 'isTensor1D'}})
assert(xs:size(2) == query:size(1),
'number of columns in xs must equal size of query')
-- create a 2D Tensor where each row is the query
-- This construction is space efficient relative to replicating query
-- queries[i] == query for all i in range
-- Thanks Clement Farabet!
local queries =
torch.Tensor(query:clone():storage(),-- clone in case query is a row of xs
1, -- offset
xs:size(1), 0, -- row index offset and stride
xs:size(2), 1) -- col index offset and stride
local distances = torch.add(queries, -1 , xs) -- queries - xs
distances:cmul(distances) -- (queries - xs)^2
distances = torch.sum(distances, 2):squeeze() -- \sum (queries - xs)^2
distances = distances:sqrt() -- Euclidean distances
v('distances', distances)
return distances
end -- Nnw.euclideanDistances
function Nnw.nearest(xs, query)
-- find nearest observations to a query
-- RETURN
-- sortedDistances : 1D Tensor
-- distances of each xs from query
-- sortedIndices : 1D Tensor
-- indices that sort the distances
local v, isVerbose = makeVerbose(false, 'Nnw.nearest')
verify(v, isVerbose,
{{xs, 'xs', 'isTensor2D'},
{query, 'query', 'isTensor1D'}})
local distances = Nnw.euclideanDistances(xs, query)
v('distances', distances)
local sortedDistances, sortedIndices = torch.sort(distances)
v('sortedDistances', sortedDistances)
v('sortedIndices', sortedIndices)
return sortedDistances, sortedIndices
end -- Nnw.nearest
function Nnw.weights(sortedDistances, lambda)
-- return values of Epanenchnov kernel using euclidean distance
local v, isVerbose = makeVerbose(false, 'KernelSmoother.weights')
verify(v, isVerbose,
{{sortedDistances, 'sortedDistances', 'isTensor1D'},
{lambda, 'lambda', 'isNumberPositive'}})
local nObs = sortedDistances:size(1)
local t = sortedDistances / lambda
v('t', t)
local one = torch.Tensor(nObs):fill(1)
local indicator = torch.le(torch.abs(t), one):type('torch.DoubleTensor')
v('indicator', indicator)
local dt = torch.mul(one - torch.cmul(t, t), 0.75)
v('dt', dt)
-- in torch, inf * 0 --> nan (not zero)
local weights = torch.cmul(dt, indicator)
v('weights', weights)
return weights
end -- Nnw.weights