@@ -58,24 +58,26 @@ graph_most_likely <- function(graph, quiet = FALSE) {
58
58
# number of nodes in the 3d grid
59
59
n <- prod(graph $ sz )
60
60
61
- # Compute the matrix TO
61
+ # Compute the matrix TO (transition * observation)
62
62
if (! quiet ) {
63
63
cli :: cli_progress_step(" Compute movement model" )
64
64
}
65
65
trans_obs <- graph_transition(graph ) * graph $ obs [graph $ t ]
66
66
67
- # Initiate the matrix providing for each node of the graph, the source id (index of the node)
68
- # with the most likely path to get there.
67
+ # Initiate the sparse 1D matrix providing for each node of the graph, the source id (index of the
68
+ # node in the 3D grid) with the cumulative max probability to get there.
69
+ # Start with prob=1 at the equipment site (log = 0)
69
70
path_s <- Matrix :: sparseMatrix(
70
71
rep(1 , length(graph $ equipment )),
71
72
graph $ equipment ,
72
- x = 1 , dims = c(1 , n )
73
+ x = 0 , dims = c(1 , n )
73
74
)
74
- # Initiate the same matrix providing the total probability of the current path so far
75
+ # Initiate the same matrix providing the cumulative total probability of the current path so far
76
+ # Not sure why x is differently specify, should be the same value for both path_s and path_max
75
77
path_max <- Matrix :: sparseMatrix(
76
78
rep(1 , length(graph $ equipment )),
77
79
graph $ equipment ,
78
- x = graph $ obs [graph $ equipment ], dims = c(1 , n )
80
+ x = log( graph $ obs [graph $ equipment ]) , dims = c(1 , n )
79
81
)
80
82
81
83
# Create a data.frame of all edges information
@@ -85,13 +87,14 @@ graph_most_likely <- function(graph, quiet = FALSE) {
85
87
node <- data.frame (
86
88
s = graph $ s ,
87
89
t = graph $ t ,
88
- to = trans_obs ,
90
+ to = log( trans_obs ) ,
89
91
stap = arrayInd(graph $ s , graph $ sz )[, 3 ]
90
92
)
91
93
92
- # Split this data.fram by stationary period (of the source)
94
+ # Split this data.frame by stationary period (of the source)
93
95
node_stap <- split(node , node $ stap )
94
96
97
+ # Compute number of nodes per stap
95
98
n_edge <- sapply(node_stap , nrow )
96
99
97
100
if (! quiet ) {
@@ -109,12 +112,14 @@ graph_most_likely <- function(graph, quiet = FALSE) {
109
112
}
110
113
111
114
for (i_s in seq_len(length(node_stap ))) {
115
+ # Select all nodes of the current stap
112
116
node_i_s <- node_stap [[i_s ]]
113
117
114
- # compute the probability of all possible transition
115
- node_i_s $ p <- path_max [node_i_s $ s ] * node_i_s $ to
118
+ # Compute the (cum) log probability of all possible transitions
119
+ node_i_s $ p <- path_max [node_i_s $ s ] + node_i_s $ to
116
120
117
- # Find the value of the maximum possible transition for each target node
121
+ # Find the value of the maximum possible transition for each target node and store it into
122
+ # path_max
118
123
max_v <- sapply(split(node_i_s $ p , node_i_s $ t ), max )
119
124
max_t <- as.numeric(names(max_v ))
120
125
path_max [max_t ] <- max_v
0 commit comments