Correlation Coefficients
pearsonr
Pearson correlation coefficient.First tensor
Second tensor (must have same size as x)
Tuple of [correlation coefficient, two-tailed p-value]
- Correlation coefficient is in range [-1, 1]
- P-value tests the null hypothesis that the correlation is zero
- r = 1: Perfect positive linear relationship
- r = 0: No linear relationship
- r = -1: Perfect negative linear relationship
r = cov(X,Y) / (std(X) * std(Y)). The p-value is computed using the t-distribution with n-2 degrees of freedom under the null hypothesis that the population correlation is zero.
Assumptions:
- Linear relationship between variables
- Both variables approximately normally distributed
- No significant outliers
- Homoscedasticity (constant variance)
spearmanr
Spearman’s rank correlation coefficient.First tensor
Second tensor (must have same size as x)
Tuple of [correlation coefficient, p-value]
- ρ (rho) is in range [-1, 1]
- P-value tests the null hypothesis of no monotonic relationship
- ρ = 1: Perfect monotonic increasing relationship
- ρ = 0: No monotonic relationship
- ρ = -1: Perfect monotonic decreasing relationship
- Detects non-linear monotonic relationships
- Robust to outliers
- No assumption of normality
- Works well with ordinal data
kendalltau
Kendall’s tau correlation coefficient.First tensor
Second tensor (must have same size as x)
Tuple of [tau coefficient, p-value]
- τ (tau) is in range [-1, 1]
- P-value uses normal approximation with tie-corrected variance
- τ = 1: All pairs concordant (perfect agreement)
- τ = 0: Equal concordant and discordant pairs
- τ = -1: All pairs discordant (perfect disagreement)
- More robust to outliers than Spearman
- Better for small sample sizes
- Has a direct interpretation (probability of concordance minus probability of discordance)
- More appropriate when many tied ranks exist
Covariance and Correlation Matrices
corrcoef
Computes the Pearson correlation coefficient matrix.Input tensor (1D or 2D). If 2D, each column is treated as a variable.
Optional second tensor. If provided, computes correlation between x and y.
Correlation matrix (symmetric with 1s on diagonal)
- For two 1D tensors: 2×2 matrix
- For 2D tensor: n×n matrix where n is number of variables (columns)
- Identifying multicollinearity in regression analysis
- Feature selection in machine learning
- Understanding relationships between multiple variables
- Exploratory data analysis
cov
Computes the covariance matrix.Input tensor (1D or 2D). If 2D, each column is treated as a variable.
Optional second tensor. If provided, computes covariance between x and y.
Delta degrees of freedom. Use 0 for population covariance, 1 for sample covariance (default).
Covariance matrix (symmetric)
- For two 1D tensors: 2×2 matrix with variances on diagonal and covariances off-diagonal
- For 2D tensor: n×n matrix where n is number of variables (columns)
- Positive covariance: Variables tend to increase together
- Negative covariance: As one increases, the other tends to decrease
- Near-zero covariance: No linear relationship
- Portfolio theory (covariance of asset returns)
- Principal Component Analysis (PCA)
- Multivariate statistical analysis
- Linear discriminant analysis