C#

C# 상관계수 (Correl 함수) 구현

by LionHeart posted Oct 20, 2015
?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

크게 작게 위로 아래로 댓글로 가기 인쇄

URL: http://be2u.tistory.com/144



상관관계에 대한 공식 정리

 

 

c# 기반 소스 코드 

private float Correlation(List<float> xList, List<float> yList)
        {
            float result = 0;

            float multiplyXYSigma = 0;
            float xSigma = 0;
            float ySigma = 0;

            float xPowSigma = 0;
            float yPowSigma = 0;
            float n = xList.Count;

            for (int i = 0; i < xList.Count; i++)
            {
                multiplyXYSigma += (xList[i] * yList[i]);
                xSigma += xList[i];
                ySigma += yList[i];

                xPowSigma += (float)Math.Pow(xList[i],2);
                yPowSigma += (float)Math.Pow(yList[i],2);
            }

            result = ((n * multiplyXYSigma) - (xSigma * ySigma)) /
                ((float)Math.Sqrt(((n * xPowSigma) - (float)Math.Pow(xSigma, 2)) * ((n * yPowSigma) - (float)Math.Pow(ySigma, 2))));

            return result;
        } 

 

float 리스트들에 대해서도 처리를 하기위해, List<float> 타입으로 파라미터를 받도록 설계하였다.

 

리턴값의 절대값이 1에 가까울수록 두개의 분포도는 직선을 이룬다.

 

상관관계에 대한 설명 참조 : http://mathworld.wolfram.com/CorrelationCoefficient.html