Skip to content

Instantly share code, notes, and snippets.

@dirmeier
Last active October 24, 2017 18:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dirmeier/908036abbfa0ebbc7e9c7053d730c99a to your computer and use it in GitHub Desktop.
Save dirmeier/908036abbfa0ebbc7e9c7053d730c99a to your computer and use it in GitHub Desktop.
Analytical solution to network propagation using the Markov random walk with restart
# create a random matrix with non-negativ values (can be anything)
affin <- matrix(runif(100), 10, 10)
# drop self-loops
diag(affin) <- 0
# create column stochastic transition matrix
trans <- sweep(affin, 2, colSums(affin), "/")
# create a matrix initial distribution where every column is one observation
p0 <- matrix(runif(10 * 10), nrow=10)
# column normalize the guys
p0 <- sweep(p0, 2, colSums(p0), "/")
# restart probability
restart.prob <- 0.3
# use `diffusr`s random walk with restart to compute the stationary distribution
# do it only on a single vector
g <- diffusr::random.walk(p0[,1], trans, restart.prob)
print(g)
# do the analytical solution of the random walk.
# again, we only use a single vector
g <- (restart.prob * solve(diag(nrow(trans)) - (1 - restart.prob) * trans)) %*% p0[,1]
print(g)
# do the analytical solution of the random walk.
# this time we use the complate matrix p0
g <- (restart.prob * solve(diag(nrow(trans)) - (1 - restart.prob) * trans)) %*% p0
print(g)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment