Please, I would like to generate a random bistochastic matrix, that is a squared matrix of non-negative numbers with each row and each column sum to 1, for example : .2 .3 .5 .6 .3 .1 .2 .4 .4 I don't know of to code this. Do you have any idea ? Thanks Florent Bresson ___________________________________________________________________________ Demandez ? ceux qui savent sur Yahoo! Questions/R?ponses
bistochastic.3x3 <- function() {
B <- matrix(0, 3, 3)
## 2 df
tmp.1 <- runif(3)
B[1,] <- tmp.1/sum(tmp.1)
## 1 df
tmp.2 <- runif(2)
B[2:3, 1] <- (1-B[1,1]) * tmp.2/sum(tmp.2)
## 1 df
B[2, 2] <- runif(1, max=min(1-B[1,2], 1-B[2,1]))
## Fill in the rest
B[2,3] <- 1-sum(B[2, 1:2])
B[3,2] <- 1-sum(B[1:2, 2])
B[3,3] <- 1-sum(B[1:2, 3])
B
}
B <- bistochastic.3x3()
apply(B, 1, sum)
apply(B, 2, sum)
To extend this to larger than 3x3 requires the same kind of
conditional generation of alternating rows and columns of the
matrix. The hard part is the extension of the two-way conditioning
I illustrated in the B[2, 2] line.
Rich
you can try something like the following:
B <- 10
N <- 5
mats <- r2dtable(B, rep(1, N), rep(1, N))
out <- matrix(0, N, N)
for(i in 1:length(mats))
out <- out + mats[[i]]
out <- out / B
out
colSums(out)
rowSums(out)
I hope it helps.
Best,
Dimitris
----
Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven
Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- Original Message -----
From: "Florent Bresson" <f_bresson at yahoo.fr>
To: <r-help at stat.math.ethz.ch>
Sent: Monday, October 16, 2006 10:22 AM
Subject: [R] Generate a random bistochastic matrix
Please, I would like to generate a random bistochastic matrix, that is
a squared matrix of non-negative numbers with each row and each
column sum to 1, for example :
.2 .3 .5
.6 .3 .1
.2 .4 .4
I don't know of to code this. Do you have any idea ?
Thanks
Florent Bresson
___________________________________________________________________________
______________________________________________
R-help at stat.math.ethz.ch 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.
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm