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
Code c# (Statistics.cs) (202 lignes)
using System; using System.Collections.Generic; using System.Text; namespace be.gaudry.model.math { /// <summary> /// Set of statistics functions /// </summary> /// /// <remarks>The class represents collection of functions used /// in statistics</remarks> /// public class Statistics { /// <summary> /// Calculate mean value /// </summary> /// /// <param name="values">Histogram array</param> /// /// <returns>Returns mean value</returns> /// /// <remarks>The input array is treated as histogram, i.e. its /// indexes are treated as values of stochastic function, but /// array values are treated as "probabilities" (total amount of /// hits).</remarks> /// public static double Mean(int[] values) { int hits; int mean = 0; int total = 0; // for all values for (int i = 0, n = values.Length; i < n; i++) { hits = values[i]; // accumulate mean mean += i * hits; // accumalate total total += hits; } return (double)mean / total; } /// <summary> /// Calculate standard deviation /// </summary> /// /// <param name="values">Histogram array</param> /// /// <returns>Returns value of standard deviation</returns> /// /// <remarks>The input array is treated as histogram, i.e. its /// indexes are treated as values of stochastic function, but /// array values are treated as "probabilities" (total amount of /// hits).</remarks> /// public static double StdDev(int[] values) { double mean = Mean(values); double stddev = 0; double centeredValue; int hits; int total = 0; // for all values for (int i = 0, n = values.Length; i < n; i++) { hits = values[i]; centeredValue = (double)i - mean; // accumulate mean stddev += centeredValue * centeredValue * hits; // accumalate total total += hits; } return System.Math.Sqrt(stddev / total); } /// <summary> /// Calculate median value /// </summary> /// /// <param name="values">Histogram array</param> /// /// <returns>Returns value of median</returns> /// /// <remarks>The input array is treated as histogram, i.e. its /// indexes are treated as values of stochastic function, but /// array values are treated as "probabilities" (total amount of /// hits).</remarks> /// public static int Median(int[] values) { /*int total = 0, n = values.Length; // for all values for (int i = 0; i < n; i++) { // accumalate total total += values[i]; } int halfTotal = total / 2; int median = 0, v = 0; // find median value for (; median < n; median++) { v += values[median]; if (v >= halfTotal) break; } return median;*/ Array.Sort(values); return values[((values.Length -1)/2)+1]; } /// <summary> /// Get range around median containing specified percentage of values /// </summary> /// /// <param name="values">Histogram array</param> /// <param name="percent">Values percentage around median</param> /// /// <returns>Returns the range which containes specifies percentage /// of values.</returns> /// public static IntRange GetRange(int[] values, double percent) { int total = 0, n = values.Length; // for all values for (int i = 0; i < n; i++) { // accumalate total total += values[i]; } int min, max, hits; int h = (int)(total * (percent + (1 - percent) / 2)); // get range min value for (min = 0, hits = total; min < n; min++) { hits -= values[min]; if (hits < h) break; } // get range max value for (max = n - 1, hits = total; max >= 0; max--) { hits -= values[max]; if (hits < h) break; } } /// <summary> /// Calculate an entropy /// </summary> /// /// <param name="values">Histogram array</param> /// /// <returns>Returns entropy value</returns> /// /// <remarks>The input array is treated as histogram, i.e. its /// indexes are treated as values of stochastic function, but /// array values are treated as "probabilities" (total amount of /// hits).</remarks> /// public static double Entropy(int[] values) { int n = values.Length; int total = 0; double entropy = 0; double p; // calculate total amount of hits for (int i = 0; i < n; i++) { total += values[i]; } // for all values for (int i = 0; i < n; i++) { // get item's probability p = (double)values[i] / total; // calculate entropy if (p != 0) entropy += (-p * System.Math.Log(p, 2)); } return entropy; } } }
Structure et Fichiers du projet
Afficher/masquer...Icône | Nom | Taille | Modification |
Pas de sous-répertoires. | |||
Icône | Nom | Taille | Modification |
| _ | Répertoire parent | 0 octets | 1732259898 22/11/2024 08:18:18 |
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.