-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.Rmd
104 lines (72 loc) · 2.56 KB
/
README.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
---
output: github_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
# gclm
This package contains the implementation of the algorithm in
Varando G, Hansen NR (2020) [Graphical continuous Lyapunov models](https://arxiv.org/abs/2005.10483)
`gclm` contains methods to estimate a sparse parametrization
of covariance matrix as solution of a continuous time Lyapunov
equation (CLE):
\[ B\Sigma + \Sigma B^t + C = 0 \]
Solving the following \(\ell_1\) penalized loss minimization
problem:
\[ \arg\min L(\Sigma(B,C)) + \lambda \rho_1(B) + \lambda_C ||C - C_0||^2_F \]
subject to \(B\) stable and \(C\) diagonal, where
\(\rho_1(B)\) is the \(\ell_1\) norm of the off-diagonal
elements of \(B\) and \(||C - C_0||^2_F \) is the
squared frobenius norm of the difference between \(C\) and
a fixed diagonal matrix \( C_0 \) (usually the identity).
## Installation
```{r, eval = FALSE}
## version on CRAN
install.packages("gclm")
## development version from github
devtools::install_github("gherardovarando/gclm")
```
## Usage
```{r}
library(gclm)
### define coefficient matrices
B <- matrix(nrow = 4, c(-4, 2, 0, 0,
0, -3, 1, 0,
0, 0, -2, 0.5,
0, 0, 0, -1), byrow = TRUE)
C <- diag(c(1,4,1,4))
### solve continuous Lyapunov equation
### to obtain real covariance matrix
Sigma <- clyap(B, C)
### obtain observations
sample <- MASS::mvrnorm(n = 1000, mu = rep(0,4), Sigma = Sigma)
### Solve minimization
res <- gclm(cov(sample), lambda = 0.4, lambdac = 0.01)
res$B
res$C
```
The CLE can be freely multiplied by a scalar and thus the
\(B,C\) parametrization can be rescaled.
As an example we can impose \( C_{11} = 1\) as in the true
\( C \) matrix, obtaining the estimators:
```{r}
C1 <- res$C / res$C[1]
B1 <- res$B / res$C[1]
B1
C1
```
#### Solutions along a regularization path
```{r}
path <- gclm.path(cov(sample), lambdac = 0.01,
lambdas = 10^seq(0, -3, length = 10))
t(sapply(path, function(res) c(lambda = res$lambda,
npar = sum(res$B!=0),
fp = sum(res$B!=0 & B==0),
tp = sum(res$B!=0 & B!=0) ,
fn = sum(res$B==0 & B!=0),
tn = sum(res$B==0 & B==0),
errs = sum(res$B!=0 & B==0) +
sum(res$B==0 & B!=0))))
```
## Related code
- Some inspiration is from the `lyapunov` package (https://github.com/gragusa/lyapunov).