How to optimize Inventory management using Python

Inventory Mangement

Inventory management is one of the most important topics that heavily affects businesses’ profit margins when it comes to operations and supply chain management. Previously, all the inventory and warehouse decisions were made based on historical data. With time, the decision nowadays is taken with the help of predictive analytics where Python can play a huge role.

In this article, we will learn about concepts like Economic Order of Quantity (EOQ), ARIMA, and exponential smoothening with their implementation in Python programming language.

Recommended: Analyze Weather Data with Python And A Weather API To Predict Weather Trends

Economic Order of Quantity ( EOQ )

Economic Order of Quantity or EOQ is an inventory management concept where we determine the optimal order quantity considering the annual demand, holding costs, ordering costs, etc.

EOQ Model Equation
EOQ Model Equation

Safety stock on the other hand helps us minimize the uncertainty and prevent the risk of stockouts. Safety stock essentially means when to order inventory to keep up with uncertainty in demand.

Safety Stock Concept
Safety Stock Graph

Now let us look at the code of the above concept.

import numpy as np
import matplotlib.pyplot as plt

# Function to calculate Economic Order Quantity (EOQ)
def calculate_eoq(demand, ordering_cost, holding_cost):
    eoq = np.sqrt((2 * demand * ordering_cost) / holding_cost)
    return eoq

# Function to calculate safety stock
def calculate_safety_stock(demand, lead_time, std_dev_demand, std_dev_lead_time, service_level):
    z = 1.96  # Z-value for 95% service level
    safety_stock = z * np.sqrt((std_dev_demand**2 * lead_time) + (demand**2 * std_dev_lead_time**2))
    return safety_stock

# Given parameters
demand_per_period = 100  # units
ordering_cost_per_order = 50  # currency units per order
holding_cost_per_unit_per_period = 2  # currency units per unit per period
lead_time = 2  # periods
std_dev_demand = 10  # units
std_dev_lead_time = 0.5  # periods
service_level = 0.95  # 95% service level

# Calculate EOQ
eoq = calculate_eoq(demand_per_period, ordering_cost_per_order, holding_cost_per_unit_per_period)

# Calculate safety stock
safety_stock = calculate_safety_stock(demand_per_period, lead_time, std_dev_demand, std_dev_lead_time, service_level)

# Visualize the inventory level over time
time_periods = np.arange(1, 101)
inventory_level = np.zeros(100)
inventory_level[0] = eoq + safety_stock
for i in range(1, 100):
    if i % lead_time == 0:
        inventory_level[i] = eoq + safety_stock
    else:
        inventory_level[i] = max(0, inventory_level[i-1] - demand_per_period)

plt.plot(time_periods, inventory_level)
plt.title('Inventory Level Over Time')
plt.xlabel('Time Period')
plt.ylabel('Inventory Level')
plt.show()

print("EOQ:", eoq)
print("Safety Stock:", safety_stock)

Let us look at the output of the above code.

EOQ Output
EOQ Output

Therefore, the batch size is around 71 units and the safety stock is around 102.

ARIMA

ARIMA stands for AutoRegressive Integrated Moving Average and is used to forecast inventory stock where there is some pattern. In this section, we will learn how to do ARIMA using libraries from Python. In this code, we will forecast airline passengers over some time.

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.arima.model import ARIMA

# Load data
data = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv')
data['Month'] = pd.to_datetime(data['Month'])
data.set_index('Month', inplace=True)

# Visualizing the Time Series Data
plt.figure(figsize=(10,6))
plt.plot(data)
plt.title('Airline Passengers Over Time')
plt.xlabel('Year')
plt.ylabel('Passenger Count')
plt.show()

# Fit ARIMA model
model = ARIMA(data, order=(5,1,0))
fit_model = model.fit()

# Making Forecasts
forecast = fit_model.forecast(steps=12)

# Visualizing the Forecasts
plt.figure(figsize=(10,6))
plt.plot(data, label='Original Data')
plt.plot(pd.date_range(start=data.index[-1], periods=13, freq='M')[1:], forecast, label='Forecast')
plt.title('Airline Passengers Forecast')
plt.xlabel('Year')
plt.ylabel('Passenger Count')
plt.legend()
plt.show()

The above code calls the dataset from GitHub about the airline passengers and forecasts the passengers for the next year. Let us look at the output of the code above.

ARIMA Over Time
ARIMA Over-Time

ARIMA is a linear equation that forecasts time series data.

  • AR – Auto-Regression creates an equation based on some past data points
  • I – Integration takes into account trends in data
  • MA – Moving Average is the average of past data points of a certain frequency
ARIMA Forecast
ARIMA Forecast

This concept of ARIMA can also be applied to inventory management when there is seasonality present.

Let us now move on to Exponential smoothing.

Exponential Smoothening

Exponential Smoothing is very useful for time series forecasting. It assigns lower weights to older observations. In this article, we will use Python programming language to understand the Exponential Smoothening technique.

Recommended: Exponential Smoothing for Time Series Data

Exponential Smoothing Formula
Exponential Smoothing Formula

Let us look at the code for Exponential smoothing.

import pandas as pd
import matplotlib.pyplot as plt
from statsmodels.tsa.holtwinters import ExponentialSmoothing

# Load data
data = pd.read_csv('https://raw.githubusercontent.com/jbrownlee/Datasets/master/airline-passengers.csv')
data['Month'] = pd.to_datetime(data['Month'])
data.set_index('Month', inplace=True)

# Visualizing the Time Series Data
plt.figure(figsize=(10,6))
plt.plot(data)
plt.title('Airline Passengers Over Time')
plt.xlabel('Year')
plt.ylabel('Passenger Count')
plt.show()

# Fit Exponential Smoothing model
model = ExponentialSmoothing(data, trend='add', seasonal='add', seasonal_periods=12)
fit_model = model.fit()

# Making Forecasts
forecast = fit_model.forecast(steps=12)

# Visualizing the Forecasts
plt.figure(figsize=(10,6))
plt.plot(data, label='Original Data')
plt.plot(pd.date_range(start=data.index[-1], periods=13, freq='M')[1:], forecast, label='Forecast')
plt.title('Airline Passengers Forecast')
plt.xlabel('Year')
plt.ylabel('Passenger Count')
plt.legend()
plt.show()

Let us look at the output of Exponential Smoothing. We use the same dataset here as well.

Exponential Smoothing Pre Data
Exponential Smoothing Pre-Data
Exponential Smoothing Forecast
Exponential Smoothing Forecast

Exponential Smoothing similar to ARIMA is also used for inventory management to provide any stockouts.

Conclusion

Here you go! Now you understand how to minimize costs and optimize profits using predictive analytics techniques. In this article, we learned what Economic Order Quantity ( EOQ ) is, what ARIMA is, and also learned Exponential Smoothing techniques.

Hope you enjoyed reading it!!!

Recommended: Time Series Analysis Using Facebook Prophet – A Complete Guide