Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Write a program in Python to caluculate the adjusted and non-adjusted EWM in a given dataframe
The Exponentially Weighted Moving Average (EWM) is a statistical technique that gives more weight to recent observations. Pandas provides two modes: adjusted (default) and non-adjusted, which handle the calculation differently during the initial periods.
Understanding EWM Parameters
The key difference between adjusted and non-adjusted EWM lies in how they handle the bias correction ?
Adjusted EWM (default): Applies bias correction to account for the initialization period
Non-adjusted EWM: Uses raw exponential weighting without bias correction
com parameter: Center of mass, controls the decay rate (higher values = slower decay)
Creating Sample Data
First, let's create a DataFrame with some missing values to demonstrate the EWM calculation ?
import numpy as np
import pandas as pd
df = pd.DataFrame({
'Id': [1, 2, 3, np.nan, 5],
'Age': [12, 13, 12, 14, 15]
})
print("Original DataFrame:")
print(df)
Original DataFrame:
Id Age
0 1.0 12
1 2.0 13
2 3.0 12
3 NaN 14
4 5.0 15
Calculating Adjusted EWM
The adjusted EWM applies bias correction and is the default behavior ?
import numpy as np
import pandas as pd
df = pd.DataFrame({
'Id': [1, 2, 3, np.nan, 5],
'Age': [12, 13, 12, 14, 15]
})
adjusted_ewm = df.ewm(com=0.5).mean()
print("Adjusted EWM:")
print(adjusted_ewm)
Adjusted EWM:
Id Age
0 1.000000 12.000000
1 1.750000 12.750000
2 2.615385 12.230769
3 2.615385 13.425000
4 4.670213 14.479339
Calculating Non-Adjusted EWM
The non-adjusted EWM uses raw exponential weighting by setting adjust=False ?
import numpy as np
import pandas as pd
df = pd.DataFrame({
'Id': [1, 2, 3, np.nan, 5],
'Age': [12, 13, 12, 14, 15]
})
non_adjusted_ewm = df.ewm(com=0.5, adjust=False).mean()
print("Non-Adjusted EWM:")
print(non_adjusted_ewm)
Non-Adjusted EWM:
Id Age
0 1.000000 12.000000
1 1.666667 12.666667
2 2.555556 12.222222
3 2.555556 13.407407
4 4.650794 14.469136
Complete Example
Here's a complete program that calculates both adjusted and non-adjusted EWM ?
import numpy as np
import pandas as pd
# Create sample DataFrame
df = pd.DataFrame({
'Id': [1, 2, 3, np.nan, 5],
'Age': [12, 13, 12, 14, 15]
})
print("Original DataFrame:")
print(df)
print("\nAdjusted EWM:")
print(df.ewm(com=0.5).mean())
print("\nNon-Adjusted EWM:")
print(df.ewm(com=0.5, adjust=False).mean())
Original DataFrame:
Id Age
0 1.0 12
1 2.0 13
2 3.0 12
3 NaN 14
4 5.0 15
Adjusted EWM:
Id Age
0 1.000000 12.000000
1 1.750000 12.750000
2 2.615385 12.230769
3 2.615385 13.425000
4 4.670213 14.479339
Non-Adjusted EWM:
Id Age
0 1.000000 12.000000
1 1.666667 12.666667
2 2.555556 12.222222
3 2.555556 13.407407
4 4.650794 14.469136
Key Differences
| Aspect | Adjusted EWM | Non-Adjusted EWM |
|---|---|---|
| Bias Correction | Yes | No |
| Initial Values | Higher smoothing | Lower smoothing |
| Parameter |
adjust=True (default) |
adjust=False |
Conclusion
Adjusted EWM provides bias-corrected exponential smoothing and is the default option. Non-adjusted EWM uses raw exponential weighting, which may be preferred for certain statistical applications where bias correction is not desired.
