1
$\begingroup$

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]
$\endgroup$
5
  • 1
    $\begingroup$ There are 4 weights {0,1,2,3}. You need three ? $\endgroup$
    – Syed
    Oct 9, 2022 at 17:43
  • $\begingroup$ With three weights it plots 1 in interval [2,3], but 1 should be for x>3 as far as I know $\endgroup$
    – joe963
    Oct 9, 2022 at 17:47
  • $\begingroup$ For this syntax, the weights go before the data. $\endgroup$
    – Syed
    Oct 9, 2022 at 18:10
  • $\begingroup$ Still doesn't work $\endgroup$
    – joe963
    Oct 9, 2022 at 18:56
  • $\begingroup$ 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. $\endgroup$
    – JimB
    Oct 10, 2022 at 15:00

2 Answers 2

0
$\begingroup$

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
 ]

Without weights

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
 ]

With weights

$\endgroup$
0
$\begingroup$

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]

cdf

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

pdf

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

$\endgroup$

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.