forked from yongjun21/loess
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhelpers_spec.js
181 lines (164 loc) · 4.98 KB
/
helpers_spec.js
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
/* eslint-env mocha */
import { expect } from 'chai'
import { create, all } from 'mathjs'
import {
weightFunc, normalize, transpose,
euclideanDist, distMatrix, weightMatrix,
polynomialExpansion, weightedLeastSquare
} from '../src/helpers.js'
const config = { }
const math = create(all, config)
describe('function weightFunc', function () {
it('should return (1-(d/dmax)^n)^n if d < dmax', function () {
expect(weightFunc(0.5, 1, 3)).to.equal(0.669921875)
expect(weightFunc(0.5, 1, 2)).to.equal(0.5625)
})
it('should return 0 if d >= dmax', function () {
expect(weightFunc(1, 1, 3)).to.equal(0)
expect(weightFunc(1, 1, 2)).to.equal(0)
})
})
describe('function transpose', function () {
const caseOne = {
test: [
[1, 2, 3, 4, 5],
[6, 7, 8, 9, 10]
],
expect: [
[1, 6],
[2, 7],
[3, 8],
[4, 9],
[5, 10]
]
}
it('should return transposed matrix', function () {
expect(transpose(caseOne.test)).to.eql(caseOne.expect)
})
})
describe('function normalize', function () {
const caseOne = {
test: [109, 8, 7, 6, 5, 4, 3, 2, 1, -100],
expect: [44.499, 3.266, 2.858, 2.449, 2.041, 1.633, 1.225, 0.816, 0.408, -40.825]
}
it('should return array divided by 10% trimmed sample deviation', function () {
const normalizedArr = normalize(caseOne.test)(caseOne.test)
expect(math.round(normalizedArr, 3)).to.eql(caseOne.expect)
})
})
describe('function euclideanDist', function () {
it('should return Euclidean distance between two vectors', function () {
expect(math.round(euclideanDist([1, 2, 3], [4, 5, 6]), 3)).to.equal(5.196)
})
})
describe('function distMatrix', function () {
const caseOne = {
coordinates: [
[1, 1],
[4, 1],
[4, 5]
],
expect: [
[0, 3, 5],
[3, 0, 4],
[5, 4, 0]
]
}
it('should return matrix of Euclidean distance between pairs of points', function () {
expect(distMatrix(caseOne.coordinates, caseOne.coordinates)).to.eql(caseOne.expect)
})
})
describe('function weightMatrix', function () {
const distMat = [
[5, 4, 3, 2, 1]
]
const caseOne = {
inputWeights: [1, 1, 1, 1, 1],
bandwidth: 0.6,
expect: [
[0, 0, 0, 0.348, 0.893]
]
}
const caseTwo = {
inputWeights: [1, 1, 1, 1, 1],
bandwidth: 2,
expect: [
[0.67, 0.82, 0.921, 0.976, 0.997]
]
}
it('span <= 1', function () {
const actual = weightMatrix(distMat, caseOne.inputWeights, caseOne.bandwidth)
actual[0] = math.round(actual[0], 3)
expect(actual).to.eql(caseOne.expect)
})
it('span > 1', function () {
const actual = weightMatrix(distMat, caseTwo.inputWeights, caseTwo.bandwidth)
actual[0] = math.round(actual[0], 3)
expect(actual).to.eql(caseTwo.expect)
})
})
describe('function polynomialExpansion', function () {
it('(a + b + c)^0 >>> (1)', function () {
expect(polynomialExpansion([1, 2, 3], 0)).to.eql([1])
})
it('(a + b + c)^1 >>> (1 + a + b + c)', function () {
expect(polynomialExpansion([1, 2, 3], 1)).to.eql([1, 1, 2, 3])
})
it('(a + b + c)^2 >>> (1 + a + b + c + a2 + ab + ac + b2 + bc + c2)', function () {
expect(polynomialExpansion([1, 2, 3], 2)).to.eql([1, 1, 2, 3, 1, 2, 3, 4, 6, 9])
})
it('should operates on arrays also', function () {
expect(polynomialExpansion([[1, 2], [3, 4]], 2)).to.eql([[1, 1], [1, 2], [3, 4], [1, 4], [3, 8], [9, 16]])
})
})
describe('function weightedLeastSquare', function () {
const caseOne = {
x: [
[1, 1, 1, 1],
[1, 3, 5, 7]
],
y: [14, 17, 19, 20],
w: [1, 1, 1, 1],
expect: {
beta: math.matrix([13.5, 1]),
yhat: math.matrix([14.5, 16.5, 18.5, 20.5]),
residual: math.matrix([-0.5, 0.5, 0.5, -0.5])
}
}
const caseTwo = {
x: [
[1, 1, 1, 1],
[1, 3, 5, 7]
],
y: [14, 17, 19, 20],
w: [1, 3, 3, 1],
expect: {
beta: math.matrix([13.75, 1]),
yhat: math.matrix([14.75, 16.75, 18.75, 20.75]),
residual: math.matrix([-0.75, 0.25, 0.25, -0.75])
}
}
const caseThree = {
x: [
[1, 1, 1, 1],
[1, 1, 1, 1]
],
y: [14, 17, 19, 20],
w: [1, 3, 3, 1]
}
it('should return vector of fitted parameters (w/o weights)', function () {
const result = weightedLeastSquare(caseOne.x, caseOne.y, caseOne.w)
expect(math.deepEqual(result.beta, caseOne.expect.beta)) &&
expect(math.deepEqual(result.yhat, caseOne.expect.yhat)) &&
expect(math.deepEqual(result.residual, caseOne.expect.residual))
})
it('should return vector of fitted parameters (with weights)', function () {
const result = weightedLeastSquare(caseTwo.x, caseTwo.y, caseTwo.w)
expect(math.deepEqual(result.beta, caseTwo.expect.beta)) &&
expect(math.deepEqual(result.yhat, caseTwo.expect.yhat)) &&
expect(math.deepEqual(result.residual, caseTwo.expect.residual))
})
it('should return error object if x is non-invertible', function () {
expect(weightedLeastSquare(caseThree.x, caseThree.y, caseThree.w)).to.contain.keys('error')
})
})