I have a random variable with a CDF that is almost everywhere differentiable, but has a point mass somewhere:
H21[s_] := Exp[lambda*(s - 2*nBar)] * (1 + lambda*(nBar - nUnder))
H22[s_] := Exp[lambda * (s - 2*nBar)] * (1 - lambda*(s - 2*nBar))
H2[s_] := Piecewise[{{H21[s],
2*nUnder <= s < nUnder + nBar}, {H22[s],
nUnder + nBar <= s < 2*nBar}, {1, s >= 2 * nBar}}, 0]
nBar = 4;
nUnder = 3.5;
lambda = 2;
Plot[{H2[s]}, {s, nUnder, 2 * nBar + 1}]
H2
here is the CDF. I believe that this CDF is corresponding to drawing twice from
$$ H(s) = \begin{cases} 0 & \text{ if } s < n_0 \\ e^{\lambda (s - n_1)} & \text{ if } n_0 \leq s \leq n_1 \\ 1 & \text{ if } s > n_1 \end{cases}$$
(where $n_0$, $n_1$ is what I call nUnder
, nBar
in my code).
I would like to verify that I computed $H_2$ correctly. So I would like to draw often from $H_2$, and compare that empirical distribution against drawing independent pairs from $H$ and adding them up.
I tried to follow this answer, but failed miserably.
custom[a_] :=
ProbabilityDistribution[{"CDF", H2[x]}, {x, 2*nUnder, 2*nBar}]
g[a_] = CDF[custom[10], x];
h = Table[{g[x], x}, {x, 0, 40}] // N //
Interpolation[#, InterpolationOrder -> 0] &;
Show[h /@ (RandomReal[{0, 1}, {100}]) //
Histogram[#, Automatic, "Probability"] &,
Plot[PDF[custom[10], x - 1/2], {x, 0, 20}]]
After defining custom
to be the ProbabilityDistribution
as in the linked answer, I thought I was good to go (I carry around the unused parameter a_
, but that should do no harm).
However, I get Interpolation::inddp: The point 0. in dimension 1 is duplicated when assigning h
. What did go wrong - and how should I proceed?
H2
is wrong. The probability of the sum of two random samples being2*nUnder
is1/E^2
(just the product of the probabilities that each of the random samples isnUnder
using your values forlambda
,nUnder
, andnBar
) and the above definition ofH2
gives2/E^2
. The correct definition ofH2
is given in my answer below - just a slight difference from what you gave above. $\endgroup$