Skip to main content

Pattern Recognition

Overview

The Pattern Recognition step detects behavioral patterns and anomalies in the data using both algorithmic and AI-based methods.

File: steps/pattern.step.ts

What It Does

  1. Algorithmic detection for common patterns
  2. Volatility analysis using coefficient of variation
  3. Cross-series pattern detection (inverse correlations)
  4. Agent enhancement for deeper pattern analysis

Detected Pattern Types

PatternDetection LogicThreshold
Upward Trend(last - first) / mean > threshold15%
Downward Trend(first - last) / mean > threshold15%
High VolatilitystdDev / mean > threshold0.25
Inverse Correlationcorrelation(A, B) < threshold-0.6

Algorithmic Detection

// Trend detection
const trendThreshold = 0.15;
const change = (last - first) / mean;
if (change > trendThreshold) {
patterns.push({ name: 'upward_trend', series: id, detail: `${id} shows upward trend` });
}

// Volatility detection
const coeffOfVariation = stdDev / mean;
if (coeffOfVariation > 0.25) {
patterns.push({ name: 'high_volatility', series: id, detail: `${id} shows high volatility` });
}

Input

{
series: Series[], // Time-series from preflight
temporal?: TemporalOutput // Stats from temporal step
}

Output

{
pattern: {
matches: string[], // Pattern descriptions
confidence: number, // 0-1 confidence score
source: 'algorithmic' | 'agent' | 'mock',
agentUsed: boolean,
detectedPatterns: Array<{
name: string, // Pattern type identifier
series: string, // Which series exhibited pattern
detail: string // Human-readable description
}>
}
}

Agent Enhancement

The pattern agent provides deeper analysis:

const agent = await getPatternAgent(mastra);
const response = await callAgent(agent, {
series: inputData.series,
temporal: inputData.temporal,
objective: inputData.objective
});

Agent can detect:

  • Complex multi-series patterns
  • Contextual anomalies
  • Behavioral cycles (weekly, monthly)
  • Onset/offset patterns

Example Output

{
"pattern": {
"matches": [
"Stress shows upward trend (+45%)",
"Energy exhibits high volatility",
"Inverse pattern: stress↑ correlates with mood↓"
],
"confidence": 0.85,
"source": "agent",
"agentUsed": true,
"detectedPatterns": [
{
"name": "upward_trend",
"series": "stress",
"detail": "Stress increased 45% over the analysis period"
},
{
"name": "high_volatility",
"series": "energy",
"detail": "Energy levels fluctuate significantly day-to-day"
},
{
"name": "inverse_correlation",
"series": "stress,mood",
"detail": "When stress increases, mood tends to decrease"
}
]
}
}

Skip Conditions

This step is skipped if:

  • stepPlan exists and doesn't include 'pattern'
  • No series data is available