Commit 39a52e19 authored by 潘栩锋's avatar 潘栩锋 🚴

1.添加 Misc.MyMath.Correl 支持double

parent e35bd63d
......@@ -268,7 +268,65 @@ namespace Misc
else
return 0;
}
/// <summary>
/// 求相关性
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
public static double Correl(IEnumerable<double> x, IEnumerable<double> y)
{
return Correl(x, 0, y, 0, x.Count());
}
/// <summary>
/// 求相关性
/// </summary>
/// <param name="x"></param>
/// <param name="y"></param>
/// <returns></returns>
public static double Correl(IEnumerable<double> x, int x_offset, IEnumerable<double> y, int y_offset, int len)
{
double sum_x = 0;
double sum_y = 0;
double sum_x2 = 0;
double sum_y2 = 0;
double sum_xy = 0;
int cnt = 0;
////避免溢出
//int max_x = Max(x, x_offset, x_offset + len - 1);
//if (max_x > 30000)
//{
//}
for (int i = 0; i < len; i++)
{
int x_i = i + x_offset;
int y_i = i + y_offset;
double xv = x.ElementAt(x_i);
double yv = y.ElementAt(y_i);
if ((!double.IsNaN(xv)) && (!double.IsNaN(yv)))
{
sum_x += xv;
sum_y += yv;
sum_x2 += xv * xv;
sum_y2 += yv * yv;
sum_xy += xv * yv;
cnt++;
}
}
if (cnt > 0)
{
double s1 = cnt * sum_xy - sum_x * sum_y;
double s2 = Math.Sqrt(cnt * sum_x2 - sum_x * sum_x);
double s3 = Math.Sqrt(cnt * sum_y2 - sum_y * sum_y);
double s4 = s1 / s2 / s3;
return s4;
}
else
return 0;
}
/// <summary>
/// 求相关性
/// </summary>
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment