User behaviour modelling and prediction
How to build simple mathematical and machine-learning models to describe and predict user actions (engagement, churn, next action) on social platforms using features from activity and network structure.
Drafted with Aria, reviewed by the AiCanCode.org team · Updated . How we write and check our notes. Spotted an error? Use Give Feedback at the bottom of the page.
Why it matters
Two or three sentences: Engineers use user behaviour modelling to make products that keep users engaged and safe. Predicting actions such as leaving the service (churn), making a purchase, or sharing content lets engineers trigger timely interventions (notifications, offers, moderation), allocate resources, and evaluate design changes.
Key ideas
Start from first principles and simple definitions. Define each term before use.
- User behaviour: observable actions by a user on a platform (for example: logins, posts, clicks). These are measured as timestamps, counts, categorical actions or sequences.
- Feature: a measurable attribute derived from raw data (for example: daily active minutes, number of friends). Features are the inputs to learning or probabilistic models.
- Label: the quantity we want to predict (for example: churn = yes/no, next action = click/post). Labels are treated as outputs in supervised learning.
- Supervised learning: a family of methods that learn a mapping from features to labels using labelled examples.
- Markov property: the future state depends only on the current state, not the full past. A Markov chain is a stochastic process satisfying this.
- Logistic regression: a supervised model that estimates probability of a binary label using a linear function of features passed through the logistic (sigmoid) function.
- Cross-entropy loss (also called log loss): the negative log-likelihood used to train probabilistic classifiers like logistic regression.
- Hawkes process: a point process where each event temporarily increases the rate of future events (self-exciting). Useful to model bursts of activity.
Assumptions commonly used and must be stated in exam answers:
- For logistic regression: assume i.i.d. training examples and a correctly specified conditional model with log-odds linear in features, logit p(y=1|x) = w·x + b; no linear separability is required (in fact, perfect separability can make the MLE diverge without regularization).
- For Markov chains: assume discrete state space and that the Markov property holds (first-order unless stated otherwise). Assume time-homogeneous transitions (constant P) unless explicitly stated otherwise.
- For Hawkes or survival models: assume stationarity or specified kernel forms; specify if parameters are time-invariant.
Derivations universities ask for (step-by-step): logistic regression parameter update using maximum likelihood (binary case):
- Define the model: probability p(x) = sigmoid(z) where z = w·x + b.
- Write likelihood for N independent examples: L = ∏_{i=1..N} p(x_i)^{y_i} (1 - p(x_i))^{1-y_i}.
- Take log to get log-likelihood: ℓ = Σ [ y_i log p(x_i) + (1-y_i) log(1-p(x_i)) ].
- Substitute p(x_i) = 1/(1+e^{-z_i}) and compute gradient wrt w and b: ∇_w ℓ = Σ (y_i - p(x_i)) x_i, ∂ℓ/∂b = Σ (y_i - p(x_i)).
- Use gradient ascent or use negative log-likelihood and gradient descent.
Markov chain steady-state derivation students are asked:
- Let P be transition matrix where P_{ij} = Pr(X_{t+1}=j | X_t=i).
- Stationary distribution π solves π = π P, with Σ_i π_i = 1.
- Solve linear system (P^T - I) π^T = 0 with normalization constraint.
What to draw and label (diagrams):
- For a 3-state Markov chain example, draw three circles labelled S1, S2, S3. Draw arrows i→j with labels P_{ij} (e.g. 0.5). Also draw self-loop arrows for P_{ii}. For stationary distribution, annotate each state with π_i.
- For logistic regression sketch: draw a two-dimensional feature plane with a straight decision boundary line. Label positive side and negative side, and annotate that log-odds is linear in features.
Note on related topics: features derived from network structure (degree, centrality) come from Node centrality measures and Graph concepts; cascade or influence modelling connects to Information cascades and Independent cascade models, which are separate topics.
Formulas
sigmoid(z) = 1 / (1 + e^{-z}) — z is unitless (z = w·x + b). Applies when modelling probabilities from scores.
p(y=1 | x) = sigmoid(w·x + b) — p is probability (unitless), w is weight vector, x is feature vector (units depend on features), b is bias (unitless). Applies to binary classification.
log-likelihood L = Σ_{i=1..N} [ y_i log p(x_i) + (1-y_i) log(1-p(x_i)) ] — L is unitless. Applies under independent examples assumption.
gradient ∇_w L = Σ_{i=1..N} (y_i - p(x_i)) x_i — units: each component is feature unit times unitless. Used for gradient ascent.
P_{ij} = Pr(X_{t+1}=j | X_t=i) — unitless probability. Applies to discrete-time Markov chains.
π = π P and Σ_i π_i = 1 — π is stationary distribution (unitless). In a finite irreducible chain π is unique; aperiodicity is required for convergence to π from any initial distribution.
AUC = probability classifier ranks random positive above random negative — unitless metric between 0 and 1, used for evaluation of binary classifiers.
Notes: Probabilities are dimensionless; model parameters inherit units so that z = w·x + b is unitless (w has reciprocal units of the corresponding feature, b is unitless). When using time as a feature, use seconds (s) or hours (h) consistently.
Worked examples
Example 1 — Logistic regression numerical (predict churn yes/no) Given: features for one user x = [degree = 10 friends, daily_minutes = 30 min]. Use units: friends (count), minutes (min). Model weights: w = [0.05 per friend, -0.02 per minute], bias b = -1.5. Label y=1 means churn.
- Compute z = w·x + b = 0.05*10 + (-0.02)*30 + (-1.5) = 0.5 - 0.6 - 1.5 = -1.6.
- Compute probability p = sigmoid(z) = 1 / (1 + e^{1.6}) ≈ 1 / (1 + 4.953) ≈ 0.168. Final answer: probability of churn ≈ 0.168 (16.8%).
Example 2 — Markov chain next-action prediction States: S1 = browse, S2 = post, S3 = logout. Transition matrix P (rows = current state, columns = next state): P = [[0.6, 0.3, 0.1], // from browse [0.2, 0.7, 0.1], // from post [0.5, 0.0, 0.5]] // from logout Question: If current state is browse, probability of being in logout after two steps?
- Starting distribution v0 = [1,0,0]. After one step v1 = v0 P = first row = [0.6,0.3,0.1].
- After two steps v2 = v1 P. Compute component for logout (index 3): v2_3 = 0.60.1 + 0.30.1 + 0.1*0.5 = 0.06 + 0.03 + 0.05 = 0.14. Final answer: probability 0.14 (14%).
Example 3 — Model answer to an exam-style question (short derivation): Question: Derive the gradient of binary cross-entropy loss for logistic regression and explain one reason to prefer cross-entropy over 0/1 loss. Model answer: see derivation under Key ideas (steps 1–5). Final gradient expression for the binary cross-entropy loss (negative log-likelihood) is ∇_w J = Σ (p(x_i) - y_i) x_i. Cross-entropy is preferred because it is differentiable and provides informative gradients for optimization; 0/1 loss is not differentiable and yields no gradient information for near-boundary updates.
Common mistakes
- Using stationary-distribution formula π = π P without checking chain is irreducible and aperiodic (uniqueness conditions).
- Mixing time units inconsistently when modelling rates (seconds vs hours) — always state and keep consistent.
- Treating events as independent when they are bursty (should consider Hawkes-like models).
- For exams: not stating assumptions (independence, Markov property), which costs marks.
For your exam
Kinds of questions this topic produces:
- Definitions: define user behaviour, feature, label, Markov chain, logistic regression, Hawkes process.
- Short notes: compare Markov chains vs sequence models; when to use regression vs sequence models.
- Derivations: derive logistic regression gradient or stationary distribution equation π = π P.
- Numericals: compute probability from logistic model, compute multi-step transition probabilities, compute eigenvector stationary distribution for small P.
- Diagrams: draw a small Markov chain and label transition probabilities; draw decision boundary for a 2D classifier. What a full-marks answer includes:
- Clear definitions with bolded terms, assumptions stated, correct derivation steps, units for any time or count features, and a final numeric answer with units where applicable.
Quick check
- What is the Markov property in one sentence?
- Write the logistic (sigmoid) function.
- In a 2-step Markov chain, if P_{12} = 0.5 and P_{23} = 0.2, can you compute Pr(1→3 in two steps) without other entries? Why or why not?
Answers: 1) The Markov property says the next state depends only on the current state, not the full past. 2)
sigmoid(z) = 1 / (1 + e^{-z}). 3) No — you need P_{11}, P_{12}, P_{13}, P_{23}, and P_{33}: Pr(1→3 in two steps) = P_{11}P_{13} + P_{12}P_{23} + P_{13}P_{33}, so the missing entries prevent calculation.
Notes about uncertainty: I have stated standard formulas and simple numerical examples. Please verify the following specifics before publishing: see the "verify" list. Verify: (a) any transition matrices have rows that sum to 1; (b) whether time-homogeneous Markov assumptions are appropriate; (c) units are consistent for any features involving time or counts; (d) whether class imbalance requires metrics beyond accuracy (e.g., AUC, PR); (e) whether regularization (L2/L1) is used in logistic regression, which modifies the loss and gradients.
Finished this topic? Mark it so your progress, study plan and readiness keep up.
Stuck on something here?