■ 상관 계수를 구하는 방법을 보여준다.
▶ 상관 계수 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 |
using System; using System.Collections.Generic; List<double> xList = new List<double>() { 5 , 10, 15, 20, 30, 20, 30 }; List<double> yList = new List<double>() { 10, 40, 20, 30, 40, 10, 20 }; double correlationCoefficient = GetCorrelationCoefficient(xList, yList); Console.WriteLine(correlationCoefficient); |
▶ 상관 계수 구하기 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
using System; using System.Collections.Generic; #region 상관 계수 구하기 - GetCorrelationCoefficient(xList, yList) /// <summary> /// 상관 계수 구하기 /// </summary> /// <param name="xList">X 리스트<param> /// <param name="yList">Y 리스트</param> /// <returns>상관 계수</returns> public double GetCorrelationCoefficient(List<double> xList, List<double> yList) { double result = 0d; double multiplyXYSigma = 0d; double xSigma = 0d; double ySigma = 0d; double xPowSigma = 0d; double yPowSigma = 0d; double xListCount = xList.Count; for(int i = 0; i < xList.Count; i++) { multiplyXYSigma += (xList[i] * yList[i]); xSigma += xList[i]; ySigma += yList[i]; xPowSigma += Math.Pow(xList[i], 2); yPowSigma += Math.Pow(yList[i], 2); } result = ((xListCount * multiplyXYSigma) - (xSigma * ySigma)) / (Math.Sqrt(((xListCount * xPowSigma) - Math.Pow(xSigma, 2)) * ((xListCount * yPowSigma) - Math.Pow(ySigma, 2)))); return result; } #endregion |