Similarity Methods (Cosine Similarty, L1 Distance, KullbackLeibler, Simetric KullbackLeibler, JensenShannon) in C#
public static double CosineSimilarty(double[] q, double[] d)
{
if (d == null || q == null) return 0;
if (q.Length != d.Length) throw new Exception("both vectors must be of similar length");
// Calculate cosine similarity:
double numer = 0;
double denom = 1;
double d1 = 0, d2 = 0;
for (int m = 0; m < q.Length; m++)
{
numer += (q[m] * d[m]);
d1 += (q[m] * q[m]);
d2 += (d[m] * d[m]);
}
denom = Math.Sqrt(d1) * Math.Sqrt(d2);
return numer / denom;
}
//----------------------------------------------------------------------------------------------------
public static double L1Distance(double[] q, double[] d)
{
if (q.Length != d.Length) throw new Exception("both vectors must be of similar length");
// Calculate cosine similarity:
double dist = 0;
for (int i = 0; i < q.Length; i++)
{
dist += Math.Abs(q[i] - d[i]);
}
return dist;
}
//----------------------------------------------------------------------------------------------------
public static double JensenShannon(double[] a, double[] b)
{
if (a.Length != b.Length) throw new Exception("both vectors must be of similar length");
double[] A_arti_B_bolu2 = new double[a.Length];
for (int i = 0; i < a.Length; i++)
{
A_arti_B_bolu2[i] = (a[i] + b[i]) / 2.0;
}
return 0.5 * (KullbackLeibler(a, A_arti_B_bolu2) + KullbackLeibler(b, A_arti_B_bolu2));
}
//----------------------------------------------------------------------------------------------------
public static double Simetric_KullbackLeibler(double[] a, double[] b)
{
if (a.Length != b.Length) throw new Exception("both vectors must be of similar length");
return 0.5 * (KullbackLeibler(a, b) + KullbackLeibler(b, a));
}
public static double KullbackLeibler(double[] q, double[] d)
{
double dist = 0;
for (int i = 0; i < q.Length; i++)
{
if (d[i] != 0 && q[i] != 0)
{
double dT = q[i] / d[i];
dist += q[i] * Math.Log(dT, 2);
}
}
return dist;
}
{
if (d == null || q == null) return 0;
if (q.Length != d.Length) throw new Exception("both vectors must be of similar length");
// Calculate cosine similarity:
double numer = 0;
double denom = 1;
double d1 = 0, d2 = 0;
for (int m = 0; m < q.Length; m++)
{
numer += (q[m] * d[m]);
d1 += (q[m] * q[m]);
d2 += (d[m] * d[m]);
}
denom = Math.Sqrt(d1) * Math.Sqrt(d2);
return numer / denom;
}
//----------------------------------------------------------------------------------------------------
public static double L1Distance(double[] q, double[] d)
{
if (q.Length != d.Length) throw new Exception("both vectors must be of similar length");
// Calculate cosine similarity:
double dist = 0;
for (int i = 0; i < q.Length; i++)
{
dist += Math.Abs(q[i] - d[i]);
}
return dist;
}
//----------------------------------------------------------------------------------------------------
public static double JensenShannon(double[] a, double[] b)
{
if (a.Length != b.Length) throw new Exception("both vectors must be of similar length");
double[] A_arti_B_bolu2 = new double[a.Length];
for (int i = 0; i < a.Length; i++)
{
A_arti_B_bolu2[i] = (a[i] + b[i]) / 2.0;
}
return 0.5 * (KullbackLeibler(a, A_arti_B_bolu2) + KullbackLeibler(b, A_arti_B_bolu2));
}
//----------------------------------------------------------------------------------------------------
public static double Simetric_KullbackLeibler(double[] a, double[] b)
{
if (a.Length != b.Length) throw new Exception("both vectors must be of similar length");
return 0.5 * (KullbackLeibler(a, b) + KullbackLeibler(b, a));
}
public static double KullbackLeibler(double[] q, double[] d)
{
double dist = 0;
for (int i = 0; i < q.Length; i++)
{
if (d[i] != 0 && q[i] != 0)
{
double dT = q[i] / d[i];
dist += q[i] * Math.Log(dT, 2);
}
}
return dist;
}
Yorumlar