RANDPOISSON
Updated 2023-10-18 19:31:38.830000
Syntax
SELECT * FROM [westclintech].[wct].[RANDPOISSON](
<@Rows, int,>
,<@lambda, float,>)
Description
Use the table-valued function RANDPOISSON to generate a sequence of random integers from the Poisson distribution for a given @lambda.
Arguments
@lamda
the lambda parameter to the distribution. @lambda 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": "29a7c8ba-ccc0-4da2-a14a-c8ba404c8505", "colName": "Seq", "colDatatype": "int", "colDesc": "A monotonically increasing sequence number"}, {"id": "50b55fe2-bada-48a8-bce5-154846404402", "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 from a Poisson distribution with @lambda = 10, COUNT the results, paste them into Excel and graph them.
SELECT X,
COUNT(*) as [COUNT]
FROM
(
SELECT X
FROM wct.RANDPOISSON( 1000000, --@Rows
10 --@lambda
)
) n
GROUP BY X
ORDER BY 1;
This produces the following result.
In this example we generate 1,000,000 random numbers from a Poisson distribution with @lambda of 4. 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 @lambda as float = 4;
DECLARE @mean as float = @lambda;
DECLARE @var as float = @lambda;
DECLARE @stdev as float = SQRT(@lambda);
DECLARE @skew as float = 1 / @stdev;
DECLARE @kurt as float = 1 / @mean;
SELECT stat,
[RANDPOISSON],
[EXPECTED]
FROM
(
SELECT x.*
FROM
(
SELECT AVG(cast(x as float)) as mean_POISSON,
STDEVP(x) as stdev_POISSON,
wct.SKEWNESS_P(x) as skew_POISSON,
wct.KURTOSIS_P(x) as kurt_POISSON
FROM wct.RANDPOISSON(@size, @lambda)
) n
CROSS APPLY
(
VALUES
('RANDPOISSON', 'avg', mean_POISSON),
('RANDPOISSON', 'stdev', stdev_POISSON),
('RANDPOISSON', 'skew', skew_POISSON),
('RANDPOISSON', 'kurt', kurt_POISSON),
('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 ([RANDPOISSON], [EXPECTED])
) P;
This produces the following result (your result will be different).
{"columns":[{"field":"stat"},{"field":"RANDPOISSON","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","RANDPOISSON":"3.999409","EXPECTED":"4"},{"stat":"kurt","RANDPOISSON":"0.255689062291338","EXPECTED":"0.25"},{"stat":"skew","RANDPOISSON":"0.501722252577688","EXPECTED":"0.5"},{"stat":"stdev","RANDPOISSON":"1.99860567664535","EXPECTED":"2"}]}
See Also
POISSONINV - The poisson inverse 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
RANDSNORMAL - Random numbers from the standard normal distribution