Extracting the mass matrix (Euclidean metric) in rstan

Hi all,

Is there a way to extract the tuned mass matrix of a chain in rstan? I’ve been looking through the documentation and haven’t been able to find a function/ example.

Thanks.

Not easily. You can dump the results into a .csv file using the sample_file argument and that .csv file has a comment in the middle of it where the diagonal elements of the mass matrix are listed, which you could parse.

Thanks. I’ll look into this.

For the benefit of anyone who ends up having the same request as me, I extracted the diagonal of the inverse mass matrix using the following steps:

First, I included the following as an argument in my stan function
sample_file <- “datadump.txt”

Then, the following code can be used to extract the diagonal entries of the inverse mass matrix from the file.

datadump <- readLines(“datadump.txt”)
massmatindex <- grep("^# Diagonal elements", datadump) + 1
xx <- datadump[massmatindex]
yy <- unlist(strsplit(xx, split=", "))
yy[1] <- substring(yy[1], 3)
massmatdiag <- as.numeric(yy)

massmatdiag is a vector containing the diagonal entries.

4 Likes