← Back to playground index

corrWith / autoCorr — Pairwise Correlation & Autocorrelation

Compute pairwise Pearson correlations between a DataFrame and a Series or another DataFrame, and compute the lag-N autocorrelation of a numeric Series. Mirrors pandas.DataFrame.corrwith() and pandas.Series.autocorr().

autoCorr — lag-N autocorrelation

Python pandas equivalent:

import pandas as pd

s = pd.Series([1, 2, 3, 4, 5])
print(s.autocorr(lag=1))   # 1.0  (perfectly linear → perfect self-correlation)
print(s.autocorr(lag=2))   # 1.0

# Alternating sign series → -1 autocorrelation at lag 1
s2 = pd.Series([1, -1, 1, -1, 1, -1])
print(s2.autocorr(lag=1))  # -1.0

# NaN when constant (zero variance)
s3 = pd.Series([5, 5, 5, 5])
print(s3.autocorr(lag=1))  # NaN

tsb equivalent:

import { Series, autoCorr } from "tsb";

const s = new Series({ data: [1, 2, 3, 4, 5] });
autoCorr(s);          // lag=1 → 1.0
autoCorr(s, 0);       // lag=0 → 1.0  (always)
autoCorr(s, 2);       // lag=2 → 1.0

const alt = new Series({ data: [1, -1, 1, -1, 1, -1] });
autoCorr(alt, 1);     // -1.0

const flat = new Series({ data: [5, 5, 5, 5] });
autoCorr(flat, 1);    // NaN

corrWith — DataFrame correlated with a Series

Python pandas equivalent:

import pandas as pd

df = pd.DataFrame({
    "A": [1, 2, 3, 4, 5],
    "B": [5, 4, 3, 2, 1],
})
s = pd.Series([1, 2, 3, 4, 5])

print(df.corrwith(s))
# A    1.0
# B   -1.0

tsb equivalent:

import { DataFrame, Series, corrWith } from "tsb";

const df = DataFrame.fromColumns({
  A: [1, 2, 3, 4, 5],
  B: [5, 4, 3, 2, 1],
});
const s = new Series({ data: [1, 2, 3, 4, 5] });

const result = corrWith(df, s);
// Series(A=1.0, B=-1.0) indexed by column names

corrWith — DataFrame correlated with another DataFrame

Python pandas equivalent:

import pandas as pd
import numpy as np

df1 = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
df2 = pd.DataFrame({"A": [1, 2, 3], "B": [6, 5, 4]})

print(df1.corrwith(df2))
# A    1.0
# B   -1.0

# Columns in only one DataFrame get NaN (drop=False default)
df3 = pd.DataFrame({"A": [1, 2, 3], "C": [7, 8, 9]})
print(df1.corrwith(df3))
# A    1.0
# B    NaN
# C    NaN

# drop=True keeps only common columns
print(df1.corrwith(df3, drop=True))
# A    1.0

tsb equivalent:

import { DataFrame, corrWith } from "tsb";

const df1 = DataFrame.fromColumns({ A: [1, 2, 3], B: [4, 5, 6] });
const df2 = DataFrame.fromColumns({ A: [1, 2, 3], B: [6, 5, 4] });

corrWith(df1, df2);                         // A=1.0, B=-1.0

const df3 = DataFrame.fromColumns({ A: [1, 2, 3], C: [7, 8, 9] });
corrWith(df1, df3);                         // A=1.0, B=NaN, C=NaN
corrWith(df1, df3, { drop: true });         // A=1.0

corrWith — axis=1 (row-wise correlation)

Python pandas equivalent:

import pandas as pd

df = pd.DataFrame({
    "A": [1, 2],
    "B": [2, 4],
    "C": [3, 6],
})
s = pd.Series([1, 2, 3])

# axis=1: correlate each ROW with the Series
print(df.corrwith(s, axis=1))
# 0    1.0
# 1    1.0

tsb equivalent:

import { DataFrame, Series, corrWith } from "tsb";

const df = DataFrame.fromColumns({ A: [1, 2], B: [2, 4], C: [3, 6] });
const s = new Series({ data: [1, 2, 3] });

corrWith(df, s, { axis: 1 });
// Series([1.0, 1.0]) indexed by row labels [0, 1]