-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
88 additions
and
84 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
// Compute the mean incrementally, measure by measure. | ||
// Then retrieve the `incrementalMean` that is the closest to the total `mean`, | ||
// within a specific range decided by `minIndex` and `maxIndex`. | ||
// This must be performend on unsorted `measures`. | ||
// - Therefore, outliers are still present and must be removed using a `filter` | ||
// - This also requires using two indexes: filtered and not. | ||
// `minIndex` is always <= `maxIndex`, i.e. a `closestMean` is always found. | ||
// Time complexity is `O(n)`. | ||
// Memory complexity is `O(1)` and very low by using a streaming logic. | ||
// This is optimized for performance, which explains the usage of imperative | ||
// programming patterns. | ||
/* eslint-disable max-statements, complexity, fp/no-let, fp/no-loops, | ||
fp/no-mutation, max-depth, no-continue */ | ||
export const findClosestMean = function ( | ||
array, | ||
{ mean, minIndex, maxIndex, filter }, | ||
) { | ||
let sum = 0 | ||
let filteredIndex = 0 | ||
let closestMean = 0 | ||
let closestMeanDiff = Number.POSITIVE_INFINITY | ||
|
||
for (const value of array) { | ||
if (!filter(value)) { | ||
continue | ||
} | ||
|
||
if (filteredIndex > maxIndex) { | ||
break | ||
} | ||
|
||
filteredIndex += 1 | ||
sum += value | ||
|
||
if (filteredIndex <= minIndex) { | ||
continue | ||
} | ||
|
||
const incrementalMean = sum / filteredIndex | ||
const meanDiff = Math.abs(mean - incrementalMean) | ||
|
||
if (closestMeanDiff > meanDiff) { | ||
closestMeanDiff = meanDiff | ||
closestMean = incrementalMean | ||
} | ||
} | ||
|
||
return closestMean | ||
} | ||
|
||
// Find the index of the first `incrementalMean` between `incrementalMeanMin` | ||
// and `incrementalMax`. | ||
export const findHotIndex = function ( | ||
array, | ||
{ minIndex, filter, incrementalMeanMin, incrementalMeanMax }, | ||
) { | ||
let sum = 0 | ||
let filteredIndex = 0 | ||
let incrementalMean = 0 | ||
|
||
for (const value of array) { | ||
if (!filter(value)) { | ||
continue | ||
} | ||
|
||
filteredIndex += 1 | ||
sum += value | ||
|
||
if (filteredIndex <= minIndex) { | ||
continue | ||
} | ||
|
||
incrementalMean = sum / filteredIndex | ||
|
||
if ( | ||
incrementalMean >= incrementalMeanMin && | ||
incrementalMean <= incrementalMeanMax | ||
) { | ||
break | ||
} | ||
} | ||
|
||
return filteredIndex - 1 | ||
} | ||
/* eslint-enable max-statements, complexity, fp/no-let, fp/no-loops, | ||
fp/no-mutation, max-depth, no-continue */ |
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