6
$\begingroup$

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 SmoothHistogram, 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]

plot

$\endgroup$

2 Answers 2

4
$\begingroup$

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}]

enter image description here

$\endgroup$
0
11
$\begingroup$

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}]

enter image description here

$\endgroup$
2
  • $\begingroup$ 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. $\endgroup$
    – Kane
    Commented May 30, 2014 at 18:21
  • $\begingroup$ @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. $\endgroup$
    – Szabolcs
    Commented May 30, 2014 at 18:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.