FORECASTING
Moving Average
Smooth a time series using a rolling window.
Example of usage
Smooth weekly retail demand before stock planning.
Usage / limitations
+Use when: Explore short-term trends.
−Limitations: Lags changes and is not by itself a complete forecasting model.
Mathematical formula
MAₜ = (1/w) Σᵢ₌₀ʷ⁻¹ xₜ₋ᵢ
Sample dataShow
{
"variable": "demand_records",
"domain": "Retail stock planning",
"feature_names": [
"week",
"units_sold"
],
"records": [
{
"week": 1,
"units_sold": 10
},
{
"week": 2,
"units_sold": 12
},
{
"week": 3,
"units_sold": 14
},
{
"week": 4,
"units_sold": 16
},
{
"week": 5,
"units_sold": 18
}
],
"window": 3
}
Code example (Python)
# DataLens.ai sample data — weekly retail sales
demand_records = [{'week': 1, 'units_sold': 10}, {'week': 2, 'units_sold': 12}, {'week': 3, 'units_sold': 14}, {'week': 4, 'units_sold': 16}, {'week': 5, 'units_sold': 18}]
window = 3
# DataLens.ai example
# Average the most recent three weekly sales values.
series = [row["units_sold"] for row in demand_records]
smoothed = [sum(series[i-window+1:i+1]) / window for i in range(window-1,len(series))]
assert smoothed == [12.0, 14.0, 16.0]
print("Expected output verified")
# DataLens.ai example
# Average the most recent three weekly sales values.
series = [row["units_sold"] for row in demand_records]
smoothed = [sum(series[i-window+1:i+1]) / window for i in range(window-1,len(series))]
assert smoothed == [12.0, 14.0, 16.0]
print("Expected output verified")