Skip to content

Commit a2e7b12

Browse files
committed
refactor(algorithms): convert statistics functions to arrow functions
Convert all exported statistical functions from function declarations to arrow functions for consistent code style: - pairedTTest - wilcoxonSignedRank - bootstrapCI - bootstrapDifferenceTest Also convert internal helper functions to arrow functions.
1 parent 3b3a38b commit a2e7b12

File tree

4 files changed

+39
-69
lines changed

4 files changed

+39
-69
lines changed

packages/algorithms/src/evaluation/statistics/bootstrap.ts

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@
2323
* console.log(ci.mean); // e.g., 0.85
2424
* ```
2525
*/
26-
export function bootstrapCI(
27-
samples: number[],
28-
confidence: number = 0.95,
29-
nBootstrap: number = 10000,
30-
seed?: number
31-
): { lower: number; upper: number; mean: number } {
26+
export const bootstrapCI = (samples: number[], confidence: number = 0.95, nBootstrap: number = 10000, seed?: number): { lower: number; upper: number; mean: number } => {
3227
if (samples.length < 2) {
3328
throw new Error('Bootstrap requires at least 2 samples');
3429
}
@@ -67,7 +62,7 @@ export function bootstrapCI(
6762
const upper = bootstrapMeans[upperIndex]!;
6863

6964
return { lower, upper, mean: observedMean };
70-
}
65+
};
7166

7267
/**
7368
* Bootstrap test for significant difference between methods.
@@ -93,18 +88,12 @@ export function bootstrapCI(
9388
* console.log(result.ci.upper); // e.g., 0.22
9489
* ```
9590
*/
96-
export function bootstrapDifferenceTest(
97-
method1Samples: number[],
98-
method2Samples: number[],
99-
nBootstrap: number = 10000,
100-
alpha: number = 0.05,
101-
seed?: number
102-
): {
91+
export const bootstrapDifferenceTest = (method1Samples: number[], method2Samples: number[], nBootstrap: number = 10000, alpha: number = 0.05, seed?: number): {
10392
pValue: number;
10493
meanDifference: number;
10594
ci: { lower: number; upper: number };
10695
significant: boolean;
107-
} {
96+
} => {
10897
if (method1Samples.length < 2 || method2Samples.length < 2) {
10998
throw new Error('Both methods require at least 2 samples');
11099
}
@@ -171,7 +160,7 @@ export function bootstrapDifferenceTest(
171160
ci,
172161
significant,
173162
};
174-
}
163+
};
175164

176165
/**
177166
* Seeded random number generator for reproducible bootstrapping.

packages/algorithms/src/evaluation/statistics/effect-size.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
* console.log(result.interpretation); // 'very-large'
2828
* ```
2929
*/
30-
export function cohensD(group1: number[], group2: number[]): {
30+
export const cohensD = (group1: number[], group2: number[]): {
3131
effectSize: number;
3232
interpretation: 'negligible' | 'small' | 'medium' | 'large' | 'very-large';
3333
magnitude: number;
34-
} {
34+
} => {
3535
if (group1.length < 2 || group2.length < 2) {
3636
throw new Error('Cohen\'s d requires at least 2 samples per group');
3737
}
@@ -72,7 +72,7 @@ export function cohensD(group1: number[], group2: number[]): {
7272
interpretation,
7373
magnitude: absD,
7474
};
75-
}
75+
};
7676

7777
/**
7878
* Cliff's delta (non-parametric effect size).
@@ -101,12 +101,12 @@ export function cohensD(group1: number[], group2: number[]): {
101101
* console.log(result.probability); // e.g., 0.96 (96% chance treatment > control)
102102
* ```
103103
*/
104-
export function cliffsDelta(group1: number[], group2: number[]): {
104+
export const cliffsDelta = (group1: number[], group2: number[]): {
105105
effectSize: number;
106106
interpretation: 'negligible' | 'small' | 'medium' | 'large';
107107
probability: number;
108108
magnitude: number;
109-
} {
109+
} => {
110110
if (group1.length === 0 || group2.length === 0) {
111111
throw new Error('Cliff\'s delta requires at least 1 sample per group');
112112
}
@@ -152,7 +152,7 @@ export function cliffsDelta(group1: number[], group2: number[]): {
152152
probability,
153153
magnitude: absDelta,
154154
};
155-
}
155+
};
156156

