Advertisement
EESweetieBot

Statistics

Feb 27th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.74 KB | None | 0 0
  1. public static void Main(string[] args)
  2.         {
  3.             int[] num = {
  4.                 52,61,91,41,40,48,25,55,63,44,
  5.                 88,55,62,58,98,51,30,73,57,95,
  6.                 95,40,85,66,87,24,65,48,96,75,
  7.                 45,36,75,71,85,24,92,50,50,57,
  8.                 72,90,77,65,70,33,61,81,72,50
  9.             };
  10.            
  11.             num = num.OrderByDescending(x => x).ToArray(); // Highest value is in the top
  12.             num.Reverse();
  13.            
  14.             Console.WriteLine("Lowest Number - " + num[num.Length-1]);
  15.             Console.WriteLine("Highest Number - " + num[0]);
  16.            
  17.             int EF = 0;
  18.             MelodyHelp.ForEach(num, x => EF += x);
  19.            
  20.             #region Undefined Group
  21.             double UD_Mean = ( EF / num.Length ); // Simple...
  22.             int UD_MeanRounded = (int)Math.Round(UD_Mean);
  23.             Console.WriteLine("Mean - " + UD_Mean);
  24.            
  25.             double UD_MedianCenter = 0;
  26.             double UD_MedianAverage = 0;
  27.             // Odd (n+1/2)
  28.             if(num.Length % 2 != 0) {
  29.                 UD_MedianCenter = num[ (num.Length + 1) / 2 ];
  30.                 UD_MedianAverage = UD_MedianCenter;
  31.             // Even
  32.             } else {
  33.                 UD_MedianAverage = (num[ (num.Length) / 2 ] + num[ ( (num.Length) / 2 ) + 1 ])/2;
  34.                 UD_MedianCenter = num[ (num.Length + 1) / 2 ];
  35.             }
  36.             Console.WriteLine("Median (n+1/2) - " + UD_MedianCenter);
  37.             Console.WriteLine("Median (Average of two middle numbers) - " + UD_MedianAverage);
  38.            
  39.             Dictionary<int, int> modeTemp1 = new Dictionary<int, int>();
  40.             // Iterate through num
  41.             foreach(int n in num) {
  42.                 if(!modeTemp1.ContainsKey(n)) {
  43.                     modeTemp1.Add(n, 1);
  44.                 } else {
  45.                     modeTemp1[n]++;
  46.                 }
  47.             }
  48.            
  49.             List<int> UD_Mode = new List<int>();
  50.            
  51.             int modeTemp2 = modeTemp1.Values.OrderByDescending(x => x).ToArray()[0]; // Get highest value
  52.             foreach(int n in modeTemp1.Keys)
  53.                 if(modeTemp1[n] == modeTemp2)
  54.                     UD_Mode.Add(n);
  55.             List<string> TEMPUDMODE = new List<string>();
  56.             foreach(int s in UD_Mode)
  57.                 TEMPUDMODE.Add(s.ToString());
  58.             Console.WriteLine("Mode(s) - " + TEMPUDMODE.BetterToString(","));
  59.            
  60.             // Variability
  61.            
  62.             int UD_Range = num[0] - num[num.Length-1]; // HIGHEST VALUE - LOWEST VALUE
  63.             Console.WriteLine("Range - " + UD_Range);
  64.            
  65.             double UD_MeanDeviation = 0;
  66.             foreach(int n in num) {
  67.                 UD_MeanDeviation += (n - UD_MeanRounded);
  68.             }
  69.             UD_MeanDeviation /= num.Length;
  70.             UD_MeanDeviation = Math.Round(UD_MeanDeviation, 2);
  71.             Console.WriteLine("Mean Deviation - " + UD_MeanDeviation);
  72.            
  73.             double UD_StandardDeviation = 0;
  74.             double UD_Variance = 0;
  75.             foreach(int n in num) {
  76.                 UD_Variance += (Math.Pow(n-UD_MeanRounded,2));
  77.             }
  78.             UD_Variance /= num.Length;
  79.             UD_Variance = Math.Round(UD_Variance, 2);
  80.             UD_StandardDeviation = Math.Round(Math.Sqrt(UD_Variance), 2);
  81.             Console.WriteLine("Variance - " + UD_Variance);
  82.             Console.WriteLine("Standard Deviation - " + UD_StandardDeviation);
  83.            
  84.             #endregion
  85.            
  86.             Console.WriteLine("--[[GROUPED DATA STARTS HERE]]--");
  87.            
  88.             #region Grouped Data
  89.            
  90.             // Rounded off to the nearest and highest odd number
  91.             int GD_ClassInterval = 0;
  92.             int CITemp1 = (int)Math.Round((double)UD_Range / 10);
  93.             if(CITemp1 % 2 == 0) // IS odd number
  94.                 CITemp1++;
  95.             GD_ClassInterval = CITemp1;
  96.             Console.WriteLine("Class Interval - " + GD_ClassInterval);
  97.            
  98.             int GD_ScoreStart = -1;
  99.            
  100.             // Start number is the nearest number below the range, which is divisible by the class interval
  101.             for(int i = UD_Range; i >= 0; i--) {
  102.                 if(i % GD_ClassInterval == 0 && i <= num[num.Length-1]) {
  103.                     GD_ScoreStart = i;
  104.                     break;
  105.                 }
  106.             }
  107.             if(GD_ScoreStart < 0) {
  108.                 Console.WriteLine("Something has gone wrong while calculating the start of the score table!");
  109.                 Thread.Sleep(-1);
  110.             }
  111.            
  112.             List<Score> GD_Scores = new List<Score>();
  113.            
  114.             List<int[]> GD_ClassRadius = new List<int[]>();
  115.             // X - (CI-1) / 40 - 44
  116.             for(int x = GD_ScoreStart; x+(GD_ClassInterval-1) < num[0]+GD_ClassInterval; x += GD_ClassInterval) {
  117.                 //Console.WriteLine("x - {0}, y - {1}".MelodyFormat(x, x + (GD_ClassInterval-1)));
  118.                 GD_ClassRadius.Add(new int[] {x, x+(GD_ClassInterval-1)});
  119.                 GD_Scores.Add(new Score() {Radius = new [] {x, x+(GD_ClassInterval-1)}, Frequency = 0});
  120.             }
  121.            
  122.             GD_ClassRadius.Reverse();
  123.             GD_Scores.Reverse();
  124.            
  125.             // Frequency
  126.             foreach(int n in num) {
  127.                 foreach(int[] r in GD_ClassRadius) {
  128.                     if(n >= r[0] && n <= r[1]) {
  129.                         int ind = GD_ClassRadius.IndexOf(r);
  130.                         GD_Scores[ind].Frequency++;
  131.                         break;
  132.                     }
  133.                 }
  134.             }
  135.            
  136.             // Comulative Frequency
  137.             int v = 0;
  138.             GD_Scores.Reverse();
  139.             foreach(Score s in GD_Scores) {
  140.                 v += s.Frequency;
  141.                 s.cf = v;
  142.                 // Also set the
  143.             }
  144.             GD_Scores.Reverse();
  145.            
  146.             //MelodyHelp.ForEach(GD_Scores, x => "{0} - {1} = {2}, {3}".MelodyFormat(x.Radius[0], x.Radius[1], x.Frequency, x.cf).WriteLine());
  147.            
  148.             Score GD_MedianClass = GD_Scores.Where(x => (num.Length/2) <= x.cf).Reverse().FirstOrDefault();
  149.             "{0} - {1}, {2}".MelodyFormat(GD_MedianClass.Radius[0], GD_MedianClass.Radius[1], GD_MedianClass.cf).WriteLine();
  150.            
  151.             // Median
  152.             double GD_Median = num.Length/2;
  153.             GD_Median -=  GD_Scores[GD_Scores.IndexOf(GD_MedianClass) + 1].cf;
  154.             GD_Median /= GD_MedianClass.Frequency;
  155.             GD_Median *= GD_ClassInterval;
  156.             GD_Median += GD_MedianClass.lb;
  157.             GD_Median = Math.Round(GD_Median, 2); // :)
  158.             Console.WriteLine("Median - " + GD_Median);
  159.            
  160.             // Mode
  161.             int HighestFrequency = 0;
  162.             foreach(Score s in GD_Scores) {
  163.                 if(s.Frequency > HighestFrequency) {
  164.                     HighestFrequency = s.Frequency;
  165.                 }
  166.             }
  167.             Score GD_ModalClass = null;
  168.             double GD_Mode = 0;
  169.             if(GD_Scores.Where(x => x.Frequency == HighestFrequency).Count() == 1) {
  170.                 GD_ModalClass = GD_Scores.Where(x => x.Frequency == HighestFrequency).First();
  171.                 //
  172.                 Score S_D1 = GD_Scores[GD_Scores.IndexOf(GD_ModalClass) - 1];
  173.                 Score S_D2 = GD_Scores[GD_Scores.IndexOf(GD_ModalClass) + 1];
  174.                 int D1 = GD_ModalClass.Frequency - S_D1.Frequency;
  175.                 int D2 = GD_ModalClass.Frequency - S_D2.Frequency;
  176.                 //
  177.                 GD_Mode = GD_ModalClass.lb + ( D1 / (D1 + D2) ) * GD_ClassInterval;
  178.                 GD_Mode.WriteLine();
  179.             }
  180.             if(GD_ModalClass != null)
  181.                 Console.WriteLine("Mode - " + GD_Mode);
  182.             else
  183.                 Console.WriteLine("Mode - NONE");
  184.            
  185.             double GD_Range = (GD_Scores[0].Radius[1] + 0.5) - (GD_Scores[GD_Scores.Count() -1].Radius[0] - 0.5);
  186.             Console.WriteLine("Range - " + GD_Range);
  187.            
  188.             double GD_Mean = 0;
  189.             foreach(Score s in GD_Scores)
  190.                 GD_Mean += s.FX;
  191.             GD_Mean /= num.Length;
  192.             int GD_MeanRounded = (int)Math.Round(GD_Mean);
  193.             Console.WriteLine("Mean - " + Math.Round(GD_Mean, 2));
  194.            
  195.             double GD_Variance = 0;
  196.             foreach(Score s in GD_Scores) {
  197.                 GD_Variance += s.Frequency * Math.Pow((s.ClassMark - GD_Mean),2);
  198.             }
  199.             GD_Variance /= num.Length-1;
  200.             Console.WriteLine("Variance - " + Math.Round(GD_Variance, 2));
  201.             Console.WriteLine("Standard Deviation - " + Math.Round(Math.Sqrt(GD_Variance), 2));
  202.            
  203.             double h = 0.0;
  204.             foreach(Score s in GD_Scores) {
  205.                 h += Math.Round(s.Frequency * Math.Pow((s.ClassMark - GD_MeanRounded),2), 2);
  206.             }
  207.             //h.WriteLine();
  208.            
  209.             // Now output it in an arranged table
  210.            
  211.             Console.WriteLine(@"Range | Frequency | Class Mark | f(X) | Lower Boundary | Comulative Frequency | (X-mean) | (X-mean)^2 | f(X-mean)^2");
  212.             foreach(Score s in GD_Scores) {
  213.                 string range = s.Radius[0] + "-" + s.Radius[1];
  214.                 string fl = range + " | " + s.Frequency + " | " + s.ClassMark + " | " + s.FX + " | " + s.lb + " | " + s.cf;
  215.                 double xm = (s.ClassMark - GD_MeanRounded);
  216.                 fl += "| {0} | {1} | {2}".MelodyFormat(Math.Round(xm, 2), Math.Round(Math.Pow(xm,2), 2), Math.Round(s.Frequency * Math.Pow(xm,2), 2));
  217.                 if(GD_MedianClass.cf == s.cf) {
  218.                     fl += "  -  Median";
  219.                 }
  220.                 if(GD_ModalClass != null && GD_ModalClass == s) {
  221.                     fl += "  -  Mode";
  222.                 }
  223.                 fl.WriteLine();
  224.             }
  225.            
  226.             #endregion
  227.            
  228.             Thread.Sleep(-1);
  229.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement