Skip to content

Commit 39d0257

Browse files
committed
store the complete KstestResult at each iteration
1 parent c0548f0 commit 39d0257

1 file changed

Lines changed: 69 additions & 11 deletions

File tree

dyson_equalizer/dyson_equalizer.py

Lines changed: 69 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import numpy as np
1010
from matplotlib import pyplot as plt
1111
from scipy.stats import ks_1samp
12+
from scipy.stats._stats_py import KstestResult # noqa
1213

1314
from dyson_equalizer.algorithm import compute_scaling_factors, compute_low_rank_approximation_mp, scale_matrix, \
1415
marchenko_pastur_cdf
@@ -17,11 +18,39 @@
1718

1819
@dataclass
1920
class IterationStatistics:
21+
"""
22+
This class stores the results of each iteration of the Dyson Equalizer
23+
24+
Attributes
25+
----------
26+
delta_Y_hat: float
27+
The mean absolute difference in values between Y_hat of this iteration and the previous one
28+
29+
x_hat_mean: float
30+
The mean of the x_hat values
31+
32+
y_hat_mean: float
33+
The mean of the y_hat values
34+
35+
kstest_result: KstestResult
36+
The result of the Kolmogorov-Smirnov test
37+
38+
"""
2039
delta_Y_hat: float = np.nan
21-
ks_pvalue: float = np.nan
2240
x_hat_mean: float = np.nan
2341
y_hat_mean: float = np.nan
42+
kstest_result: KstestResult = None
2443

44+
@property
45+
def ks_pvalue(self) -> float:
46+
"""
47+
the p-value of the Kolmogorov-Smirnov test
48+
Returns
49+
-------
50+
p-value: float
51+
the p-value of the Kolmogorov-Smirnov test
52+
"""
53+
return self.kstest_result.pvalue if self.kstest_result else np.nan
2554

2655
class DysonEqualizer:
2756
"""
@@ -143,7 +172,7 @@ def compute(
143172
self.S = svd.S
144173
self.S_hat = svd_hat.S
145174

146-
stats.ks_pvalue = self.ks_pvalue_Y_hat()
175+
stats.kstest_result = self.ks_Y_hat()
147176
self.iteration_statistics.append(stats)
148177
return self
149178

@@ -378,6 +407,24 @@ def plot_mp_density_Y_hat(
378407
ax=ax,
379408
)
380409

410+
411+
def ks_Y_hat(
412+
self
413+
) -> KstestResult:
414+
""" Computes the Kolmogorov–Smirnov test between the density of eigenvalues of ¹⁄ₙŶŶᵀ
415+
and the Marchenko-Pastur distribution
416+
417+
Returns
418+
-------
419+
result : KstestResult
420+
The result of the Kolmogorov–Smirnov test
421+
422+
"""
423+
m, n = sorted(self.Y_hat.shape)
424+
eigs = self.S_hat ** 2 / n
425+
ksr = ks_1samp(eigs, cdf=marchenko_pastur_cdf, args=[m/n])
426+
return ksr
427+
381428
def ks_pvalue_Y_hat(
382429
self
383430
) -> float:
@@ -386,14 +433,28 @@ def ks_pvalue_Y_hat(
386433
387434
Returns
388435
-------
389-
p-value : DysonEqualizer
436+
p-value : float
390437
The p-value of the Kolmogorov–Smirnov test
391438
392439
"""
393-
m, n = sorted(self.Y_hat.shape)
394-
eigs = self.S_hat ** 2 / n
440+
return self.ks_Y_hat().pvalue
441+
442+
def ks_Y(
443+
self
444+
) -> KstestResult:
445+
""" Computes the Kolmogorov–Smirnov test between the density of eigenvalues of ¹⁄ₙYYᵀ
446+
and the Marchenko-Pastur distribution
447+
448+
Returns
449+
-------
450+
result : KstestResult
451+
The result of the Kolmogorov–Smirnov test
452+
453+
"""
454+
m, n = sorted(self.Y.shape)
455+
eigs = self.S ** 2 / n
395456
ksr = ks_1samp(eigs, cdf=marchenko_pastur_cdf, args=[m/n])
396-
return ksr.pvalue
457+
return ksr
397458

398459
def ks_pvalue_Y(
399460
self
@@ -403,11 +464,8 @@ def ks_pvalue_Y(
403464
404465
Returns
405466
-------
406-
p-value : DysonEqualizer
467+
p-value : float
407468
The p-value of the Kolmogorov–Smirnov test
408469
409470
"""
410-
m, n = sorted(self.Y.shape)
411-
eigs = self.S ** 2 / n
412-
ksr = ks_1samp(eigs, cdf=marchenko_pastur_cdf, args=[m/n])
413-
return ksr.pvalue
471+
return self.ks_Y().pvalue

0 commit comments

Comments
 (0)