lundi 12 avril 2021

Inconsistent replication of numpy random generator using random.RandomState(None) or 0

I have had this problem before. At the time, I imported random a number of times. This time I import numpy a single time among all modules.

I use the code:

import numpy as np


class Model:
    def __init__(self, seed=None):
        self.np = np
        self.seed = self.np.random.RandomState(seed)

Note the max_distance is exactly the same. But the coefficient numbers are different.

output

And here (in another module, as I pass self) is how I calculate the gini coefficient:

def calculate_gini(incomes, model):
    # Sort smallest to largest
    cumm = model.np.sort(incomes)
    # Values cannot be 0
    cumm += .00001
    # Find cumulative totals
    n = cumm.shape[0]
    index = model.np.arange(1, n + 1)
    gini = ((model.np.sum((2 * index - n - 1) * cumm)) / (n * model.np.sum(cumm)))
    return gini

So. My question is: what are other possible sources of stochasticity that I'm not seing? And how can I control them?

All over the code I use random as such:

    def initialize_person(self):
        n = self.params['N_PEOPLE']
        ages = self.seed.randint(self.params['MIN_AGE'], self.params['MAX_AGE'], size=n)
        females = self.seed.choice([True, False], size=n)
        industries = self.seed.choice(list(self.industries.values()), p=[i.size for i in self.industries.values()],
                                      size=n)
        for i in range(n):
            skilled = self.seed.choice([True, False], p=[industries[i].p_skill, (1 - industries[i].p_skill)])
            income = self.seed.lognormal(industries[i].income_mean, industries[i].income_variance)
            person = Person(_id=str(i), age=ages[i], female=females[i], industry=industries[i],
                            skill=skilled, income=income)
            self.persons.append(person)

Details: This was produced in Linux Mint latest version 20.1 Ulyssa Python 3.7.7 (default, Mar 26 2020, 15:48:22) [GCC 7.3.0] :: Anaconda, Inc. on linux




Aucun commentaire:

Enregistrer un commentaire