Skip to content

Instantly share code, notes, and snippets.

@markholdt
Created June 26, 2019 05:19
Show Gist options
  • Save markholdt/0cad7a50bdbc921e54a01f09132cd8e1 to your computer and use it in GitHub Desktop.
Save markholdt/0cad7a50bdbc921e54a01f09132cd8e1 to your computer and use it in GitHub Desktop.
Covariance
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