def test_z_score(): p = np.random.rand(10) assert_array_almost_equal(norm.sf(z_score(p)), p) # check the numerical precision for p in [1.e-250, 1 - 1.e-16]: assert_array_almost_equal(z_score(p), norm.isf(p)) assert_array_almost_equal(z_score(np.float32(1.e-100)), norm.isf(1.e-300))
def z_score(self, baseline=0.0): """Return a parametric estimation of the z-score associated with the null hypothesis: (H0) 'contrast equals baseline' Parameters ---------- baseline : float, optional, Baseline value for the test statistic. Default=0.0. Returns ------- z_score : 1-d array, shape=(n_voxels,) statistical values, one per voxel """ if self.p_value_ is None or not self.baseline == baseline: self.p_value_ = self.p_value(baseline) if self.one_minus_pvalue_ is None: self.one_minus_pvalue_ = self.one_minus_pvalue(baseline) # Avoid inf values kindly supplied by scipy. self.z_score_ = z_score(self.p_value_, one_minus_pvalue=self.one_minus_pvalue_) return self.z_score_
def test_z_score(): # ################# Check z-scores computed from t-values ################# # Randomly draw samples from the standard Student’s t distribution tval = np.random.RandomState(42).standard_t(10, size=10) # Estimate the p-values using the Survival Function (SF) pval = sps.t.sf(tval, 1e10) # Estimate the p-values using the Cumulative Distribution Function (CDF) cdfval = sps.t.cdf(tval, 1e10) # Set a minimum threshold for p-values to avoid infinite z-scores pval = np.array(np.minimum(np.maximum(pval, 1.e-300), 1. - 1.e-16)) cdfval = np.array(np.minimum(np.maximum(cdfval, 1.e-300), 1. - 1.e-16)) # Compute z-score from the p-value estimated with the SF zval_sf = norm.isf(pval) # Compute z-score from the p-value estimated with the CDF zval_cdf = norm.ppf(cdfval) # Create the final array of z-scores, ... zval = np.zeros(pval.size) # ... in which z-scores < 0 estimated w/ SF are replaced by z-scores < 0 # estimated w/ CDF zval[np.atleast_1d(zval_sf < 0)] = zval_cdf[zval_sf < 0] # ... and z-scores >=0 estimated from SF are kept. zval[np.atleast_1d(zval_sf >= 0)] = zval_sf[zval_sf >= 0] # Test 'z_score' function in 'nilearn/glm/contrasts.py' assert_array_almost_equal(z_score(pval, one_minus_pvalue=cdfval), zval) # Test 'z_score' function in 'nilearn/glm/contrasts.py', # when one_minus_pvalue is None assert_array_almost_equal(norm.sf(z_score(pval)), pval) # ################# Check z-scores computed from F-values ################# # Randomly draw samples from the F distribution fval = np.random.RandomState(42).f(1, 48, size=10) # Estimate the p-values using the Survival Function (SF) p_val = sps.f.sf(fval, 42, 1e10) # Estimate the p-values using the Cumulative Distribution Function (CDF) cdf_val = sps.f.cdf(fval, 42, 1e10) # Set a minimum threshold for p-values to avoid infinite z-scores p_val = np.array(np.minimum(np.maximum(p_val, 1.e-300), 1. - 1.e-16)) cdf_val = np.array(np.minimum(np.maximum(cdf_val, 1.e-300), 1. - 1.e-16)) # Compute z-score from the p-value estimated with the SF z_val_sf = norm.isf(p_val) # Compute z-score from the p-value estimated with the CDF z_val_cdf = norm.ppf(cdf_val) # Create the final array of z-scores, ... z_val = np.zeros(p_val.size) # ... in which z-scores < 0 estimated w/ SF are replaced by z-scores < 0 # estimated w/ CDF z_val[np.atleast_1d(z_val_sf < 0)] = z_val_cdf[z_val_sf < 0] # ... and z-scores >=0 estimated from SF are kept. z_val[np.atleast_1d(z_val_sf >= 0)] = z_val_sf[z_val_sf >= 0] # Test 'z_score' function in 'nilearn/glm/contrasts.py' assert_array_almost_equal(z_score(p_val, one_minus_pvalue=cdf_val), z_val) # Test 'z_score' function in 'nilearn/glm/contrasts.py', # when one_minus_pvalue is None assert_array_almost_equal(norm.sf(z_score(p_val)), p_val) # ##################### Check the numerical precision ##################### for t in [33.75, -8.3]: p = sps.t.sf(t, 1e10) cdf = sps.t.cdf(t, 1e10) z_sf = norm.isf(p) z_cdf = norm.ppf(cdf) if p <= .5: z = z_sf else: z = z_cdf assert_array_almost_equal(z_score(p, one_minus_pvalue=cdf), z)