https://stackoverflow.com/questions/17447817/correlation-of-two-arrays-in-c-sharp

 

public double ComputeCoeff(double[] values1, double[] values2)
{
    if(values1.Length != values2.Length)
        throw new ArgumentException("values must be the same length");

    var avg1 = values1.Average();
    var avg2 = values2.Average();

    var sum1 = values1.Zip(values2, (x1, y1) => (x1 - avg1) * (y1 - avg2)).Sum();

    var sumSqr1 = values1.Sum(x => Math.Pow((x - avg1), 2.0));
    var sumSqr2 = values2.Sum(y => Math.Pow((y - avg2), 2.0));

    var result = sum1 / Math.Sqrt(sumSqr1 * sumSqr2);

    return result;
}

예)

var values1 = new List<double> { 3, 2, 4, 5 ,6 };
var values2 = new List<double> { 9, 7, 12 ,15, 17 };

var result = ComputeCoeff(values1.ToArray(), values2.ToArray());
// 0.997054485501581

Debug.Assert(result.ToString("F6") == "0.997054");

Excel 함수 사용 예)

var values1 = new List<double> { 3, 2, 4, 5 ,6 };
var values2 = new List<double> { 9, 7, 12 ,15, 17 };

// Make sure to add a reference to Microsoft.Office.Interop.Excel.dll
// and use the namespace

var application = new Application();

var worksheetFunction = application.WorksheetFunction;

var result = worksheetFunction.Correl(values1.ToArray(), values2.ToArray());

Console.Write(result); // 0.997054485501581

 

https://be2u.tistory.com/144

 

[c#] 상관계수 함수 - Correlation Coefficient Function

상관관계에 대한 공식 정리 c# 기반 소스 코드 private float Correlation(List xList, List yList) { float result = 0; float multiplyXYSigma = 0; float xSigma = 0; float ySigma = 0; float x..

be2u.tistory.com

 

728x90

'Programming Language > C#' 카테고리의 다른 글

DataTable 데이터 복제  (0) 2020.06.03
NLog 사용 샘플  (0) 2020.04.25
ChartFX Help  (0) 2020.01.19
remove comments with C#  (0) 2019.10.14
excel upload faster  (0) 2019.10.14

+ Recent posts