Skip to contents

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)$$

Usage

.Mahalanobis(x, center, cov)

Arguments

x

A matrix with \(m\) columns and \(n\) rows, where each column represents a different variable and each row a different observation.

center

The mean vector of the distribution.

cov

The covariance matrix (\(p \times p\)) of the distribution.

Value

A matrix with 1 column and \(n\) rows reprensenting 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 <- boRing:::.wcov(m, w)
boRing:::.Mahalanobis(m, covar$center, covar$cov)
#>                [,1]
#>   [1,]  9.563149960
#>   [2,]  0.343896403
#>   [3,]  1.571593466
#>   [4,]  0.625640686
#>   [5,]  1.111584818
#>   [6,]  7.624949150
#>   [7,]  0.023741450
#>   [8,]  0.481756610
#>   [9,]  1.569406219
#>  [10,]  0.211405618
#>  [11,]  1.750630209
#>  [12,]  0.787843293
#>  [13,]  2.178683996
#>  [14,]  1.003576597
#>  [15,]  1.211234999
#>  [16,]  1.168409466
#>  [17,]  0.986233474
#>  [18,]  0.434938107
#>  [19,]  0.362745904
#>  [20,]  4.000174697
#>  [21,]  0.257489933
#>  [22,]  1.990974332
#>  [23,]  0.291265135
#>  [24,]  0.804828821
#>  [25,]  2.335064845
#>  [26,]  0.010535792
#>  [27,]  2.681584750
#>  [28,]  5.120416037
#>  [29,]  1.990249374
#>  [30,]  1.645269138
#>  [31,]  1.070670071
#>  [32,]  1.859820306
#>  [33,]  1.540054300
#>  [34,]  0.441197743
#>  [35,]  2.053084129
#>  [36,]  0.114633038
#>  [37,]  1.955428023
#>  [38,]  0.934783172
#>  [39,]  3.859730141
#>  [40,]  3.155141703
#>  [41,]  0.336637625
#>  [42,]  5.712400374
#>  [43,]  1.670941564
#>  [44,]  1.862462683
#>  [45,]  0.595481806
#>  [46,]  0.511791805
#>  [47,]  5.704536445
#>  [48,]  0.828806600
#>  [49,]  0.759261103
#>  [50,]  0.118198557
#>  [51,]  1.172826722
#>  [52,]  5.275443767
#>  [53,]  0.122671589
#>  [54,]  0.743330279
#>  [55,]  5.324409077
#>  [56,]  5.061104724
#>  [57,]  1.013551187
#>  [58,]  0.379962157
#>  [59,]  0.851792962
#>  [60,]  0.731296257
#>  [61,]  1.368532496
#>  [62,]  0.106316157
#>  [63,]  0.518786013
#>  [64,]  1.392307458
#>  [65,]  4.157258837
#>  [66,]  0.688981518
#>  [67,]  0.444180707
#>  [68,]  1.076304259
#>  [69,]  2.096111875
#>  [70,]  0.265706192
#>  [71,]  4.813256159
#>  [72,]  6.134748152
#>  [73,]  0.043508673
#>  [74,]  0.754806799
#>  [75,]  1.913917599
#>  [76,]  5.395996976
#>  [77,]  0.277436262
#>  [78,]  0.025144993
#>  [79,]  0.919162814
#>  [80,]  7.718407261
#>  [81,]  2.449029647
#>  [82,]  2.770510015
#>  [83,]  8.755764452
#>  [84,]  1.297426900
#>  [85,]  1.517983502
#>  [86,]  1.313017943
#>  [87,]  4.590278126
#>  [88,]  0.081948565
#>  [89,]  1.024921576
#>  [90,]  7.505792851
#>  [91,]  0.615394282
#>  [92,]  0.978114482
#>  [93,]  1.088858522
#>  [94,]  2.605152560
#>  [95,]  0.186831821
#>  [96,]  0.158005542
#>  [97,]  0.522387311
#>  [98,]  0.477595970
#>  [99,]  0.465573836
#> [100,]  0.007248185
#> [101,]  2.394190772
#> [102,]  0.361126893
#> [103,]  2.018361649
#> [104,]  0.061281043
#> [105,]  0.194444129
#> [106,]  1.000414003
#> [107,]  0.285883519
#> [108,]  0.049891030
#> [109,]  6.862101701
#> [110,]  0.480369966
#> [111,]  0.814439078
#> [112,]  0.054413125
#> [113,]  2.604265830
#> [114,]  2.769975047
#> [115,]  0.539970275
#> [116,]  1.011854325
#> [117,]  3.887655906
#> [118,]  1.018247093
#> [119,]  2.493548582
#> [120,]  0.196994492
#> [121,]  0.042578145
#> [122,]  0.469381960
#> [123,]  0.146402884
#> [124,]  0.471660306
#> [125,]  1.234781199
#> [126,]  0.483446019
#> [127,]  1.309846204
#> [128,]  1.357857922
#> [129,]  0.563919267
#> [130,]  1.694832471
#> [131,]  1.034460196
#> [132,]  2.545964479
#> [133,]  1.449850531
#> [134,]  1.694858931
#> [135,]  9.844593888
#> [136,]  0.391524460
#> [137,]  0.112397396
#> [138,]  2.871838893
#> [139,]  4.623405683
#> [140,]  0.227961467
#> [141,]  0.822992061
#> [142,]  2.122895839
#> [143,]  0.153433941
#> [144,]  1.082292617
#> [145,]  3.976379909
#> [146,]  2.199602067
#> [147,]  1.759045344
#> [148,]  5.671176045
#> [149,]  0.379578750
#> [150,]  1.025426140
#> [151,]  3.297673590
#> [152,]  2.493210368
#> [153,]  4.514788082
#> [154,]  0.752026524
#> [155,]  0.833979312
#> [156,]  1.009203506
#> [157,]  1.001452248
#> [158,]  4.019973163
#> [159,]  2.776650859
#> [160,]  0.318630262
#> [161,]  0.790957296
#> [162,]  0.187076170
#> [163,]  0.831783607
#> [164,]  0.247312868
#> [165,]  0.046757808
#> [166,]  2.884363753
#> [167,]  0.840284378
#> [168,]  1.909117543
#> [169,]  0.140606728
#> [170,]  0.030185761
#> [171,]  4.757426137
#> [172,]  5.982467206
#> [173,]  2.235871878
#> [174,]  1.776156675
#> [175,]  0.162856870
#> [176,]  1.852609444
#> [177,]  1.636170740
#> [178,]  0.165287208
#> [179,]  0.902258982
#> [180,]  1.877395413
#> [181,]  1.276506983
#> [182,]  3.236089019
#> [183,]  2.077696296
#> [184,]  1.625059265
#> [185,]  0.744331707
#> [186,]  0.675447832
#> [187,]  0.651036459
#> [188,]  1.263595315
#> [189,]  4.368673822
#> [190,]  0.394496958
#> [191,]  0.274843764
#> [192,]  2.423473835
#> [193,]  1.580780746
#> [194,]  1.542545401
#> [195,]  0.945975247
#> [196,]  2.437931745
#> [197,]  0.342269327
#> [198,]  5.084741848
#> [199,]  1.785495829
#> [200,]  0.285333994
#> [201,]  1.209525921
#> [202,]  2.492617550
#> [203,]  0.301543610
#> [204,]  2.424824427
#> [205,]  0.294260386
#> [206,]  0.229417485
#> [207,]  1.351547814
#> [208,]  0.431976837
#> [209,]  0.597009987
#> [210,]  1.369406554
#> [211,]  0.703394789
#> [212,]  3.434997674
#> [213,]  1.091234874
#> [214,]  3.764962163
#> [215,]  0.105980469
#> [216,]  6.365682909
#> [217,]  3.379609417
#> [218,]  1.356221344
#> [219,]  4.085812989
#> [220,]  0.355574478
#> [221,]  2.229295652
#> [222,]  5.464687496
#> [223,]  3.879974541
#> [224,]  2.073401358
#> [225,]  0.078253108
#> [226,]  0.184709056
#> [227,]  5.302933314
#> [228,]  2.409813915
#> [229,]  0.960510946
#> [230,]  3.385476290
#> [231,]  0.578430804
#> [232,]  6.779803620
#> [233,]  0.923183910
#> [234,]  0.884869974
#> [235,]  0.442469257
#> [236,]  1.026807039
#> [237,]  2.299215752
#> [238,]  1.172921097
#> [239,]  0.361779743
#> [240,]  0.345023160
#> [241,]  0.864047314
#> [242,]  0.011667592
#> [243,] 11.233518800
#> [244,]  3.315742754
#> [245,]  0.305409290
#> [246,]  1.905757800
#> [247,]  1.465171924
#> [248,]  1.832379095
#> [249,]  6.549349064
#> [250,] 10.450946753
#> [251,]  0.021236104
#> [252,]  1.138529508
#> [253,]  0.029809482
#> [254,]  0.443561389
#> [255,]  3.242553886
#> [256,]  1.199246483
#> [257,]  1.154333446
#> [258,]  1.778045263
#> [259,]  1.036475113
#> [260,]  3.409039484
#> [261,]  1.436689998
#> [262,]  0.424920906
#> [263,]  2.422813164
#> [264,]  0.088570288
#> [265,]  1.956955686
#> [266,]  1.124318575
#> [267,]  0.279636180
#> [268,]  0.178540456
#> [269,]  0.603845589
#> [270,]  0.426858646
#> [271,]  0.997226978
#> [272,]  1.115010372
#> [273,]  0.367893368
#> [274,]  2.980177340
#> [275,]  1.388578773
#> [276,]  0.121670426
#> [277,]  6.703673003
#> [278,]  0.888040947
#> [279,]  3.611056297
#> [280,]  0.614159360
#> [281,]  0.298791221
#> [282,]  4.268699196
#> [283,]  0.350872269
#> [284,]  3.314025133
#> [285,]  0.274370852
#> [286,]  2.325662963
#> [287,]  2.798232732
#> [288,]  1.214563396
#> [289,]  0.893326574
#> [290,]  3.186461759
#> [291,]  0.836711141
#> [292,]  1.376246344
#> [293,]  0.148112385
#> [294,]  0.070163994
#> [295,]  2.157367335
#> [296,]  0.415392374
#> [297,]  1.287096313
#> [298,]  1.315430553
#> [299,]  0.022626556
#> [300,]  1.549926288
#> [301,]  0.110028552
#> [302,]  0.666277522
#> [303,]  0.267577017
#> [304,]  0.171900994
#> [305,]  0.242035602
#> [306,]  2.880370955
#> [307,]  2.547811270
#> [308,]  2.757711641
#> [309,]  2.496217715
#> [310,]  0.469191810
#> [311,]  1.258920844
#> [312,]  1.095838297
#> [313,]  4.526187542
#> [314,]  2.533082505
#> [315,]  0.275472069
#> [316,]  1.129384394
#> [317,]  2.155225800
#> [318,]  5.603825298
#> [319,]  1.270295535
#> [320,]  1.771175684
#> [321,]  0.127617792
#> [322,]  1.900970468
#> [323,]  2.354118650
#> [324,]  0.121718044
#> [325,]  4.670645328
#> [326,]  1.004031583
#> [327,]  0.125668209
#> [328,] 17.097795468
#> [329,]  2.694986662
#> [330,]  2.996429050
#> [331,]  1.369234743
#> [332,]  0.092143867
#> [333,]  1.506763419
#> [334,]  0.503434059
#> [335,]  0.289107805
#> [336,]  0.226602056
#> [337,]  1.232665338
#> [338,]  0.408184387
#> [339,]  0.034849672
#> [340,] 10.725804952
#> [341,]  2.969542691
#> [342,]  0.442082888
#> [343,]  2.619106505
#> [344,]  1.532414847
#> [345,]  2.993145958
#> [346,]  3.527525536
#> [347,]  0.381464952
#> [348,]  4.238349049
#> [349,]  1.748207572
#> [350,]  3.575667935
#> [351,]  7.154563835
#> [352,]  1.802449162
#> [353,]  4.954804926
#> [354,]  5.940820266
#> [355,]  2.565084946
#> [356,]  0.161584682
#> [357,]  0.377728403
#> [358,]  1.987409291
#> [359,] 10.878461655
#> [360,]  1.158897076
#> [361,]  2.413692824
#> [362,]  0.666840967
#> [363,]  1.979557419
#> [364,]  1.395549450
#> [365,]  0.825507303
#> [366,]  0.077655398
#> [367,]  4.859865399
#> [368,]  0.167321445
#> [369,]  0.643461051
#> [370,]  1.661202213
#> [371,]  0.965182616
#> [372,]  2.289828611
#> [373,]  1.552469612
#> [374,]  1.509502125
#> [375,]  2.384869399
#> [376,]  2.868894571
#> [377,]  2.344037288
#> [378,]  2.120744441
#> [379,]  1.780178944
#> [380,]  1.680481383
#> [381,]  8.989608370
#> [382,]  1.179910253
#> [383,]  1.181628971
#> [384,]  1.202446627
#> [385,]  3.079815667
#> [386,]  6.444371353
#> [387,]  0.639924132
#> [388,]  1.340630392
#> [389,]  1.566770088
#> [390,]  0.918070747
#> [391,]  5.025931767
#> [392,]  1.261200998
#> [393,]  0.873093618
#> [394,]  4.593103637
#> [395,]  1.893887246
#> [396,]  5.809708183
#> [397,]  2.848393506
#> [398,]  4.051890292
#> [399,]  1.469076227
#> [400,]  3.358126716
#> [401,]  2.294740839
#> [402,]  1.731651928
#> [403,]  2.164431955
#> [404,]  1.053862564
#> [405,]  1.093119825
#> [406,]  2.298117862
#> [407,]  2.035831980
#> [408,]  2.315042882
#> [409,]  8.609601273
#> [410,]  5.978968358
#> [411,]  0.556366441
#> [412,]  3.178451529
#> [413,]  2.431927643
#> [414,]  1.452615358
#> [415,]  2.057574531
#> [416,]  1.760912697
#> [417,]  1.720517320
#> [418,]  0.027070807
#> [419,]  0.580458269
#> [420,]  1.673761195
#> [421,]  0.106253345
#> [422,]  7.038462115
#> [423,]  1.828223467
#> [424,]  3.694822623
#> [425,]  2.567081482
#> [426,]  4.460047284
#> [427,]  3.022855464
#> [428,]  3.190655487
#> [429,]  1.260873939
#> [430,]  3.784247906
#> [431,]  1.186308209
#> [432,]  2.961478305
#> [433,]  8.214140499
#> [434,]  0.950665088
#> [435,]  1.022573272
#> [436,]  0.636705001
#> [437,]  4.253458877
#> [438,]  1.808379671
#> [439,]  4.510129838
#> [440,]  0.440739468
#> [441,]  5.206282731
#> [442,]  2.252469566
#> [443,]  0.491743468
#> [444,]  3.717400010
#> [445,]  4.619709067
#> [446,]  0.177533080
#> [447,]  0.767414850
#> [448,]  1.212502870
#> [449,]  2.317711733
#> [450,]  0.005592416
#> [451,]  8.003582343
#> [452,]  3.956320442
#> [453,]  2.082154093
#> [454,]  0.205667188
#> [455,]  1.750460558
#> [456,]  0.225017625
#> [457,]  2.482965483
#> [458,]  0.775756922
#> [459,]  1.267242367
#> [460,]  0.774868212
#> [461,]  2.462603017
#> [462,]  1.194145686
#> [463,]  0.421683881
#> [464,]  0.564466103
#> [465,]  0.049302137
#> [466,]  0.427374005
#> [467,]  0.619802472
#> [468,]  5.450708064
#> [469,]  1.841929838
#> [470,]  3.865152768
#> [471,]  0.012224887
#> [472,]  1.170142355
#> [473,]  0.142817797
#> [474,]  1.821890046
#> [475,]  2.097583436
#> [476,]  3.992544540
#> [477,]  0.164975789
#> [478,]  0.493844853
#> [479,]  2.419550928
#> [480,]  0.372728609
#> [481,]  0.253132960
#> [482,]  0.242769507
#> [483,]  1.349948425
#> [484,]  3.390349319
#> [485,]  7.036559292
#> [486,]  0.833922555
#> [487,]  0.651473828
#> [488,]  0.419061830
#> [489,]  0.272900932
#> [490,]  3.068591188
#> [491,]  0.634613104
#> [492,]  1.928730166
#> [493,]  2.108864102
#> [494,]  1.535193449
#> [495,]  0.006796491
#> [496,]  1.064295147
#> [497,]  2.536778203
#> [498,]  1.177396458
#> [499,]  5.491496659
#> [500,]  7.988927903