RANDGAMMA
Updated 2023-10-18 15:55:38.437000
Syntax
SELECT * FROM [westclintech].[wct].[RANDGAMMA](
<@Rows, int,>
,<@Shape, float,>
,<@Scale, float,>)
Description
Use the table-valued function RANDGAMMA to generate a sequence of random numbers from a gamma 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.
@Rows
the number of rows to generate. @Rows must be of the type int or of a type that implicitly converts to int.
@Shape
the shape parameter. @Shape 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": "bfa395df-6be4-46d2-835b-c0ebad3f5538", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "9244da76-8813-4f0a-bbad-bcd6d0e34584", "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 1,000,000 random numbers rounded to one decimal place from a gamma distribution with @Shape = 9 and @Scale = 0, COUNT the results, paste them into Excel, and graph them.
SELECT X,
COUNT(*) as [COUNT]
FROM
(
SELECT ROUND(X, 1) as X
FROM wct.RANDGAMMA( 1000000, --@Rows
9, --@Shape
0.5 --@Scale
)
) n
GROUP BY X
ORDER BY X;
This produces the following result.
In this example we generate 1,000,000 random numbers from a gamma distribution with @Shape of 5 and @Scale 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 @Shape as float = 5;
DECLARE @scale as float = 2;
DECLARE @mean as float = @Shape * @Scale;
DECLARE @var as float = @Shape * POWER(@Scale, 2);
DECLARE @stdev as float = SQRT(@var);
DECLARE @skew as float = 2 / SQRT(@Shape);
DECLARE @kurt as float = 6e+00 / @Shape;
SELECT stat,
[RANDGAMMA],
[EXPECTED]
FROM
(
SELECT x.*
FROM
(
SELECT MIN(x) as min_GAMMA,
AVG(x) as mean_GAMMA,
MAX(x) as max_GAMMA,
STDEVP(x) as stdev_GAMMA,
wct.SKEWNESS_P(x) as skew_GAMMA,
wct.KURTOSIS_P(x) as kurt_GAMMA
FROM wct.RANDGAMMA(@size, @Shape, @scale)
) n
CROSS APPLY
(
VALUES
('RANDGAMMA', 'avg', mean_GAMMA),
('RANDGAMMA', 'stdev', stdev_GAMMA),
('RANDGAMMA', 'skew', skew_GAMMA),
('RANDGAMMA', 'kurt', kurt_GAMMA),
('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 ([RANDGAMMA], [EXPECTED])
) P;
This produces the following result (your result will be different).
{"columns":[{"field":"stat"},{"field":"RANDGAMMA","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","RANDGAMMA":"10.0025412937376","EXPECTED":"10"},{"stat":"kurt","RANDGAMMA":"1.18154165719261","EXPECTED":"1.2"},{"stat":"skew","RANDGAMMA":"0.892342912517966","EXPECTED":"0.894427190999916"},{"stat":"stdev","RANDGAMMA":"4.47160145369588","EXPECTED":"4.47213595499958"}]}
See Also
GAMMAINV - inverse gamma 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
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