Multinom {stats}算法
[转载请注明出处,胡桃木屋 mathapply.cn ”R语言中文帮助“工做室译]app
The Multinomial Distributiondom
多维正态分布函数
Description(描述)this
Generate multinomially distributed random number vectors and compute multinomial probabilities.
生成多维正态分布随机数向量和计算多维正态分布几率。component
Usage(用法)
rmultinom(n, size, prob)
dmultinom(x, size = NULL, prob, log = FALSE)orm
Arguments(参数)对象
x
vector of length K of integers in 0:size.
长度为k的整数向量,从0:size递归
n
number of random vectors to draw.
要抽取的随机向量的个数。
size
integer, say N, specifying the total number of objects that are put into K boxes in the typical multinomial experiment. For dmultinom, it defaults to sum(x).
整数,天然数N,指定多维正态试验中被放入K个盒子对象的总数量,对于dmultnom,它默认为sum(x).
prob
numeric non-negative vector of length K, specifying the probability for the K classes; is internally normalized to sum 1. Infinite and missing values are not allowed.接口
长度为K的非负数值向量,指定K类的几率,内部规范化为1. 无限和缺失值是不容许的。
log
logical; if TRUE, log probabilities are computed.
逻辑型,若是为真,计算的是对数几率。
Details(详细说明)
If x is a K-component vector, dmultinom(x, prob) is the probability
P(X[1]=x[1], … , X[K]=x[k]) = C * prod(j=1 , …, K) p[j]^x[j]
where C is the ‘multinomial coefficient’ C = N! / (x[1]! * … * x[K]!) and N = sum(j=1, …, K) x[j].
若是x是由K部分组成的向量,几率dmultnom(x,prob)为
P(X[1]=x[1], … , X[K]=x[k]) = C * prod(j=1 , …, K) p[j]^x[j]
这里 C 是 ‘多维正态系数’ C = N! / (x[1]! * … * x[K]!) and N = sum(j=1, …, K) x[j].
By definition, each component X[j] is binomially distributed as Bin(size, prob[j]) for j = 1, …, K.
根据定义,每一个组成X[j]是二项分布,Bin(size, prob[j]), 其中j=1,...,K。
The rmultinom() algorithm draws binomials X[j] from Bin(n[j], P[j]) sequentially, where n[1] = N (N := size), P[1] = p[1] (p is prob scaled to sum 1), and for j ≥ 2, recursively, n[j] = N - sum(k=1, …, j-1) X[k] and P[j] = p[j] / (1 - sum(p[1:(j-1)])).
函数rmultinom() 的算法依次从Bin(n[j], P[j]) 中抽取X[j], 这里n[1] = N (N := size), P[1] = p[1] (p 是规范化为1的几率), 对于 j ≥ 2, 递归地, n[j] = N - sum(k=1, …, j-1) X[k] 和 P[j] = p[j] / (1 - sum(p[1:(j-1)])).
Value(返回值)
For rmultinom(), an integer K x n matrix where each column is a random vector generated according to the desired multinomial law, and hence summing to size. Whereas the transposed result would seem more natural at first, the returned matrix is more efficient because of columnwise storage.
对于rmultinom() 函数,返回的是一个K x n 的矩阵,这里每一列是根据所需的多维正态分布规则产生的随机向量。初看起来虽然说将结果转置看起来天然些,但按列返回的矩阵更有效,由于它是按列存储的。
Note(注)
dmultinom is currently not vectorized at all and has no C interface (API); this may be amended in the future.
目前dmultinom函数没有矢量化,也没有C的接口(API), 未来会对此进行修订。
See Also(另见)
Distributions for standard distributions, including dbinom which is a special case conceptually.
标准分布见Distributions,包括dbinom,从概念上来讲它是一种特殊状况。
Examples(示例)
rmultinom(10, size = 12, prob = c(0.1,0.2,0.8))
pr <- c(1,3,6,10) # normalization not necessary for generation
rmultinom(10, 20, prob = pr)
## all possible outcomes of Multinom(N = 3, K = 3) X <- t(as.matrix(expand.grid(0:3, 0:3))); X <- X[, colSums(X) <= 3] X <- rbind(X, 3:3 - colSums(X)); dimnames(X) <- list(letters[1:3], NULL) X round(apply(X, 2, function(x) dmultinom(x, prob = c(1,2,5))), 3)