pyntbci.utilities.correlation
- pyntbci.utilities.correlation(A: NDArray, B: NDArray, n_old: int = 0, avg_old: NDArray = None, cov_old: NDArray = None, running: bool = False) NDArray | tuple[NDArray, int, NDArray, NDArray][source]
Compute the correlation coefficient. Computed between two sets of variables.
- Parameters:
A (NDArray) – The first set of variables of shape (n_A, n_samples).
B (NDArray) – The second set of variables of shape (n_B, n_samples).
n_old (int (default: 0)) – Number of already observed samples. Only used if running=True.
avg_old (NDArray (default: None)) – Already observed average of concat(A, B) of shape (1, n_A + n_B). Only used if running=True.
cov_old (NDArray (default: None)) – Already observed covariance of concat(A, B) of shape (n_A + n_B, n_A + n_B). Only used if running=True.
running (bool (default: False)) – Whether to use a running correlation, adding samples to a previously accumulated covariance rather than recomputing from scratch. If True, A and B are taken to be only the newly observed samples (not the full history), and n_old/avg_old/cov_old the state accumulated so far (as returned by a previous call); use n_old=0 (default) for the first call in a sequence. Internally reuses covariance() (see its running=True mode) on concat(A, B), since correlation is simply covariance normalized by the two variances, so it is exactly as numerically stable as covariance()’s own running mode.
- Returns:
scores (NDArray) – The correlation matrix of shape (n_A, n_B).
n_new (int) – Number of samples. Only returned if running=True.
avg_new (NDArray) – The average of concat(A, B) of shape (1, n_A + n_B). Only returned if running=True.
cov_new (NDArray) – The covariance of concat(A, B) of shape (n_A + n_B, n_A + n_B). Only returned if running=True.