gpt4 book ai didi

r - 从已知百分位数生成正态分布

转载 作者:行者123 更新时间:2023-12-05 06:45:58 24 4
gpt4 key购买 nike

如果我已经知道特定的百分位数,我会尝试了解如何生成正态分布。

一位用户对类似问题 (link here) 给出了非常全面的答案,但是当我尝试使用现有数据对其进行测试时,方差太大了。

我是怎么做到的:

x <- c(5,8,11)
PercRank <- c(2.1, 51.1, 98.8)

例如,PercRank = 2.1 表示 2.1% 的数据的值/分数 <= 5(x 的第一个值)。同样,PercRank = 51.1 表示 51.1% 的数据具有值/分数 <= 8。

我按照这个link中的方法.这是我的代码:

cum.p <- c(2.1, 51.1, 98.8)/100
prob <- c( cum.p[1], diff(cum.p), .01)
x <- c(5,8,11)

freq <- 1000 # final output size that we want

# Extreme values beyond x (to sample)
init <- -(abs(min(x)) + 1)
fin <- abs(max(x)) + 1

ival <- c(init, x, fin) # generate the sequence to take pairs from
len <- 100 # sequence of each pair

s <- sapply(2:length(ival), function(i) {
seq(ival[i-1], ival[i], length.out=len)
})
# sample from s, total of 10000 values with probabilities calculated above
out <- sample(s, freq, prob=rep(prob, each=len), replace = T)

quantile(out, cum.p)
# 2% 51.1% 98.8%
# 5 8 11

c(mean(out), sd(out))
# [1] 7.834401 2.214227

所有这些都来自评论 ( linked ),到目前为止一切顺利。然后我尝试检查生成的正态分布与我的拟合值的配合情况:

data.frame(sort(rnorm(1000, mean=mean(out), sd=sd(out))))
...
# 988 13.000904
# 989 13.028881
# 990 13.076649
...
# 1000 14.567080

我很担心,因为第 988 个值(例如,1000 个样本中的 98.8%)是 13.000904,而我为 98.8% 百分位数拟合的值是 11.0

我多次重新生成分布,方差一直比需要的大。

我被难住了。如果有人能告诉我一种使方差更准确的方法,我将不胜感激。或者,这是不可避免的吗?

(我第一次在这里发帖,如果我违反了规则,我深表歉意 - 如果需要,我可以说得更清楚。)

最佳答案

您为什么不将其视为优化问题?

x <- c(5,8,11)
PercRank <- c(2.1, 51.1, 98.8)

fun <- function(par, pq) {
sum((log(pq[,1]/100)-pnorm(pq[,2], mean=par[1], sd=par[2], log.p=TRUE))^2)
}

par.estimates <- optim(c(0,1), fn=fun, pq=cbind(PercRank, x))

pnorm(11, par.estimates[[1]][1], par.estimates[[1]][2])
#[1] 0.9816948

结果似乎是合理的,但与 q=11 的预期值存在一些差异。但是,我怀疑这是您的数据的问题(例如,由于四舍五入),因为以下方法运行良好:

PercRank <- pnorm(x, 8, 2)*100
par.estimates <- optim(c(0,1), fn=fun, pq=cbind(PercRank, x))
par.estimates[[1]]
#[1] 7.999774 1.999953

当然,对于这个特定问题可能有更好的优化器。

关于r - 从已知百分位数生成正态分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19996661/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com