Click here to Skip to main content
11,925,101 members (55,391 online)
Click here to Skip to main content
Add your own
alternative version


60 bookmarked

A Generic Frequency Table in C# with Descriptive Statistics

, 26 Feb 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
Frequencies, descriptive statistics and normality testing

What's New?

GetData: The new parameter Pristine indicates if you want to get data in the same order as entered. That might be of importance in case of time-dependant data. The new Add-method stores the position(s) of each value using a Dictionary<T, List<int>>.


The exploration of empirical data is a common task in various fields. Especially the computational analysis of such data is often cumbered by the nature (the type) of the data. So - while writing statistical routines by my own - I decided to develop a "Frequency Table Class" which accepts any possible type of data.


My class has to satisfy requirements as follows:

  • Accept any type of data (especially multiple precision types)
  • Accept a given array
  • Provide methods for adding and removing single values
  • Automatically update the absolute frequency when a value is added/removed
  • A simple way to fetch mode, highest frequency ...
  • Provide a method to get the table as an array
  • Returned arrays must be sortable by frequency and value
  • Provide fields/properties to describe the table


The backbone of this class is the FrequencyTableEntry<T> structure:

//</span /> A generic structure storing the frequency information for each value</span />
public</span />

The specified type T has to implement the IComparable-Interface (needed for the sorting routine). The class stores the data in a generic Dictionary: _entries = new Dictionary<T,int>(): the _entries.Keys-Collection contains the values to count, the _entries.Values-Collection contains the absolute frequency for this particular value:

public</span />

To provide easy access to table entries, the implemented enumerator returns the structure above:

public</span />

The general Add(T value) method looks like this:

public</span />

To simplify the analysis of a given text, I have implemented a special constructor:

//</span /> Constructor - the created instance analyzes the </span />
//</span /> frequency of characters in a given string</span />
//</span /> Text: String to analyze</span />
public</span />

The associated Add method:

public</span />

In my opinion, it is useful to provide different modes regarding literal analysis. These modes are provided by TextAnalyzeMode:

public</span />

The analysis itself is performed by AnalyzeString(T Text, TextAnalyzeMode mode):

private</span />

Test for Normality

The question if given data are "Gaussian-distributed" is often raised. There are some robust and valid tests to answer this question. I have implemented the "good old" Kolmogorov-Smirnov test (KS-Test). Alternatively one can use the D'Agostino-Pearson test. There are two new properties concerning normality testing:

  • IsGaussian: Returns true if data are numerical and the computed p-value is greater than Alpha (see below)
  • Alpha: Defines the "significance level" for the KS-Test

The KS-Test method is shown below. The method returns true, if the test is applicable. In case of non-numerical data, the method returns false. The out-parameter p contains the p-value on exit. This value can be accessed by calling the P_Value property.

private</span />

