0
0
Data Analysis Pythondata~10 mins

Window functions (expanding, ewm) in Data Analysis Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Window functions (expanding, ewm)
Start with data series
Choose window type: Expanding or EWM
For each new data point:
Expanding: Calculate stats over all data so far
EWM: Calculate weighted stats with decay
Output updated series with window stats
End
We start with a data series, pick a window type (expanding or exponentially weighted), then for each new data point, calculate statistics over the window and output the updated series.
Execution Sample
Data Analysis Python
import pandas as pd
s = pd.Series([1, 2, 3, 4, 5])
expanding_mean = s.expanding().mean()
ewm_mean = s.ewm(alpha=0.5).mean()
print(expanding_mean)
print(ewm_mean)
Calculate the expanding mean and exponentially weighted mean of a simple series.
Execution Table
StepData PointExpanding Window DataExpanding MeanEWM CalculationEWM Mean
11[1]1.011.0
22[1, 2]1.50.5*2 + 0.5*1=1.51.5
33[1, 2, 3]2.00.5*3 + 0.5*1.5=2.252.25
44[1, 2, 3, 4]2.50.5*4 + 0.5*2.25=3.1253.125
55[1, 2, 3, 4, 5]3.00.5*5 + 0.5*3.125=4.06254.0625
6End----
💡 Reached end of data series, no more points to process.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5Final
s[1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5][1,2,3,4,5]
expanding_meanempty1.01.52.02.53.0[1.0,1.5,2.0,2.5,3.0]
ewm_meanempty1.01.52.253.1254.0625[1.0,1.5,2.25,3.125,4.0625]
Key Moments - 3 Insights
Why does the expanding mean change with every new data point?
Because expanding mean includes all data points from the start up to the current one, so it recalculates the average over a growing window each step (see execution_table rows 1-5).
How does the exponentially weighted mean (EWM) differ from the expanding mean?
EWM gives more weight to recent points using a decay factor (alpha). It updates the mean by combining the new point with the previous EWM mean, shown in the EWM Calculation column.
Why does EWM mean not simply average all points like expanding mean?
Because EWM uses a weighted formula that discounts older points exponentially, so older data has less influence than recent data (see EWM Calculation and EWM Mean columns).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 3. What is the expanding mean value?
A3.0
B2.0
C1.5
D2.25
💡 Hint
Check the 'Expanding Mean' column at Step 3 in the execution_table.
At which step does the EWM mean first exceed 3.0?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look at the 'EWM Mean' column in the execution_table and find when it passes 3.0.
If alpha in EWM is increased, how would the EWM mean values change?
AThey would give more weight to recent points, making EWM mean react faster.
BThey would give more weight to older points.
CThey would become the same as expanding mean.
DThey would not change.
💡 Hint
Recall that alpha controls the decay rate in EWM, higher alpha means more weight on recent data.
Concept Snapshot
Window functions process data in chunks over time.
Expanding windows grow with each new point, averaging all data so far.
Exponentially weighted windows (EWM) weight recent data more using alpha.
Use expanding() or ewm(alpha) in pandas for these.
EWM reacts faster to recent changes than expanding.
Both help analyze trends in time series data.
Full Transcript
We start with a data series and choose a window function: expanding or exponentially weighted mean (EWM). For each new data point, expanding mean calculates the average of all points from the start up to that point, so the window grows. EWM calculates a weighted average giving more importance to recent points using a decay factor alpha. The execution table shows step-by-step how the expanding mean and EWM mean update as new data points arrive. Variables track the series and the calculated means after each step. Key moments clarify why expanding mean changes with every point, how EWM differs by weighting recent data more, and why EWM does not simply average all points. The quiz tests understanding of values at specific steps and effects of changing alpha. The snapshot summarizes that expanding windows grow and average all data so far, while EWM weights recent data more, useful for analyzing trends in time series.