Minimalistic latent correlation estimators for discretized data.
Project description
Latent Correlation Estimation Package
This package provides minimalistic implementations of latent correlation estimators between pairs of continuous variables when one or both variables are discretized.
The main goal is to offer both value-based and rank-based correlation estimates in one place, with a simple and easy-to-understand implementation.
Note: Binary data is a special case of ordinal data. For clarity, we implemented binary correlations separately as a simpler example.
Supported Estimators
1. Value-Based Correlations
- Tetrachoric
- Polychoric
- Biserial
- Polyserial
These estimators assume a bivariate normal underlying distribution and estimate the correlation parameter ($\rho$) by maximizing the likelihood of the observed discretized data.
2. Rank-Based Correlations
- Suitable for binary, ordinal, and mixed (ordinal-continuous) data, and also supports continuous data through Greiner's formula.
- Based on the assumption that data arises from an arbitrary monotonic transformation of underlying bivariate Gaussian variables (Gaussian copula model).
- The correlation parameter ($\rho$) is estimated by matching the observed Kendall's tau to its expected value as a function of $\rho$. This makes these estimators invariant to marginal transformations.
Synthetic Data Scenarios
The package includes utilities for generating synthetic data to test estimator robustness across different distributional assumptions:
- Normal Distribution: Satisfies the underlying assumptions for both value-based and rank-based methods.
- Lognormal Distribution: Violates the assumptions of value-based methods due to non-normality, but holds for rank-based methods.
- Gumbel and Clayton Copulas: Violate the assumptions of both families of estimators due to the presence of tail dependence, which deviates from the Gaussian copula model.
Diagnostics & Model Fit
To verify if the data aligns with the theoretical assumptions of the Gaussian copula model, the package provides tools to visualize and compare the observed data against theoretical expectations:
- Tail Concentration Function: (For continuous data) Helps detect if dependence is concentrated in the tails, which would contradict a Gaussian assumption.
- K-plot (Kendall plot): (For continuous data) A diagnostic tool to assess the dependency structure independently of marginal distributions.
- Latent Space Comparison: For both continuous and discrete data, the package allows for a direct visual check of the Gaussian copula assumption. This is done by comparing 2D histograms or empirical categorical densities against the theoretical densities expected under the estimated latent correlation. The comparison is performed in the latent space by mapping observed data via rank transformation.
Installation
pip install latentmetrics[all]
Usage
import matplotlib.pyplot as plt
from latentmetrics import make_corr_fn, VariableType, EstimateMethod, gauss_rho_to_tau
from latentmetrics.synthesis import Synthesis
from latentmetrics.diagnostics import (
plot_tail_concentration_function,
plot_k_plot,
plot_latent_density,
)
N_SAMPLES = 1000
RHO_TRUE = 0.4
# --- Data Synthesis ---
# x, y = Synthesis.gumbel(tau=gauss_rho_to_tau(RHO_TRUE), n=N_SAMPLES)
# x, y = Synthesis.clayton(tau=gauss_rho_to_tau(RHO_TRUE), n=N_SAMPLES)
x, y = Synthesis.lognormal(rho=RHO_TRUE, n=N_SAMPLES)
x_obs = Synthesis.discretize(x, [0.25, 0.25, 0.25, 0.25])
y_obs = Synthesis.discretize(y, [0.25, 0.25, 0.25, 0.25])
# --- Correlation Estimation ---
corr_fn = make_corr_fn(VariableType.ORDINAL, VariableType.ORDINAL, method=EstimateMethod.RANK)
rho_est = corr_fn(x_obs, y_obs).estimate
# --- Visualization ---
fig, axs = plt.subplots(3, 2, figsize=(16, 18))
# 1. Latent Density (Observed/Discretized)
plot_latent_density(
x_obs, y_obs, "Latent Space (Discretized)", rho=rho_est,
ax_theoretical=axs[0, 0], ax_empirical=axs[0, 1]
)
# 2. Latent Density (Original/Continuous)
plot_latent_density(
x, y, "Latent Space (Continuous)", rho=rho_est,
ax_theoretical=axs[1, 0], ax_empirical=axs[1, 1]
)
# 3. Tail Concentration and K-Plot
plot_tail_concentration_function(x, y, rho=rho_est, ax=axs[2, 0])
plot_k_plot(x, y, rho=rho_est, mode="standard", ax=axs[2, 1])
# --- Formatting ---
# hspace adds vertical space, wspace adds horizontal space
plt.subplots_adjust(hspace=0.4, wspace=0.3)
plt.show()
Literature & References
Value-Based Correlations
-
Polychoric correlation Olsson, U. (1979). Maximum likelihood estimation of the polychoric correlation coefficient. Psychometrika, 44(4), 443–460.
-
Polyserial correlation Olsson, U., Drasgow, F., & Dorans, N. J. (1982). The polyserial correlation coefficient. Psychometrika, 47(3), 337–347.
Rank-Based & Semiparametric Modeling
- Dey, D., & Zipunnikov, V. (2022). Semiparametric Gaussian Copula Regression Modeling for Mixed Data Types (SGCRM). arXiv preprint arXiv:2205.06868.
Copula Theory & Background
-
Durante, F., Fernández-Sánchez, J., & Pappadà, R. (2015). Copulas, diagonals, and tail dependence. Fuzzy Sets and Systems, 264, 22–41.
-
Genest, C., & Favre, A. C. (2007). Everything you always wanted to know about copula modeling but were afraid to ask. Journal of Hydrologic Engineering, 12(4), 347–368.
-
Hofert, M., Kojadinovic, I., Mächler, M., & Yan, J. (2018). Elements of copula modeling with R. Springer.
Simulation & Data Synthesis
- Hofert, M. (2008). Sampling Archimedean copulas. Computational Statistics & Data Analysis, 52(12), 5163–5174.
Related Packages for Latent Correlations
If you are looking for alternative implementations or specialized features, you may find these packages useful:
R Packages
- polycor – The established standard for Polychoric and Polyserial correlations in R.
- latentcor – Efficient implementations of rank-based correlations.
Python Packages
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file latentmetrics-0.1.1.tar.gz.
File metadata
- Download URL: latentmetrics-0.1.1.tar.gz
- Upload date:
- Size: 20.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
260edbb09013642edcf693ad277c2b556f5f11c08eea6f2398111b6accf73598
|
|
| MD5 |
55d48732c4badb3a7d145c5d9c6a3512
|
|
| BLAKE2b-256 |
66103af91929618ae5e266c2f267b679c4ddcd0d8a6faeb0664f5ac219d23a0d
|
Provenance
The following attestation bundles were made for latentmetrics-0.1.1.tar.gz:
Publisher:
publish.yml on PavelNovikov/latentmetrics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latentmetrics-0.1.1.tar.gz -
Subject digest:
260edbb09013642edcf693ad277c2b556f5f11c08eea6f2398111b6accf73598 - Sigstore transparency entry: 832781279
- Sigstore integration time:
-
Permalink:
PavelNovikov/latentmetrics@b576b9140121eba8cf0c00a6a32efddeca1c771f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/PavelNovikov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b576b9140121eba8cf0c00a6a32efddeca1c771f -
Trigger Event:
release
-
Statement type:
File details
Details for the file latentmetrics-0.1.1-py3-none-any.whl.
File metadata
- Download URL: latentmetrics-0.1.1-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bcac767d8c2d02a223741b3ca3ac7a6466e664cdf53a56d075cb7f284fca3ab6
|
|
| MD5 |
3ce47f826bb6e7308c53e4e92fb968cf
|
|
| BLAKE2b-256 |
c5dc76e9830ed74acf5dab49080addb7f0ec5ba0635547c6c2f0741b1b2e867f
|
Provenance
The following attestation bundles were made for latentmetrics-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on PavelNovikov/latentmetrics
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
latentmetrics-0.1.1-py3-none-any.whl -
Subject digest:
bcac767d8c2d02a223741b3ca3ac7a6466e664cdf53a56d075cb7f284fca3ab6 - Sigstore transparency entry: 832781282
- Sigstore integration time:
-
Permalink:
PavelNovikov/latentmetrics@b576b9140121eba8cf0c00a6a32efddeca1c771f -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/PavelNovikov
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@b576b9140121eba8cf0c00a6a32efddeca1c771f -
Trigger Event:
release
-
Statement type: