DistributionHelper.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using Accord.Statistics.Testing;
  10. using Accord.Statistics;
  11. namespace DTHelperStd
  12. {
  13. public static class DistributionHelper
  14. {
  15. private const int SAMPLE_SIZE = 40;
  16. public static Dictionary<string, string> NormalTest(List<(float x, float y)> dataPoints, double minX, double maxX)
  17. {
  18. var totalCount = dataPoints.Select(p => p.y).Sum();
  19. var sampleSize = new List<int>();
  20. var samplesList = new List<double>();
  21. var bucketRange = Math.Ceiling((maxX - minX) / dataPoints.Count);
  22. for (var j= 0; j < dataPoints.Count; j++)
  23. {
  24. var r = new Random();
  25. for (var i=0; i< Math.Floor((dataPoints[j].y / totalCount) *SAMPLE_SIZE); i++)
  26. {
  27. samplesList.Add(r.NextDouble() * ((dataPoints[j].x -bucketRange/2) - (dataPoints[j].x + bucketRange/2)) + (dataPoints[j].x - bucketRange/2));
  28. }
  29. }
  30. var result = new Dictionary<string, string>();
  31. var samplesArr = samplesList.ToArray();
  32. var sw = new ShapiroWilkTest(samplesArr);
  33. Measures.Mean(samplesArr);
  34. result.Add("Statistic: ", string.Format("{0:n2}",sw.Statistic));
  35. result.Add("- PValue: ", string.Format("{0:n2}", sw.PValue));
  36. result.Add("- Distribution: ", $"{(!sw.Significant ==true ?"Normal Distribution" : "Unknown Distribution")}");
  37. result.Add("- Mean: ", Measures.Mean(samplesArr).ToString("n2"));
  38. result.Add("- Variance: ", Measures.Variance(samplesArr).ToString("n2"));
  39. result.Add("- Standard Deviation: ", Measures.StandardDeviation(samplesArr).ToString("n2"));
  40. return result;
  41. }
  42. }
  43. }