Logo

RANDEXP

Updated 2023-10-18 15:48:17.713000

Syntax

SELECT * FROM [westclintech].[wct].[RANDEXP](
  <@Rows, int,>
 ,<@lambda, float,>)

Description

Use the table-valued function RANDEXP to generate a sequence of random numbers from an exponential distribution with rate @lambda.

Arguments

@Rows

the number of rows to generate. @Rows must be of the type int or of a type that implicitly converts to int.

@lambda

the rate parameter. @lambda must be of the type float or of a type that implicitly converts to float.

Return Type

table

{"columns": [{"field": "colName", "headerName": "Name", "header": "name"}, {"field": "colDatatype", "headerName": "Type", "header": "type"}, {"field": "colDesc", "headerName": "Description", "header": "description", "minWidth": 1000}], "rows": [{"id": "d9d609f2-1431-4f98-b882-dcdf7abcfa22", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "a0cac62e-a9d5-4a65-8a1e-5e4c1a5d1f8e", "colName": "X", "colDatatype": "float", "colDesc": "The random variable"}]}

Remarks

@lambda must be greater than zero.

If @lambda is NULL then @lambda is set to 1.

If @Rows is less than 1 then no rows are returned.

Examples

In this example we create a sequence 1,000,000 random numbers rounded to one decimal place from an exponential distribution with @lambda = 1, COUNT each result, paste then into Excel, and graph them.

SELECT X,

       COUNT(*) as [COUNT]

FROM

(

    SELECT ROUND(X, 1) as X

    FROM wct.RANDEXP(   1000000, --@Rows

                        1        --@lambda

                    )

) n

GROUP BY X

ORDER BY X;

This produces the following result.

http://westclintech.com/Portals/0/images/doc_math_RANDEXP_img1.jpg

In this example we generate 1,000,000 random numbers from an exponential distribution with @lambda of 2. We calculate the mean, standard deviation, skewness, and excess kurtosis from the resultant table and compare those values to the expected values for the distribution.

DECLARE @size as int = 1000000;

DECLARE @mu as float = 2;

DECLARE @mean as float = 1e+00 / @mu;

DECLARE @var as float = POWER(@mu, -2);

DECLARE @stdev as float = SQRT(@var);

DECLARE @skew as float = 2;

DECLARE @kurt as float = 6;

SELECT stat,

       [RANDEXP],

       [EXPECTED]

FROM

(

    SELECT x.*

    FROM

    (

        SELECT AVG(x) as mean_EXP,

               STDEVP(x) as stdev_EXP,

               wct.SKEWNESS_P(x) as skew_EXP,

               wct.KURTOSIS_P(x) as kurt_EXP

        FROM wct.RANDEXP(@size, @mu)

    ) n

        CROSS APPLY

    (

        VALUES

            ('RANDEXP', 'avg', mean_EXP),

            ('RANDEXP', 'stdev', stdev_EXP),

            ('RANDEXP', 'skew', skew_EXP),

            ('RANDEXP', 'kurt', kurt_EXP),

            ('EXPECTED', 'avg', @mean),

            ('EXPECTED', 'stdev', @stdev),

            ('EXPECTED', 'skew', @skew),

            ('EXPECTED', 'kurt', @kurt)

    ) x (fn_name, stat, val_stat)

) d

PIVOT

(

    sum(val_stat)

    FOR fn_name in ([RANDEXP], [EXPECTED])

) P;

This produces the following result (your result will be different).

{"columns":[{"field":"stat"},{"field":"RANDEXP","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"},{"field":"EXPECTED","headerClass":"ag-right-aligned-header","cellClass":"ag-right-aligned-cell"}],"rows":[{"stat":"avg","RANDEXP":"0.499650127829404","EXPECTED":"0.5"},{"stat":"kurt","RANDEXP":"6.0879572189952","EXPECTED":"6"},{"stat":"skew","RANDEXP":"2.00816423222553","EXPECTED":"2"},{"stat":"stdev","RANDEXP":"0.500127369627885","EXPECTED":"0.5"}]}

See Also

EXPINV - Calculate the inverse of the cumulative distribution function of the exponential distribution.

RANDBETA - Random numbers from a beta distribution

RANDBINOM - Random numbers from a binomial distribution

RANDCAUCHY - Random numbers from a Cauchy distribution

RANDCHISQ - Random numbers from a chi-squared distribution

RANDFDIST - Random numbers from an F-distribution

RANDGAMMA - Random numbers from a gamma distribution

RANDLAPLACE - Random numbers from a LaPlace distribution

RANDLOGISTIC - Random numbers from a logistic distribution

RANDNORMAL - Random numbers from the normal distribution

RANDPOISSON - Random numbers from a Poisson distribution

RANDSNORMAL - Random numbers from the standard normal distribution

RANDTDIST - Random numbers from Student's t distribution

RANDWEIBULL - Generate a sequence of random numbers from w Weibull distribution with parameters shape (?) and scale (?).