方差函数VAKP、协方差函数COVAR 算法
计算β系数需要用到这两个函数,没找到具体的算法,请帮忙,谢谢!
[解决办法]
VAKP:
//求方差测试
static void Main(string[] args)
{
float[] data = { 5f, 4f, 8f, 9f };
Console.WriteLine(Vakp(data));
Console.ReadLine();
}
//求方差
static float Vakp(float[] data)
{
return data.Select(a =>(float)Math.Pow((a - GetAve(data)),2)).Sum() / data.Length;
}
//求和
static float GetSum(float[] data)
{
return data.Sum();
}
//求均值
static float GetAve(float[] data)
{
return data.Sum() / data.Length;
}
static float COVAR(float[] array1, float[] array2)
{
var sum = 0f;
var r1 = array1.Select(a => a - GetAve(array1)).ToArray();
var r2 = array2.Select(b => b - GetAve(array2)).ToArray();
for (int i = 0; i < array1.Length; i++)
{
sum += r1[i] * r2[i];
}
return sum / array1.Length;
}