als-statistics
Version:
A powerful and lightweight JavaScript library for descriptive statistics, regression, clustering, outlier detection, and noise analysis using a flexible table/column architecture.
21 lines (19 loc) • 651 B
JavaScript
const { tCDF } = require('./cdf')
class PearsonP {
constructor(n, cov,sigX, sigY) {
this.r = (sigX === 0 || sigY === 0) ? 0 : cov / (sigX * sigY)
this.df = n - 2;
this.t = 0;
this.p = 1;
if (this.df > 1) this.calculateP(this)
}
get significant() { return this.p < 0.0001 }
get sig() { return (this.p < 0.0001) ? "<0.0001" : this.p.toFixed(4) }
calculateP({ r, df }) {
this.t = r * Math.sqrt(df / Math.max(1 - r * r, 1e-16));
this.p = 2 * (1 - tCDF(Math.abs(this.t), df));
if (this.p < 0) this.p = 0;
if (this.p > 1) this.p = 1;
}
}
module.exports = PearsonP