mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-04-26 07:39:59 -07:00
git: reset
This commit is contained in:
22
app/src/scripts/utils/math/averages.ts
Normal file
22
app/src/scripts/utils/math/averages.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { computeSum } from "./sum";
|
||||
|
||||
export const computeAverage = (values: number[]) =>
|
||||
computeSum(values) / values.length;
|
||||
|
||||
export const computeMovingAverage = <
|
||||
T extends SingleValueData = SingleValueData,
|
||||
>(
|
||||
dataset: T[],
|
||||
interval: number,
|
||||
) => {
|
||||
if (!dataset.length) return [];
|
||||
|
||||
return dataset.map((data, index) => ({
|
||||
...data,
|
||||
value: computeAverage(
|
||||
dataset
|
||||
.slice(Math.max(index - interval + 1, 0), index + 1)
|
||||
.map((data) => data.value || 1),
|
||||
),
|
||||
}));
|
||||
};
|
||||
Reference in New Issue
Block a user