本文主要是介绍求两个向量的协方差,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
根据D(A+B)=D(A)+D(B)+2COV(A,B)计算
private double GetConv(double[] a, double[] b){if (a.Length != b.Length){return 0;//error}double cov = 0;double meana = 0;double meanb = 0;double meanab = 0;double detaa = 0;double detab = 0;double detaab = 0;for (int i = 0; i < a.Length; i++){meana += a[i];meanb += b[i];meanab += a[i]+ b[i];}meana /= a.Length;meanb /= a.Length;meanab /= a.Length;for (int i = 0; i < a.Length; i++){detaa += Math.Pow(a[i] - meana, 2);detab += Math.Pow(b[i] - meanb, 2);detaab += Math.Pow(a[i] + b[i] - meanab, 2);}cov= (detaab - detaa - detab) / 2;return cov;}
这篇关于求两个向量的协方差的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!