I have a probability density function.
Is there a mechanism to create a Distribution
object from it, such that normal Mathematica commands that usually work on distributions, such as Variance
, RandomVariate
, etc., work on it?
1 Answer
You need ProbabilityDistribution
.
Example:
distr = ProbabilityDistribution[E^(-(x^2/2))/Sqrt[2 π], {x, -Infinity, Infinity}]
Mean[distr] (* ==> 0 *)
Variance[distr] (* ==> 1 *)
Warning:
You are responsible for ensuring that the probability density function you provide is normalized over its domain. Mathematica will not verify this and will not warn you if it isn't, however it will give you incorrect results when using the distribution. I know from experience that e.g. Variance
and RandomVariate
will give incorrect results in this case.
-
$\begingroup$ +1 for the warning, I have experienced that as well. It's something that should at least be noted in the documentation. $\endgroup$ Sep 5, 2014 at 20:18
-
$\begingroup$ @Guillochon ust checked, it's mentioned under Possible Issues. $\endgroup$– SzabolcsSep 5, 2014 at 20:48
-
$\begingroup$ This is new to the documentation in Mathematica 10 (and the online documentation), it is not mentioned clearly in the Mathematica 9 documentation. There, it appears under "properties & relations" and simply says "The integral of the PDF over the distribution domain needs to be unity." $\endgroup$ Sep 5, 2014 at 21:26
-
$\begingroup$ @Guillochon I think it's a good idea to use the feedback box at the bottom of online doc pages, they seem to listen to reasonable suggestions. $\endgroup$– SzabolcsSep 5, 2014 at 21:37
-
2$\begingroup$ Addendum to the warning: in current versions, use the setting
Method -> "Normalize"
if you wantProbabilityDistribution[]
to perform normalization of your PDF on your behalf. $\endgroup$ Mar 14, 2019 at 15:26
ProbabilityDistribution
[$\mathit{pdf}$, {$x$, $x_\min$, $x_\max$}] represents the continuous distribution with PDF $\mathit{pdf}$ in the variable $x$ where the $\mathit{pdf}$ is taken to be zero for $x < x_\min$ and $x > x_\max$." $\endgroup$