Recency features

Fraud Detection in R

Tim Verdonck

Professor Data Science at KU Leuven

Authentication method vs time

recency_part1

Fraud Detection in R

Large time interval

recency_part2

Fraud Detection in R

Small time interval

recency_part3

Fraud Detection in R

Zero recency

recency_part4

Fraud Detection in R

Anomalous behavior

recency_part5

Fraud Detection in R

Definition

  • $recency=exp(-\gamma\cdot t)=e^{-\gamma t}$

    • $t$ = time-interval between two consecutive events of the same type
    • $\gamma$ = tuning parameter, typically close to 0 (e.g. 0.01, 0.02, 0.05)
    • $0 \leq recency \leq 1$
Fraud Detection in R

Recency vs time

recency_vs_time

Fraud Detection in R

How to choose parameter $\gamma$?

  • (1) choose when recency is small (e.g. 0.01) after certain amount ($t$) of time
  • (2) calculate $\gamma=-log(recency)/t$

  • Example: set $\gamma$ such that recency = 0.01 after $t$ = 180 days

    gamma <- -log(0.01)/180
    print(gamma)
    
0.02558428
Fraud Detection in R

Recency feature in R (step 1)

recency_fun <- function(t, gamma, auth_cd, freq_auth) {

n_t <- length(t) if (freq_auth[n_t] == 0) { recency <- 0 # recency = 0 when frequency = 0
} else { time_diff <- t[1] - max(t[2:n_t][auth_cd[(n_t-1):1] == auth_cd[n_t]]) # time-interval = current time - time of previous transfer with same auth_cd
recency <- exp(-gamma * time_diff) } return(recency)
}
Fraud Detection in R

Recency feature in R (step 2)

(1) Choose value for $\gamma$

gamma <- -log(0.01)/180 # = 0.0256

(2) Use rollapply(), group_by() and mutate()

library(dplyr) # needed for group_by() and mutate()
library(zoo) # needed for rollapply()
trans <- trans %>% group_by(account_name) %>%
    mutate(rec_auth = rollapply(timestamp,
                               width = list(0:-length(transfer_id)),
                               partial = TRUE,
                               FUN = recency_fun,
                               gamma, authentication_cd, freq_auth))
Fraud Detection in R
   account_name timestamp authentication_cd rec_auth fraud_flag
1           Bob     44.25              AU02    0.000          0
2         Alice     54.12              AU03    0.000          0
3           Bob     57.45              AU04    0.000          0
4           Bob     64.29              AU02    0.599          0
5         Alice     64.29              AU03    0.771          0
6           Bob     64.29              AU02    1.000          0
7         Alice     70.25              AU03    0.859          0
8           Bob     70.25              AU02    0.859          0
9         Alice     74.08              AU01    0.000          0
...         ...       ...               ...      ...        ...
37          Bob    407.17              AU02    0.002          0
38        Alice    420.17              AU03    0.717          0
39          Bob    441.34              AU03    0.000          1
40        Alice    443.24              AU04    0.000          1
Fraud Detection in R

Features based on time, frequency and recency

features1

Fraud Detection in R

Let's practice!

Fraud Detection in R

Preparing Video For Download...