metacountregressor 0.1.349__py3-none-any.whl → 1.0.4__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -459,14 +459,12 @@ class ObjectiveFunction(object):
459
459
 
460
460
  if self.G is not None:
461
461
  #TODO need to handle this for groups
462
- print('group by groups off for now')
463
- NAH_GROUP = 0
464
- if NAH_GROUP:
465
- self._distribution = ["trad| " + item for item in self._distribution
462
+
463
+
464
+ self._distribution = ["trad| " + item for item in self._distribution
466
465
  ] + ["grpd| " + item for item in self._distribution]
467
- else:
468
- self._distribution = ["trad| " + item for item in self._distribution
469
- ]
466
+
467
+
470
468
 
471
469
  # output information
472
470
  self.convergence = None
@@ -1793,23 +1791,40 @@ class ObjectiveFunction(object):
1793
1791
 
1794
1792
  counter = 0
1795
1793
  while b < self._min_characteristics and counter < max_loops:
1796
- counter +=1
1794
+ counter += 1
1797
1795
  weights = [1 if x == 0 else 0 for x in only_ints_vals]
1798
1796
  get_rdm_i = random.choices(only_ints, weights=weights)[0]
1799
1797
 
1800
- # todo if only 1 this will get stuck
1798
+ # Safeguard: Check if only one valid value exists to handle the "stuck" case
1799
+ if len(only_ints_vals) == 1 and only_ints_vals[0] == 0:
1800
+ print("Breaking out: only one valid value exists and it is 0.")
1801
+ break
1802
+
1801
1803
  if vector[get_rdm_i] == 0:
1804
+ max_inner_loops = 100 # Limit for the inner loop
1805
+ inner_counter = 0
1802
1806
  while new_j == 0:
1803
- # print('this should only ever be 3 ocasionally 2', self.get_num_discrete_values(get_rdm_i))
1804
- get_rdm_j = random.randint(
1805
- 0, self.get_num_discrete_values(get_rdm_i) - 1)
1807
+ inner_counter += 1
1808
+ if inner_counter > max_inner_loops:
1809
+ print("Breaking out of inner loop: exceeded maximum iterations.")
1810
+ break
1811
+
1812
+ get_rdm_j = random.randint(0, self.get_num_discrete_values(get_rdm_i) - 1)
1806
1813
  new_j = self.get_value(get_rdm_i, get_rdm_j)
1814
+
1815
+ # If the inner loop exited early, check and continue
1816
+ if new_j == 0:
1817
+ print("Inner loop exited without finding a valid value.")
1818
+ break
1819
+
1820
+ # Assign the value after successfully exiting the inner loop
1807
1821
  vector[get_rdm_i] = new_j
1808
1822
  prmVect[get_rdm_i] = new_j
1809
1823
 
1810
1824
  b += new_j
1811
1825
  new_j = 0
1812
1826
 
1827
+
1813
1828
  if hasattr(self, 'forced_variables'):
1814
1829
  self.force_inclusion(vector)
1815
1830
 
@@ -2477,7 +2492,7 @@ class ObjectiveFunction(object):
2477
2492
  layout = vector.copy()
2478
2493
 
2479
2494
  while max_trial <= max_routine and trial_run == 1: # and max_trial <= 200:
2480
-
2495
+
2481
2496
  obj_1, model_mod = self.makeRegression(model_nature, layout=layout, **a)
2482
2497
 
2483
2498
  if obj_best is None:
@@ -7212,7 +7227,7 @@ class ObjectiveFunction(object):
7212
7227
  if len(self.none_handler(zi)) > 0:
7213
7228
  the_name = the_name + '_zi'
7214
7229
  if model_nature is not None:
7215
- if 'XG' in model_nature:
7230
+ if 'XG' in model_nature and model_nature.get('XG') is not None:
7216
7231
  if model_nature.get('XG').shape[2] > 0:
7217
7232
  the_name = the_name + 'grp'
7218
7233
 
@@ -7285,10 +7300,11 @@ class ObjectiveFunction(object):
7285
7300
  if grouped_rpm is None:
7286
7301
  a =len(self.rdm_cor_fit)+len(self.rdm_fit) != Xr.shape[2]
7287
7302
  else:
7288
- a = len(self.none_handler(self.rdm_cor_fit)) + len(self.none_handler(self.rdm_fit)) + len(self.none_handler(grouped_rpm)) != Xr.shape[2]
7303
+ a = len(self.none_handler(self.rdm_cor_fit)) + len(self.none_handler(self.rdm_fit)) + len(self.none_handler(grouped_rpm))*self._Gnum != Xr.shape[2]
7289
7304
 
7290
7305
  if a:
7291
7306
  print('why')
7307
+
7292
7308
  print('The number of random effects does not match the data')
7293
7309
  print(Xr.shape)
7294
7310
  print(self.rdm_cor_fit)
@@ -7368,16 +7384,19 @@ class ObjectiveFunction(object):
7368
7384
  self.group_dummies.shape[2]) if self.grouped_rpm != [] else []
