简体   繁体   English

如何使用Excel中的仿真工具解决以下概率相关的问题?

[英]How can I use simulation tool in Excel for solving the following problem related to probability?

Trial Number 1 2 3 4 5........ 2000000 (two million)试用号 1 2 3 4 5........ 2000000(两百万)

Success in nth attempt 12 4 21 5 10 12第 n 次尝试成功 12 4 21 5 10 12

Note: Imagine throwing a dice where each outcome has probability of 1/10 (not 1/6 as it is usual for dice).注意:想象一下掷一个骰子,每个结果的概率都是 1/10(不是骰子通常的 1/6)。 For us "success" means throwing a "3".对我们来说,“成功”意味着投出“3”。 For each trial (see above) we keep throwing dice until we get "3".对于每次试验(见上文),我们不断掷骰子,直到得到“3”。 For example, above I assume that during first trial we threw dice 12 times and could get "3" only on 12th attempt.例如,上面我假设在第一次试验中我们掷了 12 次骰子,并且只有在第 12 次尝试时才能得到“3”。 The same for other trials.其他试验也一样。 For instance, on 5th trial we threw dice 10 times and could get "3" only on 10th attempt.例如,在第 5 次尝试中,我们掷了 10 次骰子,只有在第 10 次尝试时才能得到“3”。

We need to simulate this for 2 million times (or lower than that, let's say 500,000 times).我们需要模拟 200 万次(或低于此,假设 500,000 次)。

Eventually we need to calculate what percent of "success" happens in interval of 10-20 tries, 1-10 tries etc.最终我们需要计算在 10-20 次尝试、1-10 次尝试等间隔内发生的“成功”百分比。

For example, out of 2000000 trials in 60% of cases (1200000) we get "3" in between 10th and 20th attempts of throwing a dice.例如,在 60%(1200000)的 2000000 次试验中,我们在第 10 到 20 次掷骰子尝试中得到“3”。

Can you please help?你能帮忙吗?

I performed a manual simulation, but could not create a simulation model. Can you please help?我进行了手动模拟,但无法创建模拟 model。你能帮忙吗?

This might be not a good solution for a large dataset as is your intent.对于您的意图而言,这对于大型数据集可能不是一个好的解决方案。 Probably Excel is not the most efficient tool for that.可能 Excel 不是最有效的工具。 Anyway here is a possible approach.无论如何,这是一种可能的方法。

In cell A1 , put the following formula:在单元格A1中,输入以下公式:

=LET(maxNum, 10, trialNum, 5, maxRep, 20, event, 3, cols, SEQUENCE(trialNum,1),
  rows, SEQUENCE(maxRep, 1), rArr, RANDARRAY(maxRep, trialNum, 1, maxNum, TRUE),
  groupSize, 10, startGroups, SEQUENCE(maxRep/groupSize, 1,,groupSize),
  GROUP_PROB, LAMBDA(matrix,start,end, LET(result, BYCOL(matrix, LAMBDA(arr,
    LET(idx, IFERROR(XMATCH(event, arr),0), IF(AND(idx >= start, idx <= end), 1, 0)))),
    AVERAGE(result))),
  HREDUCE, LAMBDA(arr, LET(idx, XMATCH(event, arr),
    IF(ISNUMBER(idx), FILTER(arr, rows <= idx),event &" not found"))),
  trials, DROP(REDUCE(0, cols, LAMBDA(acc,col, HSTACK(acc,
    HREDUCE(INDEX(rArr,,col))))),,1),
  dataBlock, VSTACK("Trials", trials),
  probBlock, DROP(REDUCE(0, startGroups, LAMBDA(acc,ss,
    VSTACK(acc, LET(ee, ss+groupSize-1, HSTACK("%-Group "&ss&"-"&ee,
      GROUP_PROB(trials, ss, ee))
    ))
  )),1),
  IFERROR(HSTACK(dataBlock, probBlock), "")
)

And here is the output:这是 output: 示例 excel 文件

Explanation解释

We use LET for easy reading and composition.我们使用LET来方便阅读和作文。 We first define the parameters of the experiment:我们首先定义实验的参数:

  • maxNum , the maximum random number to be generated. maxNum ,要生成的最大随机数。 The minimum will be 1 .最小值为1
  • trialNum , the number of trials (in our case the number of columns) trialNum ,试验次数(在我们的例子中是列数)
  • maxRep , the maximum number of repetitions in our case the number of rows. maxRep ,在我们的例子中是行数的最大重复次数。
  • rows and cols rows and columns respectively rows and cols分别是行和列
  • event , our successful event, in our case 3 . event ,我们的成功事件,在我们的案例中3
  • groupSize , The size of each group for calculating the probability of each group groupSize , 每个组的大小,用于计算每个组的概率
  • startGroups The start index position of each group startGroups每个组的起始索引position
  • rArr , Random array of size maxRep x trialNum . rArr ,大小为maxRep x trialNum的随机数组。 The minimum random number will be 1 and the maximum maxNum .最小随机数将为1 ,最大maxNum The last input argument of RANDARRAY ensures it generates only integer numbers. RANDARRAY的最后一个输入参数确保它只生成 integer 个数字。

GROUP_PROB is a user LAMBDA function to calculate the probability of our successful event: number 3 was generated . GROUP_PROB是用户LAMBDA function 来计算我们成功事件的概率:生成数字 3

LAMBDA(matrix,start,end, LET(result, BYCOL(matrix, LAMBDA(arr,
 LET(idx, IFERROR(XMATCH(event, arr),0), IF(AND(idx >= start, idx <= end), 1, 0)))),
    AVERAGE(result)))

Basically, for each column ( arr ) of matrix , finds the index position of the event and check if the index position belongs to the reference interval: start , end , if so return 1 , otherwise 0 .基本上,对于matrix的每一列 ( arr ),找到event的索引 position 并检查索引 position 是否属于参考区间: startend ,如果是,则返回1 ,否则返回0 Finally, the AVERAGE function serves to calculate the probability.最后, AVERAGE function 用于计算概率。 If the event was not generated, then it counts as 0 too.如果未生成event ,则它也计为0

We use the DROP/REDUCE/VSTACK or HSTACK pattern.我们使用DROP/REDUCE/VSTACKHSTACK模式。 Please check the answer to the question: how to transform a table in Excel from vertical to horizontal but with different length provided by @DavidLeal.请检查问题的答案:@DavidLeal 提供的如何将 Excel 中的表格从垂直转换为水平但长度不同

HREDUCE user LAMBDA function filters the rArr until the event is found. HREDUCE用户LAMBDA function 过滤rArr直到找到event In case the event was not found, then it returns a string indicating the event was not found.如果未找到event ,则它返回一个字符串,指示未找到事件。

The name probBlock builds the array with all the probability groups名称probBlock用所有概率组构建数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM