指定窗口内独立事件的概率

机器算法验证 可能性 组合学
2022-04-15 08:53:58

所以我有一个事件可以每天发生,概率为 1%。天与日之间没有依赖关系。现在我可以计算事件在 Y 天内发生 X 次的概率:

  COMBIN(Y,X) * (0.01 ^ X) * [0.99 ^ (Y - X)]

其中 COMBIN() 是 Y 中 X 的组合数。现在我要计算的是过去 Z 天内任何 Y 窗口中 X 事件的概率,例如,至少发生 5 个事件的概率是多少过去 10 年中的一个 30 天窗口。非常感谢任何帮助。

谢谢

注意:原始问题已编辑以在下面的 MansT 建议中添加“至少”限定词。

2个回答

如果这些日子真的像你说的那样独立,那么让我们将事件 A 表示为任何 Y 天期间的 X 事件,并表示 P(A),即 Y 天内 X 事件的概率:

P(A) = COMBIN(Y,X) * (0.01 ^ X) * [0.99 ^ (Y - X)]

如果事件发生的概率不依赖于时间,那么对于任何 Y 天窗口都是如此。

然后你可以再次将二项式概率应用到事件 A。我们必须小心,因为现在事件 A 不独立于重叠窗口,即对于长度为 10 的窗口,事件 A 在第 1-10 天的概率不是独立于第 2-11 天。因此,以下假设不重叠窗口以确保独立性。现在,因为您想要至少一个非重叠窗口发生事件的概率,所以更容易做到:

1 - P(no overlapping windows satisfying event A).

n为时间段 Z 中大小为 Y 的非重叠窗口的数量。那么在时间段 Z 内大小为 Y 的非重叠窗口中发生 X 事件的概率为:

1 - COMBIN(n,0) * P(A)^0 * [1-P(A)]^(Y-0)

= 1 - n * [1-P(A)]^Y

这是一些代码,一种蛮力方法,可以大致了解概率。

在此我假设特定的 30 天窗口无关紧要。如果一个月底有3个,下个月初有2个,我还是算连续5个。

set.seed(1)

#number of runs
N <- 5e5

#max number of events per month (simulation tops out around 4)
n_max <- 10

#other parameters
n_years <- 10
days_per_year <- 365.24

#would be number of rows if year change was reset
n <- floor(n_years*days_per_year)

#sample is wrapped in this to handle workspace and memory
#find run lengths
my_rle <- rle(rbinom(n = n*N,prob = 0.01,size = 1))

#find run-lengths that refer to "TRUE" values sequences
idx <- which(my_rle$values ==1)

#subset out non-zero runs
cus_y <- my_rle$lengths[idx]

#pre-declare for loop
store<- numeric(length = n_max)

#put zeros into a single bin
store[1] <- sum(my_rle$lengths[-idx])/n/N

#find bin frequenceis
for (j in 1:(n_max-1)){
  store[j+1] <- length(which(cus_y==j,arr.ind=T))/n/N
}

#stage for plot and model
x    <- 0:(n_max-1)
y <- log10(store)

#subset to non-nan values
y1 <- y[1:5]
x1 <- seq(from=0,to=4,by=1)

#fit model
est <- lm(y1~x1)
summary( est)

#extrapolate
x2 <- 5
y2 <- est$coefficients[1]+est$coefficients[2]*x2
y2

#main plot
plot(x,y,ylab="Log10 frequency", xlab="run length",ylim = c(-10,0))
grid()
abline(est)

points(x2,y2,pch=19,col="Red",cex=1.2)

这是我得到的情节

在此处输入图像描述

合身给了我这个总结:

> summary( est)

Call:
lm(formula = y1 ~ x1)

Residuals:
         1          2          3          4          5 
 0.0072500  0.0007404 -0.0025619 -0.0260976  0.0206690 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)    
(Intercept) -0.01161    0.01528   -0.76    0.503    
x1          -1.99795    0.00624 -320.21 6.72e-08 ***
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1

Residual standard error: 0.01973 on 3 degrees of freedom
Multiple R-squared:      1, Adjusted R-squared:      1 
F-statistic: 1.025e+05 on 1 and 3 DF,  p-value: 6.717e-08

使用系数给出 -10.00137 作为连续 5 次运行的预期频率 log10。这是〜1e-10。5 元素序列在任何时间的估计概率为 1e-10,或 (0.01^5)。