 Create a Custom Distribution - Maple Help

Statistics

 Distribution
 create new distribution Calling Sequence Distribution(D) Distribution(T1, T2, .., Tn) Parameters

 D - ProbabilityDistribution; built-in probability distribution Ti - Equation; function or quantity of the new distribution, including PDF, CDF, mean, moment, variance, and others Description

 • The Distribution command creates a new distribution with the specified distribution, functions, or quantities.
 • The first parameter can be one of the supported distributions or a distribution function or quantity.
 • A distribution can be defined by the following functions and quantities. The other functions and quantities are calculated if possible.  If not specified otherwise, the default value for all of these functions and quantities is FAIL, indicating that this quantity needs to be computed from others.
 – CDF - operator
 Operator mapping a value $t$ to the CDF of this distribution at $t$.
 – CDFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the CDF.
 – CentralMoment - operator
 Operator mapping a positive integer $n$ to the $n$th CentralMoment of this distribution.
 – CFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the CharacteristicFunction.
 – CGF - operator
 Operator mapping a value $t$ to the CGF of this distribution at $t$.
 – CGFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the CGF.
 – CharacteristicFunction - operator
 Operator mapping a value $t$ to the CharacteristicFunction of this distribution at $t$.
 – Conditions - list(boolean)
 List of conditions on parameters occurring in the other defining equations for this distribution. The conditions need to be satisfied for the distribution to be valid. If the conditions are at any point known not to be satisfied, an error is raised; otherwise, the conditions are implicitly assumed to be satisfied. For this quantity, the default value is the empty list, []; you cannot specify FAIL.
 If infolevel for Statistics is set to at least $2$, the implicit assumptions are printed when they are made.
 – Cumulant - operator
 Operator mapping a positive integer $n$ to the $n$th Cumulant of this distribution.
 – DiscreteValueMap - operator
 See the DiscreteValueMap help page.
 – HazardRate - operator
 Operator mapping a value $t$ to the HazardRate of this distribution at $t$.
 – HodgesLehmann - algebraic
 Expression giving the Hodges-Lehmann estimator of this distribution.
 – InverseSurvivalFunction - operator
 Operator mapping a value $t$ to the InverseSurvivalFunction of this distribution at $t$.
 – ISFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the InverseSurvivalFunction.
 – Kurtosis - algebraic
 Expression giving the Kurtosis of this distribution.
 – Mean - algebraic
 Expression giving the Mean of this distribution.
 – Median - algebraic
 Expression giving the Median of this distribution.
 – MGF - operator
 Operator mapping a value $t$ to the MGF of this distribution at $t$.
 – MGFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the MGF.
 – MillsRatio - operator
 Operator mapping a value $t$ to the MillsRatio of this distribution at $t$.
 – Mode - algebraic
 Expression giving the Mode of this distribution.
 – Moment - operator
 Operator mapping a positive integer $n$ to the $n$th Moment of this distribution.
 – PDF - operator
 Operator mapping a value $t$ to the PDF of this distribution at $t$.
 – PDFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the PDF.
 – PFNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the ProbabilityFunction.
 – ProbabilityFunction - operator
 Operator mapping a value $t$ to the ProbabilityFunction of this distribution at $t$.
 – Quantile - operator
 Operator mapping a value $t$ between 0 and 1 to the $t$th Quantile of this distribution.
 – QuantileNumeric - appliable
 Procedure for efficiently computing a floating-point approximation to the Quantile.
 – RandomSample - appliable
 Procedure for efficiently computing a random sample of this distribution. The procedure should accept all of the calling sequences that Sample accepts:
 • When called with a non-negative integer argument $n$, the procedure should return the sample as a Vector of length $n$ having datatype = float.
 • When called with an integer range or a list of integer ranges, the procedure should return the sample as an Array with those dimensions having datatype = float.
 • When called with any rtable having datatype = float, the procedure should return the sample in that rtable.
 – RousseeuwCrouxQn - algebraic
 Expression giving Rousseeuw and Croux' Qn of this distribution.
 – RousseeuwCrouxSn - algebraic
 Expression giving Rousseeuw and Croux' Sn of this distribution.
 – Skewness - algebraic
 Expression giving the Skewness of this distribution.
 – Specialize - appliable
 Procedure returning the distribution that Specialize of this distribution will return. The procedure should take a list of equations as its single argument, and return a new distribution data structure; the most convenient way to produce one is to call Distribution itself.
 – StandardDeviation - algebraic
 Expression giving the StandardDeviation of this distribution.
 – StandardizedMoment - operator
 Operator mapping a positive integer $n$ to the $n$th StandardizedMoment of this distribution.
 – SFNumeric - appliable
 Procedure for efficiently computing the SurvivalFunction of this distribution.
 – Support - algebraic .. algebraic
 Range containing the Support of this distribution. If a DiscreteValueMap is specified, then this should be the support in the source domain. See the DiscreteValueMap help page for more details. If the Support is not given explicitly, an attempt is made to infer the Support from the PDF or the ProbabilityFunction, falling back to the default value of $-\mathrm{\infty }..\mathrm{\infty }$ if this attempt fails.
 – SurvivalFunction - operator
 Operator mapping a value $t$ to the SurvivalFunction of this distribution at $t$.
 – Type - equal to either discrete or continuous
 Indicates whether the distribution is discrete (meaning it can only assume finitely or countably many values, each with positive probability) or continuous (meaning the probability for it to assume any particular value is $0$). The default value for this property is continuous; it cannot be FAIL. For discrete distributions that can assume non-integer values, you will need to specify a DiscreteValueMap. See the DiscreteValueMap help page for more details.
 – Variance - algebraic
 Expression giving the Variance of this distribution. Examples

 > $\mathrm{with}\left(\mathrm{Statistics}\right):$

Create a Gamma distribution.

 > $G≔\mathrm{Distribution}\left(\mathrm{GammaDistribution}\left(a,b\right)\right)$
 ${G}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Conditions}}{,}{\mathrm{ParentName}}{,}{\mathrm{Parameters}}{,}{\mathrm{CharacteristicFunction}}{,}{\mathrm{CGF}}{,}{\mathrm{Mean}}{,}{\mathrm{Mode}}{,}{\mathrm{MGF}}{,}{\mathrm{PDF}}{,}{\mathrm{Support}}{,}{\mathrm{Variance}}{,}{\mathrm{CDFNumeric}}{,}{\mathrm{QuantileNumeric}}{,}{\mathrm{RandomSample}}{,}{\mathrm{RandomSampleSetup}}{,}{\mathrm{RandomVariate}}{,}{\mathrm{MaximumLikelihoodEstimate}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (1)
 > $Y≔\mathrm{RandomVariable}\left(G\right)$
 ${Y}{≔}{\mathrm{_R}}$ (2)
 > $\mathrm{PDF}\left(Y,c\right)$
 $\left\{\begin{array}{cc}{0}& {c}{<}{0}\\ \frac{{\left(\frac{{c}}{{a}}\right)}^{{b}{-}{1}}{}{{ⅇ}}^{{-}\frac{{c}}{{a}}}}{{a}{}{\mathrm{\Gamma }}{}\left({b}\right)}& {\mathrm{otherwise}}\end{array}\right\$ (3)

Create a new distribution.

 > $U≔\mathrm{Distribution}\left(\mathrm{=}\left(\mathrm{PDF},t↦f\left(t\right)\right),\mathrm{=}\left(\mathrm{CDF},t↦F\left(t\right)\right),\mathrm{Mean}=3\right)$
 ${U}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{CDF}}{,}{\mathrm{Conditions}}{,}{\mathrm{Mean}}{,}{\mathrm{PDF}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (4)
 > $Z≔\mathrm{RandomVariable}\left(U\right)$
 ${Z}{≔}{\mathrm{_R0}}$ (5)
 > $\mathrm{PDF}\left(Z,t\right)$
 ${f}{}\left({t}\right)$ (6)
 > $\mathrm{CDF}\left(Z,t\right)$
 ${F}{}\left({t}\right)$ (7)
 > $\mathrm{MGF}\left(Z,t\right)$
 ${{\int }}_{{-}{\mathrm{\infty }}}^{{\mathrm{\infty }}}{{ⅇ}}^{{\mathrm{_t}}{}{t}}{}{f}{}\left({\mathrm{_t}}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{\mathrm{_t}}$ (8)
 > $\mathrm{Mean}\left(Z\right)$
 ${3}$ (9)
 > $\mathrm{Moment}\left(Z,1\right)$
 ${3}$ (10)
 > $\mathrm{Moment}\left(Z,2\right)$
 ${{\int }}_{{-}{\mathrm{\infty }}}^{{\mathrm{\infty }}}{{\mathrm{_t0}}}^{{2}}{}{f}{}\left({\mathrm{_t0}}\right)\phantom{\rule[-0.0ex]{0.3em}{0.0ex}}{ⅆ}{\mathrm{_t0}}$ (11)

The following is effectively a reimplementation of the BetaDistribution with parameters $1$ and $3$. We reimplement the numeric CDF procedure, so that we can call trace in order to see when it is used.

 > $\mathrm{myPDF}≔\mathrm{unapply}\left(\mathrm{PDF}\left(\mathrm{BetaDistribution}\left(1,3\right),t\right),t\right)$
 ${\mathrm{myPDF}}{≔}{t}{↦}\left\{\begin{array}{cc}{0}& {t}{<}{0}\\ {3}{\cdot }{\left({1}{-}{t}\right)}^{{2}}& {t}{<}{1}\\ {0}& {\mathrm{otherwise}}\end{array}\right\$ (12)
 > myCDFNumeric := proc(t)  return evalf(piecewise(t<0, 0, t<1, 3*t*hypergeom([-2, 1], , t), 1)); end proc; trace(myCDFNumeric):
 ${\mathrm{myCDFNumeric}}{≔}{\mathbf{proc}}\left({t}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{return}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{evalf}}{}\left({\mathrm{piecewise}}{}\left({t}{<}{0}{,}{0}{,}{t}{<}{1}{,}{3}{*}{t}{*}{\mathrm{hypergeom}}{}\left(\left[{−}{2}{,}{1}\right]{,}\left[{2}\right]{,}{t}\right){,}{1}\right)\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end proc}}$ (13)
 > $\mathrm{dist}≔\mathrm{Distribution}\left(\mathrm{PDF}=\mathrm{myPDF},\mathrm{CDFNumeric}=\mathrm{myCDFNumeric}\right)$
 ${\mathrm{dist}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Conditions}}{,}{\mathrm{PDF}}{,}{\mathrm{CDFNumeric}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (14)
 > $X≔\mathrm{RandomVariable}\left(\mathrm{dist}\right)$
 ${X}{≔}{\mathrm{_R2}}$ (15)

This does not call the numeric CDF procedure - only the regular one (which in this case integrates the PDF expression symbolically):

 > $\mathrm{CDF}\left(X,\frac{1}{2}\right)$
 $\frac{{7}}{{8}}$ (16)

But these calling sequences do lead to a call to myCDFNumeric:

 > $\mathrm{CDF}\left(X,0.5\right)$
 {--> enter q, args = .5 <-- exit q (now in GetValue) = .8749999998}
 ${0.8749999998}$ (17)
 > $\mathrm{CDF}\left(X,\frac{1}{2},'\mathrm{numeric}'\right)$
 {--> enter q, args = 1/2 <-- exit q (now in GetValue) = .8750000000}
 ${0.8750000000}$ (18)

For Median, it also depends on the numeric option (coincidentally, this call runs via Quantile):

 > $\mathrm{Median}\left(X\right)$
 ${-}\frac{{{2}}^{{2}}{{3}}}}{{2}}{+}{1}$ (19)
 > $\mathrm{Median}\left(X,'\mathrm{numeric}'\right)$
 {--> enter q, args = 0. <-- exit q (now in extproc) = 0.} {--> enter q, args = .25 <-- exit q (now in extproc) = .578124999999975} {--> enter q, args = .25 <-- exit q (now in extproc) = .578124999999975} {--> enter q, args = 0. <-- exit q (now in extproc) = 0.} {--> enter q, args = .25 <-- exit q (now in extproc) = .578124999999975} {--> enter q, args = .216216216216226 <-- exit q (now in extproc) = .518508281839222} {--> enter q, args = .206102270110636 <-- exit q (now in extproc) = .499627215365731} {--> enter q, args = .206301958179384 <-- exit q (now in extproc) = .500004694760124} {--> enter q, args = .206299474633007 <-- exit q (now in extproc) = .50000000116623} {--> enter q, args = .206299474015915 <-- exit q (now in extproc) = .500000000000049} {--> enter q, args = .206299474015889 <-- exit q (now in extproc) = .499999999999986}
 ${0.206299474015889}$ (20)

We create a parameterized distribution with PDF $\sqrt{at}$ at appropriate values $t$. The parameter $a$ must be a positive real number.

 > $\mathrm{parameterizedPDF}≔\mathrm{unapply}\left(\mathrm{piecewise}\left(t<0,0,t<\mathrm{root}\left(\frac{9}{4a},3\right),\mathrm{sqrt}\left(at\right),0\right),t\right)$
 ${\mathrm{parameterizedPDF}}{≔}{t}{↦}\left\{\begin{array}{cc}{0}& {t}{<}{0}\\ \sqrt{{a}{\cdot }{t}}& {t}{<}\frac{{{18}}^{{1}}{{3}}}{\cdot }{\left(\frac{{1}}{{a}}\right)}^{{1}}{{3}}}}{{2}}\\ {0}& {\mathrm{otherwise}}\end{array}\right\$ (21)
 > $\mathrm{parameterized}≔\mathrm{Distribution}\left(\mathrm{PDF}=\mathrm{parameterizedPDF},\mathrm{Conditions}=\left[0
 ${\mathrm{parameterized}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Conditions}}{,}{\mathrm{PDF}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (22)

By setting infolevel, we can see when these assumptions are used.

 > $\mathrm{infolevel}\left[\mathrm{Statistics}\right]≔2$
 ${{\mathrm{infolevel}}}_{{\mathrm{Statistics}}}{≔}{2}$ (23)
 > $\mathrm{PDF}\left(\mathrm{parameterized},t\right)$
 PDF -- using the following implicit assumptions: {0 < a}
 $\left\{\begin{array}{cc}{0}& {t}{<}{0}\\ \sqrt{{a}{}{t}}& {t}{<}\frac{{{18}}^{{1}}{{3}}}{}{\left(\frac{{1}}{{a}}\right)}^{{1}}{{3}}}}{{2}}\\ {0}& {\mathrm{otherwise}}\end{array}\right\$ (24)

It turns out that the Kurtosis of this distribution is independent of $a$.

 > $\mathrm{kurtosis}≔\mathrm{Kurtosis}\left(\mathrm{parameterized}\right)$
 Statistics:-Kurtosis -- using the following implicit assumptions: {0 < a}
 ${\mathrm{kurtosis}}{≔}\frac{{203}}{{99}}$ (25)
 > $\mathrm{simplify}\left(\mathrm{kurtosis}\right)$
 $\frac{{203}}{{99}}$ (26)

The speed distribution for the molecules of an ideal gas.

 > $\mathrm{assume}\left(0
 > $\mathrm{\sigma }≔\mathrm{sqrt}\left(\frac{kT}{m}\right)$
 ${\mathrm{\sigma }}{≔}\sqrt{\frac{{\mathrm{k~}}{}{\mathrm{T~}}}{{\mathrm{m~}}}}$ (27)
 > $f≔\mathrm{simplify}\left(\mathrm{piecewise}\left(x<0,0,\frac{\mathrm{sqrt}\left(\frac{2}{\mathrm{\pi }}\right)}{{\mathrm{\sigma }}^{3}}{x}^{2}\mathrm{exp}\left(-\frac{{x}^{2}}{2{\mathrm{\sigma }}^{2}}\right)\right)\right)$
 ${f}{≔}\left\{\begin{array}{cc}{0}& {x}{<}{0}\\ \frac{\sqrt{{2}}{}{{\mathrm{m~}}}^{{3}}{{2}}}{}{{x}}^{{2}}{}{{ⅇ}}^{{-}\frac{{{x}}^{{2}}{}{\mathrm{m~}}}{{2}{}{\mathrm{k~}}{}{\mathrm{T~}}}}}{\sqrt{{\mathrm{\pi }}}{}{{\mathrm{k~}}}^{{3}}{{2}}}{}{{\mathrm{T~}}}^{{3}}{{2}}}}& {0}{\le }{x}\end{array}\right\$ (28)
 > $\mathrm{MD}≔\mathrm{Distribution}\left(\mathrm{PDF}=\mathrm{unapply}\left(f,x\right)\right)$
 ${\mathrm{MD}}{≔}{\mathbf{module}}\left({}\right)\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{option}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Distribution}}{,}{\mathrm{Continuous}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{export}}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathrm{Conditions}}{,}{\mathrm{PDF}}{;}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{}\phantom{\rule[-0.0ex]{0.5em}{0.0ex}}{\mathbf{end module}}$ (29)

Create random variable having this distribution.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{MD}\right)$
 ${X}{≔}{\mathrm{_R5}}$ (30)

Compute average molecular speed.

 > $\mathrm{Mean}\left(X\right)$
 $\frac{{2}{}\sqrt{{2}}{}\sqrt{{\mathrm{k~}}}{}\sqrt{{\mathrm{T~}}}}{\sqrt{{\mathrm{\pi }}}{}\sqrt{{\mathrm{m~}}}}$ (31)

Compare with the Maxwell distribution.

 > $\mathrm{Mean}\left(\mathrm{Maxwell}\left(\mathrm{\sigma }\right)\right)$
 $\frac{{2}{}\sqrt{{2}}{}\sqrt{\frac{{\mathrm{k~}}{}{\mathrm{T~}}}{{\mathrm{m~}}}}}{\sqrt{{\mathrm{\pi }}}}$ (32)

Compute average kinetic energy.

 > $\mathrm{simplify}\left(\mathrm{ExpectedValue}\left(\frac{1}{2}m{X}^{2}\right)\right)$
 $\frac{{3}{}{\mathrm{k~}}{}{\mathrm{T~}}}{{2}}$ (33)

Helium at 25C.

 > $g≔\mathrm{eval}\left(f,\left[T=298.15,k=\mathrm{evalf}\left(\mathrm{ScientificConstants}:-\mathrm{Constant}\left('k'\right)\right),m=4.×{10}^{-27}\right]\right)$
 ${g}{≔}\left\{\begin{array}{cc}{0}& {x}{<}{0}\\ {5.404283672}{×}{{10}}^{{-10}}{}\sqrt{{2}}{}{{x}}^{{2}}{}{{ⅇ}}^{{-}{4.858610154}{×}{{10}}^{{-7}}{}{{x}}^{{2}}}& {0}{\le }{x}\end{array}\right\$ (34)
 > $\mathrm{\alpha }≔\mathrm{eval}\left(\mathrm{\sigma },\left[T=298.15,k=\mathrm{evalf}\left(\mathrm{ScientificConstants}:-\mathrm{Constant}\left('k'\right)\right),m=4.×{10}^{-27}\right]\right)$
 ${\mathrm{\alpha }}{≔}{1014.446097}$ (35)
 > $\mathrm{He}≔\mathrm{Distribution}\left(\mathrm{PDF}=\mathrm{unapply}\left(g,x\right)\right):$
 > $\mathrm{XHe}≔\mathrm{RandomVariable}\left(\mathrm{He}\right):$

Most probable speed.

 > $\mathrm{Mode}\left(\mathrm{XHe}\right)$
 $\left\{{1434.643427}\right\}$ (36)
 > $\mathrm{Mode}\left(\mathrm{Maxwell}\left(\mathrm{\alpha }\right)\right)$
 ${1434.643428}$ (37)

Use simulation to verify the results.

 > $A≔\mathrm{Sample}\left(\mathrm{XHe},{10}^{5},\mathrm{method}=\left[\mathrm{envelope},\mathrm{range}=0..5000\right]\right):$
 > $\mathrm{Mode}\left(A\right)$
 ${1455.01192692046}$ (38)
 > $P≔\mathrm{DensityPlot}\left(\mathrm{XHe},\mathrm{range}=0..3500,\mathrm{thickness}=3,\mathrm{color}=\mathrm{red}\right):$
 > $Q≔\mathrm{Histogram}\left(A,\mathrm{range}=0..3500\right):$
 > $\mathrm{plots}\left[\mathrm{display}\right]\left(P,Q\right)$  References

 Stuart, Alan, and Ord, Keith. Kendall's Advanced Theory of Statistics. 6th ed. London: Edward Arnold, 1998. Vol. 1: Distribution Theory. Compatibility

 • The Statistics[Distribution] command was updated in Maple 16.