# How to find the mode of a continuous distribution from a sample?

First, my background is not math.

My objective is to find the value that occurs most frequently in a data sample OR the value that is most likely.

Let's say my sample is [1,5,6,6,7,10]. Finding the mode for this sample is simple (the mode is 6).

But if let's say I change the sample to [1,5,6,7,10], I don't know how to find the mode. The results that I want is 6 since 6 is the most probable data.

Problem is, I don't even know what to google (tried for hours), and even when I do find something that MAY be the answer (kernel density estimation, continuous probability distribution), I don't understand what the hell they're talking about.

The actual situation consist of hundreds of data (in floats) that are saved in Excel. I would appreciate if someone could demo it in Excel.

• Do you have to find the mode? There are other averages which are significantly easier to compute when you have float data, for example the mean or the median. Nov 18, 2011 at 11:44
• If your samples are from what you believe is a continuous distribution, then it is almost certain that all the "hundreds of data (in floats)" are all distinct numbers (as in your second example) and there is no mode of that data sample. You could try sorting and binning the data, say into 20 bins of equal width between min and max, (e.g. ask Excel to make a histogram of the data sample values) and finding the bin with the largest number of data samples. The center point of that bin is an estimate of the mode. Nov 18, 2011 at 13:04
• Mean and median is totally unsuitable for my data. The problem with frequency histogram is its hard to find optimal band width. I'm making a program so it's critical for me to have this feature working independently. Didn't anybody know the solution. Did anybody know where can I ask questions. Thanks Nov 20, 2011 at 0:42
• Not sure you fully grasped the content of @Dilip's answer so let me repeat it: the data samples you are considering will have NO MODE whatsoever. This is not as if people did not know the solution, people know that there is no solution (and if you ask the same question elsewhere every correct answer which you will get will state the same thing).
– Did
Nov 27, 2011 at 11:28
• I don't have an Excel solution for you. This is a near replicate of stats.stackexchange.com/questions/19952/… except you are asking for an Excel method. The key fact here is that you are trying to estimate the density of your data along whatever your dimension is. Apr 7, 2012 at 4:10

For the record, here are some general solution sketches that also work for high-dimensional distributions (probably too complex for the asker, though; some sort of kernel density estimation is much simpler and reasonably good):

• Train an f-GAN with reverse KL divergence, without giving any random input to the generator (i.e. force it to be deterministic).

• Train an f-GAN with reverse KL divergence, move the input distribution to the generator towards a Dirac delta function as training progresses, and add a gradient penalty to the generator loss function.

• Train a (differentiable) generative model that can tractably evaluate an approximation of the pdf at any point (I believe that e.g. a VAE, a flow-based model, or an autoregressive model would do). Then use some type of optimization (some flavor of gradient ascent can be used if model inference is differentiable) to find a maximum of that approximation.

• I believe these solutions converge as the sample size and the "network approximation power" increase (assuming training works well). Dec 20, 2018 at 1:12

Recently I faced a similar problem, and came up with this code in Wolfram Mathematica:

ModeEstimate[data_?VectorQ] :=
MaximalBy[data, PDF[SmoothKernelDistribution[data]], 1][];


But keep in mind it is a rough estimate, and can even be totally wrong if the actual continuous distribution has narrow peaks that are not adequately represented in the sample, or the sample happens to contain sporadic clusters of values. I believe there is no way to quantify uncertainty in the computed estimate without additional information about the actual distribution.

SmoothKernelDistribution function has various options that you can try to adjust to get better results for your specific use cases.