StandardDeviation
The StandardDeviation operator calculates the standard deviation over a sliding window of numeric values. It emits the standard deviation value whenever a new value arrives and the buffer is full.
Configuration
Required Parameters
id: Unique identifier for the operatorwindow_size: Number of values to include in the standard deviation calculation (must be >= 1)
Example Configuration
{
"id": "sd1",
"window_size": 20
}
Port Configuration
Inputs
- Port 0: Single input port accepting NumberData messages
Outputs
- Port 0: Single output port emitting NumberData messages with standard deviation values
Operation
The operator:
- Maintains a sliding window of the most recent
window_sizevalues - Calculates standard deviation using the formula: σ = √(Σ(x - μ)²/(n-1))
- Emits output messages containing:
- Same timestamp as the latest input message
- Value field contains the calculated standard deviation
- Output begins only when the buffer is full
Example Message Flow
| Time | Input Value | Output Value | Notes |
|---|---|---|---|
| 1 | 2.0 | - | Buffer filling |
| 3 | 4.0 | - | Buffer filling |
| 5 | 6.0 | - | Buffer filling |
| 7 | 8.0 | 2.582 | First output |
| 10 | 10.0 | 2.582 | Sliding window |
Statistical Details
The standard deviation is calculated using a numerically stable algorithm that:
- Maintains running sums and means
- Uses online variance calculation
- Properly handles buffer overflow
- Provides accurate results even with large numbers
Error Handling
The operator will throw exceptions for:
- Invalid window size (must be >= 1)
- Invalid message types
- Type mismatches on input port
Use Cases
Ideal for:
- Volatility measurement
- Signal variability analysis
- Anomaly detection
- Data quality monitoring
- Process control
Examples
Basic Usage
// Create operator with window size 5
auto sd = std::make_unique<StandardDeviation>("sd1", 5);
// Process some values
sd->receive_data(create_message<NumberData>(1, NumberData{10.0}), 0);
sd->receive_data(create_message<NumberData>(2, NumberData{12.0}), 0);
sd->execute();
Pipeline Integration
auto input = make_number_input("in1");
auto sd = std::make_unique<StandardDeviation>("sd1", 10);
auto output = make_number_output("out1");
input->connect(sd.get());
sd->connect(output.get());