<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323</link>
    <title>MATLAB Central Newsreader - Mahalanobis Distance Calculation</title>
    <description>Feed for thread: Mahalanobis Distance Calculation</description>
    <language>en-us</language>
    <copyright>&amp;copy;1994-2012 by MathWorks, Inc.</copyright>
    <webmaster>webmaster@mathworks.com</webmaster>
    <generator>MATLAB Central Newsreader</generator>
    <docs>http://blogs.law.harvard.edu/tech/rss</docs>
    <ttl>60</ttl>
    <image>
      <title>MathWorks</title>
      <url>http://www.mathworks.com/images/membrane_icon.gif</url>
    </image>
    <item>
      <pubDate>Fri, 29 Aug 2008 16:50:18 -0400</pubDate>
      <title>Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#597977</link>
      <author>Baris Yenidunya</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
I'm trying to understand how mahal function in MATLAB works.&lt;br&gt;
I see that the method used is different than the MD&lt;br&gt;
calculations in the literature. How is QR decomposition used&lt;br&gt;
here? Any help (maybe references, links etc.) will be&lt;br&gt;
appreciated.&lt;br&gt;
&lt;br&gt;
Thanks,&lt;br&gt;
&lt;br&gt;
Baris Yenidunya</description>
    </item>
    <item>
      <pubDate>Tue, 02 Sep 2008 17:49:45 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#598393</link>
      <author>Peter Perkins</author>
      <description>Baris Yenidunya wrote:&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I'm trying to understand how mahal function in MATLAB works.&lt;br&gt;
