# Learning Optimal Pricing Policies for Grocery Stores

In this example, we aim to learn an optimal pricing policy for grocery stores from observational data. We use a publicly available retail dataset ("The Complete Journey" from dunnhumby) that contains household-level transactions over two years. We illustrate how Optimal Prescriptive Trees and Optimal Policy Trees can be used to inform interpretable pricing policies.

*Why not just solve a revenue optimization model?*

Traditionally pricing is done through a revenue optimization approach where models are built to predict demand as a function of price, and then an optimization model is solved to maximize the revenue and arrive at the optimal price. This approach generally has practical limitations: either the demand estimation is too broad and not personalized to a particular grocery store, or there is not enough data from a store to estimate demand. We will see from this example that these approaches for interpretable policy learning leverage data from all grocery stores and use intrinsic features such as customer demographics to cluster stores and make optimal pricing recommendations. The resulting policy does not rely on the availability of demand information for a particular store, and moreover will give meaningful reasons for each pricing recommendation.

## Preparing the dataset

A previous study used the same dataset with a greedy, tree-based approach and found a 67% increase in revenue for strawberries. We are interested in evaluating our Optimal Trees-based approaches to see if they provide an additional lift.

We focus on strawberries as the item of interest, and follow the same data preparation process as this paper. We process the data so that each row of the resulting dataset is a shopping trip, with household characteristics, the price of the item, and whether that item was purchased as the outcome.

For brevity, we omit the details of this data processing, and instead start from the prepared dataset. For full reproducibility, the detailed data preparation script is available here.

First, we load the prepared data and convert the ordinal and mixed columns:

```
using CSV, DataFrames
using CategoricalArrays
using Statistics
df = CSV.read("grocery_pricing.csv", DataFrame)
variable_dict = Dict(
:AGE_DESC => ["19-24", "25-34", "35-44", "45-54", "55-64", "65+"],
:MARITAL_STATUS_CODE => [],
:INCOME_DESC => ["Under 15K", "15-24K", "25-34K", "35-49K", "50-74K",
"75-99K", "100-124K", "125-149K", "150-174K", "175-199K",
"200-249K", "250K+"],
:HOMEOWNER_DESC => [],
:HH_COMP_DESC => [],
:HOUSEHOLD_SIZE_DESC => ["1", "2", "3", "4", "5+"],
:KID_CATEGORY_DESC => ["1", "2", "3+"]
)
for (var, levels) in variable_dict
if var == :KID_CATEGORY_DESC
df[!, var] = IAI.make_mixed_data(df[!, var], levels)
elseif isempty(levels)
df[!, var] = categorical(df[!, var])
else
df[!, var] = categorical(df[!, var], ordered=true, levels=levels)
end
end
df = df[completecases(df), :]
```

```
97295×13 DataFrame
Row │ household_key BASKET_ID DAY price outcome AGE_DESC MARITA ⋯
│ Int64 Int64 Int64 Float64 Int64 Cat… Cat… ⋯
───────┼────────────────────────────────────────────────────────────────────────
1 │ 216 27008817920 3 2.99 0 35-44 U ⋯
2 │ 2324 27008841762 3 2.99 0 35-44 A
3 │ 2324 27008841880 3 2.99 0 35-44 A
4 │ 2305 27008850617 3 2.99 0 45-54 B
5 │ 2110 27009082349 3 2.99 1 35-44 A ⋯
6 │ 432 27009271101 3 2.99 0 19-24 U
7 │ 304 27009304297 3 2.99 0 25-34 U
8 │ 1929 27021022215 4 2.99 0 35-44 B
⋮ │ ⋮ ⋮ ⋮ ⋮ ⋮ ⋮ ⋱
97289 │ 1823 42289907120 711 2.99 0 45-54 A ⋯
97290 │ 1627 42289907311 711 2.99 0 25-34 U
97291 │ 371 42289910739 711 2.99 1 35-44 A
97292 │ 647 42289919750 711 2.99 0 35-44 A
97293 │ 647 42289919785 711 2.99 0 35-44 A ⋯
97294 │ 761 42289921056 711 2.99 0 25-34 A
97295 │ 1369 42302712189 711 2.99 0 25-34 B
7 columns and 97280 rows omitted
```

Next, we create the features, treatments (prices), and outcomes (revenue, which is prices times whether the product was purchased or not). We then separate the data into training and testing.

```
seed = 2345
X = df[:, collect(keys(variable_dict))]
y = df.outcome
t = df.price
(train_X, train_t, train_y), (test_X, test_t, test_y) = IAI.split_data(
:policy_maximize, X, t, y, train_proportion=0.5, seed=seed)
```

## Optimal Prescriptive Tree approach

We first take an Optimal Prescriptive Tree approach. Optimal Prescriptive Trees take in the features, treatments, and outcomes to learn the best prescription to maximize revenue. The trees automatically estimate what would have happened if a different price were assigned, so we do not need to worry about explicitly estimating these counterfactual outcomes.

We train the prescriptive tree with the prices discretized into 50-cent increments in the same fashion as the paper:

```
prescriptive_grid = IAI.GridSearch(
IAI.OptimalTreePrescriptionMaximizer(
random_seed=seed,
),
max_depth=1:6,
)
train_y_revenue = train_y .* train_t
train_t_discrete = round.(train_t .* 2, digits=0) ./ 2
IAI.fit!(prescriptive_grid, train_X, train_t_discrete, train_y_revenue)
```