目标是参数化对数正态分布,然后对于给定的百分位数应该具有给定的平均值和给定的值。
如何在知道样本均值和某个分位数的情况下拟合对数正态分布的参数?
机器算法验证
意思是
配件
分位数
对数正态分布
2022-03-21 09:15:21
1个回答
令和为对应正态分布的参数(分别为均值和标准差)。给定对数正态平均值和百分位数的值,我们需要找到和。
为此,设为标准正态分布函数。两条信息是
,其中。
从第一个中减去第二个并乘以产生
中的二次方程,用通常的二次公式求解。将有零个、一个或两个解决方案。接近时,可能会出现两种解。
然后通过使用任何一个原始方程,找到例如,
会做得很好。
(一种特殊情况是当时,对应于中位数,其中的公式简化为这是 @Glen_b 在Can I get the parameters of a lognormal distribution of a sample mean & median? 获得的解,它使用“ ”表示“ “。)
为了将这些估计值与数据拟合,请考虑测量拟合优度,以便在可用时区分两个解决方案。一个统计应该做的很好。此方法在以下代码中进行了说明,该代码模拟数据、执行分析、绘制数据直方图并绘制解决方案。当解决方案不合适时,它的情节就会淡出。这是一个例子。R
#
# Given a mean `m` and `alpha` quantile `z, find the matching parameters of any
# lognormal distributions.
#
f <- function(m, z, alpha) {
B <- -2 * qnorm(alpha)
C <- 2*(log(z) - log(m))
sigma <- (-B + c(-1,1)*sqrt(B^2 - 4*C)) / 2
sigma <- sigma[sigma > 0 & !is.na(sigma)]
mu <- log(m) - sigma^2 / 2
return(cbind(mu=mu, sigma=sigma))
}
#
# Compute a chi-squared statistic for data `x` corresponding to binning
# a lognormal distribution with parameter `theta` into `n` equal-size bins.
#
chi.squared <- function(theta, x, n=4) {
cutpoints <- exp(qnorm(seq(0, 1, length.out=n+1), theta[1], theta[2]))
counts <- table(cut(x, cutpoints))
expected <- length(x) / n
stat <- sum((counts - expected)^2 / expected)
}
#
# Simulate data, compute their statistics, and estimate matching lognormal
# distributions.
#
set.seed(17)
x <- exp(rnorm(20, sd=0.4))
m <- mean(x)
alpha <- 0.9
z <- quantile(x, alpha)
theta <- f(m, z, alpha)
stats <- apply(theta, 1, chi.squared, x=x)
#
# Plot the data and any matching lognormal density functions.
#
hist(x, freq=FALSE, breaks=12)
col <- "Red"
invisible(apply(theta, 1, function(q) {
stat <- chi.squared(q, x, min(length(x), 5))
curve(dnorm(log(x), q["mu"], q["sigma"])/x, add=TRUE, lwd=2,
col=hsv(0, min(1, 2/sqrt(1 + 10*stat/length(x))), 0.9))
}))