[R] Generate positive definite matrix with constraints

6 views
Skip to first unread message

mary

unread,
May 19, 2013, 10:31:39 AM5/19/13
to r-h...@r-project.org

Hi, I have a question for my simulation problem:

I would like to generate a positive (or semi def positive) covariance
matrix, non singular, in wich the spectral decomposition returns me the same
values for all dimensions but differs only in eigenvectors.

Ex.
sigma
[,1] [,2]
[1,] 5.05 4.95
[2,] 4.95 5.05

> eigen(sigma)
$values
[1] 10.0 0.1

$vectors
[,1] [,2]
[1,] 0.7071068 -0.7071068
[2,] 0.7071068 0.7071068

(In theory: Using the spectral decomposition, the matrix Σ can be re-written
as
Σ = 5 ( 1, 1) 1 + 0.05 (1, -1) 1
1 -1 )
This because I would generate another covariance matrix in wich variables
are more than 2.
Thank you

--
View this message in context: http://r.789695.n4.nabble.com/Generate-positive-definite-matrix-with-constraints-tp4667449.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

mary

unread,
May 19, 2013, 10:33:45 AM5/19/13
to r-h...@r-project.org

Sorry in the previuos message I've inverted: I would like generate ...
"same eigenvectors and different eigenvalues"





--
View this message in context: http://r.789695.n4.nabble.com/Generate-positive-definite-matrix-with-constraints-tp4667449p4667450.html

Gabor Grothendieck

unread,
May 19, 2013, 4:57:32 PM5/19/13
to mary, r-h...@r-project.org
On Sun, May 19, 2013 at 10:33 AM, mary <mary...@libero.it> wrote:
>
> Sorry in the previuos message I've inverted: I would like generate ...
> "same eigenvectors and different eigenvalues"

Try this:

m0 <- 5 + matrix(c(.05, -.05, -.05, .05), 2)
m0.values <- eigen(m0)$values
with(eigen(matrix(rnorm(4), 2)), vectors %*% diag(m0.values) %*% solve(vectors))

Gabor Grothendieck

unread,
May 19, 2013, 5:04:13 PM5/19/13
to mary, r-h...@r-project.org
On Sun, May 19, 2013 at 4:57 PM, Gabor Grothendieck
<ggroth...@gmail.com> wrote:
> On Sun, May 19, 2013 at 10:33 AM, mary <mary...@libero.it> wrote:
>>
>> Sorry in the previuos message I've inverted: I would like generate ...
>> "same eigenvectors and different eigenvalues"
>
> Try this:
>
> m0 <- 5 + matrix(c(.05, -.05, -.05, .05), 2)
> m0.values <- eigen(m0)$values
> with(eigen(matrix(rnorm(4), 2)), vectors %*% diag(m0.values) %*% solve(vectors))

I think I reversed it too. If that's not it try this:

m0.vectors <- eigen(m0)$vectors
with(eigen(matrix(rnorm(4), 2)), m0.vectors %*% diag(values) %*%
solve(m0.vectors.0))


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

Gabor Grothendieck

unread,
May 19, 2013, 5:47:45 PM5/19/13
to mary, r-h...@r-project.org
On Sun, May 19, 2013 at 5:04 PM, Gabor Grothendieck
<ggroth...@gmail.com> wrote:
> On Sun, May 19, 2013 at 4:57 PM, Gabor Grothendieck
> <ggroth...@gmail.com> wrote:
>> On Sun, May 19, 2013 at 10:33 AM, mary <mary...@libero.it> wrote:
>>>
>>> Sorry in the previuos message I've inverted: I would like generate ...
>>> "same eigenvectors and different eigenvalues"
>>
>> Try this:
>>
>> m0 <- 5 + matrix(c(.05, -.05, -.05, .05), 2)
>> m0.values <- eigen(m0)$values
>> with(eigen(matrix(rnorm(4), 2)), vectors %*% diag(m0.values) %*% solve(vectors))
>
> I think I reversed it too. If that's not it try this:
>
> m0.vectors <- eigen(m0)$vectors
> with(eigen(matrix(rnorm(4), 2)), m0.vectors %*% diag(values) %*%
> solve(m0.vectors.0))
>

That did not take into account the positive definite requirement. Try this:

m0.vectors <- eigen(m0)$vectors
t.m0.vectors <- t(m0.vectors)
n <- 3 # no of samples
tmp <- apply(rWishart(n, 2, diag(2)), 3, function(w) list(m0.vectors
%*% diag(eigen(w)$values) %*% t.m0.vectors))
simplify2array(tmp)

The result is a list of matrices.

mary

unread,
May 20, 2013, 10:01:12 AM5/20/13
to r-h...@r-project.org

Thank you but I want to create a matrix (4,4) or (8,8) not just only a (2,2)
matrix...



--
View this message in context: http://r.789695.n4.nabble.com/Generate-positive-definite-matrix-with-constraints-tp4667449p4667509.html
Sent from the R help mailing list archive at Nabble.com.

Gabor Grothendieck

unread,
May 20, 2013, 4:04:46 PM5/20/13
to mary, r-h...@r-project.org
On Mon, May 20, 2013 at 10:01 AM, mary <mary...@libero.it> wrote:
>
> Thank you but I want to create a matrix (4,4) or (8,8) not just only a (2,2)
> matrix...
>

provide an m0 matrix of the appropriate dimensions and change 2 to the
new dimension in the relevant lines.


--
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com

Reply all
Reply to author
Forward
0 new messages