作为Python的初学者,我了解到当你想将一些变量插入到一串文本中时,你会使用
的通用格式lambda = 3
shape = 6
scale = 2
size = 10000
eps = 1e-8
t = 10
# with probability 1-eps, n or less gamma distributed random variables are needed.
n = qpois(1-eps, lambda = lambda * t)
# sample from the gamma distribution. Not sure if it's ok to use the same sample every time.
# with this, the mean is of by about 10%.
# ys = c(rgamma(n = n, shape = shape, scale = scale))
# the interarrival times are exponentially distributed with rate lambda.
pp.exp = function (t0) {
# not sure how many Tn are needed :/
Tn = rexp(1000, lambda)
Sn = cumsum(Tn)
return(min(which(Sn > t0)) - 1)
}
# generate N(t) which follow the poisson process.
ns = sapply(1:size, function (i) pp.exp(t))
# generate X(t) as in the problem description.
xs = sapply(ns, function (n) {
ys = c(rgamma(n = n, shape = shape, scale = scale))
sum(ys[1:n])
})
它会输出
> # compare mean and variance of 'size' samples of X(t) for verification.
> # sample:
> (mean.s = mean(xs))
[1] 359.864
> (var.s = var(xs))
[1] 4933.277
> # theoretical:
> (mean.t = lambda * t * shape * scale)
[1] 360
> (var.t = (shape + 1) * shape * scale^2)
[1] 168
我通常可以这样做,但我认为我的问题是因为我试图使用%d来插入数字的向量/数组而不是单个项目。
请参阅下面我正在尝试运行的完整代码,但它不起作用!
"Blah blah blah %d" % some_variable
我想知道是否需要使用不同的%运算符?或者也许这是不可能的,因为它确实是一个载体?
答案 0 :(得分:1)
%s
可用于表示任何数据。将调用str
将对象转换为字符串。
您还应该在元组中包含多个项目。把它们放在一起:
print "The mean is of %s is %d" % (array, statistics.mean(array))