lundi 7 juin 2021

Random.choices not returning uniform distribution

I am trying to simulate a uniform distribution of discrete values using random.choices. Each time that a new set is generated, a key representing the unique counts is incremented.

Why is the uniform outcome ([2,2]) less likely to occur than [1,3]?

def sim_counts(size, values=[1,-1], popsize=2):
    
    count_dict = {}
    for i in range(popsize):
        X = random.choices(values,k=size)
        _, counts = np.unique(X, return_counts=True)
        
        if len(counts) == 1:
            counts = [0,counts[0]]
        key = str(np.sort(counts))

        if key not in count_dict:
            count_dict[key] = 0
            count_dict[key] +=1
        else:
            count_dict[key] +=1
            
    return count_dict
   
sim_counts(4, values=[1,-1], popsize=10000)

>>> {'[2 2]': 3747, '[0 4]': 1319, '[1 3]': 4934}



Aucun commentaire:

Enregistrer un commentaire