Implementing Dynamic pricing strategy in python — part 2

Induraj
5 min readFeb 24, 2023

--

In part 1, we saw an implementation of a machine learning algorithm to predict the price of the product. Here we will take a turn to look into the implementation of a dynamic pricing strategy using time series forecasting.

This algorithm uses historical data to forecast future demand and adjust prices accordingly. This approach takes into account seasonal patterns and trends to optimize pricing.

Facebook Prophet library

In this implementation, we use the Facebook Prophet library to predict the price of products.

  • Facebook Prophet is a time series forecasting library developed by Facebook’s Core Data Science team.
  • Prophet implements a Bayesian additive regression model, which models the trend and seasonality of the time series using a generalized additive model (GAM).
  • Handles common challenges that arise with time series data such as missing values, seasonality, outliers and changes in trend.
  • Handles holidays and events that can have an impact on time series data.
  • Easy visualization of time series data and provides a simple API for fitting and making predictions with the model.
  • It has been used in various domains such as finance, marketing, and supply chain management to forecast sales, inventory, and demand.

1. Implementation to Predict future sales of product:

In this script, we first load the Brazilian e-commerce dataset Link to dataset(click here) and preprocess it by dropping irrelevant columns, handling missing data, and converting categorical variables to numerical values. We then aggregate the sales data by date and use it to train a time series forecasting model (using the Prophet library). We create a future data frame for forecasting and use the trained model to predict prices for each day in the future. We extract the forecasted prices for the next year and plot them.

We then use the forecasted prices to set dynamic prices for today’s date. In this example, we set the dynamic price to be 10% higher than the mean forecasted price for the next year.

Importing libraries

import pandas as pd
import numpy as np
from fbprophet import Prophet
import matplotlib.pyplot as plt
from scipy import stats

Joining necessary csv together

order_items = pd.read_csv("/content/olist_order_items_dataset.csv")
orders = pd.read_csv("/content/olist_orders_dataset.csv")
order_payments = pd.read_csv("/content/olist_order_payments_dataset.csv")
products = pd.read_csv("/content/olist_products_dataset.csv")
customers = pd.read_csv("/content/olist_customers_dataset.csv")
sellers = pd.read_csv("/content/olist_sellers_dataset.csv")
product_category_translation = pd.read_csv("/content/product_category_name_translation.csv")

# Merge the datasets
merged = order_items.merge(orders, on='order_id') \
.merge(order_payments, on=['order_id']) \
.merge(products, on='product_id') \
.merge(customers, on='customer_id') \
.merge(sellers, on='seller_id') \
.merge(product_category_translation, on='product_category_name')

# Save the consolidated dataset to a CSV file
merged.to_csv('/content/brazilian_ecommerce_dataset.csv', index=False)

Data preprocessing:

# Load the dataset
df = pd.read_csv('/content/brazilian_ecommerce_dataset.csv')

# Preprocess the data
# Drop irrelevant columns and handle missing data
df = df.drop(['seller_id', 'freight_value'], axis=1)
df = df.dropna()

# For simplicity taking only 10k rows of data
df = df[:10000]

# Convert categorical variables to numerical values
df = pd.get_dummies(df, columns=['product_category_name', 'customer_state'])

Applying Facebook prophet:

# Train a time series forecasting model for each product & save those models
forecast_models = {}
unique_product_ids = df['product_id'].unique()

# For demonstration & simplicity taking only 200 products
unique_product_ids = unique_product_ids[0:200]
for product in unique_product_ids:
product_df = df[df['product_id'] == product]
# Aggregate sales data by date
product_df['order_purchase_date'] = pd.to_datetime(product_df['order_purchase_timestamp']).dt.date
sales_data = product_df.groupby(['order_purchase_date']).agg({'price': 'sum'}).reset_index()
sales_data = sales_data.rename(columns={'order_purchase_date': 'ds', 'price': 'y'})
if len(sales_data)>=50:
# Train a time series forecasting model
m = Prophet()
m.fit(sales_data)
forecast_models[product] = m

Predicting the price of products:

# Use the forecast models to predict prices for each product

for product in forecast_models:
m = forecast_models[product]
future = m.make_future_dataframe(periods=365)
forecast = m.predict(future)
future_prices = forecast[['ds', 'yhat']].tail(365)
mean_price = future_prices['yhat'].mean()
dynamic_price = mean_price * 1.1 # set dynamic price 10% higher than mean price

print("Product:", product)
print("Dynamic price:", dynamic_price)

2. Implementation to predict the average sales of next year:

once the data is preprocessed as in the previous section, take the dataset and apply the following.

# Aggregate sales data by date
df['order_purchase_date'] = pd.to_datetime(df['order_purchase_timestamp']).dt.date
sales_data = df.groupby(['order_purchase_date']).agg({'price': 'sum'}).reset_index()
sales_data = sales_data.rename(columns={'order_purchase_date': 'ds', 'price': 'y'})

# Remove outliers from sales data using Z-score
sales_data = sales_data[(np.abs(stats.zscore(sales_data['y'])) < 1.5)]

# Train a time series forecasting model
m = Prophet()
m.fit(sales_data)

# Create a future dataframe for forecasting
future = m.make_future_dataframe(periods=365)

# Use the model to make price predictions for each day in the future
forecast = m.predict(future)

# Extract the forecasted prices
forecasted_prices = forecast[['ds', 'yhat']].tail(365)

Plotting:

# Plot the forecasted prices
fig = m.plot(forecast)
plt.scatter(sales_data['ds'], sales_data['y'], color='red', label='Actual')
plt.plot(forecast['ds'], forecast['yhat'], color='blue', label='Forecast')
plt.title('Actual vs Forecasted Daily Sales')
plt.xlabel('Date')
plt.ylabel('Total Sales (BRL)')
plt.legend()
plt.show()

As we see in the below figure, the forecast made through fbprophet is not as good as we expect. The model shows more errors. However Fbprobhet performance can be increased by following the suggestions below.

3. Improving the performance of the fbprophet library:

check this link to know more about the parameters in fbprobhet library. (link-1) & (link-2)

  1. Increase the number of iterations: By default, the fbprophet algorithm runs for 1000 iterations, but this can be increased to improve the accuracy of the model. However, this may also increase the time it takes to train the model.
  2. Add additional regressors: If you have access to additional data, such as weather or holiday information, you can add these as regressors to the model to help improve its accuracy.
  3. Adjust seasonality parameters: fbprophet models use Fourier series to model seasonality, and the number of Fourier terms used to model seasonality can be adjusted. Increasing the number of Fourier terms can help capture more complex seasonal patterns, but may also increase the risk of overfitting.
  4. Adjust changepoint parameters: fbprophet models use changepoints to model changes in trends over time. The number of changepoints and the distance between them can be adjusted. Adding more changepoints can improve the model's ability to capture sudden changes in trends, but may also increase the risk of overfitting.
  5. Use a custom seasonality model: fbprophet allows you to specify your own custom seasonalities, which can help improve the model's accuracy if the default seasonalities do not fit your data well.
  6. Use cross-validation: fbprophet provides a built-in cross-validation function that can be used to evaluate the model's performance and help tune its parameters.
  7. Remove outliers: Outliers in the data can negatively impact the accuracy of the model. Removing outliers or using a robust regression algorithm can help improve the model’s performance.
  8. Normalize the data: If the data has widely varying scales, normalizing it can help the model perform better.
  9. Increase the number of trees in random forest regression: If using fbprophet with random forest regression, increasing the number of trees can help improve the accuracy of the model. However, this may also increase the time it takes to train the model.

--

--