Mahalanobis
computes the squared Mahalanobis distance of
all rows in x
and the vector \(\mu\) = center
with
respect to \(\Sigma\) = cov
. This is (for vector x
)
defined as $$D^2 = (x - \mu)' \Sigma^{-1} (x - \mu)$$
Value
A matrix with 1 column and \(n\) rows representing the estimated distance for each observation.
Author
Simon Garnier, garnier@njit.edu
Examples
m <- matrix(c(rnorm(500, 6), rnorm(500, 11, 3)), ncol = 2)
w <- runif(500)
covar <- gravitree:::.wcov(m, w)
gravitree:::.Mahalanobis(m, covar$center, covar$cov)
#> [,1]
#> [1,] 2.445815711
#> [2,] 0.459763563
#> [3,] 5.897250217
#> [4,] 0.042577211
#> [5,] 1.928393423
#> [6,] 1.047890336
#> [7,] 3.886558636
#> [8,] 0.623677873
#> [9,] 1.138508305
#> [10,] 1.514488468
#> [11,] 1.971039076
#> [12,] 7.554240911
#> [13,] 3.648212343
#> [14,] 2.786811566
#> [15,] 0.230034749
#> [16,] 4.135206683
#> [17,] 1.220525350
#> [18,] 0.020600877
#> [19,] 1.997009667
#> [20,] 2.080813072
#> [21,] 0.765971295
#> [22,] 0.125467617
#> [23,] 2.087922470
#> [24,] 0.660847899
#> [25,] 3.176788753
#> [26,] 0.506772074
#> [27,] 0.940800198
#> [28,] 8.512542956
#> [29,] 1.584476103
#> [30,] 2.855151254
#> [31,] 1.221820563
#> [32,] 0.397688029
#> [33,] 0.328187046
#> [34,] 2.194050647
#> [35,] 0.215140614
#> [36,] 0.588576328
#> [37,] 4.090748679
#> [38,] 0.525626924
#> [39,] 0.191741613
#> [40,] 2.091291811
#> [41,] 3.444461939
#> [42,] 0.471981449
#> [43,] 1.978120079
#> [44,] 0.522828310
#> [45,] 0.188965727
#> [46,] 7.218340015
#> [47,] 0.228273441
#> [48,] 0.565798482
#> [49,] 0.391295374
#> [50,] 3.998332349
#> [51,] 1.306661063
#> [52,] 1.070353120
#> [53,] 3.488767320
#> [54,] 0.122163240
#> [55,] 0.065446161
#> [56,] 0.965782232
#> [57,] 5.056030779
#> [58,] 6.377850922
#> [59,] 3.064258626
#> [60,] 0.493535323
#> [61,] 1.187693751
#> [62,] 0.520418133
#> [63,] 4.346013227
#> [64,] 2.274522118
#> [65,] 8.672388880
#> [66,] 2.077334460
#> [67,] 1.517186703
#> [68,] 0.558074445
#> [69,] 0.419290688
#> [70,] 2.930989874
#> [71,] 2.832142960
#> [72,] 2.080441235
#> [73,] 2.289441717
#> [74,] 0.487781413
#> [75,] 1.974048051
#> [76,] 0.253378903
#> [77,] 3.575772501
#> [78,] 0.903789418
#> [79,] 0.012929770
#> [80,] 0.273442986
#> [81,] 3.509706979
#> [82,] 1.377307891
#> [83,] 7.325480451
#> [84,] 0.223133369
#> [85,] 0.496315474
#> [86,] 2.958641281
#> [87,] 7.376475964
#> [88,] 1.488368675
#> [89,] 0.145532052
#> [90,] 0.471644984
#> [91,] 2.464198787
#> [92,] 1.041825169
#> [93,] 0.866043622
#> [94,] 0.366985793
#> [95,] 0.802069603
#> [96,] 2.343761673
#> [97,] 0.503268979
#> [98,] 0.698735948
#> [99,] 1.869708191
#> [100,] 4.052819367
#> [101,] 0.258392760
#> [102,] 1.130333460
#> [103,] 1.387738620
#> [104,] 4.965043441
#> [105,] 3.254553939
#> [106,] 1.809393107
#> [107,] 1.305329801
#> [108,] 0.597460419
#> [109,] 0.738682979
#> [110,] 0.764569827
#> [111,] 1.683170061
#> [112,] 8.141406837
#> [113,] 2.483695659
#> [114,] 0.439203375
#> [115,] 1.250886929
#> [116,] 3.856443377
#> [117,] 1.918819038
#> [118,] 3.490688788
#> [119,] 0.464230024
#> [120,] 3.254960141
#> [121,] 4.677650898
#> [122,] 2.340268975
#> [123,] 2.674801445
#> [124,] 0.579417197
#> [125,] 0.835158462
#> [126,] 1.311727489
#> [127,] 0.255941473
#> [128,] 0.803196903
#> [129,] 0.589182764
#> [130,] 0.924582915
#> [131,] 1.378645240
#> [132,] 1.971716297
#> [133,] 0.993167081
#> [134,] 1.855674734
#> [135,] 1.828459261
#> [136,] 1.626906036
#> [137,] 2.110165468
#> [138,] 2.330316956
#> [139,] 0.926836412
#> [140,] 0.723974358
#> [141,] 0.710826289
#> [142,] 0.766046802
#> [143,] 3.202522298
#> [144,] 1.149168903
#> [145,] 1.466898108
#> [146,] 1.402565295
#> [147,] 1.375568985
#> [148,] 0.802833706
#> [149,] 3.734838124
#> [150,] 2.022576894
#> [151,] 0.770372269
#> [152,] 0.037174385
#> [153,] 1.504868795
#> [154,] 0.449517615
#> [155,] 0.859734540
#> [156,] 0.809271840
#> [157,] 0.421783751
#> [158,] 1.474689057
#> [159,] 0.698003967
#> [160,] 0.975519113
#> [161,] 4.951988969
#> [162,] 1.654574166
#> [163,] 0.367726415
#> [164,] 0.653500354
#> [165,] 3.168926367
#> [166,] 1.560669520
#> [167,] 1.443552779
#> [168,] 0.089286111
#> [169,] 0.302148608
#> [170,] 6.543543435
#> [171,] 5.150591420
#> [172,] 1.222967229
#> [173,] 1.484949285
#> [174,] 1.237464508
#> [175,] 0.441489544
#> [176,] 0.962471023
#> [177,] 0.693559541
#> [178,] 3.188946189
#> [179,] 0.193210235
#> [180,] 7.434754594
#> [181,] 3.440860624
#> [182,] 5.678828683
#> [183,] 3.293035904
#> [184,] 1.947421906
#> [185,] 1.199394471
#> [186,] 7.843306553
#> [187,] 5.775801492
#> [188,] 7.020256730
#> [189,] 0.875009241
#> [190,] 1.778549191
#> [191,] 1.765078436
#> [192,] 0.762404721
#> [193,] 1.270218120
#> [194,] 0.442224674
#> [195,] 5.333073707
#> [196,] 0.392860339
#> [197,] 4.591474294
#> [198,] 1.136653459
#> [199,] 1.465851959
#> [200,] 0.257464255
#> [201,] 1.097468735
#> [202,] 1.708050910
#> [203,] 0.398281421
#> [204,] 0.111745836
#> [205,] 5.964121464
#> [206,] 0.460568015
#> [207,] 1.638013854
#> [208,] 0.475267866
#> [209,] 5.853102656
#> [210,] 6.080641738
#> [211,] 7.090289702
#> [212,] 0.179776046
#> [213,] 4.944582506
#> [214,] 1.799229945
#> [215,] 0.381176915
#> [216,] 1.545356193
#> [217,] 2.880881697
#> [218,] 0.229081187
#> [219,] 2.124590108
#> [220,] 7.079871687
#> [221,] 1.007108291
#> [222,] 2.404684097
#> [223,] 0.198351694
#> [224,] 1.947060869
#> [225,] 1.051409754
#> [226,] 1.241759333
#> [227,] 0.120401249
#> [228,] 0.379510917
#> [229,] 0.416133241
#> [230,] 2.748582921
#> [231,] 0.070793943
#> [232,] 2.378622807
#> [233,] 3.455822281
#> [234,] 2.783864504
#> [235,] 3.221214621
#> [236,] 0.862725895
#> [237,] 1.933844375
#> [238,] 0.327843406
#> [239,] 2.450245190
#> [240,] 4.064289267
#> [241,] 1.141442693
#> [242,] 2.002614995
#> [243,] 1.021262551
#> [244,] 3.924899583
#> [245,] 3.092338709
#> [246,] 1.598557223
#> [247,] 0.009193830
#> [248,] 1.015670360
#> [249,] 0.273906614
#> [250,] 0.011590898
#> [251,] 0.757044051
#> [252,] 1.191803404
#> [253,] 2.417214504
#> [254,] 1.269161505
#> [255,] 2.081690099
#> [256,] 0.780579492
#> [257,] 0.050995256
#> [258,] 2.766450446
#> [259,] 0.473591450
#> [260,] 2.412151230
#> [261,] 0.128138143
#> [262,] 4.324149840
#> [263,] 0.144724845
#> [264,] 2.709781670
#> [265,] 0.879801809
#> [266,] 4.691158578
#> [267,] 3.192705874
#> [268,] 1.856559614
#> [269,] 0.516272723
#> [270,] 0.061361261
#> [271,] 3.467460175
#> [272,] 5.572025940
#> [273,] 0.755633302
#> [274,] 0.369831469
#> [275,] 0.024629808
#> [276,] 3.664897029
#> [277,] 5.156703732
#> [278,] 0.405895619
#> [279,] 0.807971395
#> [280,] 1.060688499
#> [281,] 1.172438899
#> [282,] 1.093520164
#> [283,] 0.458809385
#> [284,] 0.499232290
#> [285,] 0.254337521
#> [286,] 1.183398857
#> [287,] 1.292867171
#> [288,] 5.744316394
#> [289,] 1.672889846
#> [290,] 2.310933071
#> [291,] 2.065576964
#> [292,] 1.274122997
#> [293,] 1.546065211
#> [294,] 1.318720857
#> [295,] 3.819022776
#> [296,] 2.488954904
#> [297,] 7.297141494
#> [298,] 0.583532933
#> [299,] 2.165248279
#> [300,] 4.134813111
#> [301,] 2.695970488
#> [302,] 1.338493506
#> [303,] 1.268512323
#> [304,] 1.750882021
#> [305,] 0.116002313
#> [306,] 0.934875823
#> [307,] 0.334618719
#> [308,] 6.169685658
#> [309,] 2.969734191
#> [310,] 2.527197847
#> [311,] 0.492359993
#> [312,] 4.317731243
#> [313,] 2.269431526
#> [314,] 4.084209556
#> [315,] 1.120910014
#> [316,] 0.001722332
#> [317,] 4.613215647
#> [318,] 1.112090321
#> [319,] 1.207245933
#> [320,] 1.078248508
#> [321,] 7.780862088
#> [322,] 2.044137880
#> [323,] 3.509263634
#> [324,] 0.156921720
#> [325,] 4.829253035
#> [326,] 2.407553241
#> [327,] 1.587710868
#> [328,] 1.759834883
#> [329,] 4.901374729
#> [330,] 0.299973399
#> [331,] 1.521518183
#> [332,] 4.559645309
#> [333,] 1.406576393
#> [334,] 1.738182264
#> [335,] 0.101831970
#> [336,] 1.406862482
#> [337,] 1.568726991
#> [338,] 1.675970175
#> [339,] 6.112661069
#> [340,] 0.593718971
#> [341,] 0.299265261
#> [342,] 3.827855488
#> [343,] 4.001075246
#> [344,] 5.945362164
#> [345,] 1.686603203
#> [346,] 2.890352632
#> [347,] 0.745973031
#> [348,] 1.139633489
#> [349,] 0.979373587
#> [350,] 2.545570389
#> [351,] 0.536029311
#> [352,] 0.337503625
#> [353,] 0.312269527
#> [354,] 2.602316428
#> [355,] 1.848358796
#> [356,] 3.450120522
#> [357,] 1.531987402
#> [358,] 6.841308093
#> [359,] 1.221097067
#> [360,] 1.059445466
#> [361,] 0.164084885
#> [362,] 1.470146432
#> [363,] 3.026527461
#> [364,] 2.951081366
#> [365,] 3.336104797
#> [366,] 0.749470152
#> [367,] 3.478886297
#> [368,] 1.599565872
#> [369,] 0.944646484
#> [370,] 1.679109166
#> [371,] 0.525692944
#> [372,] 0.880032023
#> [373,] 2.112133954
#> [374,] 1.283586273
#> [375,] 0.701881415
#> [376,] 1.108543490
#> [377,] 0.114676163
#> [378,] 0.826938030
#> [379,] 0.359194275
#> [380,] 1.674890239
#> [381,] 1.475719354
#> [382,] 0.203501316
#> [383,] 0.089684585
#> [384,] 1.136264639
#> [385,] 1.952054898
#> [386,] 0.141287510
#> [387,] 0.353729236
#> [388,] 0.720466606
#> [389,] 0.589909466
#> [390,] 1.112668439
#> [391,] 0.221963226
#> [392,] 1.418444247
#> [393,] 3.071686401
#> [394,] 1.567759666
#> [395,] 0.392934423
#> [396,] 6.151723811
#> [397,] 3.713560152
#> [398,] 3.194195938
#> [399,] 0.659350960
#> [400,] 0.421792271
#> [401,] 4.247330479
#> [402,] 3.862579438
#> [403,] 2.539150373
#> [404,] 0.845061456
#> [405,] 2.262178698
#> [406,] 2.208572666
#> [407,] 0.501061420
#> [408,] 4.991852587
#> [409,] 2.116874648
#> [410,] 3.257611776
#> [411,] 0.009028097
#> [412,] 0.603585952
#> [413,] 0.583637621
#> [414,] 2.223217738
#> [415,] 1.684700659
#> [416,] 0.282759988
#> [417,] 0.174404527
#> [418,] 3.403825512
#> [419,] 0.910364736
#> [420,] 0.018817519
#> [421,] 1.095837903
#> [422,] 2.011360264
#> [423,] 1.847886750
#> [424,] 3.410089547
#> [425,] 8.248817325
#> [426,] 0.844530912
#> [427,] 0.589342376
#> [428,] 5.196575889
#> [429,] 6.051567603
#> [430,] 2.868899738
#> [431,] 0.178156082
#> [432,] 0.893300743
#> [433,] 0.215098996
#> [434,] 1.074115134
#> [435,] 4.790834691
#> [436,] 1.133394490
#> [437,] 3.115423482
#> [438,] 0.293067113
#> [439,] 0.833051431
#> [440,] 3.497971766
#> [441,] 2.715483019
#> [442,] 0.367580654
#> [443,] 1.817618055
#> [444,] 1.063534455
#> [445,] 0.146704719
#> [446,] 0.354191392
#> [447,] 3.684019961
#> [448,] 1.198070023
#> [449,] 2.646811065
#> [450,] 0.213525620
#> [451,] 2.767726312
#> [452,] 0.217205949
#> [453,] 3.228247975
#> [454,] 5.944578857
#> [455,] 0.236562187
#> [456,] 4.910084990
#> [457,] 4.490571519
#> [458,] 0.171956340
#> [459,] 1.064928359
#> [460,] 0.158416447
#> [461,] 1.987391638
#> [462,] 0.107332464
#> [463,] 7.104836642
#> [464,] 0.692787906
#> [465,] 1.664303093
#> [466,] 1.590761191
#> [467,] 3.665835618
#> [468,] 0.039357522
#> [469,] 2.376093115
#> [470,] 0.377560316
#> [471,] 1.608163065
#> [472,] 1.212613561
#> [473,] 1.478031710
#> [474,] 4.871499825
#> [475,] 2.107748679
#> [476,] 3.161797786
#> [477,] 4.293864167
#> [478,] 0.204603354
#> [479,] 2.063045294
#> [480,] 2.623249768
#> [481,] 0.292071610
#> [482,] 1.235161887
#> [483,] 0.445685376
#> [484,] 1.661981325
#> [485,] 3.957527217
#> [486,] 1.669362403
#> [487,] 0.206103928
#> [488,] 0.451117362
#> [489,] 5.698126322
#> [490,] 6.540704974
#> [491,] 0.317507150
#> [492,] 0.545048979
#> [493,] 1.085235163
#> [494,] 2.013962045
#> [495,] 1.239088132
#> [496,] 1.436155512
#> [497,] 1.501863923
#> [498,] 0.806757416
#> [499,] 0.160831347
#> [500,] 2.006011023