library(truncdist)
运行以下代码时,extrunc()和vartrunc()的输出是正确的:
# Mean and variance of Gamma distribution
m <- 40; v <- 9
# Derive shape and scale parameters
a <- m^2/v; s <- v/m
# Checks: mean = shape * scale, variance = mean * scale^2
a*s; a*s^2
# Generate 1M random samples from Gamma distribution
x <- rgamma(10^6,shape=a,rate=1/s)
# Checks
mean(x); var(x)
# Mean and variance of corresponding theoretical Gamma
extrunc("gamma",a=0,b=Inf,shape=a,rate=1/s) # = 40
vartrunc("gamma",a=0,b=Inf,shape=a,rate=1/s) # = 9
但是,将平均值从40更改为44: m < - 44; v&lt; - 9
再次运行代码,输出如下:
extrunc("gamma",a=0,b=Inf,shape=a,rate=1/s) # = 3.8101e-06
vartrunc("gamma",a=0,b=Inf,shape=a,rate=1/s) # = 1945