How to execute single exponential smoothening using Python code (without using packages)
The Data Scientist's Dispatch
Demystifying Exponential Smoothing Simulations
In today’s edition, we take a look under the hood of a Python-based demand forecasting simulation. Whether you are managing inventory or predicting sales trends, understanding how "smoothing" constants influence your forecast is key.
Let's break down the code you’ve shared into its functional clusters.
1. The Toolkit (Imports)
Every great simulation starts with the right tools. Here, we utilize three standard Python libraries:
2. The Architecture (Function Definition)
The core of our logic resides in the exp_for function. In this cluster, we define how the simulation behaves. Interestingly, the code is designed to generate a unique set of $\alpha$ (alpha) and $\beta$ (beta) values for every time step in the 16-period window.
Recommended by LinkedIn
3. The Smoothing Logic (The Loop)
This is where the math happens. The simulation iterates through 16 periods. For each period, it calculates a new forecast using the formula:
Forecast_t = (alpha*S ales_t) + ((1 - alpha) * Previous_Forecast)
By rounding these values to two decimal places, we keep the output clean and professional, mimicking real-world financial reporting.
4. Execution & Initialization
Outside the function, we see the initialization phase. We generate a series of random sales data (using a normal distribution between 140 and 200) and then trigger the function to produce our results.
The Final Script
Below is the complete, integrated code block for your reference.
import random
import math
import statistics
def exp_for(sales_input, alpha_input, beta_input):
# Cluster: Generating random coefficients for the simulation
# alpha represents the weight of the most recent data
alpha = [round(random.random(), 2) for _ in range(16)]
alpha_1 = [round(1 - a, 2) for a in alpha]
# beta is often used in double exponential smoothing for trend
beta = [round(random.random(), 2) for _ in range(16)]
beta_1 = [round(1 - b, 2) for b in beta]
# Cluster: Generating simulated sales data with a Gaussian distribution
sales = [random.gauss(200, 140) for _ in range(16)]
forecast = []
# Start the forecast with the mean of the simulated sales
prev_f = statistics.mean(sales)
# Cluster: The Exponential Smoothing Loop
for i in range(16):
# Weighted average of current sales and previous forecast
forecast_1 = alpha[i] * sales[i] + alpha_1[i] * prev_f
current_f = round(forecast_1, 2)
forecast.append(current_f)
prev_f = current_f # Update the previous forecast for the next iteration
return forecast
# --- Main Execution ---
# Initializing sample data
alpha_init = [round(random.random(), 2) for _ in range(16)]
beta_init = [round(random.random(), 2) for _ in range(16)]
sales_init = [random.uniform(140, 200) for _ in range(16)]
# Run the simulation
results = exp_for(sales_init, alpha_init, beta_init)
print("Simulated 16-Period Forecast:")
print(results)
if you want to learn and apply machine learning in supply chain, then join our upcoming cohort starting on 17th March 2026 weekend batches.
for details, please email krishnaidu@mathnal.tech or WhatsApp us @ +9