private static void EvaluateCorrectness(ParameterTimeSeries a, ParameterTimeSeries b, int minimumElementsRequired) { if (a == null) { throw new ArgumentNullException("a", "First argument is null"); } if (b == null) { throw new ArgumentNullException("b", "Second argument is null"); } if (a.Type != b.Type) { throw new ArgumentException("Series type mismatch."); } if (a.Length < minimumElementsRequired) { throw new ArgumentException("First series contains less than " + minimumElementsRequired + " elements.", "a"); } if (b.Length < minimumElementsRequired) { throw new ArgumentException("Second series contains less than " + minimumElementsRequired + " elements.", "b"); } }
private static ParameterTimeSeries FitTimes(ParameterTimeSeries large, ParameterTimeSeries narrow) { if (large.Type != narrow.Type) { return(large); } List <DateTime> targetTimes = new List <DateTime>(); List <int> values = new List <int>(); if (narrow.Length > 1) { int iLarge = 0; int iNarrow = 0; while (iLarge < large.Length && iNarrow < narrow.Length) { if (large.TargetTimes[iLarge] >= narrow.TargetTimes[iNarrow]) { targetTimes.Add(large.TargetTimes[iLarge]); values.Add(large.Values[iLarge]); iNarrow++; } iLarge++; } } else if (narrow.Length == 1) { targetTimes.Add(narrow.MinTargetTime); values.Add(large.MaxValue); } return(new ParameterTimeSeries(targetTimes, values, large.Type, large.GroupingTime, large.GroupingTimeKind)); }
public static double MAE(ParameterTimeSeries a, ParameterTimeSeries b) { EvaluateCorrectness(a, b, 1); string type = a.Type; int length = Math.Min(a.Length, b.Length); int sumOfErrors = 0; for (var i = 0; i < length; i++) { sumOfErrors += ParametersFactory.GetValueDistance(type, a.Values[i], b.Values[i]); } double result = (double)sumOfErrors / length; return(result); }
public static double RMSE(ParameterTimeSeries forecast, ParameterTimeSeries real) { EvaluateCorrectness(forecast, real, 1); string type = forecast.Type; int length = Math.Min(forecast.Length, real.Length); double sumOfSquareErrors = 0; for (var i = 0; i < length; i++) { sumOfSquareErrors += Math.Pow((double)(ParametersFactory.GetValueDistance(type, real.Values[i], forecast.Values[i])), 2); } double result = Math.Sqrt(sumOfSquareErrors / length); return(result); }
public static double MASE(ParameterTimeSeries forecast, ParameterTimeSeries real) { EvaluateCorrectness(forecast, real, 2); string type = forecast.Type; int length = Math.Min(forecast.Length, real.Length); int sumOfNaiveErrors = 0; int sumOfErrors = ParametersFactory.GetValueDistance(type, real.Values[0], forecast.Values[0]); for (var i = 1; i < length; i++) { sumOfErrors += ParametersFactory.GetValueDistance(type, real.Values[i], forecast.Values[i]); sumOfNaiveErrors += ParametersFactory.GetValueDistance(type, real.Values[i], real.Values[i - 1]); } double result = (double)sumOfErrors * (length - 1) / (length * sumOfNaiveErrors); return(result); }
public static double MAPE(ParameterTimeSeries forecast, ParameterTimeSeries real) { EvaluateCorrectness(forecast, real, 1); string type = forecast.Type; if (ParametersFactory.IsZeroable(type)) { throw new ArgumentException("MAPE of zeroable values not allowed."); } int length = Math.Min(forecast.Length, real.Length); double sumOfPercantageErrors = 0; for (var i = 0; i < length; i++) { sumOfPercantageErrors += Math.Abs((double)ParametersFactory.GetValueDistance(type, real.Values[i], forecast.Values[i]) / real.Values[i]); } double result = sumOfPercantageErrors / length; return(result); }
public static Dictionary <StatisticMethods, double> CalculateMany(IEnumerable <StatisticMethods> methods, ParameterTimeSeries forecast, ParameterTimeSeries real) { var result = new Dictionary <StatisticMethods, double>(); if (methods.Contains(StatisticMethods.All)) { methods = new List <StatisticMethods> { StatisticMethods.Mae, StatisticMethods.Mase, StatisticMethods.Mape, StatisticMethods.Rmse }; } foreach (var method in methods.Distinct()) { try { result.Add(method, GetFunc(method)(forecast, real)); } catch (Exception e) { OnCalculatingFailed(method, e.Message); } } return(result); }