157157
/**
158158
* Glass's delta (alternative to Cohen's d).
@@ -172,10 +172,10 @@ export function cliffsDelta(group1: number[], group2: number[]): {
172172
* console.log(result.effectSize); // e.g., 1.8
173173
* ```
174174
*/
175-
export function glassDelta(treatment: number[], control: number[]): {
175+
export const glassDelta = (treatment: number[], control: number[]): {
176176
effectSize: number;
177177
interpretation: 'negligible' | 'small' | 'medium' | 'large' | 'very-large';
178-
} {
178+
} => {
179179
if (treatment.length < 2 || control.length < 2) {
180180
throw new Error('Glass\'s delta requires at least 2 samples per group');
181181
}
@@ -206,7 +206,7 @@ export function glassDelta(treatment: number[], control: number[]): {
206206
}
207207

208208
return { effectSize, interpretation };
209-
}
209+
};
210210

211211
/**
212212
* Rank-biserial correlation (non-parametric effect size).
@@ -229,11 +229,11 @@ export function glassDelta(treatment: number[], control: number[]): {
229229
* console.log(result.effectSize); // e.g., 0.70 (large effect)
230230
* ```
231231
*/
232-
export function rankBiserialCorrelation(group1: number[], group2: number[]): {
232+
export const rankBiserialCorrelation = (group1: number[], group2: number[]): {
233233
effectSize: number;
234234
correlation: number;
235235
interpretation: 'negligible' | 'small' | 'medium' | 'large';
236-
} {
236+
} => {
237237
if (group1.length === 0 || group2.length === 0) {
238238
throw new Error('Rank-biserial correlation requires at least 1 sample per group');
239239
}
@@ -310,4 +310,4 @@ export function rankBiserialCorrelation(group1: number[], group2: number[]): {
310310
correlation,
311311
interpretation,
312312
};
313-
}
313+
};

packages/algorithms/src/evaluation/statistics/multiple-comparison.ts

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,7 @@
2020
* console.log(result.significant); // [true, false, false, false, false]
2121
* ```
2222
*/
23-
export function bonferroniCorrection(
24-
pValues: number[],
25-
alpha: number = 0.05
26-
): { correctedAlpha: number; significant: boolean[] } {
23+
export const bonferroniCorrection = (pValues: number[], alpha: number = 0.05): { correctedAlpha: number; significant: boolean[] } => {
2724
if (pValues.length === 0) {
2825
return { correctedAlpha: alpha, significant: [] };
2926
}
@@ -35,7 +32,7 @@ export function bonferroniCorrection(
3532
const significant = pValues.map(p => p < correctedAlpha);
3633

3734
return { correctedAlpha, significant };
38-
}
35+
};
3936

4037
/**
4138
* Benjamini-Hochberg FDR correction.
@@ -60,10 +57,7 @@ export function bonferroniCorrection(
6057
* console.log(result.significant); // [true, true, false, false, false]
6158
* ```
6259
*/
63-
export function benjaminiHochberg(
64-
pValues: number[],
65-
fdr: number = 0.05
66-
): { adjustedPValues: number[]; significant: boolean[] } {
60+
export const benjaminiHochberg = (pValues: number[], fdr: number = 0.05): { adjustedPValues: number[]; significant: boolean[] } => {
6761
if (pValues.length === 0) {
6862
return { adjustedPValues: [], significant: [] };
6963
}
@@ -100,7 +94,7 @@ export function benjaminiHochberg(
10094
}
10195

10296
return { adjustedPValues, significant };
103-
}
97+
};
10498

10599
/**
106100
* Holm-Bonferroni method (step-down procedure).
@@ -124,10 +118,7 @@ export function benjaminiHochberg(
124118
* console.log(result.significant); // [true, false, false, false, false]
125119
* ```
126120
*/
127-
export function holmBonferroni(
128-
pValues: number[],
129-
alpha: number = 0.05
130-
): { adjustedPValues: number[]; significant: boolean[] } {
121+
export const holmBonferroni = (pValues: number[], alpha: number = 0.05): { adjustedPValues: number[]; significant: boolean[] } => {
131122
if (pValues.length === 0) {
132123
return { adjustedPValues: [], significant: [] };
133124
}
@@ -181,7 +172,7 @@ export function holmBonferroni(
181172
}
182173

183174
return { adjustedPValues, significant };
184-
}
175+
};
185176

