Wolfram Language

Extended Probability & Statistics

Random Sampling from a Singular PDF

Define a formula distribution by a probability density function. The density function is not continuous and contains an infinite singularity.

In[1]:=
Click for copyable input
dist = ProbabilityDistribution[ Piecewise[{{1/(4 Sqrt[Abs[x]]), -1 < x < 0}, {2/3, 1/4 < x <= 1}}, 0], {x, -1, 1}]
Out[1]=

Generate a random sample from the distribution and compare its histogram with the density function.

In[2]:=
Click for copyable input
sample = RandomVariate[dist, 10^5];
show complete Wolfram Language input
In[3]:=
Click for copyable input
Show[Histogram[sample, {-1, 1, 0.05}, "PDF", PlotTheme -> "Detailed"], Plot[PDF[dist, x], {x, -1, 1}, ImageSize -> Medium, PlotTheme -> "Marketing", PlotRange -> {0, 2.5}], ImageSize -> Medium]
Out[3]=

Related Examples

de es fr ja ko pt-br ru zh