■ 표준 편차를 구하는 방법을 보여준다.
▶ 예제 코드 (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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
using System; #region 표준 편차 구하기 - GetStandardDeviation(valueArray, average) /// <summary> /// 표준 편차 구하기 /// </summary> /// <param name="valueArray">값 배열</param> /// <param name="average">평균</param> /// <returns>표준 편차</returns> public double GetStandardDeviation(double[] valueArray, double average) { int valueCount = valueArray.Length; if(valueCount == 0) { return 0d; } double standardDeviation = 0d; double variance = 0d; try { for(int i = 0; i < valueCount; i++) { variance += Math.Pow(valueArray[i] - average, 2); } standardDeviation = Math.Sqrt(SafeDivide(variance, valueCount)); } catch(Exception) { throw; } return standardDeviation; } #endregion #region 안전하게 나누기 - SafeDivide(value1, value2) /// <summary> /// 안전하게 나누기 /// </summary> /// <param name="value1">값 1</param> /// <param name="value2">값 2</param> /// <returns>나눈 값</returns> private double SafeDivide(double value1, double value2) { double result = 0d; try { if((value1 == 0) || (value2 == 0)) { return 0d; } result = value1 / value2; } catch { } return result; } #endregion |