mvnorm
MultivariateNormalizer(n_dim)
Bases: Normalizer
Online multivariate normalizer with decorrelation via covariance estimation.
Transforms multivariate data to have zero mean, unit variance, and zero correlation (decorrelation). Uses online estimation of the covariance matrix and computes the inverse square root for transformation.
The transformation is:
where \(\mu\) is the estimated mean and \(\Sigma\) is the estimated covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_dim |
int
|
Number of dimensions/features in the data. |
required |
Attributes:
| Name | Type | Description |
|---|---|---|
n |
int
|
Number of observations seen so far. |
muhat |
ndarray
|
Estimated mean vector, shape (n_dim,). |
Sigmahat |
ndarray
|
Estimated covariance matrix (computed from _M), shape (n_dim, n_dim). |
invsqrtSigmahat |
ndarray
|
Inverse square root of covariance matrix (\(\Sigma^{-1/2}\)), shape (n_dim, n_dim). |
Examples:
from onorm import MultivariateNormalizer
import numpy as np
normalizer = MultivariateNormalizer(n_dim=3)
# Generate correlated data
cov = np.array([[1, 0.5, 0.3], [0.5, 1, 0.4], [0.3, 0.4, 1]])
X = np.random.multivariate_normal([0, 0, 0], cov, size=100)
for x in X:
normalizer.partial_fit(x)
x_new = np.array([1.0, 1.0, 1.0])
x_normalized = normalizer.transform(x_new.copy())
# x_normalized will be decorrelated with zero mean and unit variance
Notes
- Time complexity: O(d²) per observation for fitting, O(d²) for transformation
- Space complexity: O(d²) for storing covariance matrix
- The covariance matrix is computed using Welford's online algorithm
- Transformation uses Cholesky decomposition of the inverse covariance
References
Source code in src/onorm/mvnorm.py
Sigmahat: np.ndarray
property
Compute the sample covariance matrix.
Returns:
| Type | Description |
|---|---|
ndarray
|
Sample covariance matrix, shape (n_dim, n_dim). Returns identity matrix if n <= 1. |
invsqrtSigmahat: np.ndarray
property
Compute the inverse square root of the covariance matrix.
Returns:
Computed via Cholesky decomposition of the inverse covariance matrix.
Returns:
| Type | Description |
|---|---|
ndarray
|
Inverse square root of covariance matrix, shape (n_dim, n_dim). Returns identity matrix if covariance is not positive definite. |
from_dict(data)
classmethod
Deserialize a normalizer from a dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data |
dict
|
Dictionary created by to_dict(). |
required |
Returns:
| Type | Description |
|---|---|
MultivariateNormalizer
|
Deserialized normalizer instance. |
Source code in src/onorm/mvnorm.py
from_json(json_str)
classmethod
partial_fit(x)
Update mean and covariance estimates with a new observation.
Uses Welford's online algorithm to incrementally update the mean and covariance matrix.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
ndarray
|
A 1-D array of shape (n_dim,) representing a new observation. |
required |
See Also
Normalizer.partial_fit : Base class method for incremental fitting.
References
Source code in src/onorm/mvnorm.py
reset()
Reset the normalizer to initial state.
Reinitializes observation count, mean, and covariance accumulator (_M) to zeros.
Source code in src/onorm/mvnorm.py
to_dict()
Serialize the normalizer state to a dictionary.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with JSON-serializable metadata and base64-encoded arrays. |
Source code in src/onorm/mvnorm.py
to_json()
transform(x)
Apply multivariate normalization (decorrelation and standardization).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x |
ndarray
|
A 1-D array of shape (n_dim,) to normalize. |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
Decorrelated and standardized array of shape (n_dim,). |