I need to generate samples from a mixed distribution
40% samples come from Gaussian(mean=2,sd=8)
20% samples come from Cauchy(location=25,scale=2)
40% samples come from Gaussian(mean = 10, sd=6)
To do this, i wrote the following function :
dmix <- function(x){
prob <- (0.4 * dnorm(x,mean=2,sd=8)) + (0.2 * dcauchy(x,location=25,scale=2)) + (0.4 * dnorm(x,mean=10,sd=6))
return (prob)
}
And then tested with:
foo = seq(-5,5,by = 0.01)
vector = NULL
for (i in 1:1000){
vector[i] <- dmix(foo[i])
}
hist(vector)
I'm getting a histogram like this (which I know is wrong) -
What am I doing wrong? Can anyone give some pointers please?
hist(dmix(seq(-5,5,by = 0.01)))
without looping andvector
– Wincerdmix <- function(x = 100) {prob <- c(rnorm(x * .4, mean = 2, sd = 8), rcauchy(x * .2, location = 25, scale = 2), rnorm(x * .4, mean = 10, sd = 6)); hist(prob)}; dmix()
– Pylon