Statistics.cs

Description du code

Statistics.cs est un fichier du projet PhotoBrol.
Ce fichier est situé dans /var/www/bin/sniplets/bibliobrol/photobrol/.

Projet PhotoBrol :

Editeur d'images en CSharp.

Code source ou contenu du fichier

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace be.gaudry.model.math
  6. {
  7. /// <summary>
  8. /// Set of statistics functions
  9. /// </summary>
  10. ///
  11. /// <remarks>The class represents collection of functions used
  12. /// in statistics</remarks>
  13. ///
  14. public class Statistics
  15. {
  16. /// <summary>
  17. /// Calculate mean value
  18. /// </summary>
  19. ///
  20. /// <param name="values">Histogram array</param>
  21. ///
  22. /// <returns>Returns mean value</returns>
  23. ///
  24. /// <remarks>The input array is treated as histogram, i.e. its
  25. /// indexes are treated as values of stochastic function, but
  26. /// array values are treated as "probabilities" (total amount of
  27. /// hits).</remarks>
  28. ///
  29. public static double Mean(int[] values)
  30. {
  31. int hits;
  32. int mean = 0;
  33. int total = 0;
  34.  
  35. // for all values
  36. for (int i = 0, n = values.Length; i < n; i++)
  37. {
  38. hits = values[i];
  39. // accumulate mean
  40. mean += i * hits;
  41. // accumalate total
  42. total += hits;
  43. }
  44. return (double)mean / total;
  45. }
  46.  
  47. /// <summary>
  48. /// Calculate standard deviation
  49. /// </summary>
  50. ///
  51. /// <param name="values">Histogram array</param>
  52. ///
  53. /// <returns>Returns value of standard deviation</returns>
  54. ///
  55. /// <remarks>The input array is treated as histogram, i.e. its
  56. /// indexes are treated as values of stochastic function, but
  57. /// array values are treated as "probabilities" (total amount of
  58. /// hits).</remarks>
  59. ///
  60. public static double StdDev(int[] values)
  61. {
  62. double mean = Mean(values);
  63. double stddev = 0;
  64. double centeredValue;
  65. int hits;
  66. int total = 0;
  67.  
  68. // for all values
  69. for (int i = 0, n = values.Length; i < n; i++)
  70. {
  71. hits = values[i];
  72. centeredValue = (double)i - mean;
  73.  
  74. // accumulate mean
  75. stddev += centeredValue * centeredValue * hits;
  76. // accumalate total
  77. total += hits;
  78. }
  79.  
  80. return System.Math.Sqrt(stddev / total);
  81. }
  82.  
  83. /// <summary>
  84. /// Calculate median value
  85. /// </summary>
  86. ///
  87. /// <param name="values">Histogram array</param>
  88. ///
  89. /// <returns>Returns value of median</returns>
  90. ///
  91. /// <remarks>The input array is treated as histogram, i.e. its
  92. /// indexes are treated as values of stochastic function, but
  93. /// array values are treated as "probabilities" (total amount of
  94. /// hits).</remarks>
  95. ///
  96. public static int Median(int[] values)
  97. {
  98. /*int total = 0, n = values.Length;
  99.  
  100.   // for all values
  101.   for (int i = 0; i < n; i++)
  102.   {
  103.   // accumalate total
  104.   total += values[i];
  105.   }
  106.  
  107.   int halfTotal = total / 2;
  108.   int median = 0, v = 0;
  109.  
  110.   // find median value
  111.   for (; median < n; median++)
  112.   {
  113.   v += values[median];
  114.   if (v >= halfTotal)
  115.   break;
  116.   }
  117.  
  118.   return median;*/
  119. Array.Sort(values);
  120. return values[((values.Length -1)/2)+1];
  121. }
  122.  
  123. /// <summary>
  124. /// Get range around median containing specified percentage of values
  125. /// </summary>
  126. ///
  127. /// <param name="values">Histogram array</param>
  128. /// <param name="percent">Values percentage around median</param>
  129. ///
  130. /// <returns>Returns the range which containes specifies percentage
  131. /// of values.</returns>
  132. ///
  133. public static IntRange GetRange(int[] values, double percent)
  134. {
  135. int total = 0, n = values.Length;
  136.  
  137. // for all values
  138. for (int i = 0; i < n; i++)
  139. {
  140. // accumalate total
  141. total += values[i];
  142. }
  143.  
  144. int min, max, hits;
  145. int h = (int)(total * (percent + (1 - percent) / 2));
  146.  
  147. // get range min value
  148. for (min = 0, hits = total; min < n; min++)
  149. {
  150. hits -= values[min];
  151. if (hits < h)
  152. break;
  153. }
  154. // get range max value
  155. for (max = n - 1, hits = total; max >= 0; max--)
  156. {
  157. hits -= values[max];
  158. if (hits < h)
  159. break;
  160. }
  161. return new IntRange(min, max);
  162. }
  163.  
  164. /// <summary>
  165. /// Calculate an entropy
  166. /// </summary>
  167. ///
  168. /// <param name="values">Histogram array</param>
  169. ///
  170. /// <returns>Returns entropy value</returns>
  171. ///
  172. /// <remarks>The input array is treated as histogram, i.e. its
  173. /// indexes are treated as values of stochastic function, but
  174. /// array values are treated as "probabilities" (total amount of
  175. /// hits).</remarks>
  176. ///
  177. public static double Entropy(int[] values)
  178. {
  179. int n = values.Length;
  180. int total = 0;
  181. double entropy = 0;
  182. double p;
  183.  
  184. // calculate total amount of hits
  185. for (int i = 0; i < n; i++)
  186. {
  187. total += values[i];
  188. }
  189.  
  190. // for all values
  191. for (int i = 0; i < n; i++)
  192. {
  193. // get item's probability
  194. p = (double)values[i] / total;
  195. // calculate entropy
  196. if (p != 0)
  197. entropy += (-p * System.Math.Log(p, 2));
  198. }
  199. return entropy;
  200. }
  201. }
  202. }

