Department of Information Technology
Experiment 05
Number
Problem
Predicting Customer Behavior Using Markov Chains
Statement
A retail company aims to predict customer behavior based on past
interactions. Customers transition between different states such as browsing
products, adding items to the cart, making a purchase, or abandoning the
site.
To achieve this, we model customer behavior as a Markov Chain with the
following states:
● Browsing (B): The customer is viewing products.
● Adding to Cart (C): The customer adds items to the cart.
● Purchase (P): The customer completes a purchase.
● Abandon (A): The customer leaves without purchasing.
A transition matrix defines the probabilities of moving between these states.
The goal is to analyze customer pathways, compute long-term behavior
using steady-state probabilities, and visualize the transitions using a Markov
model.
This analysis can help businesses optimize marketing strategies, improve
customer retention, and increase conversions.
Resources Hardware: Desktop/Laptop Software:Google Collab
/
Apparatus
Required
Code: import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
def compute_steady_state(T):
"""
Computes the steady-state probability vector for a
given transition matrix T.
"""
# Number of states
n = T.shape[0]
# Solve the equation πT = π, subject to sum(π) = 1
A = np.vstack([T.T - np.eye(n), np.ones(n)])
b = np.zeros(n+1)
b[-1] = 1 # Ensuring sum of probabilities is 1
steady_state = np.linalg.lstsq(A, b, rcond=None)[0]
return steady_state
# Transition matrix from the problem statement
T = np.array([
[0.6, 0.3, 0.05, 0.05], # Browsing (B)
[0.2, 0.5, 0.3, 0.0], # Add to Cart (C)
[0.0, 0.0, 1.0, 0.0], # Purchase (P) (Absorbing
state)
[0.0, 0.0, 0.0, 1.0] # Abandon (A) (Absorbing
state)
])
steady_state = compute_steady_state(T)
# Print the steady-state probabilities
states = ["Browsing (B)", "Add to Cart (C)", "Purchase
(P)", "Abandon (A)"]
print("Steady-State Probabilities:")
for state, prob in zip(states, steady_state):
print(f"{state}: {prob:.4f}")
# Visualization of the Markov Chain
def draw_markov_chain(T, states):
G = nx.DiGraph()
for i in range(len(states)):
for j in range(len(states)):
if T[i, j] > 0: # Only show transitions
with probability > 0
G.add_edge(states[i], states[j],
weight=T[i, j])
pos = nx.spring_layout(G)
labels = {(states[i], states[j]): f"{T[i, j]:.2f}"
for i in range(len(states)) for j in range(len(states))
if T[i, j] > 0}
plt.figure(figsize=(8, 6))
nx.draw(G, pos, with_labels=True,
node_color='lightblue', edge_color='gray',
node_size=3000, font_size=10)
nx.draw_networkx_edge_labels(G, pos,
edge_labels=labels)
plt.title("Markov State Transition Diagram")
plt.show()
draw_markov_chain(T, states)
Output
`
Business Insights & Applications
Using the Markov model, we can extract insights and apply strategies
to improve customer behavior:
1. Customer Retention Strategies
○ If the probability of abandonment is high, introduce
discounts or reminders to retain customers.
2. Marketing Optimization
○ Identify the best way to transition customers from
Browsing → Purchase efficiently.
○ Use targeted ads, promotions, or recommendations to
increase conversion rates.
3. A/B Testing for Web Design
○ Experiment with UI/UX changes to observe their impact
` on Markov transition probabilities.
○ Improve design elements that encourage purchases and
reduce abandonment.
Conclusion The Markov model helps predict customer behavior, highlighting key
areas for improvement. High retention loss suggests many users drop
off before purchasing, requiring better engagement strategies. A low
conversion rate indicates the need for optimized marketing and UX
enhancements. Businesses can use targeted marketing, discounts,
and A/B testing to improve customer retention and sales.