@tpmjs/tools-anomaly-detect-mad
Detect anomalies (outliers) in numeric data using the Median Absolute Deviation (MAD) method. MAD is a robust statistic that is resistant to outliers themselves, making it more reliable than standard deviation for detecting anomalies. The modified z-score threshold of 3.5 is commonly used (equivalent to ±3 standard deviations in normal distribution).
Test @tpmjs/tools-anomaly-detect-mad (anomalyDetectMADTool) with AI-powered execution
0/2000 characters
Install this tool and use it with the AI SDK
npm install @tpmjs/tools-anomaly-detect-madpnpm add @tpmjs/tools-anomaly-detect-madyarn add @tpmjs/tools-anomaly-detect-madbun add @tpmjs/tools-anomaly-detect-maddeno add npm:@tpmjs/tools-anomaly-detect-madimport { anomalyDetectMADTool } from '@tpmjs/tools-anomaly-detect-mad';import { generateText } from 'ai';
import { openai } from '@ai-sdk/openai';
import { anomalyDetectMADTool } from '@tpmjs/tools-anomaly-detect-mad';
const result = await generateText({
model: openai('gpt-4o'),
tools: { anomalyDetectMADTool },
prompt: 'Your prompt here...',
});
console.log(result.text);Available configuration options
dataarrayArray of numeric values to analyze for anomalies
thresholdnumberModified z-score threshold for anomaly detection. Default: 3.5 (recommended). Lower values = more sensitive. Common values: 2.5 (sensitive), 3.5 (balanced), 4.5 (conservative)
Schema extracted: 1/1/2026, 8:18:10 AM
Detect anomalies (outliers) in numeric data using the Median Absolute Deviation (MAD) method.
The Median Absolute Deviation (MAD) is a robust statistic for detecting outliers. Unlike standard deviation-based methods (which are themselves influenced by outliers), MAD is resistant to extreme values, making it more reliable for anomaly detection.
Traditional approach (standard deviation):
MAD approach:
npm install @tpmjs/tools-anomaly-detect-mad
import { anomalyDetectMADTool } from '@tpmjs/tools-anomaly-detect-mad'; import { generateText } from 'ai'; const result = await generateText({ model: yourModel, tools: { detectAnomalies: anomalyDetectMADTool }, toolChoice: 'required', prompt: 'Find anomalies in this data: [10, 12, 11, 13, 10, 95, 12, 11, 14, 10]', });
import { anomalyDetectMADTool } from '@tpmjs/tools-anomaly-detect-mad'; const result = await anomalyDetectMADTool.execute({ data: [10, 12, 11, 13, 10, 95, 12, 11, 14, 10], threshold: 3.5, // Optional, defaults to 3.5 }); console.log(result); // { // anomalies: [ // { // value: 95, // index: 5, // deviation: 83.5, // zScore: 28.177 // } // ], // anomalyIndices: [5], // statistics: { // median: 11.5, // mad: 2, // threshold: 3.5, // totalPoints: 10, // anomalyCount: 1, // anomalyPercentage: 10 // } // }
data (required): Array of numeric values to analyze (minimum 3 values)threshold (optional): Modified z-score threshold for anomaly detection
3.5 (recommended, equivalent to ±3σ in normal distribution)0.1 to 10| Threshold | Sensitivity | Use Case |
|---|---|---|
| 2.5 | High | Detect subtle anomalies, exploratory analysis |
| 3.0 | Moderate-High | Balanced detection |
| 3.5 | Balanced (default) | General purpose, recommended |
| 4.0 | Moderate-Low | More conservative |
| 4.5+ | Low | Only extreme outliers |
{ anomalies: Array<{ value: number; // The anomalous value index: number; // Position in original array deviation: number; // Absolute deviation from median zScore: number; // Modified z-score (based on MAD) }>; anomalyIndices: number[]; // Quick array of anomaly positions statistics: { median: number; // Median of dataset mad: number; // Median Absolute Deviation threshold: number; // Threshold used totalPoints: number; // Total data points anomalyCount: number; // Number of anomalies found anomalyPercentage: number; // Percentage of data that are anomalies }; }
Anomalies are sorted by absolute z-score (most extreme first).
The MAD method works as follows:
M = median(data)|x_i - M| for each data pointMAD = median(|x_i - M|)z_i = 0.6745 × (x_i - M) / MAD|z_i| > thresholdThe constant 0.6745 is the 75th percentile of the standard normal distribution, which makes the MAD-based z-score comparable to traditional z-scores.
Server response times:
const responseTimes = [120, 115, 130, 125, 118, 3500, 122, 119, 128, 121]; const result = await anomalyDetectMADTool.execute({ data: responseTimes }); // Detects the 3500ms outlier
Sensor readings with noise:
const temperatures = [20.1, 20.3, 19.9, 20.2, 45.0, 20.0, 19.8, 20.4]; const result = await anomalyDetectMADTool.execute({ data: temperatures, threshold: 3.0, // More sensitive for safety-critical applications }); // Detects the 45.0 degree spike
Financial transactions:
const transactions = [25.50, 32.10, 28.75, 31.20, 2500.00, 29.80]; const result = await anomalyDetectMADTool.execute({ data: transactions }); // Flags the unusual $2500 transaction
Quality control:
const measurements = [10.02, 10.01, 9.99, 10.00, 10.02, 10.50, 10.01]; const result = await anomalyDetectMADTool.execute({ data: measurements, threshold: 2.5, // Sensitive to detect quality issues early }); // Detects measurements outside acceptable tolerance
All values identical:
const data = [5, 5, 5, 5, 5]; const result = await anomalyDetectMADTool.execute({ data }); // Returns: anomalyCount: 0, mad: 0
MAD = 0 with variation:
const data = [10, 10, 10, 10, 15]; // Median = 10, but one different value const result = await anomalyDetectMADTool.execute({ data }); // Special handling: flags the 15 as anomaly with zScore: Infinity
Consider the dataset: [10, 12, 11, 13, 10, 95, 12, 11, 14, 10]
Standard Deviation Method:
MAD Method:
Use MAD when:
Consider alternatives when:
MIT
Downloads/month
0
Quality Score