問題
使用 C#求標準差
解決方法
/// <summary> /// 標準差(StandardDifference) /// </summary> /// <param name="val">歷史報酬率</param> /// <returns>標準差</returns> private double GetSD(List<double> val) { double avg = AVG(val); //double _result = 0; //foreach (double d in val) _result += Math.Pow(d - avg, 2); double _result =(from a in val select Math.Pow(a - avg, 2)).Sum(); double _sum = _result / (double)(val.Count - 1); double _Sqrt = Math.Sqrt(_sum); return _Sqrt; } private double AVG(List<double> val) { double _avg = SUM(val) / (double)val.Count; return _avg; } private double SUM(List<double> val) { var _sum = (from g in val select g).Sum(); return _sum; }
沒有留言:
張貼留言