4
$\begingroup$

This question already has an answer here:

I have some data.I plot the histogram with SmoothHistogram in Mathematica. If the y-axis of the histogram is my p(x) function, I want to plot f(x)=(1/n)ln(p(x)) with Mathematica. I don't know how?!

data = Import["Desktop/kmc final result.txt", "List"];
d = SmoothHistogram[data, Automatic, "PDF"];
time = 200;

f[x_] := -1/time Log[d, x]
$\endgroup$

marked as duplicate by Yves Klett, Öskå, ybeltukov, RunnyKine, Artes Sep 4 '14 at 12:24

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 1
    $\begingroup$ Welcome! Please add working code to demonstrate your problem. As it is, we cannot sensibly answer because of lack of information. $\endgroup$ – Yves Klett Sep 4 '14 at 8:37
  • $\begingroup$ Data = Import["Desktop/kmc final result.txt", "List"]; d = SmoothHistogram[Data, Automatic, "PDF"]; time = 200; f[x_] := -1/time Log[d, x]; $\endgroup$ – sara kaviani Sep 4 '14 at 8:49
5
$\begingroup$

With some data

data = RandomVariate[NormalDistribution[], 1000]

the SmoothHistogram plot:

SmoothHistogram[data]

SmoothHistogramPlot

is using SmoothKernelDistribution.
You can create the same DataDistribution object using

dist = SmoothKernelDistribution[data]

and then extract the values you see in the plot at a specific single point e.g.

PDF[dist, 0.5]

0.347436

create a table of values, e.g.:

Table[{x, PDF[dist, x]}, {x, -3.5, 3.5, 0.1}]

or use it within Plot or any other suitable function:

time = 200;
f[x_] := -1/time Log[PDF[dist, x]]
Plot[f[x], {x, -4, 4}]

Plot2

$\endgroup$
  • $\begingroup$ So you mean that there is no difference between "smoothkerneldistribution" and "SmoothHistogram"?! $\endgroup$ – sara kaviani Sep 4 '14 at 8:52
  • $\begingroup$ That is how I read the documentation: "SmoothHistogram[data] by default plots the PDF of [data], based on a smooth kernel density estimate. ... The specifications for bandwidth bw and kernel are the same as for SmoothKernelDistribution" $\endgroup$ – Karsten 7. Sep 4 '14 at 8:57
  • $\begingroup$ Thanx alot dear friend! $\endgroup$ – sara kaviani Sep 4 '14 at 9:03
2
$\begingroup$

As a an alternative, you can also extract the data out of the SmoothHistogram object. This way is a bit dirty and should be done carefully because the "magic index" 1,2,1,3,3,1 could be dependent on Mathematica version and other parameters. This is for v10:

data = RandomVariate[NormalDistribution[], 1000];
dataHist = SmoothHistogram[data]

enter image description here

now directly access the list of plotted points by dataHist[[1, 2, 1, 3, 3, 1]]

dataHist[[1, 2, 1, 3, 3, 1]] // ListLinePlot
{#[[1]], 1/1000*Log[#[[2]]]} & /@dataHist[[1, 2, 1, 3, 3, 1]] // ListLinePlot

enter image description here enter image description here

$\endgroup$
  • $\begingroup$ one small issue to be aware of , the plot range of SmoothHistogram seems to cut off the very ends of the tails. $\endgroup$ – george2079 Sep 5 '14 at 18:10

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