943,318 Members | Top Members by Rank

Ad:
  • C# Code Snippet
  • Views: 2373
  • C# RSS
0

Descriptive statistics

by on Nov 9th, 2008
A bunch of basic statistical functions.
I could have made a class here and I could have used more features of C#. But just to keep it simple I left it as a console application. So the modifications you have to make if you're into C, C++, Java etc. should be minor.
C# Code Snippet (Toggle Plain Text)
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace Statistics
  7. {
  8. class Program
  9. {
  10. static void Main(string[] args)
  11. {
  12. Intro();
  13. //////////////////////////////////////////////////////////
  14. // TestAr could come from a file or userinput or whatever.
  15. // Here TestAr is an array just for simplicity.
  16. /////////////////////////////////////////////////////////
  17. double[] TestAr = { 3.3, 5.77, 7.34, 9, 4.1, 13.66, 6.2, 7.65, 11.3, 12.67, 9, 10.03, 12.6, 3.76 };
  18.  
  19. Console.WriteLine("\tThe mean is ............. : {0}", mean(TestAr.Length, TestAr));
  20. Console.WriteLine("\tThe standard error is ... : {0}", stderr(TestAr.Length, TestAr));
  21. Console.WriteLine("\tThe median is ............ : {0}", median(TestAr.Length, TestAr));
  22. Console.WriteLine("\tThe standard deviation is : {0}", stddev(TestAr.Length, TestAr));
  23. Console.WriteLine("\tThe variance is .......... : {0}", variance(TestAr.Length, TestAr));
  24. Console.WriteLine("\tThe range is ............. : {0}", range(TestAr.Length, TestAr));
  25. Console.WriteLine("\tThe minimum is ........... : {0}", min(TestAr.Length, TestAr));
  26. Console.WriteLine("\tThe maximum is ........... : {0}", max(TestAr.Length, TestAr));
  27. Console.WriteLine("\tThe sum is ............... : {0}", sum(TestAr.Length, TestAr));
  28. Console.WriteLine("\tThe number of data is .... : {0}", TestAr.Length);
  29. Console.WriteLine("");
  30. Console.WriteLine("=========================================================");
  31.  
  32. Console.ReadKey();
  33. }
  34.  
  35. static void Intro()
  36. {
  37. Console.WriteLine("=========================================================");
  38. Console.WriteLine("| |");
  39. Console.WriteLine("| Descriptive STATISTICS |");
  40. Console.WriteLine("| |");
  41. Console.WriteLine("=========================================================");
  42. Console.WriteLine("");
  43. }
  44.  
  45. /// ==================================================================
  46. /// Sums up all the numbers in an array arr with length len.
  47. /// //////////////////////////////////////////////////////////////////
  48. static double sum(int len, double[] arr)
  49. {
  50. double sum = 0.0;
  51. for (int i = 0; i < len; i++)
  52. {
  53. sum += arr[i];
  54. }
  55. return sum;
  56. }
  57.  
  58. /// ==================================================================
  59. /// Calculates the MEAN of an array arr with length len.
  60. /// The mean is given by sum of numbers/amount of numbers.
  61. /// //////////////////////////////////////////////////////////////////
  62. static double mean(int len, double[] arr)
  63. {
  64. return sum(len, arr) / len;
  65. }
  66.  
  67. /// ==================================================================
  68. /// Calculates the sample VARIANCE of an array arr with length len.
  69. /// It is the sum of the squares of the difference of the numbers with
  70. /// their mean, divided by the amount of numbers minus one.
  71. /// //////////////////////////////////////////////////////////////////
  72. static double variance(int len, double[] arr)
  73. {
  74. double m = mean(len, arr);
  75. double sum = 0.0;
  76. double d = 0.0;
  77.  
  78. for (int i = 0; i < len; i++)
  79. {
  80. d = arr[i] - m;
  81. sum += d * d;
  82. }
  83. return sum / (len - 1);
  84. }
  85.  
  86. /// ==================================================================
  87. /// Calculates the STANDARD DEVIATION of an array arr with length len.
  88. /// It is the square root of the variance
  89. /// //////////////////////////////////////////////////////////////////
  90. static double stddev(int len, double[] arr)
  91. {
  92. return Math.Sqrt(variance(len,arr));
  93. }
  94.  
  95. /// ==================================================================
  96. /// Calculates the STANDARD ERROR of an array arr with length len.
  97. /// It is the standard deviation divided by the square root of the
  98. /// amount of numbers.
  99. /// //////////////////////////////////////////////////////////////////
  100. static double stderr(int len, double[] arr)
  101. {
  102. return stddev(len, arr) / Math.Sqrt(len);
  103. }
  104.  
  105. /// ==================================================================
  106. /// Calculates the MINIMUM of an array arr with length len.
  107. /// //////////////////////////////////////////////////////////////////
  108. static double min(int len, double[] arr)
  109. {
  110. double mini = double.MaxValue;
  111. for (int i = 0; i < len; i++)
  112. {
  113. if (arr[i] < mini)
  114. {
  115. mini = arr[i];
  116. }
  117. }
  118. return mini;
  119. }
  120.  
  121. /// ==================================================================
  122. /// Calculates the MAXIMUM of an array arr with length len.
  123. /// //////////////////////////////////////////////////////////////////
  124. static double max(int len, double[] arr)
  125. {
  126. double maxi = double.MinValue;
  127. for (int i = 0; i < len; i++)
  128. {
  129. if (arr[i] > maxi)
  130. {
  131. maxi = arr[i];
  132. }
  133. }
  134. return maxi;
  135. }
  136.  
  137. /// ==================================================================
  138. /// Calculates the RANGE of an array arr with length len.
  139. /// It is the difference between the largest and the smallest number.
  140. /// //////////////////////////////////////////////////////////////////
  141. static double range(int len, double[] arr)
  142. {
  143. return max(len, arr) - min(len, arr);
  144. }
  145.  
  146. /// ==================================================================
  147. /// Calculates the MEDIAN of an array arr with length len.
  148. /// It is the middle value of an array of sorted numbers.
  149. /// //////////////////////////////////////////////////////////////////
  150. static double median(int len, double[] arr)
  151. {
  152. int i, j;
  153. double temp;
  154. for (i = 0; i < len - 1; i++)
  155. {
  156. for (j = i + 1; j < len; j++)
  157. {
  158. if (arr[j] < arr[i])
  159. {
  160. temp = arr[i];
  161. arr[i] = arr[j];
  162. arr[j] = temp;
  163. }
  164. }
  165. }
  166. if (len % 2 == 0)
  167. return ((arr[len / 2] + arr[len / 2 - 1]) / 2.0);
  168. else
  169. return arr[len / 2];
  170. }
  171. }
  172. }
Message:
Previous Thread in C# Forum Timeline: WPF to Class Library
Next Thread in C# Forum Timeline: Share folder & files and setting permission on C# over the internet





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC