# How to plot correct empirical distribution?

I have data that represents number of values in intervals. So, I have intervals like: [0,1], [1,2], [2,3] and so on. My data is [23,11,3], which means in interval [0,1] there are 23 values, inside [1,2] 11 values etc. But when I try to plot my function I get error about sizes:

values = {23,11,3}
sum =  Total[values]
emp = EmpiricalDistribution[values/sum -> {  0, 1, 2, 3}]
Plot[CDF[emp][x], {x, 0, 3}, Frame -> True, PlotRange -> {0, 1.1},GridLines -> Automatic]

• There are 4 weights {0,1,2,3}. You need three ?
– Syed
Oct 9, 2022 at 17:43
• With three weights it plots 1 in interval [2,3], but 1 should be for x>3 as far as I know Oct 9, 2022 at 17:47
• For this syntax, the weights go before the data.
– Syed
Oct 9, 2022 at 18:10
• Still doesn't work Oct 9, 2022 at 18:56
• What you have is a discrete version (i.e., censored version) of a continuous distribution. As such it's neither fish nor fowl. So some Mathematica functions will give you the desired plots (as in my answer) but that answer won't give you correct random samples from your discrete distribution.
– JimB
Oct 10, 2022 at 15:00

It wasn't clear to me whether you wanted to impose weights to the data.

First, let's plot without weights.

emp = EmpiricalDistribution[values/sum]

Plot[CDF[emp][x], {x, 0, 3},
Frame -> True,
PlotRange -> {0, 1.1},
GridLines -> Automatic
]


Next, let's apply weights.

I will use {1,2,3} as an example

    empWeight = EmpiricalDistribution[{1, 2, 3} -> values/sum]


Now plot it and note the difference with the unweighted plot

Plot[CDF[empWeight][x], {x, 0, 3},
Frame -> True,
PlotRange -> {0, 1.1},
GridLines -> Automatic
]


Here's one way to obtain a plot of the CDF (and PDF) but it only works with a constant binwidth of 1 (which I think is a long-standing limitation of ProbabilityDistribution for discrete distributions - but I would certainly like to be corrected if that's not true: ProbabilityDistribution only accepts dx=1 for discrete distributions)

values = {23, 11, 3}
bins = {0, 1, 2, 3}
dist = ProbabilityDistribution[Sum[Boole[bins[[i]] <= x < bins[[i + 1]]] values[[i]]/(Total[values]),
{i, 1, Length[values]}], {x, Min[bins], Max[bins], 1}]
Plot[CDF[dist, x], {x, Min[bins] - 1, Max[bins] + 1},
Exclusions -> None]


Plot[PDF[dist, x] // Evaluate, {x, Min[bins] - 1, Max[bins] + 1},
Exclusions -> None]


(Why PDF requires // Evaluate to plot correctly and CDF does not is a mystery to me.)