This repository was archived by the owner on Oct 2, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmethod-2.js
More file actions
67 lines (55 loc) · 2.04 KB
/
Copy pathmethod-2.js
File metadata and controls
67 lines (55 loc) · 2.04 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
var Stopwatch = require('statman-stopwatch');
var stopwatch = new Stopwatch();
stopwatch.start();
const dataForge = require('data-forge');
const moment = require('moment');
stopwatch.stop();
console.log("Time to require: " + stopwatch.read());
stopwatch.start();
const data = [];
for (let i = 0; i < 100; i += 1) {
data.push({ date: moment().add(i, 'days').toDate(), value: i });
}
const df = new dataForge.DataFrame(data).setIndex('date');
const gapExists = (pairA, pairB) => {
// Return true if there is a gap longer than a day.
// Log when we enter this function
// console.log('gapExists [pairA, pairB]', [pairA, pairB]);
const startDate = pairA[1].date;
const endDate = pairB[1].date;
const gapSize = moment(endDate).startOf('day').diff(moment(startDate).startOf('day'), 'days');
return gapSize > 1;
};
const gapFiller = (pairA, pairB) => {
const startDate = pairA[1].date;
const endDate = pairB[1].date;
const gapSize = moment(endDate).startOf('day').diff(moment(startDate).startOf('day'), 'days');
const numEntries = gapSize - 1;
const newEntries = [];
for (let entryIndex = 0; entryIndex < numEntries; entryIndex += 1) {
const newValue = { date: pairA[1].date, value: pairA[1].value };
newValue.date = moment(pairA[1].date).add(entryIndex + 1, 'days').toDate();
newEntries.push([
moment(pairA[0]).add(entryIndex + 1, 'days').toDate(), // New index
newValue, // New value
]);
}
return newEntries;
};
const dfWithoutGaps = df.fillGaps(gapExists, gapFiller);
// START OF KEY PART
const mySeries = new dataForge.Series(dfWithoutGaps.getSeries('value').toArray());
// END OF KEY PART
stopwatch.stop();
console.log("Time to create DataFrame and getSeries: " + stopwatch.read());
stopwatch.start();
const smaPeriod = 3;
const smaSeries = mySeries
.rollingWindow(smaPeriod)
.select(window => val = window.sum() / smaPeriod);
stopwatch.stop();
console.log("Time for rolling window: " + stopwatch.read());
stopwatch.start();
smaSeries.toPairs();
stopwatch.stop();
console.log("Time for toPairs: " + stopwatch.read());