r - 季节性和稀有订单数量的蒙特卡罗模拟
问题描述
给定订单数量的季节性需求模式和可能的订单数量的历史样本。如何执行 MCS?
# months 1:12
mon <- 1:12
# seasonal probability density of rare orders including the material "A"
prob <- sin( -(mon + 2.5) * 2 * pi / 12)
prob <- prob - min(prob) + 0.5
prob <- prob / (sum(prob))
plot(mon, prob, type = "b", main = "seasonality and density of probabilities", xlab = "months", ylab = "probability", ylim = c(0, .2))
# historical order quantities except 0 for "A"
quantity_A <- c(15, 3.4, 3.4) # there are 3 observations, in the other months quantiy should be 0
paste("expected average of simulated 12 months=", round(sum(quantity_A) / 12, 4))
想要:多个“历史上”(=数量可以是:15、3.4 或 0)模拟年(=12 个月),其数量包络类似于上面的季节性概率。
示例:0 0 0 0 15 0 3.4 0 0 0 0
。因此,如果我多次运行模拟,形状quantity_A
应该与季节性模式相匹配。
解决方案
最终这就是我所做的。它旨在模拟每 12 个月不超过 6 个非零订单数量的订单数量。
set.seed(76543)
# months 1:12
mon <- 1:12
# seasonal probability density of rare orders including the material "A"
prob <- sin( -(mon + 2.5) * 2 * pi / 12)
prob <- prob - min(prob) + 0.5
prob <- prob / (sum(prob))
plot(mon, prob, type = "b", main = "seasonality and density of probabilities", xlab = "months", ylab = "probability", ylim = c(0, .2))
# historical order quantities except 0 for "A"
quantity_A <- c(15, 3.4, 3.4) # there are 3 observations, in the other months quantiy should be 0
paste("expected average of simulated 12 months=", round(sum(quantity_A) / 12, 4))
# expected average of simulated 12 months= 1.8167
# sample 1 year = 12 months
sample_year <- function(probs, qtys) {
res <- sample(qtys, 12, replace = TRUE) # sample historical order quantities ..
res[(runif(12) / length(qtys)) > probs] <- 0 # .. but only accept, if greater than PDF
res
}
sims <- t(replicate(100000, sample_year(prob, quantity_A)))
barplot(colSums(sims))
# there is definitely a peak in the high season
paste("simulated average = ", round(mean(rowMeans(sims)), 4))
# simulated average = 1.8166
colSums(sims)/sum(sims)
plot(cumsum(prob), cumsum(colSums(sims)/sum(sims)), main = "simulated vs target probabilities", type = "b")
但我绝对仍然有兴趣获得对我的问题的答复。
推荐阅读
- angular - Ionic 5 角度后退导航
- ios - didBeginContact 委托方法未触发 ARKit 碰撞检测
- java - Java Y/N 循环和字符串
- python - 我想将嵌套列表转换为python中的简单列表
- acumatica - “在系统中找不到批次/序列号 nbr('anyNumber')”,为什么我会得到这个?
- python - 如何根据来自不同列表的单词匹配拆分字符串?
- php - 加载公共 app.js 时,引导日期选择器不起作用
- r - 如何将 Rmarkdown 文件编入嵌入绘图的 html 文件?
- flutter - 如何创建一个对话框,就像您在移动设备键盘中按住表情符号时出现的那样?
- azure - 无法获取昨天或在使用 powershell 之前创建的存储帐户中的 blob