How can a likelihood separate crimes from detection?

Simulate hidden crimes and imperfect detection, write the likelihood of a detected crime as a product of two probabilities, and estimate both logit models with scipy.optimize.minimize.

After this lesson, you can estimate the probability of a crime and the probability of its detection from data that show only the detected crimes.

What does a detected crime tell me?

A crime is observed only when it occurs and is detected, so the probability of an observed crime is the product of the two probabilities, and an observed zero mixes lawful cases with undetected crimes. In 10,000 simulated cases of possible insider trading in a made-up stock, 36.76% involve a crime, but only 15.70% involve a detected crime.

\[ P(Y_i = 1) = p_i\,q_i, \qquad p_i = \frac{1}{1 + e^{-(a + b x_i)}}, \qquad q_i = \frac{1}{1 + e^{-(c + d s_i)}} \]

Here \(Y_i\) is 1 for a detected crime in case \(i\), \(p_i\) the probability of a crime, which depends on the trader’s incentive \(x_i\), and \(q_i\) the probability that a crime is detected, which depends on the later scrutiny \(s_i\). The simulation sets \(a = -1\), \(b = 1.2\), \(c = -0.5\) and \(d = 1.1\).

expit computes the logistic function \(1/(1 + e^{-z})\). rng.uniform(-3, 3, 10000) draws the two scores, and rng.random(10000) < probability draws each outcome with its probability. & keeps the crimes that are also detected. The estimation uses only incentive, scrutiny and observed.

The following box simulates the cases.

import numpy as np
import pandas as pd
from scipy.special import expit
from scipy.optimize import minimize

rng = np.random.default_rng(42)
cases = pd.DataFrame({"incentive": rng.uniform(-3, 3, 10000), "scrutiny": rng.uniform(-3, 3, 10000)})
crime = rng.random(10000) < expit(-1 + 1.2 * cases["incentive"])
detected = rng.random(10000) < expit(-0.5 + 1.1 * cases["scrutiny"])
cases["observed"] = crime & detected
print(round(crime.mean(), 4), round(cases["observed"].mean(), 4))
0.3676 0.157

How do I estimate both probabilities?

Maximum likelihood chooses the four coefficients that make the observed outcomes most probable, and the fitted values, −0.91, 1.20, −0.61 and 1.06, are close to the true ones. The model can tell the two parts apart because the incentive enters only the crime model and the scrutiny only the detection model, and both are logit. Without these restrictions, multiplying every crime probability by 1.05 and dividing every detection probability by 1.05 would leave every product unchanged.

\[ \mathcal{L}(\theta) = -\frac{1}{N}\sum_{i=1}^{N}\Big[Y_i \ln(p_i q_i) + (1 - Y_i)\ln(1 - p_i q_i)\Big] \]

Here \(\theta = (a, b, c, d)\), \(N\) is the number of cases, and \(\mathcal{L}\) the negative mean log-likelihood, which minimize makes as small as possible.

probabilities returns the crime and detection probabilities for a set of coefficients. In negative_log_likelihood, np.clip keeps the products away from 0 and 1, so the logarithms stay finite, np.where takes \(\ln(p_i q_i)\) for a detected crime and \(\ln(1 - p_i q_i)\) otherwise, and np.log1p(-x) computes \(\ln(1 - x)\). minimize(..., method="BFGS") starts from four zeros, and result.x holds the fitted coefficients.

The following box estimates the coefficients.

def probabilities(theta):
    crime_intercept, crime_slope, detection_intercept, detection_slope = theta
    return expit(crime_intercept + crime_slope * cases["incentive"]), expit(detection_intercept + detection_slope * cases["scrutiny"])

def negative_log_likelihood(theta):
    crime_probability, detection_probability = probabilities(theta)
    observed_probability = np.clip(crime_probability * detection_probability, 1e-12, 1 - 1e-12)
    return -np.where(cases["observed"], np.log(observed_probability), np.log1p(-observed_probability)).mean()

result = minimize(negative_log_likelihood, np.zeros(4), method="BFGS")
print(result.success, round(result.fun, 4))
print(result.x.round(2))
True 0.2872
[-0.91  1.2  -0.61  1.06]

result.success says that the algorithm met its stopping rule, which leaves open whether the model is right. In real investigations, scrutiny that responds to suspicion would break the restriction that it affects only detection.

What do the fitted probabilities show?

The fitted coefficients give each case its own crime and detection probabilities. Case 2 has a higher fitted crime probability than case 0, 0.843 against 0.745, but a much lower detection probability, 0.076 against 0.688, so it is less likely to show up as a detected crime.

The following box prints the fitted probabilities of cases 0 and 2.

crime_fit, detection_fit = probabilities(result.x)
fitted = pd.DataFrame({"crime": crime_fit, "detection": detection_fit, "observed": crime_fit * detection_fit})
print(fitted.loc[[0, 2]].round(3))
   crime  detection  observed
0  0.745      0.688     0.513
2  0.843      0.076     0.064

Your turn

Print the fitted probabilities of cases 6 and 7. You should see similar crime probabilities, 0.727 and 0.761, and detection probabilities of 0.220 and 0.919.

.loc[[6, 7]] selects the two cases.

print(fitted.loc[[6, 7]].round(3))
   crime  detection  observed
6  0.727      0.220      0.16
7  0.761      0.919      0.70