Skip to main content

logisticpca

logisticpca provides projection-based Logistic Principal Component Analysis for binary matrices, together with a probabilistic PCA helper for continuous data.

Version 0.4.0 is a breaking methodological correction. Versions 0.3.x implemented a free-score logistic SVD while exposing it as LogisticPCA. Version 0.4.0 implements the projection formulation of Landgraf and Lee (2020), with a single orthonormal loading basis and a closed-form projection for complete new rows.

Installation

pip install logisticpca

For development:

git clone https://github.com/azaidr/logisticpca.git
cd logisticpca
python -m pip install -e ".[dev]"
pytest

Basic use

import numpy as np
from logisticpca import LogisticPCA

rng = np.random.default_rng(42)
X = rng.binomial(1, 0.15, size=(500, 100))

model = LogisticPCA(
    n_components=3,
    m=4.0,
    max_iter=1000,
    tol=1e-5,
)

scores = model.fit_transform(X)
probabilities = model.predict_proba(X)

print(scores.shape)                    # (500, 3)
print(model.converged_)
print(model.termination_reason_)
print(model.average_deviance_)
print(model.deviance_explained_)

For complete rows, fit_transform(X) and fit(X).transform(X) return the same score matrix by construction.

Selecting the component count and saturation constant

The default selector uses row folds plus entrywise masking. The model is fitted on training rows. For each held-out row, the score is estimated from observed entries only, and deviance is evaluated only on the withheld entries.

from logisticpca import CVLogisticPCA

selector = CVLogisticPCA(
    component_grid=[1, 2, 3, 4, 5],
    m_grid=[2.0, 4.0, 6.0],
    cv=5,
    validation="entrywise",
    validation_fraction=0.20,
    score_method="bernoulli",
    random_state=42,
)

scores = selector.fit_transform(X)
print(selector.best_n_components_)
print(selector.best_m_)
print(selector.cv_results_)

validation="row_reconstruction" is also available. That mode uses the full held-out row to compute its projection score and then reconstructs the same row. It measures complete-row reconstruction, not entrywise predictive performance, and it can favor larger ranks.

Partially observed rows

X_partial = X[:10].astype(float)
observed_mask = np.ones_like(X_partial, dtype=bool)
observed_mask[:, :20] = False
X_partial[:, :20] = np.nan

scores_partial = model.transform_partial(
    X_partial,
    observed_mask=observed_mask,
    method="bernoulli",
    ridge=1e-6,
)

The default partial-score method maximizes a ridge-penalized Bernoulli likelihood using observed entries only. A finite-saturated-parameter least-squares projection is available with method="least_squares".

Constant columns

All-zero and all-one training columns are excluded from the loading subspace. With main_effects=True, they are represented as finite intercept-only features with intercepts -m and +m, respectively. Their loading rows are zero. This prevents constant columns from driving Bernoulli intercepts toward infinity.

If every column is constant, fitting raises an error because no nonconstant subspace exists.

Model definition

Let

Q = 2X - 1
Eta = mQ
Theta = 1 mu' + (Eta - 1 mu') U U'

where U has orthonormal columns. The model minimizes Bernoulli deviance using a majorization-minimization update based on the global one-quarter curvature bound of the logistic loss.

The m parameter is a finite approximation to the saturated natural parameters. Version 0.4.0 requires m > 0. The unstable unconstrained Newton update for m is not implemented. Use a prespecified value or select among a bounded grid with CVLogisticPCA.

Interpretation of reported deviance

loss_history_ contains average Bernoulli deviance per matrix entry, including the initialization. reconstruction_deviance_ is the total fitted deviance. deviance_explained_ is calculated over nonconstant features because empirically constant features have zero null deviance.

Computational scope

The current implementation requires a dense matrix and forms feature-by-feature Gram matrices. Memory use scales quadratically and eigendecomposition scales cubically in the number of nonconstant features. Filter unsupported or nearly empty features before fitting very wide diagnosis-code or EHR matrices.

Probabilistic PCA

The package retains the separate EM-based probabilistic PCA helper:

from logisticpca import prob_pca

W, latent_mean, feature_means, sigma2 = prob_pca(
    continuous_X,
    num_components=3,
    return_sigma=True,
    random_state=42,
)

Migration from 0.3.x

Results from 0.4.0 are not numerically comparable with 0.3.x because the estimator changed from free-score logistic SVD to projection Logistic PCA. See MIGRATION.md. The historical 0.3.5 tag should remain available for exact reproduction of analyses that used the earlier implementation.

Reference

Landgraf, A. J., and Lee, Y. (2020). Dimensionality reduction for binary data through the projection of natural parameters. Journal of Multivariate Analysis, 180, 104668. https://doi.org/10.1016/j.jmva.2020.104668

License

MIT License. See LICENSE.

Metadata

Release files for logisticpca 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for logisticpca 0.4.0
File Size Uploaded Provenance Actions
logisticpca-0.4.0.tar.gz 24.5 kB Without provenance Details for logisticpca-0.4.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for logisticpca 0.4.0
File Interpreter ABI Platform Actions
logisticpca-0.4.0-py3-none-any.whl Python 3 none any Details for logisticpca-0.4.0-py3-none-any.whl

Total release size: 41.4 kB

Release files / logisticpca-0.4.0.tar.gz

Download URL logisticpca-0.4.0.tar.gz
Size 24.5 kB
Tags Source
SHA-256 checksum
How to use checksums
934300e929a06a891f0c5f8340361d43865e4ae5582a47d7a967982558f73112
BLAKE2b-256 checksum
How to use checksums
b69680ab11e0846d25220dcaf8ba5a547061b2286996263aa01be402f9fc7f61
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.5

Release files / logisticpca-0.4.0-py3-none-any.whl

Download URL logisticpca-0.4.0-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7ce90d9967101d0d3b2423240fcdd6a2b7929b465ddb66f14c543bf70d142e0d
BLAKE2b-256 checksum
How to use checksums
46a01ce28d8046ec1f9b45af9735116432cf1d55b120d3cfa07235e4e44aa61f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.5

Security for logisticpca 0.4.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page