Conditional capability
I'm trying to create a matrix of conditional probabilities from these conditional probabilities:
I rewrote the code without a loop
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)
This outputs a table with abst columns with the number of occurrences. I certainly need it to be% of the total in the row, but I would like to start the loop first and get the output of the matrix.
Loop Original - Must be largely ignored as I changed most of the encoding setting
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
}
The columns will be the percentiles defined abst
, while the rows will be 5% of the tiles defined signalt
. The idea is that through a boolean vector a 1 is created, where the absolute returns PercChange
should be in columns, and then plot the probabilities for each one signalt
.
However, I am unable to produce a conclusion - can anyone spot the error (s)? thanks in advance
The desired output should look something like the attached image .
source to share
Sounds like you want cut
orfindInterval
An example of what the result looks like with these functions
> 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
source to share