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)$
 $\left\{\begin{array}{cc}{0}& {t}{<}{0}\\ {{ⅇ}}^{{-}\frac{{m}}{{2}}}{}{{t}}^{{4}}{}{\left({1}{-}{t}\right)}^{{2}}{}{{ⅇ}}^{\frac{{m}{}{t}}{{2}}}{}\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}\right\$ (6)
 > $\mathrm{CDF}\left(\mathrm{NonCentralBeta}\left(5,3,m\right),t\right)$
 $\left\{\begin{array}{cc}{0}& {t}{\le }{0}\\ \frac{\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}}}{{8}}& {t}{\le }{1}\\ {1}& {1}{<}{t}\end{array}\right\$ (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)$
 $\left\{\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}\right\$ (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{\Gamma }}{}\left({1}{+}{n}{+}{p}\right){}{\mathrm{\Gamma }}{}\left({q}\right)}{{\mathrm{\Gamma }}{}\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{\pi }}}$ (15)
 > $\mathrm{PDF}\left(\mathrm{Cauchy}\left(0,1\right),t\right)$
 $\frac{{1}}{\left({{t}}^{{2}}{+}{1}\right){}{\mathrm{\pi }}}$ (16)

Compute some probabilities.

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

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~}}}}$ (18)
 > $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\$ (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{PDF}}{,}{\mathrm{Conditions}}{;}\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{\pi }}}{}\sqrt{{\mathrm{m~}}}}$ (22)

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}}$ (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}{≔}\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\$ (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.643427}\right\}$ (25)

Use simulation to verify the results.

 > $A≔\mathrm{Sample}\left(\mathrm{XHe},{10}^{5}\right)$
  (26)
 > $\mathrm{Mode}\left(A\right)$
 ${1394.58033170991}$ (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}\left[\mathrm{display}\right]\left(P,Q\right)$