1 #include "stdafx.h"
2
EuclidianDistance(TPoint & x,TPoint & y)3 double EuclidianDistance(TPoint& x, TPoint& y){
4 double accu = 0;
5 for (int i = 0; i < x.size(); i++){
6 accu += pow(x[i] - y[i], 2);
7 }
8 return sqrt(accu);
9 }
10
EuclidianDistance2(TPoint & x,TPoint & y)11 double EuclidianDistance2(TPoint& x, TPoint& y){
12 double accu = 0;
13 for (int i = 0; i< x.size(); i++){
14 accu += pow(x[i] - y[i], 2);
15 }
16 return (accu);
17 }
18
19 // alpha - kernel sharpness. sharp - a more
EDKernel(TPoint & x,TPoint & y,double a)20 double EDKernel (TPoint& x, TPoint& y, double a){
21 return 1/(1+a*EuclidianDistance2(x, y));
22 }
23
24 // ss - sigma squared. sharp - a less
GKernel(TPoint & x,TPoint & y,double ss)25 double GKernel (TPoint& x, TPoint& y, double ss){
26 int d = x.size();
27 return pow((2*PI*ss), - d/2) * exp(-EuclidianDistance2(x, y) / (2*ss)); //04.04.2014 added power d
28 }
29
30 // ss - sigma squared. sharp - a less
VarGKernel(TPoint & x,TPoint & y,double ss)31 double VarGKernel(TPoint& x, TPoint& y, double ss){
32 int d = x.size();
33 return pow((2 * PI*ss), -d / 2) * exp(-EuclidianDistance2(x, y) / (2 * ss)); //04.04.2014 added power d
34 }
35
36 // alpha - kernel sharpness. sharp - a more
EKernel(TPoint & x,TPoint & y,double a)37 double EKernel (TPoint& x, TPoint& y, double a){
38 return exp(-a*EuclidianDistance(x, y));
39 }
40
41 // alpha - triangle sharpness. sharp - a more. a in (0..pi/2)
TriangleKernel(TPoint & x,TPoint & y,double a)42 double TriangleKernel (TPoint& x, TPoint& y, double a){
43 return 1/(EuclidianDistance(x, y)+0.000001)*tan(a);
44 }
45
PotentialDepths(TMatrix & points,TVariables & cardinalities,TMatrix & depths,double (* Kernel)(TPoint & x,TPoint & y,double a),double a)46 void PotentialDepths(TMatrix& points, TVariables& cardinalities, /*OUT*/ TMatrix& depths, double (*Kernel) (TPoint& x, TPoint& y, double a), double a){
47 PotentialDepths(points, cardinalities, points, depths, Kernel, a, 0);
48 }
49
PotentialDepths(TMatrix & points,TVariables & cardinalities,TMatrix & testpoints,TMatrix & depths,double (* Kernel)(TPoint & x,TPoint & y,double ss),double ss,int ignoreself)50 void PotentialDepths(TMatrix& points, TVariables& cardinalities, TMatrix& testpoints, /*OUT*/ TMatrix& depths, double (*Kernel) (TPoint& x, TPoint& y, double ss), double ss, int ignoreself){
51 int classBeginning = 0;
52
53 bool var = Kernel == VarGKernel;
54 double weight = 1;
55 TMatrix* classPoints;
56 TPoint* classPointsDepths;
57 int error;
58
59 // loop classes
60 for (int i = 0; i < cardinalities.size(); classBeginning += cardinalities[i], i++){
61
62 if (var){
63 if (classPoints) delete[] classPoints;
64 classPoints = new TMatrix(points.begin() + classBeginning, points.begin() + classBeginning + cardinalities[i]);
65 if (!classPointsDepths)
66 classPointsDepths = new TPoint(cardinalities[i]);
67 else if (classPointsDepths->size() < cardinalities[i])
68 classPointsDepths->resize(cardinalities[i]);
69
70 for (int c = 0; c < cardinalities[i]; c++){
71 (*classPointsDepths)[c] = 1 - ZonoidDepth(*classPoints, points[classBeginning + c], error);
72 }
73 }
74
75 // loop all the points, find their potential relatively to class i
76 for (int p = 0; p < testpoints.size(); p++){
77 double pointDepth = 0;
78
79 // loop the points of i-th class, find the point's potential
80 for (int c = 0; c < cardinalities[i]; c++){
81 // if (ignoreself && p == classBeginning + c) // ignore the potential created by this point
82 // continue;
83
84 if (var)
85 weight = (*classPointsDepths)[c];
86
87 pointDepth += Kernel(testpoints[p], points[classBeginning + c], ss*weight);
88 }
89 depths[p][i] = pointDepth;
90 }
91
92 if (false) { //28.05.2014 no normalization
93 int n = ignoreself ? points.size() - 1 : points.size();
94
95 // normalize
96 for (int p = 0; p < testpoints.size(); p++){
97 depths[p][i] *= 1.0 / n; //04.04.2014 1.0*cardinalities[i]/points.size()/Kernel(points[0], points[0], a);
98 }
99 }
100 }
101
102 if (var){
103 delete[] classPoints;
104 delete[] classPointsDepths;
105 }
106 }
107
108