ktg-plugin-marketplace/plugins/linkedin-thought-leadership/scripts/analytics/src/utils/stats.ts
Kjell Tore Guttormsen 39f8b275a6 feat(linkedin-thought-leadership): v1.0.0 — initial open-source import
Build LinkedIn thought leadership with algorithmic understanding,
strategic consistency, and AI-assisted content creation. Updated for
the January 2026 360Brew algorithm change.

16 agents, 25 commands, 6 skills, 9 hooks, 24 reference docs.

Personal data sanitized: voice samples generalized to template,
high-engagement posts cleared, region-specific references replaced
with placeholders.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-07 22:09:03 +02:00

63 lines
1.7 KiB
TypeScript

import type { TrendDirection } from "../models/types.js";
/**
* Calculate arithmetic mean of values.
* Returns 0 for empty array.
*/
export function mean(values: number[]): number {
if (values.length === 0) return 0;
const sum = values.reduce((acc, val) => acc + val, 0);
return sum / values.length;
}
/**
* Calculate population standard deviation.
* Returns 0 for empty or single-element array.
*/
export function standardDeviation(values: number[]): number {
if (values.length <= 1) return 0;
const avg = mean(values);
const squaredDiffs = values.map((val) => Math.pow(val - avg, 2));
const variance = mean(squaredDiffs);
return Math.sqrt(variance);
}
/**
* Determine trend direction based on percentage change.
* Returns "up" if change > threshold, "down" if change < -threshold, "stable" otherwise.
* Default threshold is 5%.
*/
export function trendDirection(
current: number,
previous: number,
threshold: number = 5
): TrendDirection {
const change = percentChange(current, previous);
if (change > threshold) return "up";
if (change < -threshold) return "down";
return "stable";
}
/**
* Calculate percentage change between current and previous values.
* Returns 0 if previous is 0.
*/
export function percentChange(current: number, previous: number): number {
if (previous === 0) return 0;
return ((current - previous) / previous) * 100;
}
/**
* Calculate how many standard deviations a value is from the mean.
* Returns 0 if standard deviation is 0.
*/
export function deviationsFromMean(value: number, values: number[]): number {
const avg = mean(values);
const stdDev = standardDeviation(values);
if (stdDev === 0) return 0;
return (value - avg) / stdDev;
}