Last active
September 21, 2020 12:43
-
-
Save polyfractal/5c584f515c4dbd2d5a8f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var alter = require('../lib/alter.js'); | |
var _ = require('lodash'); | |
var Chainable = require('../lib/classes/chainable'); | |
module.exports = new Chainable('movingstd', { | |
args: [ | |
{ | |
name: 'inputSeries', | |
types: ['seriesList'] | |
}, | |
{ | |
name: 'sigma', | |
types: ['number'] | |
} | |
], | |
aliases: ['mvstd'], | |
help: 'Calculate the moving standard deviation over a given window.', | |
fn: function movingaverageFn(args) { | |
return alter(args, function (eachSeries, _window) { | |
var pairs = eachSeries.data; | |
eachSeries.data = _.map(pairs, function (point, i) { | |
if (i < _window) { return [point[0], null]; } | |
var average = _.chain(pairs.slice(i - _window, i)) | |
.map(function (point) { | |
return point[1]; | |
}).reduce(function (memo, num) { | |
return (memo + num); | |
}).value() / _window; | |
var variance = _.chain(pairs.slice(i - _window, i)) | |
.map(function (point) { | |
return point[1]; | |
}).reduce(function (memo, num) { | |
return memo + Math.pow(num - average, 2); | |
}).value() / (_window - 1); | |
return [point[0], Math.sqrt(variance)]; | |
}); | |
return eachSeries; | |
}); | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var reduce = require('../lib/reduce.js'); | |
var Chainable = require('../lib/classes/chainable'); | |
module.exports = new Chainable('showIfGreater', { | |
args: [ | |
{ | |
name: 'inputSeries', | |
types: ['seriesList'] | |
}, | |
{ | |
name: 'compare', | |
types: ['seriesList', 'number'] | |
} | |
], | |
help: 'Shows each point in inputSeries if it is larger than the equivalent point in the compared series', | |
fn: function divideFn(args) { | |
return reduce(args, function (a, b) { | |
return a > b ? a : null; | |
}); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment