I have posted a problem in Stack Mathematics regarding a Metropolis Hastings algorithm in graph which someone can read it here
(A code solution is written in the stack mathematics link but it is in the CoCalc and I do not know how to translate in R.)
In a nutshell the problem is: Consider a finite, undirected, connected graph πΊ=(π,πΈ) with vertex set π and edge set πΈ. We do not know the length of |π| of the vertices of πΊ, nor its structure. We can only see local information about πΊ. E.g. if we are at a vertex π₯βπ we can see the neighbors of π₯, i.e. the vertices π¦βπ for which (π₯,π¦)βπΈ, as well as how many neighbors π₯'s neighbors have. Let us denote by π(π₯) the degree of π₯βπ the number of neighbors of π₯.
For simplicity let us assume that the graph has 5 vertices.
How can I write the MH algorithm in R for this specific problem or translate the Cocalc in the stack math answer ?
Edit, (i) ## CoCalc code in R (rather slow).
## CoCalc code in R.
library(igraph)
# g <- graph(c(1,2, 2,4, 4,1, 1,3), directed=FALSE)
g <- graph_from_literal(A-B, B-D, D-A, A-C)
cur = 1
freq <- rep(0, vcount(g))
names(freq) <- as_ids(V(g))
nit <- 1E4
set.seed(1)
system.time({
for ( i in seq(nit) ) {
neigh <- V(g)[.nei(cur)]
nextnode <- neigh[sample(length(neigh), 1)]
if (runif(1) < degree(g, cur) / degree(g, nextnode) ){
cur <- nextnode
}
freq[cur] <- freq[cur] + 1
}
})
freq <- freq / sum(freq)
freq
Output.
user system elapsed
4.63 0.05 5.13
A B D C
0.2471 0.2477 0.2463 0.2589
Regarding your point (ii) in Stack Mathematics. If in an (un)dirceted graph all cycles are even, then the graph is bipartite and aperiodic. As in this example. Degrees may be different.
library(igraph)
g <- graph_from_literal(A-X, X-B, B-Y, Y-A, A-Z)
degree(g)
mmm <- as.matrix(g[])
mmm <- mmm / rowSums(mmm)
## Calculate power serie.
mms <- mmm
mms <- mms %*% mmm; mms