7369
7385
  X_set = df_tf[:, :, indices4]
7370
7386
  XG = np.tile(self.group_dummies, len(self.grouped_rpm)) * X_set if X_set.shape[2] != 0 else None
7387
+ if XG is not None:
7388
+ model_nature['XG'] = XG
7389
+
7371
7390
  else:
7372
7391
  XG = None
7373
7392
  X = df_tf[:, :, indices]
7374
7393
  XH = df_tf[:, :, indices5]
7375
7394
 
7376
- if XG is not None:
7395
+ if XG is not None and self.is_multi:
7377
7396
  indices4_test = np.repeat(self.get_named_indices(self.grouped_rpm),
7378
7397
  self.group_dummies_test.shape[2]) if self.grouped_rpm != [] else []
7379
7398
  XGtest = np.tile(self.group_dummies_test, len(self.grouped_rpm)) * df_test[:, :, indices4_test]
7380
- model_nature['XG'] = XG
7399
+ #model_nature['XG'] = XG
7381
7400
  model_nature['XGtest'] = XGtest
7382
7401
 
7383
7402
  model_nature['X'] = X
@@ -7396,7 +7415,7 @@ class ObjectiveFunction(object):
7396
7415
  if np.isin(X, [np.inf, -np.inf, None, np.nan]).any(): # type ignore
7397
7416
  raise Exception('there is some kind of error in X')
7398
7417
 
7399
- # numpy data setup fpr estimation
7418
+ # numpy data setup for estimation
7400
7419
  indices2 = self.get_named_indices(self.rdm_fit)
7401
7420
  Xr = df_tf[:, :, indices2]
7402
7421
  if self.rdm_cor_fit is not None:
