Online algorithm for calculating absolute deviation
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
In the realm of data analysis and statistics, the absolute deviation is a measure that quantifies the deviation of data points from a central value, such as the mean or median. An "online algorithm" computes desired values incrementally, handling data points one at a time. This technique is particularly useful for processing data streams or large datasets where storage and computation needs must be minimized.
This article delves into how online algorithms can be effectively employed to calculate absolute deviations, ensuring memory-efficient and rapid computation.
Absolute Deviation
Absolute deviation refers to the absolute differences between each data point and a measure of central tendency (mean, median, etc.). Formally, the absolute deviation from the mean () for a dataset with mean is given by:
Similarly, absolute deviation from the median is expressed as:
Calculating absolute deviation using traditional batch processing methods may involve storing large volumes of data, thus prompting interest in online algorithms.
Online Algorithm for Absolute Deviation
Basic Concept
An online algorithm processes elements in real-time, updating the required statistics as new data points arrive. It is particularly beneficial for large datasets or continuous data streams. Here's how we can develop an online algorithm for computing the absolute deviation from the mean.
Incremental Updates
To use an online method for calculating absolute deviation, we must also be able to update the mean incrementally. Given a sequence of observations up to time , the mean can be updated as follows when a new observation arrives:
Using the updated mean, we can compute the current absolute deviation:
- Initialize the cumulative absolute deviation (
CAD) and mean (mu). - For each new data point : • Update mean: • Compute the absolute difference: • Update
CAD:CAD = CAD + \Delta• ComputeAD:
Example
Let's process a sequence of data points step by step and update the absolute deviation:
| Step | Data Point () | Mean () | Absolute Deviation (AD) |
| 1 | 5 | 5 | 0 |
| 2 | 8 | 6.5 | 1.5 |
| 3 | 7 | 6.6667 | 1.1111 |
| 4 | 10 | 7.5 | 1.875 |
For each new data point, the mean, absolute deviation, and absolute difference are recalculated, thus maintaining an updated measure without revisiting past data.
Considerations for Absolute Deviation from Median
Calculating the absolute deviation from the median in an online fashion is more challenging due to the nature of the median, which necessitates knowing the relative order of data points. Several sophisticated methods, such as the exponential smoothing or window-based approaches, are required to approximate real-time computation efficiently.
Key Points Summary
| Concept | Description |
| Absolute Deviation | Measures the average absolute difference from a central value (mean/median). |
| Online Algorithm | Processes data incrementally, updating results in real-time. |
| Incremental Mean Update | Uses past mean and new data to calculate an updated mean. |
| Absolute Deviation Calculation | Uses updated mean to compute deviation; requires memory-efficient cumulative updates. |
Conclusion
Online algorithms for calculating absolute deviation are invaluable in scenarios with large or streaming datasets. By efficiently updating central measures and deviations incrementally, these algorithms offer significant computational savings, enabling real-time analytics and decision-making. Developing proficiency in such techniques can greatly enhance one's toolkit in data analysis and statistics.
Related reading
- Options for deploying R models in production
- Ordinal classification packages and algorithms
- out of sample definition
- Outliers using RPCA
- Onlogn Algorithm - Find three evenly spaced ones within binary string
- ONLogN algorithm for the following problem
- Outline plotting algorithm
- Output differences when changing order of batch, shuffle and repeat

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.