pearsonCorr(a, b) computes the Pearson correlation between two
Series. dataFrameCorr(df) and dataFrameCov(df)
produce symmetric N×N correlation and covariance matrices — mirroring
pandas.DataFrame.corr() and
pandas.DataFrame.cov().
Edit any code block below and press ▶ Run
(or Ctrl+Enter) to execute it live in your browser.
pearsonCorr(a, b) computes the Pearson correlation coefficient
between two Series, aligning on shared index labels and ignoring missing
values. Returns a number in [−1, 1], or NaN when a valid
result cannot be computed.
import { Series, pearsonCorr } from "tsb";
const temperature = new Series({ data: [22, 24, 28, 31, 35], name: "temp_C" });
const ice_cream = new Series({ data: [120, 145, 190, 230, 285], name: "sales" });
const r = pearsonCorr(temperature, ice_cream);
console.log("Pearson r:", r.toFixed(4)); // strong positive correlation
// Negative correlation
const warm_clothes = new Series({ data: [310, 280, 210, 150, 90], name: "jackets" });
console.log("r (temp vs jackets):", pearsonCorr(temperature, warm_clothes).toFixed(4));
// Missing values are dropped per-pair
const c = new Series({ data: [1, null, 3, 4] });
const d = new Series({ data: [2, 4, 6, 8] });
console.log("r (nulls dropped):", pearsonCorr(c, d).toFixed(4));
// Require at least 5 valid pairs — returns NaN when fewer exist
console.log("r (minPeriods=5):", pearsonCorr(c, d, { minPeriods: 5 }));
Click ▶ Run to execute
dataFrameCorr(df) returns a symmetric N×N DataFrame where
entry [i, j] is the Pearson correlation between columns i and
j. Diagonal entries are always 1. Only numeric columns are
included; string columns are silently skipped.
import { DataFrame, dataFrameCorr } from "tsb";
const df = DataFrame.fromColumns({
height: [160, 172, 185, 155, 168],
weight: [55, 72, 90, 48, 65],
age: [22, 35, 28, 19, 31],
city: ["A", "B", "C", "A", "B"], // non-numeric — skipped
});
const r = dataFrameCorr(df);
console.log("columns:", [...r.columns.values]);
console.log("shape:", r.shape);
// Read off-diagonal entries via iat() (positional access)
const rHW = r.col("weight").iat(0); // corr(height, weight)
const rHA = r.col("age").iat(0); // corr(height, age)
console.log("height–weight r:", rHW.toFixed(4));
console.log("height–age r: ", rHA.toFixed(4));
console.log("diagonal:", [r.col("height").iat(0), r.col("weight").iat(1), r.col("age").iat(2)]);
Click ▶ Run to execute
dataFrameCov(df) returns the sample covariance matrix
(denominator n − 1). Pass { ddof: 0 } for
population covariance. Diagonal entries are the variance of each column.
import { DataFrame, dataFrameCov } from "tsb";
const returns = DataFrame.fromColumns({
AAPL: [ 0.02, 0.01, -0.03, 0.04, 0.01],
GOOG: [ 0.01, 0.02, -0.02, 0.03, 0.02],
TSLA: [-0.05, 0.08, -0.10, 0.12, -0.03],
});
const cov = dataFrameCov(returns);
console.log("Covariance matrix:");
for (const col of cov.columns.values) {
const vals = [...cov.col(col).values].map(v => (v).toFixed(6));
console.log(col + ":", vals.join(" "));
}
// Compare sample (ddof=1) vs population (ddof=0) variance
const varSample = dataFrameCov(returns, { ddof: 1 }).col("AAPL").iat(0);
const varPop = dataFrameCov(returns, { ddof: 0 }).col("AAPL").iat(0);
console.log("\nAAPL sample variance:", varSample.toFixed(6));
console.log("AAPL population variance:", varPop.toFixed(6));
Click ▶ Run to execute
Write your own corr & cov code below. All exports from tsb are available:
DataFrame, Series, pearsonCorr,
dataFrameCorr, dataFrameCov, and more.
import { DataFrame, Series, pearsonCorr, dataFrameCorr, dataFrameCov } from "tsb";
// Try it! Explore correlation and covariance.
const a = new Series({ data: [1, 2, 3, 4, 5] });
const b = new Series({ data: [2, 4, 6, 8, 10] });
console.log("Perfect positive r:", pearsonCorr(a, b));
const df = DataFrame.fromColumns({
x: [1, 2, 3, 4, 5],
y: [5, 4, 3, 2, 1],
z: [2, 4, 6, 8, 10],
});
console.log("\nCorrelation matrix:");
for (const col of dataFrameCorr(df).columns.values) {
const vals = [...dataFrameCorr(df).col(col).values].map(v => (v).toFixed(2));
console.log(col + ":", vals.join(" "));
}
console.log("\nCovariance matrix:");
for (const col of dataFrameCov(df).columns.values) {
const vals = [...dataFrameCov(df).col(col).values].map(v => (v).toFixed(2));
console.log(col + ":", vals.join(" "));
}
Click ▶ Run to execute