online algorithms
absolute deviation
statistical methods
real-time computation
data analysis

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.

Practice ML system design

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 (ADmeanAD_{\text{mean}}) for a dataset X=x1,x2,,xnX = {x_1, x_2, \ldots, x_n} with mean μ\mu is given by:

AD_mean=1n_i=1nx_iμAD\_{\text{mean}} = \frac{1}{n} \sum\_{i=1}^{n} |x\_i - \mu|

Similarly, absolute deviation from the median is expressed as:

AD_median=1n_i=1nx_imedian(X)AD\_{\text{median}} = \frac{1}{n} \sum\_{i=1}^{n} |x\_i - \text{median}(X)|

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 tt, the mean can be updated as follows when a new observation xt+1x_{t+1} arrives:

μ_t+1=μ_t+x_t+1μ_tt+1\mu\_{t+1} = \mu\_t + \frac{x\_{t+1} - \mu\_t}{t+1}

Using the updated mean, we can compute the current absolute deviation:

  1. Initialize the cumulative absolute deviation (CAD ) and mean (mu ).
  2. For each new data point xt+1x_{t+1}: • Update mean: μt+1=μt+xt+1μtt+1\mu_{t+1} = \mu_t + \frac{x_{t+1} - \mu_t}{t+1} • Compute the absolute difference: Δ=xt+1μt\Delta = |x_{t+1} - \mu_t| • Update CAD : CAD = CAD + \Delta
    • Compute AD : AD=CADt+1AD = \frac{CAD}{t+1}

Example

Let's process a sequence of data points step by step and update the absolute deviation:

StepData Point (xx)Mean (μ\mu)Absolute Deviation (AD)
1550
286.51.5
376.66671.1111
4107.51.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

ConceptDescription
Absolute DeviationMeasures the average absolute difference from a central value (mean/median).
Online AlgorithmProcesses data incrementally, updating results in real-time.
Incremental Mean UpdateUses past mean and new data to calculate an updated mean.
Absolute Deviation CalculationUses 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.