gpt4 book ai didi

r - 条件概率

转载 作者:行者123 更新时间:2023-12-05 01:13:54 28 4
gpt4 key购买 nike

我正在尝试从中创建条件概率矩阵:

我重写了没有循环的代码

a # Signal Vector 
b # Price Change Vector
Signalt<- seq(0, 1, 0.05) # Produce the 5% tiles
abst <- c(seq(1, 1.02, by = 0.0025), 2) #Produce the 0% to 2% tiles with 0.25% increments. Added 1 to include price change of 0 in `temp`

xbool = ((Signal >= Signalt[1] & a < Signalt[1 + 1]) *1) # 1 for True 0 for False
temp = (PercChange + 1) * xbool
temp2 <- temp[which(temp > 0)]
CondProb <- cut(temp2, abst, include.lowest = T)
table(CondProb)

这将输出带有 abst 列和出现次数的表格。我当然需要它占每行总数的百分比,但我希望首先能够运行循环并获得矩阵输出。


Loop Original - 由于我更改了大部分编码设置,因此应该在很大程度上忽略

Signal <- runif(100)
PercChange <- abs((rnorm(100)/100))
signalt <- seq(0, 1, 0.05)
abst <- seq(0, c(0.02:1), 0.0025)



CondDistMat <- matrix(0, nrow = length(signalt), ncol = length(abst))

for(j in 1:length(signalt - 1)){
xbool = (is.na((Signal >= signalt[j] & Signal < signalt[j + 1]) ) * 1)
ysubset = (PercChange * xbool[j] )
CondProb = hist(ysubset, breaks = abst, freq = TRUE)
CondDistMat[signalt, abst] <- CondProb$density
}

列将是 abst 定义的百分位数,而行将是 signalt 定义的 5% 分块。这个想法是通过 bool 向量产生 1,其中绝对返回 PercChange 应该在列中,然后绘制每个 signalt 的概率。

但是我无法生成输出 - 谁能发现错误?提前致谢

所需的输出应该类似于附加图像 Conditional Probabilities .

最佳答案

听起来你想要 cutfindInterval

这些函数输出的示例

> cut(rnorm(9), breaks = -6:6)
[1] (0,1] (-2,-1] (0,1] (1,2] (0,1] (-1,0] (-2,-1] (0,1] (-1,0]
12 Levels: (-6,-5] (-5,-4] (-4,-3] (-3,-2] (-2,-1] (-1,0] (0,1] (1,2] ... (5,6]
> findInterval(rnorm(9), -6:6)
[1] 7 6 7 6 8 9 7 7 6

关于r - 条件概率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14604419/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com