LogisticRegression

Guide & worked example · Source

LogisticRegression(optimizer=None, maxiter=5000, tol=0.0001, device=None)

Logistic Regression for binary classification with MLE estimation.

Estimates the relationship between a binary outcome and covariates using the logistic (sigmoid) link function. Provides asymptotically efficient maximum likelihood estimates with proper standard errors.

The model assumes: P(y=1|X) = σ(X’β) where σ is the sigmoid function.

Examples: >>> model = LogisticRegression(optimizer=torch.optim.LBFGS, maxiter=100) >>> X = torch.randn(1000, 5) >>> y = (torch.randn(1000) > 0).float() >>> model.fit(X, y) >>> model.summary()

Attributes

Name Description
device
history
maxiter
optimizer_class
params
tol

Methods

Name Description
fit Fit the model using maximum likelihood estimation.
predict Predict binary class labels.
predict_proba Predict class probabilities P(y=1|X).
summary Print a formatted summary of model estimation results.
to Move model parameters and fitted data to device.

fit

LogisticRegression.fit(
    X,
    y,
    init_params=None,
    verbose=False,
    fe=None,
    fe_design=None,
    hdfe_index=None,
    offset=None,
    batch_size=None,
    stderr=True,
)

Fit the model using maximum likelihood estimation.

Optimizes the model parameters to minimize the negative log-likelihood using the specified PyTorch optimizer. After convergence, computes standard errors via the Fisher information matrix.

Args: X: Design matrix of shape (n_samples, n_features). Assumes X includes an intercept column if desired. y: Target vector of shape (n_samples,). init_params: Initial parameter values. For FE-aware GLMs, may be a packed parameter vector or a dict with coef and fe_coef. verbose: If True, prints convergence information. fe: Optional list of 1D fixed-effect ID vectors. fe_design: Optional list of sparse fixed-effect incidence matrices. hdfe_index: Optional index of the FE block to use for fast inference. offset: Optional offset vector added to the linear predictor. batch_size: Optional mini-batch size for first-order optimizers. stderr: Whether to compute standard errors.

Returns: Self (fitted estimator) with populated params dictionary.

Examples: >>> model = LogisticRegression() >>> model.fit(X_train, y_train, verbose=True) >>> print(model.params[“coef”])

predict

LogisticRegression.predict(
    X,
    threshold=0.5,
    fe=None,
    fe_design=None,
    offset=None,
)

Predict binary class labels.

Args: X: Design matrix of shape (n_samples, n_features). threshold: Probability threshold for assigning class 1. fe: Optional FE inputs for FE-aware models. fe_design: Optional sparse FE design inputs for FE-aware models. offset: Optional offset vector for FE-aware models.

Returns: Predicted class labels of shape (n_samples,).

predict_proba

LogisticRegression.predict_proba(X, fe=None, fe_design=None, offset=None)

Predict class probabilities P(y=1|X).

Args: X: Design matrix of shape (n_samples, n_features). fe: Optional FE inputs for FE-aware models. fe_design: Optional sparse FE design inputs for FE-aware models. offset: Optional offset vector for FE-aware models.

Returns: Predicted probabilities of shape (n_samples,).

summary

LogisticRegression.summary(alpha=0.05)

Print a formatted summary of model estimation results.

Displays a comprehensive table including parameter estimates, standard errors, t-statistics, p-values, and confidence intervals (if standard errors are available).

Args: alpha: Significance level for confidence intervals (default: 0.05 for 95% confidence intervals).

Examples: >>> model.fit(X, y) >>> model.summary(alpha=0.01) # 99% confidence intervals

to

LogisticRegression.to(device)

Move model parameters and fitted data to device.