平均数、标准差、相关系数
using System;
using System.Collections.Generic;
using System.Text;
namespace AppMath
{
public class Maths
{
/// <summary>
/// 求出数据平均值,并保留三位小数
/// </summary>
/// <param name="Valist">数据集合</param>
/// <returns></returns>
public double average(List<double> Valist)
{
double sum = 0;
foreach (double d in Valist)
{
sum = sum + d;
}
double revl = System.Math.Round(sum / Valist.Count, 3);
return revl;
}
/// <summary>
/// 求数据集合标准差
/// </summary>
/// <param name="ValList"></param>
/// <returns></returns>
public double stdev(List<double> ValList)
{
double avg = average(ValList);
double sumstdev = 0;
foreach (double d in ValList)
{
sumstdev = sumstdev + (d - avg) * (d - avg);
}
double stdeval = System.Math.Sqrt(sumstdev);
return System.Math.Round(stdeval, 3);
}
/// <summary>
/// 相关系数,要求两个集合数量必须相同
/// </summary>
/// <param name="array1">数组一</param>
/// <param name="array2">数组二</param>
/// <returns></returns>
public double correl(List<double> array1, List<double> array2)
{
//数组一
double avg1 = average(array1);
double stdev1 = stdev(array1);
//数组二
double avg2 = average(array2);
double stdev2 = stdev(array2);
double sum = 0;
for (int i = 0; i < array1.Count && i < array2.Count; i++)
{
sum = sum + ((array1[i] - avg1) / stdev1) * ((array2[i] - avg2) / stdev2);
}
return Math.Round(sum, 3);
}
}
}
using System.Collections.Generic;
using System.Text;
namespace AppMath
{
public class Maths
{
/// <summary>
/// 求出数据平均值,并保留三位小数
/// </summary>
/// <param name="Valist">数据集合</param>
/// <returns></returns>
public double average(List<double> Valist)
{
double sum = 0;
foreach (double d in Valist)
{
sum = sum + d;
}
double revl = System.Math.Round(sum / Valist.Count, 3);
return revl;
}
/// <summary>
/// 求数据集合标准差
/// </summary>
/// <param name="ValList"></param>
/// <returns></returns>
public double stdev(List<double> ValList)
{
double avg = average(ValList);
double sumstdev = 0;
foreach (double d in ValList)
{
sumstdev = sumstdev + (d - avg) * (d - avg);
}
double stdeval = System.Math.Sqrt(sumstdev);
return System.Math.Round(stdeval, 3);
}
/// <summary>
/// 相关系数,要求两个集合数量必须相同
/// </summary>
/// <param name="array1">数组一</param>
/// <param name="array2">数组二</param>
/// <returns></returns>
public double correl(List<double> array1, List<double> array2)
{
//数组一
double avg1 = average(array1);
double stdev1 = stdev(array1);
//数组二
double avg2 = average(array2);
double stdev2 = stdev(array2);
double sum = 0;
for (int i = 0; i < array1.Count && i < array2.Count; i++)
{
sum = sum + ((array1[i] - avg1) / stdev1) * ((array2[i] - avg2) / stdev2);
}
return Math.Round(sum, 3);
}
}
}