-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathload.js
More file actions
191 lines (177 loc) · 4.87 KB
/
Copy pathload.js
File metadata and controls
191 lines (177 loc) · 4.87 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
function uintify (arr) {
return new Uint8Array(Float64Array.from(arr).buffer)
}
function flat (arr) {
return [].concat.apply([], arr)
}
function transpose (arr) {
return arr[0].map((x, i) => arr.map(x => x[i]))
}
function prepare (arr) {
const farr = flat(arr)
for (let i = 0; i < farr.length - 2; i++) {
if (isNaN(farr[i + 1])) {
farr[i + 1] = farr[i]
}
}
return farr
}
const defaults = {
method: 0,
optimizer: 6,
s: 0,
verbose: false,
transpose: false,
auto: false,
approximation: 1,
search: 1
}
const params = {
p: 1,
d: 0,
q: 1,
P: 0,
D: 0,
Q: 0
}
const paramsAuto = {
p: 5,
d: 2,
q: 5,
P: 2,
D: 1,
Q: 2
}
module.exports = function (m) {
const _fit_sarimax = m.cwrap('fit_sarimax', 'number', ['array', 'array', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'boolean'])
const _predict_sarimax = m.cwrap('predict_sarimax', 'number', ['number', 'array', 'array', 'array', 'number'])
const _fit_autoarima = m.cwrap('fit_autoarima', 'number', ['array', 'array', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'number', 'boolean'])
const _predict_autoarima = m.cwrap('predict_autoarima', 'number', ['number', 'array', 'array', 'array', 'number'])
const _free_sarimax = m.cwrap('free_sarimax', null, ['number'])
const _free_autoarima = m.cwrap('free_autoarima', null, ['number'])
const _free_result = m.cwrap('free_result', null, ['number'])
function getResults (addr, l) {
const res = [[], []]
for (let i = 0; i < l * 2; i++) {
res[i < l ? 0 : 1].push(m.HEAPF64[addr / Float64Array.BYTES_PER_ELEMENT + i])
}
return res
}
function ARIMA () {
// Preserve the old functional API: ARIMA(ts, len, opts)
if (!(this instanceof ARIMA)) {
console.warn('Calling ARIMA as a function will be deprecated in the future')
var tmp = new ARIMA(arguments[2])
tmp.train(arguments[0])
var result = tmp.predict(arguments[1])
tmp.destroy()
return result
}
// A new, class API has opts as the only argument here: new ARIMA (opts)
const opts = arguments[0] || {}
const o = Object.assign({}, defaults, opts.auto ? paramsAuto : params, opts)
if (Math.min(o.method, o.optimizer, o.p, o.d, o.q, o.P, o.D, o.Q, o.s) < 0) {
throw new Error('Model parameter can\'t be negative')
}
if ((o.P + o.D + o.Q) === 0) {
o.s = 0
} else if (o.s === 0) {
o.P = o.D = o.Q = 0
}
this.options = o
}
ARIMA.prototype.train = function (ts, exog = []) {
const o = this.options
if (o.transpose && Array.isArray(exog[0])) {
exog = transpose(exog)
}
// Guard against short series that would crash WASM (Issue #4)
var minLength
if (o.auto) {
minLength = Math.max(2 * o.s + o.d, 20)
} else {
minLength = o.d + o.s * o.D + Math.max(o.p + o.q, 2 * o.s, 10)
}
if (ts.length < minLength) {
throw new Error(
'Series too short (' + ts.length + ' values). ' +
'Minimum length for these parameters is ' + minLength
)
}
// Free previous model if re-training (Issue #11)
if (this.model) {
if (o.auto) {
_free_autoarima(this.model)
} else {
_free_sarimax(this.model)
}
this.model = null
}
this.ts = uintify(prepare(ts))
this.exog = uintify(prepare(exog))
this.lin = ts.length
this.nexog = exog.length > 0 ? (Array.isArray(exog[0]) ? exog.length : 1) : 0
this.model = o.auto
? _fit_autoarima(
this.ts, this.exog,
o.p, o.d, o.q,
o.P, o.D, o.Q, o.s,
this.nexog,
this.lin,
o.method,
o.optimizer,
o.approximation,
o.search,
o.verbose
)
: _fit_sarimax(
this.ts, this.exog,
o.p, o.d, o.q,
o.P, o.D, o.Q, o.s,
this.nexog,
this.lin,
o.method,
o.optimizer,
o.verbose
)
return this
}
ARIMA.prototype.fit = function (...a) {
return this.train(...a)
}
ARIMA.prototype.predict = function (l, exog = []) {
const o = this.options
if (o.transpose && Array.isArray(exog[0])) {
exog = transpose(exog)
}
const addr = o.auto
? _predict_autoarima(
this.model,
this.ts,
this.exog, // old
uintify(prepare(exog)), // new
l
)
: _predict_sarimax(
this.model,
this.ts,
this.exog, // old
uintify(prepare(exog)), // new
l
)
var results = getResults(addr, l)
_free_result(addr)
return results
}
ARIMA.prototype.destroy = function () {
if (this.model) {
if (this.options.auto) {
_free_autoarima(this.model)
} else {
_free_sarimax(this.model)
}
this.model = null
}
}
return ARIMA
}