3
$\begingroup$

In a previous post, the following answer was given:

ClearAll[dist] 
dist[α_?NumericQ, β_?NumericQ] :=
 TransformedDistribution[0.5` + 4.830917874396135` Sqrt[0.01071225` - 10 x^2], 
  Distributed[x, TruncatedDistribution[{0, 0.02135225}, GammaDistribution[α, β]]]]

and

H = RandomVariate[dist[7.788017927062043, 0.0011475109935367525], 5000]

did indeed provide what I was looking for. Except the last step. The last step is to use SmoothHistogram on $H$ and present the data in terms of "Probability". However, reading up on SmoothHistogram, "PDF", "CF" and so on are options, but not "Probability".

So, how can I show the output of SmoothHistogram as "Probability"?

The actual data I am using is:

   s1 =  {0.990009, 0.985514, 0.985514, 0.977022, 0.990509, 0.976523, 0.98201, 0.965034, 0.982517, 0.962537, 0.96903, 0.990009, 0.983016, 0.992007, 0.983016, 0.991508, 0.97952, 0.977522, 0.975524, 0.982017, 0.983016, 0.987012, 0.968531, 0.930569, 0.990509, 0.975024, 0.962037, 0.984015, 0.985514, 0.976023, 0.984515, 0.974025, 0.973526, 0.985014, 0.978021, 0.976523, 0.982517, 0.962037, 0.982017, 0.976523, 0.961538, 0.978521, 0.986013, 0.983016, 0.98951, 0.98951, 0.991008, 0.968031, 0.992507, 0.980519, 0.975024, 0.978521, 0.973026, 0.984515, 0.981018, 0.985514, 0.98901, 0.983016, 0.988511, 0.985014, 0.983516, 0.981018, 0.980019, 0.984015, 0.986513, 0.994005, 0.981518, 0.948051, 0.983516, 0.982517, 0.976023, 0.982517, 0.972527, 0.986513, 0.97952, 0.971028, 0.978021, 0.974025, 0.970029, 0.983516, 0.986513, 0.990509, 0.964035, 0.961538, 0.982517, 0.986513, 0.966533, 0.975024, 0.982517, 0.97952, 0.961538, 0.968031, 0.993506, 0.978021, 0.910589, 0.962537, 0.93906, 0.967032, 0.958541, 0.971028, 0.970029, 0.954545, 0.983016, 0.983016, 0.960039, 0.938061, 0.978021, 0.961038, 0.986513, 0.983016, 0.96953, 0.976023, 0.982517, 0.977522, 0.977022, 0.946053, 0.953546, 0.951048, 0.942557, 0.956043, 0.946053, 0.923076, 0.98901, 0.987012, 0.999, 0.985014, 0.986513, 0.983016, 0.994505, 0.986013, 0.995004, 0.995004, 0.993506, 0.988011, 0.986013, 0.985514, 0.988511, 0.991008, 0.974525, 0.992007, 0.987012, 0.971528, 0.985014, 0.990509, 0.994005, 0.988511, 0.98901, 0.988511, 0.992007, 0.98901, 0.992007, 0.991508, 0.991008, 0.985514, 0.985514, 0.987012, 0.98901, 0.97902, 0.983016, 0.987012, 0.981518, 0.98951, 0.995504, 0.986513, 0.985014, 0.971028, 0.974525, 0.97902, 0.975524, 0.987012, 0.988511, 0.997002, 0.972027, 0.987512, 0.984515, 0.977522, 0.974525, 0.975024, 0.973526, 0.986013, 0.982017, 0.966033, 0.967032, 0.951548, 0.966533, 0.984515, 0.984515, 0.985514, 0.986013, 0.984515, 0.986513, 0.986513, 0.987012, 0.993506, 0.987512, 0.992507, 0.98901, 0.986513, 0.984515, 0.988511, 0.985014, 0.995504, 0.981018, 0.990509, 0.996503, 0.991508, 0.996003, 0.968531, 0.992007, 0.988011, 0.992007, 0.996503, 0.991008, 0.985514, 0.990509, 0.986513, 0.994505, 0.998001, 0.988011, 0.996003, 0.988011, 0.981518, 0.987512, 0.984015, 0.986013, 0.993506, 0.985514, 0.987512, 0.983516, 0.986513, 0.984515, 0.956043, 0.996503, 0.984015, 0.992007, 0.987012, 0.996003, 0.907592, 0.996003, 0.999}
$\endgroup$
4
  • 1
    $\begingroup$ I am not sure what probability would even mean in this case ... A histogram is discrete. It has bins. It makes sense to ask the probability that a data point falls in a certain bin. SmoothHistogram is inherently continuous, thus probability makes no sense. Only probability density does. Voting to close as the question is due to a mathematical misunderstanding. $\endgroup$
    – Szabolcs
    Commented Oct 7, 2018 at 19:34
  • $\begingroup$ That is true. My problem is that I have a Histogram (as you say, discrete) that I would like to overlay with the smooth curve given by $dist$ where probability is on the y axis. Thinking about what you say, I guess that is not possible. $\endgroup$
    – user120911
    Commented Oct 7, 2018 at 19:39
  • 1
    $\begingroup$ Instead of using SmoothHistogram, you could compute a SmoothKernelDistribution, then Plot the PDF of its output with an appropriate scaling factor (i.e. the bin width) to match the binned histogram. $\endgroup$
    – Szabolcs
    Commented Oct 7, 2018 at 19:58
  • 2
    $\begingroup$ @Szabolcs I'd argue the other way around: Get a PDF from both and stick with the PDF (no other scaling). The "Probability" option for Histogram gives relative frequencies (which is far better than using the default Counts as that accounts for different sample sizes). I think that one almost always wants (or should want) a vertical axis for a histogram that is independent of the sample size. If one is interested in estimated probability density function, then probability density should be the vertical axis. (And I agree that Probability makes no sense if there are no bins.) $\endgroup$
    – JimB
    Commented Oct 8, 2018 at 0:38

1 Answer 1

9
$\begingroup$

You can use "PDF" as the third argument for both Histogram and SmoothHistogram ... and then, if you have to, you can rescale the vertical axis to "Probability" values:

SeedRandom[1]
data = RandomVariate[dist[7.788017927062043, 0.0011475109935367525], 5000];
colors = {LightBlue, Green};
{hist1, hist2} =  Histogram[data, Automatic, #, 
     ChartStyle -> Last[colors = RotateRight[colors]], PlotLabel -> #,
     PerformanceGoal -> "Speed"] & /@ {"PDF", "Probability"};
shist = SmoothHistogram[data, Automatic, "PDF", PlotStyle -> Directive[Thick, Red]];


Row[{Show[hist1, ImageSize -> 300], Show[hist1, shist, ImageSize -> 300]}]

enter image description here

We first find the scaling factor using the maximum heights in hist1 and hist2:

scale = Divide @@ (Max[Cases[#[[1]], Rectangle[{_, _}, {_, h_}, ___] :> h, ∞]] & /@ 
   {hist1, hist2});

Since the scaling of the vertical axis is linear, we can simply re-scale the tick labels using Charting`FindTicks:

Row[{Show[hist2, ImageSize -> 300], 
  Show[hist1, shist, Ticks -> {Automatic, Charting`FindTicks[{0, scale}, {0, 1}]},
     PlotLabel -> "Probability", ImageSize -> 300]}]

enter image description here

Update: Using data = s1 we get the following pictures:

Row[{Show[hist1, ImageSize -> 300], Show[hist1, shist, ImageSize -> 300]}]

enter image description here

Row[{Show[hist2, ImageSize -> 300], 
  Show[hist1, shist, Ticks -> {Automatic, Charting`FindTicks[{0, scale}, {0, 1}]}, 
   PlotLabel -> "Probability", ImageSize -> 300]}]

enter image description here

 

$\endgroup$
4
  • $\begingroup$ +1 for "if you have to" (although the rest of the answer is good, too). $\endgroup$
    – JimB
    Commented Oct 8, 2018 at 5:23
  • $\begingroup$ Thank you @JimB. $\endgroup$
    – kglr
    Commented Oct 8, 2018 at 5:27
  • $\begingroup$ @kglr I must admit I am having trouble implementing your solution using my actual data. Could you please show me how the code changes given my data, which I have added in an edit? $\endgroup$
    – user120911
    Commented Oct 8, 2018 at 9:47
  • 3
    $\begingroup$ @user120911, the only change you need to do is to replace the second line with data = s1. I posted the pictures you should get when you do this replacement. $\endgroup$
    – kglr
    Commented Oct 8, 2018 at 10:14

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.