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
- Algorithmic detection for common patterns
- Volatility analysis using coefficient of variation
- Cross-series pattern detection (inverse correlations)
- Agent enhancement for deeper pattern analysis
Detected Pattern Types
| Pattern | Detection Logic | Threshold |
|---|---|---|
| Upward Trend | (last - first) / mean > threshold | 15% |
| Downward Trend | (first - last) / mean > threshold | 15% |
| High Volatility | stdDev / mean > threshold | 0.25 |
| Inverse Correlation | correlation(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:
stepPlanexists and doesn't include'pattern'- No
seriesdata is available