Skip to main content

Robust conditional normalization of continuous markers using polynomial surface fitting over categorical and continuous covariates

Project description

covnorm

Scikit-learn compatible transformer for robust conditional Z-score normalization of a continuous marker against categorical and continuous covariates.

A single polynomial curve is fitted over mu and sigma across the entire dataset using rolling overlapping bins sorted by the continuous covariate. Mu and sigma per bin are estimated via Q-Q regression on Box-Cox transformed values, with iterative conditional Z-score outlier rejection (samples with |Z| > 3.372 against the fitted surface are dropped and the surface is refit, repeated up to n_iterations times). The Box-Cox lambda is selected by a grid search that maximises Q-Q linearity rather than marginal normality.

After fitting the shared surface, a post-hoc categorical correction is applied: the mean and standard deviation of the base Z-scores are computed within each categorical group and used to rescale final Z-scores, giving Z_corrected = (Z_base - mu_cat) / sigma_cat. This avoids overfitting independent surfaces to small groups.

Data requirement: target values must be strictly positive (Box-Cox transform). Shift your data if it contains zeros or negatives.

Supports up to 2 categorical covariates and exactly 1 continuous covariate.

Installation

pip install covnorm

Usage

import numpy as np
from covnorm import RobustConditionalNormalizer

# X columns: [sex, batch, age, marker]
# sex=0, batch=1 → categorical
# age=2 → continuous (exactly one supported)
# marker=3 → target to normalize

X = np.load("data.npy")  # shape (n_samples, 4)

normalizer = RobustConditionalNormalizer(
    categorical_cols=[0, 1],
    continuous_cols=[2],
    target_col=3,
    n_bins=6,                      # target number of rolling windows
    bin_size=120,                  # samples per rolling window
    log_transform_continuous=True, # recommended when covariates span orders of magnitude
)

X_norm = normalizer.fit_transform(X)

The target column in X_norm contains Z-scores. All other columns are unchanged.

It follows the scikit-learn fit / transform / fit_transform API and is compatible with Pipeline.

Parameters

Parameter Default Description
categorical_cols Column indices treated as categorical grouping variables
continuous_cols Column indices used as continuous covariates for surface fitting
target_col Column index of the marker to normalize
n_bins 6 Target number of rolling windows (controls stride)
bin_size 120 Samples per rolling window (Mørkved et al. use 120)
degree 3 Polynomial degree of the mu/sigma curve
n_iterations 3 Maximum iterative conditional outlier-removal passes
log_transform_continuous False Apply log10 to the continuous covariate before fitting (recommended when it spans orders of magnitude, e.g. age in years)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

covnorm-0.3.0.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

covnorm-0.3.0-py3-none-any.whl (11.1 kB view details)

Uploaded Python 3

File details

Details for the file covnorm-0.3.0.tar.gz.

File metadata

  • Download URL: covnorm-0.3.0.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for covnorm-0.3.0.tar.gz
Algorithm Hash digest
SHA256 7b00e3cf03c94e238f09c97bcb03efd16dd1d4c8350d96ad2c3b60852467b624
MD5 2f18718841a1d3f9c95530aa3e5a1240
BLAKE2b-256 62ed10887c0b334f2415756c1eedb11c37d91ca47d4dffe5f19787aed31d59c6

See more details on using hashes here.

Provenance

The following attestation bundles were made for covnorm-0.3.0.tar.gz:

Publisher: publish.yml on danilotat/covnorm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file covnorm-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: covnorm-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 11.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for covnorm-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2c080182747f37b96773be7c863932654822394aaa4b8c38b0c6c7d6c8bd663b
MD5 995864270937842bfb7104b2148dccb6
BLAKE2b-256 b09155105d861ea24e194f3398e8f96dff6f319123b71cc67e5ccbd29846a63b

See more details on using hashes here.

Provenance

The following attestation bundles were made for covnorm-0.3.0-py3-none-any.whl:

Publisher: publish.yml on danilotat/covnorm

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page