&amp;gt; I see that the method used is different than the MD&lt;br&gt;
&amp;gt; calculations in the literature. How is QR decomposition used&lt;br&gt;
&amp;gt; here? Any help (maybe references, links etc.) will be&lt;br&gt;
&amp;gt; appreciated.&lt;br&gt;
&lt;br&gt;
Baris, presumably by &quot;in the literature&quot;, you mean formulas like the one that appears in the help for MAHAL.  That's fine for understanding what the Mahalanobis distance is, but not so good for computational purposes.  MAHAL rewrites that explicit inversion of the cov matrix as a solution of a linear system, by noticing that the (squared) Mahalanobis distance can be expressed as&lt;br&gt;
&lt;br&gt;
D = Y0*S^(-1)*Y0'&lt;br&gt;
&amp;nbsp;&amp;nbsp;= Y0*(X0'*X0)^(-1)*Y0'&lt;br&gt;
&amp;nbsp;&amp;nbsp;= Y0*((Q*R)'*(Q*R))^(-1)*Y0'&lt;br&gt;
&amp;nbsp;&amp;nbsp;= Y0*(R'*R)^(-1)*Y0'&lt;br&gt;
&amp;nbsp;&amp;nbsp;= Y0*R^(-1) * (Y0*R^(-1))'&lt;br&gt;
&lt;br&gt;
and then noticing that you can do that using MATLAB's backslash operator.&lt;br&gt;
&lt;br&gt;
Hope this helps.</description>
    </item>
    <item>
      <pubDate>Wed, 03 Sep 2008 18:35:04 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#598550</link>
      <author>Baris Yenidunya</author>
      <description>Well it really does, thanks a lot Peter&lt;br&gt;
&lt;br&gt;
Baris Yenidunya</description>
    </item>
    <item>
      <pubDate>Sun, 14 Sep 2008 15:15:04 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#600272</link>
      <author>Dilber Ayhan</author>
      <description>Mr. Perkins,&lt;br&gt;
&lt;br&gt;
after your explanation to my friend Bar&amp;#305;&amp;#351;, I wonder that in the matlab function of mahal, Why the equation is multiplied with (rx-1)? I did not find where it comes.&lt;br&gt;
&lt;br&gt;
the equation;&lt;br&gt;
&lt;br&gt;
ri = R'\(Y-M)';&lt;br&gt;
d = sum(ri.*ri,1)'*(rx-1);&lt;br&gt;
I think sum(ri.*ri,1)' is just Y0*R^(-1) * (Y0*R^(-1))'&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
but what about (rx-1)?&lt;br&gt;
&lt;br&gt;
thanks&lt;br&gt;
&lt;br&gt;
dilber ayhan&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;g9jubp$pkv$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Baris Yenidunya wrote:&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I'm trying to understand how mahal function in MATLAB works.&lt;br&gt;
&amp;gt; &amp;gt; I see that the method used is different than the MD&lt;br&gt;
&amp;gt; &amp;gt; calculations in the literature. How is QR decomposition used&lt;br&gt;
&amp;gt; &amp;gt; here? Any help (maybe references, links etc.) will be&lt;br&gt;
&amp;gt; &amp;gt; appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Baris, presumably by &quot;in the literature&quot;, you mean formulas like the one that appears in the help for MAHAL.  That's fine for understanding what the Mahalanobis distance is, but not so good for computational purposes.  MAHAL rewrites that explicit inversion of the cov matrix as a solution of a linear system, by noticing that the (squared) Mahalanobis distance can be expressed as&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; D = Y0*S^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*(X0'*X0)^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*((Q*R)'*(Q*R))^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*(R'*R)^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*R^(-1) * (Y0*R^(-1))'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and then noticing that you can do that using MATLAB's backslash operator.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps.</description>
    </item>
    <item>
      <pubDate>Sun, 14 Sep 2008 19:38:01 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#600285</link>
      <author>Dilber Ayhan</author>
      <description>Hi,&lt;br&gt;
&lt;br&gt;
as a second question, is multicollinearity prevented by using QR decomposition in mahal function? I knew it works, but using my data set, mahal function did not solve with mahal function and gave an error as &quot;the matrix is singular&quot;&lt;br&gt;
since there is multicollinearity (since correlation matrix includes 1s)&lt;br&gt;
thanks,&lt;br&gt;
&lt;br&gt;
dilber ayhan&lt;br&gt;
&lt;br&gt;
Peter Perkins &amp;lt;Peteal.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;g9jubp$pkv$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Baris Yenidunya wrote:&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; I'm trying to understand how mahal function in MATLAB works.&lt;br&gt;
&amp;gt; &amp;gt; I see that the method used is different than the MD&lt;br&gt;
&amp;gt; &amp;gt; calculations in the literature. How is QR decomposition used&lt;br&gt;
&amp;gt; &amp;gt; here? Any help (maybe references, links etc.) will be&lt;br&gt;
&amp;gt; &amp;gt; appreciated.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Baris, presumably by &quot;in the literature&quot;, you mean formulas like the one that appears in the help for MAHAL.  That's fine for understanding what the Mahalanobis distance is, but not so good for computational purposes.  MAHAL rewrites that explicit inversion of the cov matrix as a solution of a linear system, by noticing that the (squared) Mahalanobis distance can be expressed as&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; D = Y0*S^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*(X0'*X0)^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*((Q*R)'*(Q*R))^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*(R'*R)^(-1)*Y0'&lt;br&gt;
&amp;gt;   = Y0*R^(-1) * (Y0*R^(-1))'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; and then noticing that you can do that using MATLAB's backslash operator.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Hope this helps.</description>
    </item>
    <item>
      <pubDate>Mon, 15 Sep 2008 01:13:34 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#600301</link>
      <author>Peter Perkins</author>
      <description>Dilber Ayhan wrote:&lt;br&gt;
&lt;br&gt;
&amp;gt; but what about (rx-1)?&lt;br&gt;
&lt;br&gt;
I inadvertently left that out of my response to Baris.&lt;br&gt;
&lt;br&gt;
&amp;gt; Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;g9jubp$pkv$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&lt;br&gt;
&amp;gt;&amp;gt; D = Y0*S^(-1)*Y0'&lt;br&gt;
&amp;gt;&amp;gt;   = Y0*(X0'*X0)^(-1)*Y0'&lt;br&gt;
&lt;br&gt;
Obviously, the estimate of S is (X0'*X0)/(n-1).</description>
    </item>
    <item>
      <pubDate>Mon, 15 Sep 2008 01:18:37 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#600302</link>
      <author>Peter Perkins</author>
      <description>Dilber Ayhan wrote:&lt;br&gt;
&amp;gt; Hi,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; as a second question, is multicollinearity prevented by using QR decomposition in mahal function? I knew it works, but using my data set, mahal function did not solve with mahal function and gave an error as &quot;the matrix is singular&quot;&lt;br&gt;
&amp;gt; since there is multicollinearity (since correlation matrix includes 1s)&lt;br&gt;
&lt;br&gt;
There may be a standard or unique or useful way to define the Mahalanobis distance for a singular cov matrix, but I'm not familiar with it.  You could perhaps compute a distance along the degenerate subspace using a reduced cov matrix; MAHAL does not do that.</description>
    </item>
    <item>
      <pubDate>Wed, 17 Sep 2008 20:15:20 -0400</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#600758</link>
      <author>Dilber Ayhan</author>
      <description>Thanks for your replies, Mr. Perkins&lt;br&gt;
&lt;br&gt;
regards,&lt;br&gt;
&lt;br&gt;
dilber ayhan&lt;br&gt;
&lt;br&gt;
&lt;br&gt;
Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;gakd5d$khh$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dilber Ayhan wrote:&lt;br&gt;
&amp;gt; &amp;gt; Hi,&lt;br&gt;
&amp;gt; &amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; as a second question, is multicollinearity prevented by using QR decomposition in mahal function? I knew it works, but using my data set, mahal function did not solve with mahal function and gave an error as &quot;the matrix is singular&quot;&lt;br&gt;
&amp;gt; &amp;gt; since there is multicollinearity (since correlation matrix includes 1s)&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; There may be a standard or unique or useful way to define the Mahalanobis distance for a singular cov matrix, but I'm not familiar with it.  You could perhaps compute a distance along the degenerate subspace using a reduced cov matrix; MAHAL does not do that.</description>
    </item>
    <item>
      <pubDate>Sun, 04 Jan 2009 22:44:02 -0500</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#619833</link>
      <author>Dilber Ayhan</author>
      <description>Hi Peter,&lt;br&gt;
&lt;br&gt;
in the calculation of MD, the values of Y should be standardized, since it is encouraged. This means;&lt;br&gt;
&lt;br&gt;
Z=(Y-M)/stdev should be used with covariance function&lt;br&gt;
But in the mahal function it isnot divided by standard deviation of Y values?If we want to use matlab mahal function, should we add this in to the formula, or it is already added?&lt;br&gt;
&lt;br&gt;
Can you just explain this?&lt;br&gt;
&lt;br&gt;
Thank you for your reply.&lt;br&gt;
&lt;br&gt;
Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;gakcru$h5o$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; Dilber Ayhan wrote:&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; but what about (rx-1)?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; I inadvertently left that out of my response to Baris.&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt; Peter Perkins &amp;lt;Peter.PerkinsRemoveThis@mathworks.com&amp;gt; wrote in message &amp;lt;g9jubp$pkv$1@fred.mathworks.com&amp;gt;...&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; &amp;gt;&amp;gt; D = Y0*S^(-1)*Y0'&lt;br&gt;
&amp;gt; &amp;gt;&amp;gt;   = Y0*(X0'*X0)^(-1)*Y0'&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Obviously, the estimate of S is (X0'*X0)/(n-1).</description>
    </item>
    <item>
      <pubDate>Mon, 05 Jan 2009 17:21:19 -0500</pubDate>
      <title>Re: Mahalanobis Distance Calculation</title>
      <link>http://www.mathworks.com/matlabcentral/newsreader/view_thread/235323#619922</link>
      <author>Peter Perkins</author>
      <description>Dilber Ayhan wrote:&lt;br&gt;
&amp;gt; Hi Peter,&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; in the calculation of MD, the values of Y should be standardized, since it is encouraged. This means;&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Z=(Y-M)/stdev should be used with covariance function&lt;br&gt;
&amp;gt; But in the mahal function it isnot divided by standard deviation of Y values?If we want to use matlab mahal function, should we add this in to the formula, or it is already added?&lt;br&gt;
&amp;gt; &lt;br&gt;
&amp;gt; Can you just explain this?&lt;br&gt;
&lt;br&gt;
Dilber, I'm not sure what you mean.  There's a standard formula for the Mahalanobis distance; my original reply transforms that from a textbook formula into a computationally useful one.  You may be referring to the fact that I used the notation Y0 to refer to a &quot;centered&quot; version of Y, but neglected to explain that notation.  If you want to compute the MD by also standardizing with respect to variance, and then using the correlation matrix, that's would be perfectly reasonable and equivalent.  But that isn't what you've described above.</description>
    </item>
  </channel>
</rss>

