Created
June 26, 2019 05:19
-
-
Save markholdt/0cad7a50bdbc921e54a01f09132cd8e1 to your computer and use it in GitHub Desktop.
Covariance
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static double Covariance(this IEnumerable<double> returnsA, IEnumerable<double> returnsM) | |
{ | |
int n = returnsA.Count(); | |
double avgReturnM = returnsA.Average(); | |
double avgReturnA = returnsM.Average(); | |
double sumCv = 0; | |
for (int i = 0; i < n ; i++) | |
sumCv += (returnsA.ElementAt(i) - avgReturnM) * (returnsM.ElementAt(i) - avgReturnA); | |
return sumCv / n; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment