Last active
October 23, 2025 02:22
-
-
Save nsdevaraj/578fae316ad79b014f340151af5b9789 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import { topNPareto, topNPercentile } from './statsUtil.js'; | |
| const salesData = [ | |
| { product: 'A', revenue: 100 }, | |
| { product: 'B', revenue: 50 }, | |
| { product: 'C', revenue: 30 }, | |
| { product: 'D', revenue: 20 } | |
| ]; | |
| // Top 80% Pareto (A + B = 150/200 = 75%, but we include until >=80%; here A+B=75%, so add C for 90%) | |
| console.log(topNPareto(salesData, 80)); | |
| // Output: [ | |
| // { product: 'A', revenue: 100, cumulativePercent: 50 }, | |
| // { product: 'B', revenue: 50, cumulativePercent: 75 }, | |
| // { product: 'C', revenue: 30, cumulativePercent: 90 } // Cumulative hits >=80% | |
| // ] | |
| // Top 95th Percentile (cutoff ~95; only A qualifies) | |
| console.log(topNPercentile(salesData, 95)); | |
| // Output: [ { product: 'A', revenue: 100 } ] |
This file contains hidden or 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
| import ss from 'simple-statistics'; // For percentile calc | |
| /** | |
| * Calculates Top N% of Total (Pareto-style): Items contributing to at least N% of aggregate total. | |
| * @param {Array} data - Array of objects with 'value' key (e.g., revenue). | |
| * @param {number} nPercent - Threshold (e.g., 80 for 80%). | |
| * @returns {Array} Filtered/sorted items + cumulative % details. | |
| * Steps: Aggregate/sort descending, compute running total / overall total, stop at threshold. | |
| */ | |
| export function topNPareto(data, nPercent) { | |
| if (!data || data.length === 0) return []; | |
| // Extract values and sort descending | |
| const items = [...data].sort((a, b) => b.value - a.value); | |
| const total = items.reduce((sum, item) => sum + item.value, 0); | |
| const threshold = (nPercent / 100) * total; | |
| let cumulative = 0; | |
| const result = []; | |
| for (const item of items) { | |
| cumulative += item.value; | |
| result.push({ | |
| ...item, | |
| cumulativePercent: (cumulative / total) * 100 | |
| }); | |
| if (cumulative >= threshold) break; | |
| } | |
| return result; | |
| } | |
| /** | |
| * Calculates Top N Percentile: Items >= Nth percentile of the value distribution. | |
| * @param {Array} data - Array of objects with 'value' key. | |
| * @param {number} nPercentile - Percentile (e.g., 95 for top 5%). | |
| * @returns {Array} Filtered items (original order) where value >= cutoff. | |
| * Steps: Compute percentile cutoff, filter >= cutoff. | |
| */ | |
| export function topNPercentile(data, nPercentile) { | |
| if (!data || data.length === 0) return []; | |
| // Extract values for percentile calc | |
| const values = data.map(item => item.value); | |
| const p = nPercentile / 100; // simple-statistics expects 0-1 | |
| const cutoff = ss.quantile(values, p); // Or ss.quantileSorted if pre-sorted | |
| // Filter items >= cutoff (preserves original order) | |
| return data.filter(item => item.value >= cutoff); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment