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

 > with(Statistics):

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

 > PDF(NonCentralBeta(5, 3, m), t);
 $\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)
 > CDF(NonCentralBeta(5, 3, m), t);
 $\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 := RandomVariable(Beta(p, q));
 ${X}{≔}{\mathrm{_R3}}$ (8)
 > PDF(X, t);
 $\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)
 > Mean(X);
 $\frac{{p}}{{p}{+}{q}}$ (10)
 > StandardDeviation(X);
 $\frac{\sqrt{\frac{{p}{}{q}}{{p}{+}{q}{+}{1}}}}{{p}{+}{q}}$ (11)
 > Moment(X, n);
 $\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 := RandomVariable(Normal(0, 1));
 ${X}{≔}{\mathrm{_R4}}$ (13)
 > Y := RandomVariable(Normal(0, 1));
 ${Y}{≔}{\mathrm{_R5}}$ (14)

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

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

Compute some probabilities.

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

The speed distribution for the molecules of an ideal gas.

 > assume(m > 0, k > 0, T > 0);
 > sigma := sqrt(k*T/m);
 ${\mathrm{\sigma }}{≔}\sqrt{\frac{{\mathrm{k~}}{}{\mathrm{T~}}}{{\mathrm{m~}}}}$ (18)
 > f := simplify(piecewise(x < 0, 0, sqrt(2/Pi)/sigma^3*x^2*exp(-x^2/2/sigma^2)));
 ${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)
 > MD := Distribution(PDF = unapply(f, x));
 ${\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 := RandomVariable(MD);
 ${X}{≔}{\mathrm{_R8}}$ (21)

Compute average molecular speed.

 > Mean(X);
 $\frac{{2}{}\sqrt{{2}}{}\sqrt{{\mathrm{k~}}}{}\sqrt{{\mathrm{T~}}}}{\sqrt{{\mathrm{\pi }}}{}\sqrt{{\mathrm{m~}}}}$ (22)

Compute average kinetic energy.

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

Helium at 25C.

 > g := eval(f, [T = 298.15, k = evalf(ScientificConstants:-Constant('k')), m = 4e-27]);
 ${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)
 > He := Distribution(PDF = unapply(g, x)):
 > XHe := RandomVariable(He):

Most probable speed.

 > Mode(XHe);
 $\left\{{1434.643427}\right\}$ (25)

Use simulation to verify the results.

 > A := Sample(XHe, 10^5);
  (26)
 > Mode(A);
 ${1394.58033170991}$ (27)
 > P := DensityPlot(XHe, range = 0..3500, thickness = 3, color = red):
 > Q := Histogram(A, range = 0..3500):
 > plots[display](P, Q);