1
$\begingroup$

Below in code are two versions. I believe plot1 might be correct but not sure why its shifted to the left and how to fix it. plot2 is copied directly from the wolfman online documentation but using my own data. It doesn't look normalized and the continuous curve is almost flat. I know something is likely wrong with both of them. Please suggest edits.

Goal is to normalize histogram of probability density so that we have a mean at zero, standard deviation is 1, and total area under graph is 1. The graph should look similar to image:

a typical normalized histogram borrowed from wolfram documentation

raw = {1.32, 1.37, 1.43, 1.32, 1.36, 1.33, 1.38, 1.35, 1.48, 1.28, 
   1.31, 1.52, 1.51, 1.33, 1.32, 1.27, 1.35, 1.40, 1.27, 1.39, 1.50, 
   1.31, 1.34, 1.48, 1.36, 1.33, 1.40, 1.29, 1.35, 1.36, 1.33, 1.30, 
   1.28, 1.32, 1.34, 1.33, 1.29, 1.34, 1.34, 1.29, 1.35, 1.52, 1.29, 
   1.38, 1.40, 1.28, 1.36, 1.36, 1.32, 1.62, 1.36, 1.34, 1.33, 1.33, 
   1.30, 1.31, 1.33, 1.32, 1.36, 1.41};

(* plot1 almost looks correct except for shifting from mean *)
plotHistPDF[raw_, n_ : 3] := 
 Block[{len, μ, σ, bin, hist, dist},
  len = Length[raw];
  μ = Mean[raw];
  σ = StandardDeviation[raw];
  hist = Histogram[raw, Automatic, "PDF"];
  dist = Plot[
           PDF[NormalDistribution[μ, σ], x], {x, μ - n σ, μ + n σ}, 
           PlotRange -> All, Filling -> Axis];
  Show[
    hist, dist, 
    PlotLabel -> Style["Probabality Density Distribution", Bold, Orange], 
    AxesLabel -> {x, ρ}, ImageSize -> imgsize]
  ]

(* plot2 appears correctly placed but curve is flat. should be a bell shaped curve *)
Show[
  Histogram[raw, Automatic, "PDF"], 
  Plot[PDF[NormalDistribution[0, 1], x], {x, -4, 4}]
]
$\endgroup$
2
  • 2
    $\begingroup$ You need to change NormalDistribution[0,1] to NormalDistribution[Mean[raw], StandardDeviation[raw]] and {x, -4, 4} to {x, 1.2, 1.7}. If that fixes things, then don't be surprised if this question is closed as I don't see it helping anyone in the future. $\endgroup$
    – JimB
    May 26, 2020 at 19:49
  • $\begingroup$ By "shifted" do you mean that the left tail of the normal PDF is cut off, or that the distributions seem to be mismatched? If it's the first one, add PlotRange->All to Show. The second one is apparently caused by the fact that your raw data isn't normal, with its mode a bit to the left of its mean. $\endgroup$
    – aooiiii
    May 26, 2020 at 19:58

2 Answers 2

1
$\begingroup$

To summarize @JimB's comment, you should plot the PDF of the normal distribution that most closely resembles your data, i.e. the one with the same mean and standard deviation:

Show[
  Histogram[raw, Automatic, "PDF"],
  Plot[
    PDF[NormalDistribution @@ Through[{Mean, StandardDeviation}[raw]], x],
    {x, -4, 4}, PlotRange -> All,
    Filling -> Axis
  ]
]

plot with PDF


@aooiiii also mentioned another important point. You say that the plot of the PDF is "shifted to the left and how to fix it". If you mean that the distributions seem mismatched, then you have to realize that your raw data does not appear normally distributed, with its mode a bit to the left of its mean.

$\endgroup$
1
  • $\begingroup$ I meant it is shifted left away from the mean. is it possible to center the distribution so that it's mean is at zero and entire area under plot is equal to 1. believe that is called a normalized probability density or something like it. that is my goal. $\endgroup$ May 27, 2020 at 5:29
2
$\begingroup$

I get this plot: followed @JimB advice. Time to learn some statistics, again.

Histogram Plot

$\endgroup$

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