# Function that is a good fit to the plot made with SmoothHistogram

How can I find a function that fits the plot from the SmoothHistogram function?

For instance if I have data like this and plot a Smooth Histogram, how can I get a function that fits the plot?

data = {4, 4, 4, 4, 1, 1, 4, 1, 5, 5, 5, 5, 5};
SmoothHistogram[data]


-

This is one approach yielding an interpolating function:

  data = {4, 4, 4, 4, 1, 1, 4, 1, 5, 5, 5, 5, 5};
f = Interpolation[
DeleteDuplicates[(List @@
First@First@
Cases[ SmoothHistogram[data] , _Line,
Infinity]), #1[[1]] == #2[[1]] &]]

Plot[f[x], {x, 0, 6.5}]


-
I appreciate the accept, buy @szabolcs answer really is the best. –  george2079 Jun 2 '14 at 13:12

SmoothHistogram uses kernel density estimation. So the correct and direct way to obtain the function is to construct a SmoothKernelDistribution, then take its PDF.

distr = SmoothKernelDistribution[data]

Plot[PDF[distr, x], {x, 0, 6}]


-
What is an actual function though? For instance, it looks like I could fit a polynomial to this curve. I want to know the actual best fitting function, not just recreate the plot via some other means. –  Kane May 30 '14 at 18:21
@Kane As I said in the answer, these functions use kernel density estimation. Look up this term to find out what this function is precisely. There are several ways to do kernel density estimation. I don't know what Mathematica's defaults are, but you have full control over the details (i.e. the form of the kernel and the bandwidth) using the documented options. Mathematica uses interpolation to speed up computing these functions, so you won't get a symbolic formula for it (this wouldn't be of much use anyway). You can compute the function values numerically the way I showed. –  Szabolcs May 30 '14 at 18:29