Skip to main content

Pure Python SPACE: sparse partial correlation estimation

Project description

space-graph

PyPI version Python 3.10+ License: GPL-3.0-or-later DOI

Discover which variables in your dataset are directly related to each other, even after accounting for all other variables. Given a data matrix, SPACE estimates a sparse network of partial correlations -- connections that remain after removing indirect effects. Designed for settings where the number of variables can far exceed the number of samples (e.g. genomics).

Pure Python implementation of SPACE (Sparse Partial Correlation Estimation) from Peng et al. (2009), with no R or C dependencies.

Paper: Sparse Partial Correlation Estimation for High-Dimensional Data

Install

pip install space-graph

Optional Numba (faster inner jsrm loop):

pip install 'space-graph[numba]'

From GitHub:

pip install git+https://github.com/shahrozeabbas/space-graph.git

Usage

import numpy as np
from space_graph import SPACE

X = np.random.randn(20, 5)

model = SPACE(
    alpha=0.7,
    max_outer_iter=2,
    max_inner_iter=500,
    tol=1e-6,
    weight='uniform',
)

model.fit(X)

print(model.partial_correlation_)

Choosing alpha

Use SPACE.select_alpha(X, alphas) to pick the regularization strength by BIC (Peng et al. 2009, Sec. 2.4). It fits across the grid and returns the alpha minimizing sum_i [n * log(RSS_i) + log(n) * k_i], where k_i is the number of neighbors of variable i.

import numpy as np
from space_graph import SPACE

template = SPACE(max_outer_iter=3)
alphas = np.geomspace(0.005, 1.0, 15)
best_alpha = template.select_alpha(X, alphas)

model = SPACE(alpha=best_alpha, max_outer_iter=3).fit(X)

Pass return_curve=True to also get the per-alpha BIC vector for plotting. The template is not mutated — reuse or discard it.

Penalty

  • alpha: overall regularization strength (like sklearn).
  • gamma (γ) in [0, 1]: mix between L1 and L2 terms.
    • lam1 = alpha * gamma (L1 / sparsity)
    • lam2 = alpha * (1 - gamma) (L2 / ridge)
    • Default gamma=1 matches R space::space.joint default lam2 = 0.

Options

  • tol (default 1e-6): inner solver convergence + active-set threshold, same scale as the reference C code.
  • weight: default uniform (equivalently equal, unit weights). Other modes:
    • sig — reweight each variable by its residual variance (updates each outer iteration).
    • degree — reweight by node connectivity (updates each outer iteration).
    • custom ndarray of length p — internally normalized to mean 1, so alpha's scale stays comparable to the other modes.
  • backend (default auto): inner joint sparse regression (JSRM) solver. numpy always uses pure NumPy. auto uses Numba when installed (lazy on first fit), otherwise NumPy. numba requires Numba and raises ImportError if it is missing. The first fit with auto or numba pays a one-time compile cost.

Tests

pytest

Optional: build libjsrm_test.so from ../space/src/JSRM.c to run the ctypes cross-check in tests/test_space.py.

License

GPL-3.0-or-later (same family as the original space R package).

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

space_graph-1.0.0.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

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

space_graph-1.0.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file space_graph-1.0.0.tar.gz.

File metadata

  • Download URL: space_graph-1.0.0.tar.gz
  • Upload date:
  • Size: 16.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for space_graph-1.0.0.tar.gz
Algorithm Hash digest
SHA256 108282085b6787703b2509c4d20f265608e5fd99a29fdbd5e84c65bcb3a17734
MD5 469fb6c57e89a40748dd49d928e89376
BLAKE2b-256 6b160569ca48676fd98cd47bea2a79f5eda0ec6e0512f16b3bca055ed1052bd2

See more details on using hashes here.

File details

Details for the file space_graph-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: space_graph-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.12

File hashes

Hashes for space_graph-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3a18a558f924705b116488feb0ddd84940b6adf4f559e728bfa638492b2ef92
MD5 b533d0d709468a14d5407537d98a6a27
BLAKE2b-256 74fed5a1da1e56fbff8f3f6e0d2a1c31f85a749ee661b7247fbdfe0d8fdbca42

See more details on using hashes here.

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