Calculates the entropy. - CSharp System

CSharp examples for System:Math

Description

Calculates the entropy.

Demo Code


using System.Collections.Generic;
using System;/* w  ww . j a  va  2 s.c o m*/

public class Main{
        /// <summary>
        ///     Calculates the entropy.
        /// </summary>
        /// <typeparam name="T">The type of number to calculate entropy for.</typeparam>
        /// <param name="histogram">The histogram with the data</param>
        /// <param name="sampleSize">The total number of samples.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="histogram"/> is <see langword="null" />.</exception>
        public static double CalculateEntropy<T>(this IEnumerable<KeyValuePair<T, int>> histogram, int sampleSize)
        {
            if (histogram == null)
            {
                throw new ArgumentNullException(nameof(histogram));
            }

            double entropy = 0;
            foreach (KeyValuePair<T, int> numberCount in histogram)
            {
                double probability = (double)numberCount.Value / sampleSize;
                entropy += probability * Math.Log(1 / probability, 2);
            }
            return entropy;
        }
        /// <summary>
        ///     Calculates the entropy.
        /// </summary>
        /// <typeparam name="T">The type of number to calculate entropy for.</typeparam>
        /// <param name="histogram">The histogram with the data</param>
        /// <param name="sampleSize">The total number of samples.</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException"><paramref name="histogram"/> is <see langword="null" />.</exception>
        public static double CalculateEntropy<T>(this IDictionary<T, int> histogram, int sampleSize)
        {
            if (histogram == null)
            {
                throw new ArgumentNullException(nameof(histogram));
            }
                    
            return CalculateEntropy(histogram as IEnumerable<KeyValuePair<T, int>>, sampleSize);
        }
}

Related Tutorials