To compute the "test distribution" (the Gaussian CDF in this case) we need the so called error function. I have used the Erf-implementation written by Miroslav Stampar (see Special Function(s) for C#), which is a translation of the Cephes Math Library by Stephen L. Moshier.

Descriptive Statistics

I think it is useful to implement some fundamental statistical properties inside the class.

Cumulative Frequencies

First of all, it is needed to implement a method which returns the empirical distribution function (the cumulative density function) of the given data:

public</span />

(Sorry for that strange formatting - this edit tool...)

Where Are My Data??

Ok - you need an array of the added data? That is the way:

public</span />

What Else?

There are some public properties concerning descriptive statistics:

  • Mean
  • Median
  • Mode
  • Minimum
  • Maximum
  • VarianceSample
  • VariancePop (unbiased estimator)
  • StandardDevSample
  • StandardDevPop (unbiased estimator)
  • StandardError
  • Sum
  • SampleSize - the number of data (read only)
  • HighestFrequency - the highest frequency observed
  • SmallestFrequency - the smallest frequency
  • ScarcestValue - the scarcest value
  • Kurtosis
  • KurtosisExcess
  • Skewness

If the data is not numerical, all properties above will return double.NaN.


Here is a list of the remaining public properties and methods.


  • Length - The number of table entries (read only)
  • Tag - An object which can be set by the user (writable)
  • Description - The description of the table (writable)
  • P_Value (contains the p value computed by the Kolmogorov-Smirnov Test)


  • Add(T Value) and Add(T Test, TextAnalyzeMode mode)
  • Remove(T Value)
  • GetTableAsArray() and GetTableAsArray(FrequencyTableSortOrder order) (sorting is done by using the Quicksort-Algorithm)
  • GetEnumerator()
  • ContainsValue(T value)
  • GetCumulativeFrequencyTable(CumulativeFrequencyTableFormat Format)
  • GetData(bool Pristine) - Returns the data as an array (sorted or in input order)
  • GetRelativeFrequency(T value, out double relFreq)

The code is (I think so) well documented so you can use it to get a detailed insight into my solution. I am sure that this solution is not perfect, but it is a good starting point.

For a better overview, I have added a compiled help file (see download at the top of this page).


  • Version 1.0 - 18 Jan '07
    • Initial release
  • Version 1.5 - 04 Feb '07
    • Minor bug fixes (highest frequency was not set correctly)
    • Added normality testing
    • Added descriptive statistics
  • 09 Feb '07
    • P_Value added, release number not changed
  • Version 2.0 26 Feb '07
    • GetData(bool Pristine) added


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

V. Thieme
Web Developer
Germany Germany
Anesthesiologist from Germany
- first contact: 1985 - ATARI 800 XE (there was a great assembler: ATMAS II)
- special interests: my son, number theory, statistics, linear algebra, medicine (of course)

You may also be interested in...

Comments and Discussions

QuestionProblem downloading source Pin
CJM73MS192810-Jul-13 2:41
memberCJM73MS192810-Jul-13 2:41 
GeneralTime Delay of two Frequencies Pin
mangotj9-Jun-10 18:30
membermangotj9-Jun-10 18:30 
GeneralThis is great and easy to use -- Thanks Pin
Michael Yourshaw25-Jul-08 18:48
memberMichael Yourshaw25-Jul-08 18:48 
GeneralDictionary and performance Pin
bronislav27-Apr-08 2:49
memberbronislav27-Apr-08 2:49 
QuestionHow to add to a frequency > 1 Pin
theperm30-Aug-07 8:51
membertheperm30-Aug-07 8:51 
GeneralDo, ut des Pin
visalia12-Apr-07 6:51
membervisalia12-Apr-07 6:51 
GeneralRe: Do, ut des Pin
V. Thieme17-Jan-09 0:38
memberV. Thieme17-Jan-09 0:38 
GeneralSuggestion Pin
TemplMetaProg27-Feb-07 0:51
memberTemplMetaProg27-Feb-07 0:51 
GeneralRe: Suggestion Pin
V. Thieme27-Feb-07 4:25
memberV. Thieme27-Feb-07 4:25 
GeneralExcellent class with additional functions.... [modified] Pin
scslmd31-Jan-07 12:56
memberscslmd31-Jan-07 12:56 
GeneralRe: Excellent class with additional functions.... Pin
Volker Thieme1-Feb-07 8:11
memberVolker Thieme1-Feb-07 8:11 
GeneralThis is a very nice post! Pin
Grav-Vt27-Jan-07 5:57
memberGrav-Vt27-Jan-07 5:57 
GeneralRe: This is a very nice post! Pin
Volker Thieme27-Jan-07 7:35
memberVolker Thieme27-Jan-07 7:35 
GeneralOutstanding Pin
AreJay25-Jan-07 7:37
memberAreJay25-Jan-07 7:37 
GeneralRe: Outstanding Pin
Volker Thieme25-Jan-07 10:10
memberVolker Thieme25-Jan-07 10:10 
GeneralAwesome Pin
Steve Hansen18-Jan-07 22:04
memberSteve Hansen18-Jan-07 22:04 
GeneralRe: Awesome Pin
Volker Thieme18-Jan-07 22:16
memberVolker Thieme18-Jan-07 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.151125.3 | Last Updated 26 Feb 2007
Article Copyright 2007 by V. Thieme
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid