Commands for Computing Properties of Random Variables - Maple Programming Help

Home : Support : Online Help : Statistics and Data Analysis : Statistics Package : Random Variables : Statistics/RandomVariables

Commands for Computing Properties of Random Variables

 The Statistics package provides a wide range of tools for working with random variables. This includes tools for creating random variables from specific distributions, commands for computing basic quantities, and related functions, simulation and visualization routines.

Creating New Random Variables

 • Random variables are created using the RandomVariable command.
 > with(Statistics):
 > X := RandomVariable(Normal(5, 1));
 ${X}{≔}{\mathrm{_R}}$ (1)
 > Y := RandomVariable(Normal(7, 1));
 ${Y}{≔}{\mathrm{_R0}}$ (2)
 • Random variables can be distinguished from ordinary variables (names) by their attributes. Type RandomVariable can be used to query whether a given Maple object is a random variable or not.
 > type(X, RandomVariable);
 ${\mathrm{true}}$ (3)
 > type(Z, RandomVariable);
 ${\mathrm{false}}$ (4)
 > indets(X+Y+Z, RandomVariable);
 $\left\{{\mathrm{_R}}{,}{\mathrm{_R0}}\right\}$ (5)

Computing with Random Variables

 • The Statistics package provides a number of tools for computing basic quantities and functions. Single random variables as well as algebraic expressions (e.g. linear combinations, products, etc.) involving random variables are supported. Different random variables involved in an expression are considered to be independent. By default, all computations involving random variables are performed symbolically.

 compute the average absolute deviation cumulative distribution function central moments cumulant generating function characteristic function cumulants cumulant generating function cumulative distribution function deciles compute expected values hazard (failure) rate geometric mean harmonic mean hazard (failure) rate Hodges-Lehmann statistic interquartile range inverse survival function kurtosis generate a procedure for calculating statistical quantities arithmetic mean average absolute deviation from the mean median compute the median absolute deviation moment generating function Mills ratio mode moments moment generating function order statistics probability density function percentiles compute the probability of an event probability density function probability function quadratic mean quantiles quartiles create new random variables Rousseeuw and Croux' Qn Rousseeuw and Croux' Sn skewness standard deviation standard error of the sampling distribution standardized moments support set of a random variable survival function variance coefficient of variation

Examples

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

Compute the PDF and the CDF of the non-central beta distribution.

 > $\mathrm{PDF}\left(\mathrm{NonCentralBeta}\left(5,3,m\right),t\right)$
 ${{}\begin{array}{cc}{0}& {t}{<}{0}\\ {{ⅇ}}^{{-}\frac{{1}}{{2}}{}{m}}{}{{t}}^{{4}}{}{\left({1}{-}{t}\right)}^{{2}}{}{{ⅇ}}^{\frac{{1}}{{2}}{}{m}{}{t}}{}\left(\frac{{1}}{{16}}{}{{m}}^{{3}}{}{{t}}^{{3}}{+}\frac{{21}}{{8}}{}{{m}}^{{2}}{}{{t}}^{{2}}{+}\frac{{63}}{{2}}{}{m}{}{t}{+}{105}\right)& {t}{\le }{1}\\ {0}& {\mathrm{otherwise}}\end{array}$ (6)
 > $\mathrm{CDF}\left(\mathrm{NonCentralBeta}\left(5,3,m\right),t\right)$
 ${{}\begin{array}{cc}{0}& {t}{\le }{0}\\ \frac{{1}}{{8}}{}\left({{m}}^{{2}}{}{{t}}^{{4}}{-}{2}{}{{m}}^{{2}}{}{{t}}^{{3}}{+}{{m}}^{{2}}{}{{t}}^{{2}}{+}{24}{}{m}{}{{t}}^{{3}}{-}{52}{}{m}{}{{t}}^{{2}}{+}{28}{}{m}{}{t}{+}{120}{}{{t}}^{{2}}{-}{280}{}{t}{+}{168}\right){}{{t}}^{{5}}{}{{ⅇ}}^{{-}\frac{{1}}{{2}}{}{m}{+}\frac{{1}}{{2}}{}{m}{}{t}}& {t}{\le }{1}\\ {1}& {1}{<}{t}\end{array}$ (7)

Compute the PDF, mean, standard deviation and moments of a Beta random variable.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{Β}\left(p,q\right)\right)$
 ${X}{≔}{\mathrm{_R3}}$ (8)
 > $\mathrm{PDF}\left(X,t\right)$
 ${{}\begin{array}{cc}{0}& {t}{<}{0}\\ \frac{{{t}}^{{-}{1}{+}{p}}{}{\left({1}{-}{t}\right)}^{{-}{1}{+}{q}}}{{\mathrm{Β}}{}\left({p}{,}{q}\right)}& {t}{<}{1}\\ {0}& {\mathrm{otherwise}}\end{array}$ (9)
 > $\mathrm{Mean}\left(X\right)$
 $\frac{{p}}{{p}{+}{q}}$ (10)
 > $\mathrm{StandardDeviation}\left(X\right)$
 $\frac{\sqrt{\frac{{p}{}{q}}{{p}{+}{q}{+}{1}}}}{{p}{+}{q}}$ (11)
 > $\mathrm{Moment}\left(X,n\right)$
 $\frac{{\mathrm{Γ}}{}\left({1}{+}{n}{+}{p}\right){}{\mathrm{Γ}}{}\left({q}\right)}{{\mathrm{Γ}}{}\left({n}{+}{p}{+}{q}\right){}{\mathrm{Β}}{}\left({p}{,}{q}\right){}\left({n}{+}{p}\right)}$ (12)

Create two normal random variables.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(0,1\right)\right)$
 ${X}{≔}{\mathrm{_R4}}$ (13)
 > $Y≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(0,1\right)\right)$
 ${Y}{≔}{\mathrm{_R5}}$ (14)

Compute the density of X/Y. Compare the result with the Cauchy density.

 > $\mathrm{PDF}\left(\frac{X}{Y},t\right)$
 $\frac{{1}}{\left({{t}}^{{2}}{+}{1}\right){}{\mathrm{π}}}$ (15)
 > $\mathrm{PDF}\left(\mathrm{Cauchy}\left(0,1\right),t\right)$
 $\frac{{1}}{\left({{t}}^{{2}}{+}{1}\right){}{\mathrm{π}}}$ (16)

Compute some probabilities.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{Normal}\left(0,1\right)\right):$
 > $\mathrm{Probability}\left({X}^{2}
 $\frac{{1}}{{2}}{}{\mathrm{erf}}{}\left(\frac{{1}}{{2}}{}\sqrt{{2}}\right)$ (17)

The speed distribution for the molecules of an ideal gas.

 > $\mathrm{assume}\left(0
 > $\mathrm{σ}≔\sqrt{\frac{kT}{m}}$
 ${\mathrm{σ}}{≔}\sqrt{\frac{{\mathrm{k~}}{}{\mathrm{T~}}}{{\mathrm{m~}}}}$ (18)
 > $f≔\mathrm{simplify}\left(\mathrm{piecewise}\left(x<0,0,\frac{\sqrt{\frac{2}{\mathrm{π}}}{x}^{2}{ⅇ}^{-\frac{{x}^{2}}{2{\mathrm{σ}}^{2}}}}{{\mathrm{σ}}^{3}}\right)\right)$
 ${f}{≔}{{}\begin{array}{cc}{0}& {x}{<}{0}\\ \frac{\sqrt{{2}}{}{{\mathrm{m~}}}^{{3}{/}{2}}{}{{x}}^{{2}}{}{{ⅇ}}^{{-}\frac{{1}}{{2}}{}\frac{{{x}}^{{2}}{}{\mathrm{m~}}}{{\mathrm{k~}}{}{\mathrm{T~}}}}}{\sqrt{{\mathrm{π}}}{}{{\mathrm{k~}}}^{{3}{/}{2}}{}{{\mathrm{T~}}}^{{3}{/}{2}}}& {0}{\le }{x}\end{array}$ (19)
 > $\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}}$ (20)

Create random variable having this distribution.

 > $X≔\mathrm{RandomVariable}\left(\mathrm{MD}\right)$
 ${X}{≔}{\mathrm{_R8}}$ (21)

Compute average molecular speed.

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

Compute average kinetic energy.

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

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}{≔}{{}\begin{array}{cc}{0}& {x}{<}{0}\\ {5.404273295}{}{{10}}^{{-10}}{}\sqrt{{2}}{}{{x}}^{{2}}{}{{ⅇ}}^{{-}{4.858603936}{}{{10}}^{{-7}}{}{{x}}^{{2}}}& {0}{\le }{x}\end{array}$ (24)
 > $\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.644346}\right\}$ (25)

Use simulation to verify the results.

 > $A≔\mathrm{Sample}\left(\mathrm{XHe},{10}^{5}\right)$
 ${A}{≔}\left[\begin{array}{c}{\mathrm{1 .. 100000}}{\mathrm{Vector}}{[}{\mathrm{row}}{]}\\ {\mathrm{Data Type:}}{\mathrm{float}}{[}{8}{]}\\ {\mathrm{Storage:}}{\mathrm{rectangular}}\\ {\mathrm{Order:}}{\mathrm{Fortran_order}}\end{array}\right]$ (26)
 > $\mathrm{Mode}\left(A\right)$
 ${1499.36185224997}$ (27)
 > $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}[\mathrm{display}]\left(P,Q\right)$