Kinin
Kinin

Reputation: 55

How to insert probabilities into a matrix?

what would be a good program that could automate and fill out the matrix A?

We have the col vector:

col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)
col
[1]  1  1  2  3  4  5 10  7  7  3  1  5  3  7  6  3  4  2  1  1  2  2  6  4  8
[26]  8  9  1  3  2

And we have the matrix:

A=rbind(c(0:10),c(1,rep(0,10)),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
A
      [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11]
 [1,]    0    1    2    3    4    5    6    7    8     9    10
 [2,]    1    0    0    0    0    0    0    0    0     0     0
 [3,]    2    0    0    0    0    0    0    0    0     0     0
 [4,]    3    0    0    0    0    0    0    0    0     0     0
 [5,]    4    0    0    0    0    0    0    0    0     0     0
 [6,]    5    0    0    0    0    0    0    0    0     0     0
 [7,]    6    0    0    0    0    0    0    0    0     0     0
 [8,]    7    0    0    0    0    0    0    0    0     0     0
 [9,]    8    0    0    0    0    0    0    0    0     0     0
[10,]    9    0    0    0    0    0    0    0    0     0     0
[11,]   10    0    0    0    0    0    0    0    0     0     0

The first column of matrix A represents the preceding values in the col vector

The first row of the matrix A represents the following values in the col vector In the matrix A, we would like to replace the 0's and store the conditional probabilities.

By looking at the col vector, I look at all the instances that involve 1 as a preceding number such as 1,1,2, 1,5, 1,1,2 1,3.

And I came up with the following conditional probabilities:

Given that the preceding number was 1 in col vector, the probability that the following number is 1 equals to: 2/6.

Given that the preceding number was 1, the probability that the following number is 2 equals to:2/6.

Given that the preceding number was 1, the probability that the following number is 3 equals to:1/6.

Given that the preceding number was 1, the probability that the following number is 5 equals to:1/5.

We use those values to fill the first row of Matrix A.And we obtain a new version of A.

A=rbind(c(0:10),c(1,2/6,2/6,1/6,0,1/6,0,0,0,0,0),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
> A
      [,1]      [,2]      [,3]      [,4] [,5]      [,6] [,7] [,8] [,9] [,10] [,11]
 [1,]    0 1.0000000 2.0000000 3.0000000    4 5.0000000    6    7    8     9    10
 [2,]    1 0.3333333 0.3333333 0.1666667    0 0.1666667    0    0    0     0     0
 [3,]    2 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [4,]    3 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [5,]    4 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [6,]    5 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [7,]    6 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [8,]    7 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
 [9,]    8 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
[10,]    9 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0
[11,]   10 0.0000000 0.0000000 0.0000000    0 0.0000000    0    0    0     0     0

We want to fill out the 2nd row, the 3rd all the way until 10.

I did it manually but what would be a good program that could automate and fill out the matrix A?

Upvotes: 0

Views: 155

Answers (2)

Bulat
Bulat

Reputation: 6969

Not sure if this is the most efficient approach, using aggregate and dcast:

# Get data.
col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)

# Make shifted vector and make a data frame.
index <- 1:length(col) - 1
index <- tail(index, length(col) - 1)
col.shift <-  c(col[index + 1], NA)
df <- data.frame(list("value" = col, "next.value" = col.shift))

# Count number of values per combination.
df$count <- 1
# Count number of value appearences..
df.agg.row <- aggregate(count ~ value, df, FUN = sum)

# Pivot the data.
library(reshape2)
res <- dcast(df, value ~ next.value, fun.aggregate = length)

# Get probability of number (row) being followed by number (col).
res2 <- res[, 2:11] / df.agg.row$count 

Upvotes: 1

Kinin
Kinin

Reputation: 55

Thank you all for your input. I was able to find an answer to my own question. If anyone is interested then you can check my solution.

A=rbind(c(0:10),c(1,rep(0,10)),c(2,rep(0,10)),c(3,rep(0,10)),c(4,rep(0,10)),c(5,rep(0,10)),c(6,rep(0,10)),c(7,rep(0,10)),c(8,rep(0,10)),c(9,rep(0,10)),c(10,rep(0,10)))
    col=c(1,1,2,3,4,5,10,7,7,3,1,5,3,7,6,3,4,2,1,1,2,2,6,4,8,8,9,1,3,2)
    j=length(col)
    A
    while (j>1){
    if (col[j]==col[j-1]){A[match(col[j],A[,1]),match(col[j-1],A[1,])]=A[match(col[j],A[,1]),match(col[j-1],A[1,])]+1}else{
    if (col[j]!=col[j-1]){A[match(col[j],A[,1]),match(col[j-1],A[1,])]=A[match(col[j],A[,1]),match(col[j-1],A[1,])]+1} }
    j=j-1
    }
    A
    A=t(A)
    A=A[-1,-1]
    for (i in 1:nrow(A)){
    A[i,]=A[i,]/sum(A[1,])
    }
    A

Upvotes: 0

Related Questions