def test_maha(): # Want implementation of Mahalanobis distance to match this R session: # > x1 <- round(rnorm(10,3), 3) # > x2 <- round(x1 + rnorm(10), 3) # > x3 <- round(x2 + runif(10), 3) # > x1 # [1] 3.853 2.401 2.253 3.067 1.887 3.293 3.995 2.559 2.785 2.228 # > x2 # [1] 4.294 1.915 1.315 4.641 1.611 2.838 3.696 1.337 2.853 2.434 # > x3 # [1] 4.785 2.352 2.023 4.978 2.329 3.101 4.494 2.204 3.468 3.075 # > obs <- cbind(x1, x2, x3) # > S <- var(obs) # > S # x1 x2 x3 # x1 0.5020374 0.6667232 0.633355 # x2 0.6667232 1.4434718 1.326026 # x3 0.6333550 1.3260262 1.248315 # > mahalanobis(obs, c(mean(x1), mean(x2), mean(x3)), S) # [1] 2.1838336 1.9673401 1.3335029 4.9191627 2.1246818 5.3297995 4.9022487 # [8] 2.5335913 0.1952562 1.5105832 from scitbx.array_family import flex from dials.algorithms.statistics.fast_mcd import cov, maha_dist_sq # test Mahalanobis distance. x1 = flex.double( (3.853, 2.401, 2.253, 3.067, 1.887, 3.293, 3.995, 2.559, 2.785, 2.228)) x2 = flex.double( (4.294, 1.915, 1.315, 4.641, 1.611, 2.838, 3.696, 1.337, 2.853, 2.434)) x3 = flex.double( (4.785, 2.352, 2.023, 4.978, 2.329, 3.101, 4.494, 2.204, 3.468, 3.075)) cols = [x1, x2, x3] center = [flex.mean(e) for e in cols] covmat = cov(x1, x2, x3) maha = maha_dist_sq(cols, center, covmat) from libtbx.test_utils import approx_equal R_result = [ 2.1838336, 1.9673401, 1.3335029, 4.9191627, 2.1246818, 5.3297995, 4.9022487, 2.5335913, 0.1952562, 1.5105832, ] assert approx_equal(list(maha), R_result)
def test_maha(): # Want implementation of Mahalanobis distance to match this R session: #> x1 <- round(rnorm(10,3), 3) #> x2 <- round(x1 + rnorm(10), 3) #> x3 <- round(x2 + runif(10), 3) #> x1 # [1] 3.853 2.401 2.253 3.067 1.887 3.293 3.995 2.559 2.785 2.228 #> x2 # [1] 4.294 1.915 1.315 4.641 1.611 2.838 3.696 1.337 2.853 2.434 #> x3 # [1] 4.785 2.352 2.023 4.978 2.329 3.101 4.494 2.204 3.468 3.075 #> obs <- cbind(x1, x2, x3) #> S <- var(obs) #> S # x1 x2 x3 #x1 0.5020374 0.6667232 0.633355 #x2 0.6667232 1.4434718 1.326026 #x3 0.6333550 1.3260262 1.248315 #> mahalanobis(obs, c(mean(x1), mean(x2), mean(x3)), S) # [1] 2.1838336 1.9673401 1.3335029 4.9191627 2.1246818 5.3297995 4.9022487 # [8] 2.5335913 0.1952562 1.5105832 from scitbx.array_family import flex from dials.algorithms.statistics.fast_mcd import maha_dist_sq, cov # test Mahalanobis distance. x1 = flex.double((3.853, 2.401, 2.253, 3.067, 1.887, 3.293, 3.995, 2.559, 2.785, 2.228)) x2 = flex.double((4.294, 1.915, 1.315, 4.641, 1.611, 2.838, 3.696, 1.337, 2.853, 2.434)) x3 = flex.double((4.785, 2.352, 2.023, 4.978, 2.329, 3.101, 4.494, 2.204, 3.468, 3.075)) cols = [x1, x2, x3] center = [flex.mean(e) for e in cols] covmat = cov(x1, x2, x3) n = len(cols[0]) maha = maha_dist_sq(cols, center, covmat) from libtbx.test_utils import approx_equal R_result = [2.1838336, 1.9673401, 1.3335029, 4.9191627, 2.1246818, 5.3297995, 4.9022487, 2.5335913, 0.1952562, 1.5105832] assert approx_equal(list(maha), R_result) print "OK" return