# Construct Distribution Histogram From Random Variable

Given a Beta Random Variable $X$ with parameters $\alpha, \beta$ and a positive constant $n$, suppose I am interested in the distribution of:

$$Y:=\lfloor nX\rfloor$$

Suppose I want a histogram showing the distribution of $Y$ in Mathematica. How can I go about plotting this histogram? Is it necessary that I generate many samples to approximate it first, or can Mathematica calculate it perfectly? Would you please provide some example code showing how you can obtain and plot this distribution?

• You'll get more help if you show what you've tried and when you get a good answer consider upvoting it or accepting it. (You haven't accepted an answer since December 2017.)
– JimB
Commented Sep 4, 2018 at 2:27

If you are interested in the distribution of $Y$, you don't want a histogram of counts. $Y$ is a discrete random variable. You want the vertical axis to be the estimated probability for the values of $Y$.

A DiscretePlot is what you want. One way to get the appropriate plot is to use HistogramList to get the probabilities.

data = Floor /@ (20 RandomVariate[BetaDistribution[4, 3], 1000]);

probability = HistogramList[data, {Min[data] - 1/2, Max[data] + 1/2, 1}, "PDF"]
(* {{3/2, 5/2, 7/2, 9/2, 11/2, 13/2, 15/2, 17/2, 19/2, 21/2, 23/2, 25/2,
27/2, 29/2, 31/2, 33/2}, {1/500, 13/500, 21/1000, 51/1000, 29/500,
23/250, 93/1000, 1/8, 97/1000, 57/500, 19/200, 9/100, 31/500, 57/1000, 17/1000}} *)

DiscretePlot[probability[[2, i - Min[data] + 1]], {i, Min[data], Max[data]}]


If you really have to have something that looks like a histogram (like if you boss insists on it or you're stuck in the 20th century), then you need to make sure that the bars are centered on the integer values:

Histogram[data, {Min[data] - 1/2, Max[data] + 1/2, 1}, "PDF"]


If you don't include {Min[data] - 1/2, Max[data] + 1/2, 1}, then the default will have the bars centered on 0.5, 1.5, 2.5, etc., which are values that $Y$ can't take on.

ClearAll[td]
td[n_, α_, β_] := TransformedDistribution[Floor[n x],

sample = RandomVariate[td[10, 2, 4], 500] ;
Histogram[sample]


Histogram[sample, Automatic, "PDF"]


Expectation[x, x \[Distributed] td[10, 2, 4]]


5667/2000

• Maybe it's my use of Mathematica 10.4 but unless I explicitly ask for the bins to be centered on integers, I get the integers being on the bin boundaries.
– JimB
Commented Sep 4, 2018 at 3:05
• @JimB, the picture is obtained in version 11.3 (Wolfram Cloud). I also get integers bin boundaries in version 9.
– kglr
Commented Sep 4, 2018 at 3:08
Histogram[Floor /@ (20 RandomVariate[BetaDistribution[4, 3], 1000])]