Skip to content
Prev 137357 / 398500 Next

Mixture of Distributions

I just realised I made a bad mistaqke (see below)
On 21-Feb-08 21:39:56, Ted Harding wrote:
S0 <- cbind(rf1(n),rf2(n),rf3(n))
ix <- sample(c(1,2,3),n,3,prob=c(p1,p2,p3),replace=TRUE)

So far so good!

S  <- S0[,ix]

But this will not do what I intended (i.e. select element ix[1]
from the first row os S0, ix[2] from the second row, and so on).

Instead, it will return an nxn matrix with n rows, and n columns
which are copies of columns of S0 in the order selected by ix!

The following will do the correct thing, though there must
be a neater way of doing it! The example below has been
corrected.

S <- S0[,1]*(ix==1) + S0[,2]*(ix==2) + S0[,3]*(ix==3)
rf1 <- function(n){rnorm(n,0,1)} ## normal distribution with mean=0
rf2 <- function(n){rnorm(n,4,1)} ## normal distribution with mean=4
rf3 <- function(n){rnorm(n,9,1)} ## normal distribution with mean=9

p1 <- 0.2; p2 <- 0.3; p3 <-0.5
S0 <- cbind(rf1(500),rf2(500),rf3(500))
ix <- sample(c(1,2,3),500,prob=c(p1,p2,p3),replace=TRUE)
S <- S0[,1]*(ix==1) + S0[,2]*(ix==2) + S0[,3]*(ix==3)

hist(S,breaks=50)
And again!
Ted.

--------------------------------------------------------------------
E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
Fax-to-email: +44 (0)870 094 0861
Date: 21-Feb-08                                       Time: 22:20:04
------------------------------ XFMail ------------------------------