incrementalstats.welcht
1from __future__ import annotations 2import numpy as np 3 4from .mean_var import IncrementalMeanVariance 5 6class IncrementalWelcht: 7 """ 8 Incremental Welch-t between 2 groups (0 and not 0) 9 """ 10 11 def __init__(self, nsamples): 12 """Initialize with the #samples we're computing Welch-t over""" 13 nX = nsamples 14 self._mv0 = IncrementalMeanVariance(nX) 15 self._mv1 = IncrementalMeanVariance(nX) 16 self._n = 0 17 18 def add(self, other: IncrementalWelcht): 19 """Merges another object of IncrementalWelcht into this object. This is useful in 20 parallelized computations, where different nodes compute Welcht-t over different 21 ranges of rows""" 22 self._mv0.add(other.mv0) 23 self._mv1.add(other.mv1) 24 25 def update(self, group, row): 26 """Updates the Welch-t state with a group id (0 or not 0) and a single row of samples""" 27 if group == 0: 28 self._mv0.update(row) 29 else: 30 self._mv1.update(row) 31 32 def getWelcht(self): 33 """Returns the Welch-t statistic. NaNs are zero'd and a small factor is added in the 34 denominator to prevent infinities""" 35 m0 = self._mv0.getMean() 36 v0 = self._mv0.getVariance() 37 n0 = self._mv0.getN() 38 m1 = self._mv1.getMean() 39 v1 = self._mv1.getVariance() 40 n1 = self._mv1.getN() 41 42 x = (m0 - m1) / np.sqrt((v0 / n0) + (v1 / n1) + 1e-12) 43 44 x = np.nan_to_num(x, copy = False) 45 46 return x 47 48 def getN(self): 49 """Number of observations""" 50 return self._mv0.getN() + self._mv1.getN()
class
IncrementalWelcht:
7class IncrementalWelcht: 8 """ 9 Incremental Welch-t between 2 groups (0 and not 0) 10 """ 11 12 def __init__(self, nsamples): 13 """Initialize with the #samples we're computing Welch-t over""" 14 nX = nsamples 15 self._mv0 = IncrementalMeanVariance(nX) 16 self._mv1 = IncrementalMeanVariance(nX) 17 self._n = 0 18 19 def add(self, other: IncrementalWelcht): 20 """Merges another object of IncrementalWelcht into this object. This is useful in 21 parallelized computations, where different nodes compute Welcht-t over different 22 ranges of rows""" 23 self._mv0.add(other.mv0) 24 self._mv1.add(other.mv1) 25 26 def update(self, group, row): 27 """Updates the Welch-t state with a group id (0 or not 0) and a single row of samples""" 28 if group == 0: 29 self._mv0.update(row) 30 else: 31 self._mv1.update(row) 32 33 def getWelcht(self): 34 """Returns the Welch-t statistic. NaNs are zero'd and a small factor is added in the 35 denominator to prevent infinities""" 36 m0 = self._mv0.getMean() 37 v0 = self._mv0.getVariance() 38 n0 = self._mv0.getN() 39 m1 = self._mv1.getMean() 40 v1 = self._mv1.getVariance() 41 n1 = self._mv1.getN() 42 43 x = (m0 - m1) / np.sqrt((v0 / n0) + (v1 / n1) + 1e-12) 44 45 x = np.nan_to_num(x, copy = False) 46 47 return x 48 49 def getN(self): 50 """Number of observations""" 51 return self._mv0.getN() + self._mv1.getN()
Incremental Welch-t between 2 groups (0 and not 0)
IncrementalWelcht(nsamples)
12 def __init__(self, nsamples): 13 """Initialize with the #samples we're computing Welch-t over""" 14 nX = nsamples 15 self._mv0 = IncrementalMeanVariance(nX) 16 self._mv1 = IncrementalMeanVariance(nX) 17 self._n = 0
Initialize with the #samples we're computing Welch-t over
19 def add(self, other: IncrementalWelcht): 20 """Merges another object of IncrementalWelcht into this object. This is useful in 21 parallelized computations, where different nodes compute Welcht-t over different 22 ranges of rows""" 23 self._mv0.add(other.mv0) 24 self._mv1.add(other.mv1)
Merges another object of IncrementalWelcht into this object. This is useful in parallelized computations, where different nodes compute Welcht-t over different ranges of rows
def
update(self, group, row):
26 def update(self, group, row): 27 """Updates the Welch-t state with a group id (0 or not 0) and a single row of samples""" 28 if group == 0: 29 self._mv0.update(row) 30 else: 31 self._mv1.update(row)
Updates the Welch-t state with a group id (0 or not 0) and a single row of samples
def
getWelcht(self):
33 def getWelcht(self): 34 """Returns the Welch-t statistic. NaNs are zero'd and a small factor is added in the 35 denominator to prevent infinities""" 36 m0 = self._mv0.getMean() 37 v0 = self._mv0.getVariance() 38 n0 = self._mv0.getN() 39 m1 = self._mv1.getMean() 40 v1 = self._mv1.getVariance() 41 n1 = self._mv1.getN() 42 43 x = (m0 - m1) / np.sqrt((v0 / n0) + (v1 / n1) + 1e-12) 44 45 x = np.nan_to_num(x, copy = False) 46 47 return x
Returns the Welch-t statistic. NaNs are zero'd and a small factor is added in the denominator to prevent infinities