import numpy as np
from pprint import pprint
from crabbymetrics import Logit
np.set_printoptions(precision=4, suppress=True)Logit Example
This page mirrors examples/logit_example.py.
1 Fit A Binary Logit Model
rng = np.random.default_rng(2)
n = 800
k = 4
beta = np.array([1.2, -0.8, 0.4, -1.1])
intercept = 0.3
x = rng.normal(size=(n, k))
logits = intercept + x @ beta
probs = 1.0 / (1.0 + np.exp(-logits))
y = rng.binomial(1, probs).astype(np.int32)
model = Logit(alpha=1.0, max_iterations=200)
model.fit(x, y)
print("true intercept:", intercept)
print("true coef:", beta)
pprint(model.summary())true intercept: 0.3
true coef: [ 1.2 -0.8 0.4 -1.1]
{'coef': array([ 1.1245, -0.785 , 0.4493, -1.1894]),
'coef_se': array([0.1058, 0.1029, 0.0934, 0.1113]),
'intercept': 0.2866745643642197,
'intercept_se': 0.09089945628323781,
'vcov': array([[ 0.0083, 0.0014, -0.001 , 0.0007, -0.0008],
[ 0.0014, 0.0112, -0.0028, 0.0017, -0.004 ],
[-0.001 , -0.0028, 0.0106, -0.0004, 0.0031],
[ 0.0007, 0.0017, -0.0004, 0.0087, -0.0016],
[-0.0008, -0.004 , 0.0031, -0.0016, 0.0124]])}
2 Prediction surfaces
predict_lin() returns the latent index \(x_i'\hat\beta + \hat\alpha\). predict() applies the inverse-logit link to return fitted probabilities. predict_label() thresholds those probabilities into class labels.
check = x[:8]
out = {
"eta": model.predict_lin(check),
"probability": model.predict(check),
"label@0.50": model.predict_label(check),
"label@0.35": model.predict_label(check, cutoff=0.35),
}
pprint(out){'eta': array([ 3.628 , 0.3457, 1.8463, 0.8611, 2.4948, 0.7456, 1.4082,
-0.2518]),
'label@0.35': array([1, 1, 1, 1, 1, 1, 1, 1], dtype=int32),
'label@0.50': array([1, 1, 1, 1, 1, 1, 1, 0], dtype=int32),
'probability': array([0.9741, 0.5856, 0.8637, 0.7029, 0.9238, 0.6782, 0.8035, 0.4374])}