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.
EDITED
Using None
might have been the problem. But still not working with 0.
Minimal example working, so it's deeper in the code and not about importing anything (the example imports the same modules)...
EDITED2 I'm now guessing it has to do with sort in a list?
Not even: PYTHONHASHSEED=0 python main.py
from Disable hash randomization from within python program worked
EDITED3: By the way, this is not a simple problem as the straight answer may suggest. It's actually a hard problem.
I use the code:
import numpy as np
class Model:
def __init__(self, seed=0):
self.np = np
self.seed = self.np.random.RandomState(seed)
Note the max_distance
is exactly the same. But the coefficient numbers are different.
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