UNPKG

everyutil

Version:

A comprehensive library of lightweight, reusable utility functions for JavaScript and TypeScript, designed to streamline common programming tasks such as string manipulation, array processing, date handling, and more.

22 lines (21 loc) 850 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.rollingStdDev = void 0; /** * Calculates the standard deviation in a moving window over the input array. * @author @dailker * @param {number[]} array - The input array of numbers. * @param {number} window - The window size for the rolling calculation. * @returns {number[]} An array of rolling standard deviations. */ function rollingStdDev(array, window) { const result = []; for (let i = 0; i <= array.length - window; i++) { const win = array.slice(i, i + window); const mean = win.reduce((a, b) => a + b, 0) / win.length; const std = Math.sqrt(win.reduce((a, b) => a + Math.pow(b - mean, 2), 0) / win.length); result.push(std); } return result; } exports.rollingStdDev = rollingStdDev;