samedi 17 mars 2018

Generating Correlated Binomial Variable

Suppose that I have 4(n=4) missiles with hitting probability of p(p=0.5). They are fired at the same time, at the same environment. For that reason, each missile should have correlation with other three missile. For example when corr=1, all missiles hit or miss. When corr=0, they are distributed binomially and independently.

Challenging part is that correlation can not be -1. Since there are two outcome (miss or hit).

So I want to generate a random discrete binomial value (lets say between 0 and 4 with a probability of 0.4 and correlation=0.6)

My code is on the below.

n=4 #size
p=0.4 # probability
corr=0.6 # correlation
trial=10000 #number of trials


p=rep(p,n)
rho=corr
off<-rmvbin(trial, p, bincorr=(1-rho)*diag(n)+rho)
off
          [,1] [,2] [,3] [,4]
    [1,]    0    0    1    0
    [2,]    0    0    0    0
    [3,]    1    1    1    1      # This part gives correlated 
    [4,]    1    0    1    0      # Bernoulli var for 10000
    [5,]    0    0    0    0      # trial. When you sum each
    [6,]    1    1    1    1      # row, you get random correlated
    [7,]    1    1    1    0      # number of missiles.
    [8,]    0    0    0    0
    ........................
[10000,]    0    0    1    0

cor(off)
          [,1]      [,2]      [,3]      [,4]
[1,] 1.0000000 0.6015165 0.5987907 0.6005857   # correlation is as 
[2,] 0.6015165 1.0000000 0.6019365 0.6012273   # demanded.
[3,] 0.5987907 0.6019365 1.0000000 0.5972144   #
[4,] 0.6005857 0.6012273 0.5972144 1.0000000   #

But, when size (n) gets bigger, the robustness and accuracy of code is decreasing.

Is there any way to generate it? (Generating a integer number between an interval with a given correlation.)




Aucun commentaire:

Enregistrer un commentaire