Logo

RANDWEIBULL

Updated 2023-10-18 19:39:12.350000

Syntax

SELECT * FROM [westclintech].[wct].[RANDWEIBULL](
  <@Rows, int,>
 ,<@Shape, float,>
 ,<@Scale, float,>)

Description

Use the table-valued function RANDWEIBULL to generate a sequence of random numbers from the Weibull distribution with parameters @Shape and @Scale.

Arguments

@Scale

the scale parameter. @Scale must be of the type float or of a type that implicitly converts to float.

@Shape

the shape parameter. @Shape must be of the type float or of a type that implicitly converts to float.

@Rows

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

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": "e6d778d7-2643-4dc9-8253-2cd43d4d74a8", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "883b810b-b429-49de-8a85-fd97d72d6277", "colName": "X", "colDatatype": "float", "colDesc": "The random variable"}]}

Remarks

@Shape must be greater than zero.

@Scale must be greater than zero.

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

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

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

Examples

In this example we create a sequence of 1,000,000 random numbers rounded to two decimal places from a Weibull distribution with @Shape = 5 and @Scale = 1, COUNT the results, paste them into Excel and graph them.

SELECT X,
       COUNT(*) as [COUNT]
FROM
(
    SELECT ROUND(X, 2) as X
    FROM wct.RANDWEIBULL(   1000000, --@Rows
                            5,       --@Shape
                            1        --@Scale
                        )
) n
GROUP BY X
ORDER BY X;

This produces the following result.

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

In this example we generate 1,000,000 random numbers from a Weibull distribution with @shape of 0.5 and @scale of 1. 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 @shape as float = 0.5;
DECLARE @scale as float = 1;
DECLARE @mean as float = @scale * wct.GAMMA(1e+00 + 1e+00 / @shape);
DECLARE @var as float = POWER(@scale, 2) * ((wct.GAMMA(1 + 2 / @shape) - POWER(
          wct.GAMMA(1 + 1 / @shape), 2)));
DECLARE @stdev as float = SQRT(@var);
DECLARE @skew as float
    = (POWER(@scale, 3) * wct.GAMMA(1 + 3 / @shape) - 3 * @mean * @var - POWER(
              @mean, 3)) / POWER(@stdev, 3);
DECLARE @kurt as float
    = (POWER(@scale, 4) * wct.GAMMA(1 + 4 / @shape) - 4 * @skew * POWER(@stdev, 3)
              * @mean - 6 * POWER(@mean, 2) * @var
       - POWER(@mean, 4)
      ) / POWER(@stdev, 4) - 3;
SELECT stat,
       [RANDWEIBULL],
       [EXPECTED]
FROM
(
    SELECT x.*
    FROM
    (
        SELECT AVG(x) as mean_WEIBULL,
               STDEVP(x) as stdev_WEIBULL,
               wct.SKEWNESS_P(x) as skew_WEIBULL,
               wct.KURTOSIS_P(x) as kurt_WEIBULL
        FROM wct.RANDWEIBULL(@size, @shape, @scale)
    ) n
        CROSS APPLY
    (
        VALUES
            ('RANDWEIBULL', 'avg', mean_WEIBULL),
            ('RANDWEIBULL', 'stdev', stdev_WEIBULL),
            ('RANDWEIBULL', 'skew', skew_WEIBULL),
            ('RANDWEIBULL', 'kurt', kurt_WEIBULL),
            ('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 ([RANDWEIBULL], [EXPECTED])
) P;

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

{"columns":[{"field":"stat"},{"field":"RANDWEIBULL","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","RANDWEIBULL":"2.0030448849346","EXPECTED":"2"},{"stat":"kurt","RANDWEIBULL":"86.8376706587384","EXPECTED":"84.72"},{"stat":"skew","RANDWEIBULL":"6.63436577845547","EXPECTED":"6.61876121339938"},{"stat":"stdev","RANDWEIBULL":"4.46758312588545","EXPECTED":"4.47213595499958"}]}

See Also

WEIBULLINV - The inverse Weibull distribution function

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

RANDEXP - Random numbers from an exponential 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