Morten
Morten

Reputation: 223

Conditional Probability

I am trying to create a matrix of the conditional probabilities from this:

I have rewritten the code without the 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 the table with abst columns with the number of occurences. I of course need it to be in % of total per row, but I would like first to be able to run the loop and get the matrix output.


Loop Original - Should be largely ignored as I have changed most of the coding setup

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 by abst while the rows will be 5% tiles defined by signalt. The idea is through the boolean vector to produce 1's where the absolute returns PercChange should be in the columns, and then plot the probabilities for each signalt of this.

I am however not being able to produce an output - can anyone spot the error(s)? Thanks in advance

The desired output should look something like the attached imageConditional Probabilities.

Upvotes: 3

Views: 4329

Answers (1)

Dason
Dason

Reputation: 61933

It sounds like you want cut or findInterval

An example of what the output 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

Upvotes: 1

Related Questions