Qu

Statistics & estimation

Correlation heatmap

corrcoef(X) + heatmap(corrcoef(X)).

catalog/qu_corrcoef_heatmap.qu

Runs end-to-end on the reference interpreter (`qu run`). Exercises: corrcoef(X) — the full pairwise Pearson correlation matrix of a data matrix X (rows = samples, cols = features), an NxN Mat (N = number of features), symmetric, diagonal of 1s. heatmap(...) — the existing colored-grid renderer, fed corrcoef's own Mat output directly (distinct from corr_heatmap/corrplot, which do the same computation over a Table's numeric columns and push the plot themselves in one call).

How it works

Setup

--- A small 5-sample, 3-feature data matrix ------------------------------- col 0 = a, col 1 = b = 2*a (perfectly correlated), col 2 = c (partially correlated with both) -- hand-checkable, not a random draw.

X = [1, 2, 5; 2, 4, 3; 3, 6, 6; 4, 8, 2; 5, 10, 9]

Compute

R = corrcoef(X)
print("corrcoef(X) = {R}")          # [1,1,0.404145; 1,1,0.404145; 0.404145,0.404145,1]

# Sanity checks against the pairwise `corr` builtin -------------------------
print("corr(a,b)   = {corr(X[:,0], X[:,1])}")   # 1        (b = 2*a exactly)
print("corr(a,c)   = {corr(X[:,0], X[:,2])}")   # ~0.4041
print("R[0,1]      = {R[0, 1]}")                # matches corr(a,b)
print("R diagonal  = {R[0,0]}, {R[1,1]}, {R[2,2]}")  # 1, 1, 1 (self-correlation)

Plot

--- The real verified example: heatmap(corrcoef(X)) ----------------------

heatmap(R, colormap="viridis")

show plot

Results

printed
corrcoef(X) = [1, 1, 0.404145; 1, 1, 0.404145; 0.404145, 0.404145, 1]
corr(a,b)   = 1
corr(a,c)   = 0.404145
R[0,1]      = 1
R diagonal  = 1, 1, 1
1 1 1 1 0.404 0.404 1 1 1 1 0.404 0.404 0.404 0.404 0.404 0.404 1 1
Drawn by running the program above.

The whole program

Runnable as it stands: qu run catalog/qu_corrcoef_heatmap.qu

# qu_corrcoef_heatmap.qu — corrcoef(X) + heatmap(corrcoef(X)).

#

# Runs end-to-end on the reference interpreter (`qu run`). Exercises:

#   corrcoef(X)   — the full pairwise Pearson correlation matrix of a data

#                   matrix X (rows = samples, cols = features), an NxN Mat

#                   (N = number of features), symmetric, diagonal of 1s.

#   heatmap(...)  — the existing colored-grid renderer, fed corrcoef's own

#                   Mat output directly (distinct from corr_heatmap/corrplot,

#                   which do the same computation over a Table's numeric

#                   columns and push the plot themselves in one call).


backend auto

#%% Setup

# --- A small 5-sample, 3-feature data matrix -------------------------------

# col 0 = a, col 1 = b = 2*a (perfectly correlated), col 2 = c (partially

# correlated with both) -- hand-checkable, not a random draw.

X = [1, 2, 5; 2, 4, 3; 3, 6, 6; 4, 8, 2; 5, 10, 9]

#%% Compute

R = corrcoef(X)
print("corrcoef(X) = {R}")          # [1,1,0.404145; 1,1,0.404145; 0.404145,0.404145,1]


# Sanity checks against the pairwise `corr` builtin -------------------------

print("corr(a,b)   = {corr(X[:,0], X[:,1])}")   # 1        (b = 2*a exactly)

print("corr(a,c)   = {corr(X[:,0], X[:,2])}")   # ~0.4041

print("R[0,1]      = {R[0, 1]}")                # matches corr(a,b)

print("R diagonal  = {R[0,0]}, {R[1,1]}, {R[2,2]}")  # 1, 1, 1 (self-correlation)


#%% Plot

# --- The real verified example: heatmap(corrcoef(X)) ----------------------

heatmap(R, colormap="viridis")

show plot