00001 /************************************************************************************************** 00002 Software License Agreement (BSD License) 00003 00004 Copyright (c) 2011-2013, LAR toolkit developers - University of Aveiro - http://lars.mec.ua.pt 00005 All rights reserved. 00006 00007 Redistribution and use in source and binary forms, with or without modification, are permitted 00008 provided that the following conditions are met: 00009 00010 *Redistributions of source code must retain the above copyright notice, this list of 00011 conditions and the following disclaimer. 00012 *Redistributions in binary form must reproduce the above copyright notice, this list of 00013 conditions and the following disclaimer in the documentation and/or other materials provided 00014 with the distribution. 00015 *Neither the name of the University of Aveiro nor the names of its contributors may be used to 00016 endorse or promote products derived from this software without specific prior written permission. 00017 00018 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR 00019 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 00020 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR 00021 CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00022 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00023 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00024 IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 00025 OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 ***************************************************************************************************/ 00032 #include <mtt/metrics.h> 00033 00034 double mahalanobis(Vector2d&y,Vector2d&mean,Matrix2d& cov) 00035 { 00036 VectorXd a=y-mean; 00037 MatrixXd i= cov.inverse(); 00038 double squared=a.transpose()*i*a; 00039 return sqrt(squared); 00040 } 00041 00042 double biVariatePDF(Vector2d& x,Vector2d& m,Matrix2d& cov) 00043 { 00044 double a,b,c,d,e; 00045 double s1,s2,rho; 00046 00047 s1=sqrt(cov(0,0)); 00048 s2=sqrt(cov(1,1)); 00049 rho=cov(0,1)/(s1*s2); 00050 00051 a=1./(2*M_PI*s1*s2*sqrt(1-rho*rho)); 00052 b=1./(2*(1-rho*rho)); 00053 c=pow(x(0)-m(0),2)/(s1*s1); 00054 d=pow(x(1)-m(1),2)/(s2*s2); 00055 e=(2*rho*(x(0)-m(0))*(x(1)-m(1)))/(s1*s2); 00056 00057 double f = a*exp(-b*(c+d-e)); 00058 00059 if(f<1e-12) 00060 f=0; 00061 00062 return f; 00063 }