Structure et Fichiers du projet

Afficher/masquer...


Répertoires contenus dans /var/www/bin/sniplets/bibliobrol/photobrol/model/math/ 
IcôneNomTailleModification
Pas de sous-répertoires.
IcôneNomTailleModification
| _ Répertoire parent0 octets1734895914 22/12/2024 20:31:54
Fichiers contenus dans /var/www/bin/sniplets/bibliobrol/photobrol/model/math/ 
IcôneNomTailleModificationAction
IcôneNomTailleModificationAction
Afficher le fichier .cs|.csStatistics.cs6.11 Ko31/10/2018 18:32:51-refusé-
Afficher le fichier .cs|.csIntRange.cs2.64 Ko31/10/2018 18:32:51-refusé-
Afficher le fichier .cs|.csHistogram.cs2.96 Ko31/10/2018 18:32:51-refusé-

Utilisation de l'explorateur de code

  • Navigation :
    • Un clic sur une icône de répertoire ouvre ce répertoire pour en afficher les fichiers.
    • Lorsque le répertoire en cours ne contient pas de sous-répertoires il est possible de remonter vers le répertoire parent.
    • La structure de répertoires en treetable (tableau en forme d'arborescence) n'est plus possibledans cette version.
    • Un clic sur une icône de fichier ouvre ce fichier pour en afficher le code avec la coloration syntaxique adaptée en fonction du langage principal utilisé dans le fichier.
  • Affichage :
    • Il est possible de trier les répertoires ou les fichiers selon certains critères (nom, taille, date).
  • Actions :
    • Les actions possible sur les fichiers dépendent de vos droits d'utilisateur sur le site. Veuillez activer le mode utilisateur pour activer les actions.

English translation

You have asked to visit this site in English. For now, only the interface is translated, but not all the content yet.

If you want to help me in translations, your contribution is welcome. All you need to do is register on the site, and send me a message asking me to add you to the group of translators, which will give you the opportunity to translate the pages you want. A link at the bottom of each translated page indicates that you are the translator, and has a link to your profile.

Thank you in advance.

Document created the 30/10/2009, last modified the 26/10/2018
Source of the printed document:https://www.gaudry.be/en/cs-photobrol-source-rf-model/math/Statistics.cs.html

The infobrol is a personal site whose content is my sole responsibility. The text is available under CreativeCommons license (BY-NC-SA). More info on the terms of use and the author.