mercredi 13 décembre 2017

Summing a list of random variables to 1 under a given constraint

l would like to generate 6 random variables between 0 and 1 [0,1] and deduce the seventh from the sum of the six values. For instance we have :

alpha1,alpha2,alpha3,alpha4,alpha5,alpha6

are generated randomly in this interval [0,1]. Now l would like to deduce alpha7 as follow :

sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1

hence :

alpha7= 1-sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)

l did the following :

>>> r = numpy.random.random(6) 
>>> r
array([ 0.7415801 ,  0.43230563,  0.29287991,  0.41897992,  0.54627315,
        0.4071017 ])
>>> r.sum()
2.8391204236894456
alpha7=1-r.sum()
-1.8391204236894456

However it violates :

sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1

because sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6) should be less than 1 and alpha7 is in [0,1] such that

**sum(alpha1,alpha2,alpha3,alpha4,alpha5,alpha6)+ alpha7 =1**




Aucun commentaire:

Enregistrer un commentaire