added more functions
This commit is contained in:
parent
a1c82ae0b8
commit
cca9ac03dd
8 changed files with 1563 additions and 2 deletions
|
|
@ -101,6 +101,8 @@ export function pearsonCorrelation(
|
|||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Calculate Spearman rank correlation coefficient
|
||||
*/
|
||||
|
|
@ -602,8 +604,276 @@ export function grangerCausalityTest(
|
|||
optimalLag: bestLag
|
||||
};
|
||||
}
|
||||
/**
|
||||
* Calculate Distance Correlation
|
||||
*/
|
||||
export function distanceCorrelation(x: number[], y: number[]): CorrelationResult {
|
||||
if (x.length !== y.length || x.length < 2) {
|
||||
throw new Error('Arrays must have same length and at least 2 observations');
|
||||
}
|
||||
|
||||
// Helper functions
|
||||
const n = x.length;
|
||||
|
||||
// Calculate distance matrices
|
||||
const a = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
const b = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
a[i][j] = Math.abs(x[i] - x[j]);
|
||||
b[i][j] = Math.abs(y[i] - y[j]);
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate double centered distance matrices
|
||||
const aMeanRow = a.map(row => row.reduce((sum, val) => sum + val, 0) / n);
|
||||
const bMeanRow = b.map(row => row.reduce((sum, val) => sum + val, 0) / n);
|
||||
const aMeanTotal = aMeanRow.reduce((sum, val) => sum + val, 0) / n;
|
||||
const bMeanTotal = bMeanRow.reduce((sum, val) => sum + val, 0) / n;
|
||||
|
||||
const A = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
const B = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
A[i][j] = a[i][j] - aMeanRow[i] - aMeanRow[j] + aMeanTotal;
|
||||
B[i][j] = b[i][j] - bMeanRow[i] - bMeanRow[j] + bMeanTotal;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate distance covariance and variances
|
||||
let dcov = 0;
|
||||
let dvarX = 0;
|
||||
let dvarY = 0;
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
dcov += A[i][j] * B[i][j];
|
||||
dvarX += A[i][j] * A[i][j];
|
||||
dvarY += B[i][j] * B[i][j];
|
||||
}
|
||||
}
|
||||
|
||||
dcov = Math.sqrt(dcov / (n * n));
|
||||
dvarX = Math.sqrt(dvarX / (n * n));
|
||||
dvarY = Math.sqrt(dvarY / (n * n));
|
||||
|
||||
const correlation = dvarX * dvarY === 0 ? 0 : dcov / Math.sqrt(dvarX * dvarY);
|
||||
|
||||
// Approximate p-value (permutation test)
|
||||
let pValue = 1;
|
||||
const numPermutations = 100;
|
||||
|
||||
for (let p = 0; p < numPermutations; p++) {
|
||||
const yPermuted = shuffleArray([...y]);
|
||||
const bPermuted = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
bPermuted[i][j] = Math.abs(yPermuted[i] - yPermuted[j]);
|
||||
}
|
||||
}
|
||||
|
||||
const bMeanRowPermuted = bPermuted.map(row => row.reduce((sum, val) => sum + val, 0) / n);
|
||||
const bMeanTotalPermuted = bMeanRowPermuted.reduce((sum, val) => sum + val, 0) / n;
|
||||
|
||||
const BPermuted = Array(n).fill(null).map(() => Array(n).fill(0));
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
BPermuted[i][j] = bPermuted[i][j] - bMeanRowPermuted[i] - bMeanRowPermuted[j] + bMeanTotalPermuted;
|
||||
}
|
||||
}
|
||||
|
||||
let dcovPermuted = 0;
|
||||
for (let i = 0; i < n; i++) {
|
||||
for (let j = 0; j < n; j++) {
|
||||
dcovPermuted += A[i][j] * BPermuted[i][j];
|
||||
}
|
||||
}
|
||||
dcovPermuted = Math.sqrt(dcovPermuted / (n * n));
|
||||
|
||||
if (dcovPermuted >= dcov) {
|
||||
pValue++;
|
||||
}
|
||||
}
|
||||
|
||||
pValue /= (numPermutations + 1);
|
||||
const significance = pValue < 0.05;
|
||||
|
||||
return {
|
||||
correlation,
|
||||
pValue,
|
||||
significance
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Mutual Information
|
||||
*/
|
||||
export function mutualInformation(x: number[], y: number[], numBins: number = 10): CorrelationResult {
|
||||
if (x.length !== y.length || x.length < 2) {
|
||||
throw new Error('Arrays must have same length and at least 2 observations');
|
||||
}
|
||||
|
||||
const n = x.length;
|
||||
|
||||
// Calculate histograms
|
||||
const xMin = Math.min(...x);
|
||||
const xMax = Math.max(...x);
|
||||
const yMin = Math.min(...y);
|
||||
const yMax = Math.max(...y);
|
||||
|
||||
const xBinWidth = (xMax - xMin) / numBins;
|
||||
const yBinWidth = (yMax - yMin) / numBins;
|
||||
|
||||
const jointHistogram = Array(numBins).fill(null).map(() => Array(numBins).fill(0));
|
||||
const xHistogram = Array(numBins).fill(0);
|
||||
const yHistogram = Array(numBins).fill(0);
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
const xBin = Math.floor((x[i] - xMin) / xBinWidth);
|
||||
const yBin = Math.floor((y[i] - yMin) / yBinWidth);
|
||||
|
||||
if (xBin >= 0 && xBin < numBins && yBin >= 0 && yBin < numBins) {
|
||||
jointHistogram[xBin][yBin]++;
|
||||
xHistogram[xBin]++;
|
||||
yHistogram[yBin]++;
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate probabilities
|
||||
const jointProbabilities = jointHistogram.map(row => row.map(count => count / n));
|
||||
const xProbabilities = xHistogram.map(count => count / n);
|
||||
const yProbabilities = yHistogram.map(count => count / n);
|
||||
|
||||
// Calculate mutual information
|
||||
let mi = 0;
|
||||
for (let i = 0; i < numBins; i++) {
|
||||
for (let j = 0; j < numBins; j++) {
|
||||
if (jointProbabilities[i][j] > 0 && xProbabilities[i] > 0 && yProbabilities[j] > 0) {
|
||||
mi += jointProbabilities[i][j] * Math.log(jointProbabilities[i][j] / (xProbabilities[i] * yProbabilities[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const correlation = mi; // Use MI as correlation measure
|
||||
|
||||
// Approximate p-value (permutation test)
|
||||
let pValue = 1;
|
||||
const numPermutations = 100;
|
||||
|
||||
for (let p = 0; p < numPermutations; p++) {
|
||||
const yPermuted = shuffleArray([...y]);
|
||||
let miPermuted = 0;
|
||||
|
||||
const jointHistogramPermuted = Array(numBins).fill(null).map(() => Array(numBins).fill(0));
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
const xBin = Math.floor((x[i] - xMin) / xBinWidth);
|
||||
const yBin = Math.floor((yPermuted[i] - yMin) / yBinWidth);
|
||||
|
||||
if (xBin >= 0 && xBin < numBins && yBin >= 0 && yBin < numBins) {
|
||||
jointHistogramPermuted[xBin][yBin]++;
|
||||
}
|
||||
}
|
||||
|
||||
const jointProbabilitiesPermuted = jointHistogramPermuted.map(row => row.map(count => count / n));
|
||||
|
||||
for (let i = 0; i < numBins; i++) {
|
||||
for (let j = 0; j < numBins; j++) {
|
||||
if (jointProbabilitiesPermuted[i][j] > 0 && xProbabilities[i] > 0 && yProbabilities[j] > 0) {
|
||||
miPermuted += jointProbabilitiesPermuted[i][j] * Math.log(jointProbabilitiesPermuted[i][j] / (xProbabilities[i] * yProbabilities[j]));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (miPermuted >= mi) {
|
||||
pValue++;
|
||||
}
|
||||
}
|
||||
|
||||
pValue /= (numPermutations + 1);
|
||||
const significance = pValue < 0.05;
|
||||
|
||||
return {
|
||||
correlation,
|
||||
pValue,
|
||||
significance
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Cross-Correlation
|
||||
*/
|
||||
export function crossCorrelation(x: number[], y: number[], maxLag: number): number[] {
|
||||
const n = x.length;
|
||||
if (n !== y.length) {
|
||||
throw new Error('Arrays must have the same length');
|
||||
}
|
||||
|
||||
const correlations: number[] = [];
|
||||
|
||||
for (let lag = -maxLag; lag <= maxLag; lag++) {
|
||||
let sum = 0;
|
||||
let count = 0;
|
||||
|
||||
for (let i = 0; i < n; i++) {
|
||||
const yIndex = i + lag;
|
||||
|
||||
if (yIndex >= 0 && yIndex < n) {
|
||||
sum += (x[i] - average(x)) * (y[yIndex] - average(y));
|
||||
count++;
|
||||
}
|
||||
}
|
||||
|
||||
const stdX = Math.sqrt(x.reduce((sum, xi) => sum + Math.pow(xi - average(x), 2), 0) / (n - 1));
|
||||
const stdY = Math.sqrt(y.reduce((sum, yi) => sum + Math.pow(yi - average(y), 2), 0) / (n - 1));
|
||||
|
||||
const correlation = count > 0 ? sum / ((count - 1) * stdX * stdY) : 0;
|
||||
correlations.push(correlation);
|
||||
}
|
||||
|
||||
return correlations;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate Autocorrelation
|
||||
*/
|
||||
export function autocorrelation(x: number[], lag: number): number {
|
||||
const n = x.length;
|
||||
if (lag >= n) {
|
||||
throw new Error('Lag must be less than the length of the array');
|
||||
}
|
||||
|
||||
let sum = 0;
|
||||
for (let i = lag; i < n; i++) {
|
||||
sum += (x[i] - average(x)) * (x[i - lag] - average(x));
|
||||
}
|
||||
|
||||
const std = Math.sqrt(x.reduce((sum, xi) => sum + Math.pow(xi - average(x), 2), 0) / (n - 1));
|
||||
return sum / ((n - lag - 1) * std * std);
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to shuffle an array (Fisher-Yates shuffle)
|
||||
*/
|
||||
function shuffleArray<T>(array: T[]): T[] {
|
||||
const newArray = [...array];
|
||||
for (let i = newArray.length - 1; i > 0; i--) {
|
||||
const j = Math.floor(Math.random() * (i + 1));
|
||||
[newArray[i], newArray[j]] = [newArray[j], newArray[i]];
|
||||
}
|
||||
return newArray;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to calculate the average of an array of numbers
|
||||
*/
|
||||
function average(arr: number[]): number {
|
||||
if (arr.length === 0) return 0;
|
||||
return arr.reduce((a, b) => a + b, 0) / arr.length;
|
||||
}
|
||||
|
||||
function getRanks(arr: number[]): number[] {
|
||||
const sorted = arr.map((val, idx) => ({ val, idx })).sort((a, b) => a.val - b.val);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue