# Producing a probability distribution numerically

Given that Mathematica could not produce a symbolic solution to the problem stated here, how can I produce many values of $$A$$ to then create $$B$$ numerically?

A = TransformedDistribution[Sqrt[x^2], x \[Distributed] NormalDistribution[\[Mu], \[Sigma]]]

B = TransformedDistribution[1/2 + Sqrt[c^2 d^2 (d^2 - c x^2)]/(2 c d^2), x \[Distributed] A]


An answer for the symbolic solution is given here.

But to generate random samples, one needs to specify all 4 parameters: $$\mu$$, $$\sigma$$, $$c$$, and $$d$$. Also, the random variable associated with distribution $$B$$ takes on only real values 100% of the time when $$c<0$$. Otherwise, some non-zero fraction of the time the values are complex.

(* Define probability distributions *)
A = TransformedDistribution[Sqrt[x^2], x \[Distributed] NormalDistribution[μ, σ]];
B = TransformedDistribution[1/2 + Sqrt[c^2 d^2 (d^2 - c x^2)]/(2 c d^2), x \[Distributed] A];

(* Set parameter values *)
(* One will get real values 100% of the time only if c < 0 *)
parms = {μ -> 1, σ -> 2, c -> -2, d -> 10};

(* Generate random samples *)
n = 10000;
xA = RandomVariate[A /. parms, n];
xB = 1/2 + Sqrt[c^2 d^2 (d^2 - c xA^2)]/(2 c d^2) /. parms;

(* Nonparametric density estimate *)
(* The "Bounded" option is necessary because the random variable has a maximum of 0 *)
skd = SmoothKernelDistribution[xB, Automatic, {"Bounded", {-∞, 0}, "Gaussian"}];

(* Plot histogram and nonparametric density estimate *)
Show[Histogram[xB, Automatic, "PDF", PlotRange -> {{Min[xB], 0}, All},
PlotLabel -> Style[ToString[parms], Bold, 18],
Frame -> True, FrameLabel -> (Style[#, Bold, 18] &)
/@ {"\!$$\*SubscriptBox[\(X$$, $$B$$]\)", "Probability density"}],
Plot[PDF[skd, z], {z, Min[xB], 0}, PlotRange -> {{Min[xB], 0}, All}]]


The histogram is only needed to help see that the "Bounded" option in "SmoothKernelDistribution" is necessary.