Simulation of positive definite matrices constrained by an undirected or acyclic directed graph structure.
An R package for simulating positive definite matrices constrained by acyclic directed and undirected graphs.
The package is available on
CRAN, to get the latest stable version use:
Alternatively, using the R package
devtools one may install the development version:
The only R package required for
igraph, which can also be installed from CRAN.
First, we generate a random undirected graph with
3 nodes and density
0.5. Then we generate, using our
2 matrices consistent with such random graphical structure.
library(gmat)ug <- rgraph(p = 3, d = 0.5)igraph::print.igraph(ug)#> IGRAPH 75df7f1 U--- 3 1 -- Erdos renyi (gnp) graph#> + attr: name (g/c), type (g/c), loops (g/l), p (g/n)#> + edge from 75df7f1:#>  1--3port(N = 2, ug = ug)#> , , 1#>#> [,1] [,2] [,3]#> [1,] 1.2329616 0.0000000 0.6513035#> [2,] 0.0000000 0.1578579 0.0000000#> [3,] 0.6513035 0.0000000 0.5332192#>#> , , 2#>#> [,1] [,2] [,3]#> [1,] 1.787082 0.0000000 1.296915#> [2,] 0.000000 0.0465929 0.000000#> [3,] 1.296915 0.0000000 1.214313
We appreciate how the zero pattern is shared by all of the simulated matrices. The return value is an array, and so the individual matrices can be accessed as
matrices[, , n], where
n is the index of the matrix we want to retrieve from the sample, ranging from
We may also sample correlation matrices using i.i.d. coefficients in their upper Cholesky factor
chol_iid(N = 2)#> , , 1#>#> [,1] [,2] [,3]#> [1,] 1.0000000 0.201973 -0.4340289#> [2,] 0.2019730 1.000000 -0.6814360#> [3,] -0.4340289 -0.681436 1.0000000#>#> , , 2#>#> [,1] [,2] [,3]#> [1,] 1.0000000 -0.3758306 -0.1389546#> [2,] -0.3758306 1.0000000 -0.6183038#> [3,] -0.1389546 -0.6183038 1.0000000
A specific zero pattern can be enforced in
U using an acyclic digraph.
dag <- rgraph(p = 3, d = 0.5, dag = TRUE)m <- chol_iid(dag = dag)[, , 1]L <- t(chol(anti_t(m)))U <- t(anti_t(L))igraph::print.igraph(dag)#> IGRAPH fdee8a0 D--- 3 1 --#> + edge from fdee8a0:#>  2->3print(U)#> [,1] [,2] [,3]#> [1,] 1 0.0000000 0.0000000#> [2,] 0 0.9836563 -0.1800561#> [3,] 0 0.0000000 1.0000000print(m)#> [,1] [,2] [,3]#> [1,] 1 0.0000000 0.0000000#> [2,] 0 1.0000000 -0.1800561#> [3,] 0 -0.1800561 1.0000000
See more examples and paper references at the documentation website for the package.
In this version the functionality of
gmat has been significantly extended.
In addition to the functionality already available for sampling covariance
matrices, possibly with a zero pattern specified by an undirected graph, now the
package also allows sampling correlation matrices with zero entries on their
Cholesky factor, represented by an acyclic digraph.
diagdom() have been refactored in order to unify the
approaches for undirected graphs and the new functions for acyclic digraphs.
This has had some consequences in terms of the behaviour of the two functions.
ugis now the fourth argument, instead of the second one, for both functions.
ugis not provided, unless explicitly stating a parameter
d < 1.
dhave been changed. Now by default one
3 x 3full matrix is returned.
rentrieshas been removed for both functions. In the future maybe this argument is reintroduced with a more complete checking of its validity depending on the properties of the function.
khas been removed from
diagdom(), and its functionality is now implemented by the utility function
The main addition in this version are functions for correlation matrix sampling, possibly with constraints on the upper Cholesky factorization, which correspond to an acyclic digraph representation. Some side utility functions are also provided.
chol_polar(). These three functions return a sample of correlation matrices, possibly with an average percentage of zeros in their upper Cholesky factor, which can be also predefined by a given acyclic digraph. See more details at their documentation.
mh_u(), used by
chol_mh(), allows to sample the upper Cholesky factor of a correlation matrix by sampling vectors on hemispheres of different dimensions. More information on their documentation.
rgraph(), which is a simple wrapper of some functionality in package igraph for random graph generation.
anti_t()computes the anti transpose of a matrix. This is mainly useful for testing, since it is involved in the acyclic digraph representation of the upper and lower Cholesky factors.
vectorize()for extracting the upper/lower triangle in a covariance/correlation matrix sample as returned by the functions in the package.
NAMESPACE, but instead explicitly called throughout the package using