186177
/**
187178
* Storey's q-value method (FDR-based with estimation of true nulls).
@@ -202,11 +193,7 @@ export function holmBonferroni(
202193
* console.log(result.significant); // Which tests are significant at FDR=0.05
203194
* ```
204195
*/
205-
export function storeyQValues(
206-
pValues: number[],
207-
fdr: number = 0.05,
208-
lambda: number = 0.5
209-
): { qValues: number[]; significant: boolean[]; pi0: number } {
196+
export const storeyQValues = (pValues: number[], fdr: number = 0.05, lambda: number = 0.5): { qValues: number[]; significant: boolean[]; pi0: number } => {
210197
if (pValues.length === 0) {
211198
return { qValues: [], significant: [], pi0: 1 };
212199
}
@@ -243,4 +230,4 @@ export function storeyQValues(
243230
}
244231

245232
return { qValues, significant, pi0 };
246-
}
233+
};

packages/algorithms/src/evaluation/statistics/paired-tests.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,7 @@
2222
* console.log(result.significant); // true
2323
* ```
2424
*/
25-
export function pairedTTest(
26-
method1Results: number[],
27-
method2Results: number[],
28-
alpha: number = 0.05
29-
): { pValue: number; tStatistic: number; significant: boolean } {
25+
export const pairedTTest = (method1Results: number[], method2Results: number[], alpha: number = 0.05): { pValue: number; tStatistic: number; significant: boolean } => {
3026
if (method1Results.length !== method2Results.length) {
3127
throw new Error('Paired samples must have equal length');
3228
}
@@ -63,7 +59,7 @@ export function pairedTTest(
6359
tStatistic,
6460
significant: pValue < alpha,
6561
};
66-
}
62+
};
6763

6864
/**
6965
* Wilcoxon signed-rank test (non-parametric alternative to paired t-test).
@@ -84,11 +80,7 @@ export function pairedTTest(
8480
* console.log(result.pValue); // e.g., 0.031 (significant at α=0.05)
8581
* ```
8682
*/
87-
export function wilcoxonSignedRank(
88-
method1Results: number[],
89-
method2Results: number[],
90-
alpha: number = 0.05
91-
): { pValue: number; statistic: number; significant: boolean } {
83+
export const wilcoxonSignedRank = (method1Results: number[], method2Results: number[], alpha: number = 0.05): { pValue: number; statistic: number; significant: boolean } => {
9284
if (method1Results.length !== method2Results.length) {
9385
throw new Error('Paired samples must have equal length');
9486
}
@@ -170,15 +162,17 @@ export function wilcoxonSignedRank(
170162
statistic: w,
171163
significant: pValue < alpha,
172164
};
173-
}
165+
};
174166

175167
/**
176168
* Calculate two-tailed p-value from t-statistic using t-distribution.
177169
*
178170
* Uses approximation of t-distribution CDF.
179171
* For large degrees of freedom, converges to normal distribution.
172+
* @param t
173+
* @param df
180174
*/
181-
function twoTailedPValue(t: number, df: number): number {
175+
const twoTailedPValue = (t: number, df: number): number => {
182176
// Approximate t-distribution with normal for df > 30
183177
if (df > 30) {
184178
const z = standardScoreToZ(t, df);
@@ -189,22 +183,22 @@ function twoTailedPValue(t: number, df: number): number {
189183
// In production, use proper t-distribution tables or numerical integration
190184
const z = t; // Simplified - less accurate for small df
191185
return 2 * (1 - normalCDF(Math.abs(z)));
192-
}
186+
};
193187

194188
/**
195189
* Convert t-score to approximate z-score (Welch-Satterthwaite approximation).
190+
* @param t
191+
* @param df
196192
*/
197-
function standardScoreToZ(t: number, df: number): number {
198-
// For large df, t approaches normal
199-
return t;
200-
}
193+
const standardScoreToZ = (t: number, df: number): number => t;
201194

202195
/**
203196
* Standard normal cumulative distribution function.
204197
*
205198
* Uses Abramowitz and Stegun approximation (error < 0.00005).
199+
* @param x
206200
*/
207-
function normalCDF(x: number): number {
201+
const normalCDF = (x: number): number => {
208202
const a1 = 0.254829592;
209203
const a2 = -0.284496736;
210204
const a3 = 1.421413741;
@@ -219,4 +213,4 @@ function normalCDF(x: number): number {
219213
const y = 1.0 - (((((a5 * t + a4) * t) + a3) * t + a2) * t + a1) * t * Math.exp(-x * x);
220214

221215
return 0.5 * (1.0 + sign * y);
222-
}
216+
};

0 commit comments

Comments
 (0)