CPSC 330 Lecture 20: Survival analysis

Varada Kolhatkar

Focus on the breath!

Recap: What approach would you try? (1/3)

Identify the problem type, suggest an approach, and explain what clues guide your choice.

A. Finding the right product

Suppose the UBC Store wants its online search to match “something to keep my coffee hot on the bus” with products described as “insulated travel mugs.” You have the store’s product titles and descriptions. How could you retrieve relevant products even when the wording differs?

B. What should I watch next?

You are building a YouTube recommendation prototype. You have viewer IDs, video IDs, and records of which videos each viewer clicked, but no ratings or video descriptions. Suggest three videos a returning viewer might enjoy.

Discuss: If someone clicks a video, does that mean they liked it?

Recap: What approach would you try? (2/3)

Identify the problem type, suggest an approach, and explain what clues guide your choice.

C. How much electricity will campus need tomorrow?

UBC facilities staff have two years of hourly electricity-use records for campus buildings. They want to predict demand for each hour tomorrow to plan electricity purchases. They also have weather records and the academic calendar. What information could they use when making the forecast this evening?

D. Who visited the wildlife crossing?

A conservation team has thousands of camera-trap photos, each showing a deer, bear, or coyote, but only 600 have species labels. They want to classify the remaining photos without training a large model from scratch.

Recap: What approach would you try? (3/3)

E. When will a telecom customer leave?

A telecom company wants to predict how long a new customer will stay. You have each customer’s observed months since signup and whether they have left the service. Many customers are still subscribed when the data are collected.

  • Would you frame this as classification or regression?
  • What target would you assign to a customer who has been subscribed for eight months and is still active?
  • Would you keep that customer in the training data? Why?

Today’s goals

  • Recognize what a right-censored outcome tells us.
  • Represent outcomes using duration and event status.
  • Explain the limits of familiar classification and regression targets.
  • Interpret retention curves and model output in the lecture demo.

When will a customer leave?

Churn means a customer stops using a company’s service.

Let’s continue with the telecom company from our recap. Its retention team wants to anticipate time to churn.

  • Event: a customer leaves the service.
  • Time origin: the customer’s signup.

What information would we need to learn from past customers?

Meet the customer data

Telco Customer Churn: one row per customer. These are six actual records.

Customer tenure (months) Churn
7590-VHVEG 1 No
5575-GNVDE 34 No
3668-QPYBK 2 Yes
7795-CFOCW 45 No
9237-HQITU 2 Yes
9305-CDSKC 8 Yes

Other columns include Contract, MonthlyCharges, and InternetService.

Teaching assumption: interpret tenure as months from signup to departure or the end of observation, and Churn as whether departure was observed.

Could we use classification?

Suppose we train a classifier with Churn as the target.

  • What question would it learn to answer?
  • Does that answer how long a new customer will stay?
  • Does Churn = No mean the customer will never leave?

Will this customer churn within six months?

Classification can answer this question, but do our records tell us the answer for every customer?

What we observed Churn within six months of signup?
Customer left at month 4 Yes
Customer was still subscribed at month 8 No
Customer was still subscribed when observation ended at month 2 We don’t know

For the last customer, we don’t know what happened between months 2 and 6. We cannot label them “No.”

Could we predict tenure with regression?

Customer tenure (months) Churn
7590-VHVEG 1 No
5575-GNVDE 34 No
3668-QPYBK 2 Yes
7795-CFOCW 45 No
9237-HQITU 2 Yes
9305-CDSKC 8 Yes

Could we use tenure as the target for a regressor?

For the customer with tenure = 34 and Churn = No, do we know their final subscription length?

Same duration, different information

Customer Observed months Churn observed? What we know
A 4 Yes Churn at month 4
B 4 No Stayed beyond month 4
C 12 No Stayed beyond month 12

B and C have incomplete lifetimes, not zero churn risk.

Same cutoff, different information

Right-censoring

Right censoring: observation ends before churn is observed.

Customer B is still subscribed at month 4:

  • We know their time to churn is greater than 4 months.
  • Their eventual churn time is unknown.

Time-to-event problems

The same structure appears in many settings:

  • Time until a customer leaves a subscription service
  • Time until death in a patient study
  • Time until equipment fails
  • Duration of unemployment until finding a job
  • Waiting time until surgery

We care about how long until a defined event occurs, but observation may end before we see it.

Approach 1: Use only customers who churned

Keep rows with Churn = Yes and fit a regressor to their completed subscription lengths.

churned = train.loc[train["Churn"].eq("Yes")]
# Fit a regressor using churned customers only?

Now every retained row has an observed event time. What did we lose?

Your turn: discard active customers?

Customer B is still subscribed when observation ends at month 4.

If we remove their row:

  • What useful information have we discarded?
  • If we estimate average time to churn using only customers who have already left, would you expect it to be too short or too long? Why?

Approach 2: Treat every tenure as final

Keep all rows and use recorded tenure as the regression target, regardless of Churn.

Customer Observation Regression target
A Left at month 4 4
B Still subscribed at month 4 4

We have a number for everyone. Does it mean the same thing?

Your turn: did B leave at month 4?

For customer B, setting the target to 4 claims that:

    1. They left at month 4.
    1. They stayed at least 4 months; their departure time is unknown.
    1. They will never leave.

Which claim does an ordinary regression target encode? Which claim does the record support?

Why the shortcuts fail

  • Events only: discards censored customers’ evidence and selects customers whose departures were observed.
  • Follow-up as lifetime: invents departures when observation ends.

Changing regressors or tuning hyperparameters does not repair the targets.

Customer B tells us \(T>4\), not \(T=4\). Discarding B loses that information.

Here, \(T\) is months from signup to churn.

Approach 3: Survival analysis

Survival analysis models time to an event while accounting for censoring.

  • Use completed durations when departures are observed.
  • Use lower bounds when observation ends before a departure is observed.
  • Estimate probabilities of remaining subscribed across time.

In the demo:

  • Kaplan–Meier: estimate overall and group retention curves.
  • Cox regression: relate several customer features to time to churn.

Preserve the two-part outcome

Customer tenure event What we know
A 4 1 Churn at month 4
B 4 0 Stayed beyond month 4
C 12 0 Stayed beyond month 12
  • event = 1: churn observed (Churn = Yes).
  • event = 0: churn not observed (Churn = No).

Duration and event status form the outcome, not the predictors.

What should we predict instead?

Let \(T\) be months from signup to churn. The survival function is:

\[S(t)=P(T>t)\]

If \(S(12)=0.8\):

  • 80% stay beyond 12 months from signup.
  • 20% churn within 12 months of signup.

This is different from staying another 12 months for a customer who is already subscribed.

Lecture demo

Open the lecture 20 demo

  1. Inspect and clean: can a clean column still be an unsuitable predictor?
  2. Overall retention: estimate and read a Kaplan–Meier curve.
  3. Contract groups: compare curves and discuss what differences mean.
  4. Several features: fit a Cox model and interpret coefficient signs.

If time permits: compare predicted survival curves for different contract profiles.

After the demo: what would you tell the team?

The contract-group curves differ, and the Cox model associates some features with faster churn.

  • What does a survival curve’s value at 12 months mean?
  • Do the group curves show that changing a contract would prevent churn?
  • Is predicted churn risk enough to decide whom to contact?

What did we learn?

  • Censored records contain useful partial information.
  • Preserve duration and event status.
  • Familiar targets can discard information or invent event times.
  • Survival curves describe retention across durations.
  • Group comparisons and model predictions do not establish intervention effects.

Define the event, time origin, and prediction time first.

Further reading