@@ -0,0 +1,43 @@
1
+ from solution import ObjectiveFunction
2
+ from metaheuristics import (harmony_search,differential_evolution,simulated_annealing)
3
+ #from . import helperprocess
4
+ import pandas as pd
5
+
6
+ df = pd.read_csv("https://raw.githubusercontent.com/zahern/data/refs/heads/main/rural_int.csv")
7
+ y = df['crashes'] # Frequency of crashes
8
+
9
+ df.drop(columns=[ 'year', 'orig_ID',
10
+ 'jurisdiction', 'town', 'maint_region', 'weather_station', 'dummy_winter_2', 'month', 'inj.fat', 'PDO', 'zonal_ID', 'ln_AADT', 'ln_seg'], inplace=True) # was dropped postcode
11
+
12
+
13
+
14
+ arguments_hs = {'_par': 0.3, '_hms': 20}
15
+ arguments = {'test_percentage': 0.2, 'complexity_level': 5, 'reg_penalty':0} #Objective args
16
+ # Step 2: Process Data
17
+ model_terms = {
18
+ 'Y': 'crashes', # Dependent variable
19
+ 'group': 'county', # Grouping column (if any)
20
+ 'panels': 'element_ID', # Panel column (if any)
21
+ 'Offset': None # Offset column (if any)
22
+ }
23
+
24
+
25
+ X = df.drop(columns=['crashes']) # setup X based on data
26
+ X.columns
27
+ print(X.columns)
28
+
29
+ manual_fit_spec = {
30
+ 'fixed_terms': ['const', 'DP10'],
31
+ 'rdm_terms': [ 'DX32:normal'],
32
+ 'rdm_cor_terms': [],
33
+ 'group_rdm': ['DPO1:triangular'],
34
+ 'hetro_in_means': [],
35
+ 'transformations': ['no', 'no', 'no', 'no', 'no', 'no'],
36
+ 'dispersion': 0
37
+ }
38
+ arguments = {'test_percentage': 0.2, 'complexity_level': 6, 'reg_penalty':0, 'group':'county', 'panels':'element_ID'} #Objective args
39
+ arguments['Manual_Fit'] = manual_fit_spec
40
+ #initial_solution = None
41
+ obj_fun = ObjectiveFunction(X, y, **arguments)
42
+ initial_solution = None
43
+ results_hs = harmony_search(obj_fun, initial_solution, **arguments_hs)
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: metacountregressor
3
- Version: 0.1.349
3
+ Version: 1.0.4
4
4
  Summary: Extensive Testing for Estimation of Data Count Models
5
5
  Home-page: https://github.com/zahern/CountDataEstimation
6
6
  Author: Zeke Ahern
@@ -11,10 +11,11 @@ metacountregressor/pareto_file.py,sha256=whySaoPAUWYjyI8zo0hwAOa3rFk6SIUlHSpqZiL
11
11
  metacountregressor/pareto_logger__plot.py,sha256=mEU2QN4wmsM7t39GJ_XhJ_jjsdl09JOmG0U2jICrAkI,30037
12
12
  metacountregressor/setup.py,sha256=5UcQCCLR8Fm5odA3MX78WwahavxFq4mVD6oq0IuQvAY,936
13
13
  metacountregressor/single_objective_finder.py,sha256=jVG7GJBqzSP4_riYr-kMMKy_LE3SlGmKMunNhHYxgRg,8011
14
- metacountregressor/solution.py,sha256=8IuzIgkb5FWer_caL4p-9yTf7YYIzIlbHIqMjxe83DQ,323208
14
+ metacountregressor/solution.py,sha256=IAOnmKtuW30EZfLjwm2LlvdAR5uii721A9Q43NbSFpc,323897
15
+ metacountregressor/test_code.py,sha256=_7Emm2JbhK_NVhxoqMhshN2JeHZtihZuSDl3Jpe7Ajk,1641
15
16
  metacountregressor/test_generated_paper2.py,sha256=pwOoRzl1jJIIOUAAvbkT6HmmTQ81mwpsshn9SLdKOg8,3927
16
- metacountregressor-0.1.349.dist-info/licenses/LICENSE.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
17
- metacountregressor-0.1.349.dist-info/METADATA,sha256=dMpFX0OOkwZhUdxjsP14o6j6WfkqvskPE5TvAxFc8U8,23620
18
- metacountregressor-0.1.349.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
19
- metacountregressor-0.1.349.dist-info/top_level.txt,sha256=zGG7UC5WIpr76gsFUpwJ4En2aCcoNTONBaS3OewwjR0,19
20
- metacountregressor-0.1.349.dist-info/RECORD,,
17
+ metacountregressor-1.0.4.dist-info/licenses/LICENSE.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
18
+ metacountregressor-1.0.4.dist-info/METADATA,sha256=f0gPjk7T2733AJEYUwJRv7CDmiaSA6uipvtR38kvmTQ,23618
19
+ metacountregressor-1.0.4.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
20
+ metacountregressor-1.0.4.dist-info/top_level.txt,sha256=zGG7UC5WIpr76gsFUpwJ4En2aCcoNTONBaS3OewwjR0,19
21
+ metacountregressor-1.0.4.dist-info/RECORD,,