Brighton Webs Ltd.
statistical and data services for industry
Home
Index
Feedback

Mode

For discrete distributions, the mode is the value with the greatest frequency and for continuous ones, it is the point where the probability density is at a maximum.  It is possible for a distribution to have two or more modes.

The advantage of the mode over the mean is that it is less sensitive the presence of anomalous values in the data:

The calculation of the mode for discrete distributions is straightforward, simply examine each value and select the one with the greatest frequency.  In pseudo code, the process for identifying a single mode looks like this:

for i=0 to N-1
  if x(i) > mode then mode=x(i)
next i

Unlike the the calculation of the mean and median, there is no single accepted method for calculating the mode of a sample of values from a continuous distribution.  Two methods which give varying results are described below.

Mode from Histogram

The simplest way of calculating the mode is base on a histogram:

Step 1

Divide the range into an appropriate number of intervals.

Step 2

Determine the number of values which fall into each interval.

Step 3

Identify the interval with greatest number of values, then estimate the mode by taking the mean of the lower and upper values of the interval.

An example is shown in graphic form below:

This approach is sensitive to the size of the interval chosen.  With large datasets it may be possible to increase the precision of the estimate by using small interval sizes, however, a small one may make it difficult to get a reliable result.

Mode by successive Bisection

An alternative approach is based on the successive bisection of an array of values sorted into ascending order.  The logic for this approach is based on the Probability Density curve.  For a given interval, the probability density will be proportional to:

Where intsize is the number of values being inspected and i is the start of the interval.   This function will have its maximum value around the Mode.  Thus for a given value of intsize, the range will have its minimum value.  We can use this to construct a search algorithm to successively refine the estimate of the mode.

Step 1

Sort the array of values into ascending order.  (Hint, if the array size is, say more than 1,000, use an efficient sorting algorithm such as QuickSort, otherwise you may not live long enough to get to Step 2).

Step 2

Set the search interval to half the array size.  Then for intervals starting at i, record the value of i where the range has its minimum value.  In graphic terms the process looks like this:

In pseudo code, it looks like this:

i_beg=0
i_end=N-1

intsize = (i_end-i_beg)\2

for i = i_beg to i_end-intsize

  range=x(i+intsize)-x(i)

    if i=i_beg then
      i_min=i_beg
      range_min=range
    elseif range < range_min then
      i_min=i
      range_min=range
    end if

next i

Once the loop has completed, i_min will be the index of the array of intsize containing the modal interval.

Step 3

This step is a repeat  of step 2 with i_beg set to the value of i_min and i_end set to i_min+intsize from Step 2.  This illustrated in the graphic

Keep on repeating Step 3 until the Interval size is less than or equal to one (if i_beg=i_end, then the interval size is zero).

Step 4

Compute the mode from this pseudo code:

if intsize=1 then
  mode=(x(i_end)+x(i_beg))/2
else
  mode=x(i_beg)
end if

At the time of writing, this algorithm has been tested on a dataset derived from a skewed distribution with x values such that the difference of cumulative probability between them was 0.001, 0.0001, 0.00001 etc..  This has has produced results close to the analytical value for the distribution.  Further testing on random datasets is in progress.

In its current form, the algorithm only identifies the first modal interval that it encounters and further development is needed to handle multi-modal datasets.  It may also be possible to increase the computational efficiency by performing the search with increments larger than one during the early passes.

 

For more information: info@brighton-webs.co.uk