From 81ab1aaca10241e6766a4ef3d07338a3476788c0 Mon Sep 17 00:00:00 2001 From: baogorek Date: Thu, 24 Apr 2025 11:23:56 -0400 Subject: [PATCH 1/5] removed seed arithmetic --- policyengine_core/commons/formulas.py | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 4695cf9c..198fa884 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -330,12 +330,7 @@ def random(population): # Generate random values for each entity values = np.array( - [ - np.random.default_rng( - seed=id * 100 + population.simulation.count_random_calls - ).random() - for id in entity_ids - ] + [np.random.default_rng(seed=id).random() for id in entity_ids] ) return values From 85c86ea1ff92c01557a0bc3b2e0970595b01975a Mon Sep 17 00:00:00 2001 From: baogorek Date: Thu, 24 Apr 2025 17:49:50 -0400 Subject: [PATCH 2/5] fix for negative seed --- policyengine_core/commons/formulas.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 198fa884..5b8915d7 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -19,6 +19,7 @@ from policyengine_core.variables.variable import Variable import json +import pdb T = TypeVar("T") @@ -329,9 +330,7 @@ def random(population): entity_ids = population(f"{population.entity.key}_id", period) # Generate random values for each entity - values = np.array( - [np.random.default_rng(seed=id).random() for id in entity_ids] - ) + values = np.random.default_rng(seed=min(entity_ids)).random(size=len(entity_ids)) return values From 9a95c32eac4ae40488ea5164d09418d354e49b4b Mon Sep 17 00:00:00 2001 From: baogorek Date: Thu, 24 Apr 2025 17:55:19 -0400 Subject: [PATCH 3/5] Removing pdb import --- policyengine_core/commons/formulas.py | 1 - 1 file changed, 1 deletion(-) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index 5b8915d7..b4a37e1d 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -19,7 +19,6 @@ from policyengine_core.variables.variable import Variable import json -import pdb T = TypeVar("T") From d51e6a0c9bf75c2e2b47e8ce670470455ce2a70e Mon Sep 17 00:00:00 2001 From: baogorek Date: Thu, 24 Apr 2025 17:57:24 -0400 Subject: [PATCH 4/5] policyengine_core/commons/formulas.py --- policyengine_core/commons/formulas.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/policyengine_core/commons/formulas.py b/policyengine_core/commons/formulas.py index b4a37e1d..74178747 100644 --- a/policyengine_core/commons/formulas.py +++ b/policyengine_core/commons/formulas.py @@ -329,7 +329,9 @@ def random(population): entity_ids = population(f"{population.entity.key}_id", period) # Generate random values for each entity - values = np.random.default_rng(seed=min(entity_ids)).random(size=len(entity_ids)) + values = np.random.default_rng(seed=min(entity_ids)).random( + size=len(entity_ids) + ) return values From 4e2e1117e89eef51f6cc3de6dce1e36a5adedd34 Mon Sep 17 00:00:00 2001 From: baogorek Date: Fri, 25 Apr 2025 08:36:32 -0400 Subject: [PATCH 5/5] changelogging --- changelog_entry.yaml | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..cacec347 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,6 @@ +- bump: patch + changes: + fixed: + - Removed the possibility of a negative seed in random() within commons/formulas.py + changed: + - As part of the negative seed fix in formulas.py, a single seed and generator was used to generate a random sequence, rather than a random number generator and seed combination being deployed for each entity id. This saves around 5-10 seconds of computation.