metacountregressor 1.0.13__py3-none-any.whl → 1.0.19__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.
@@ -177,6 +177,7 @@ def main(args, **kwargs):
177
177
  df['Offset'] = np.log(df['AADT'])
178
178
  # Drop Y, selected offset term and ID as there are no panels
179
179
  X = df.drop(columns=['FREQ', 'ID', 'AADT'])
180
+
180
181
  # Step 0: Process Data
181
182
  model_terms = {
182
183
  'Y': 'FREQ', # Replace 'FREQ' with the name of your dependent variable
@@ -187,10 +188,35 @@ def main(args, **kwargs):
187
188
  a_des, df = helperprocess.set_up_analyst_constraints(df, model_terms)
188
189
  # some example argument, these are defualt so the following line is just for claritity
189
190
  AMALAN = False
191
+ ZEKE = False
190
192
  if AMALAN:
193
+ print('testing code')
194
+ args = {'algorithm': 'hs', 'test_percentage': 0, 'test_complexity': 6, 'instance_number': 1,
195
+ 'val_percentage': 0, 'obj_1': 'bic', '_obj_2': 'RMSE_TEST', "MAX_TIME": 600, 'desicions':a_des, 'is_multi': 1}
196
+ elif ZEKE:
197
+ X = X.drop(columns=['Offset'])
198
+ manual_fit_spec = {
199
+ 'fixed_terms': ['const', 'FC', 'SLOPE', 'AVESNOW'],
200
+ 'rdm_terms': ['URB:normal'],
201
+ 'rdm_cor_terms': [],
202
+ 'grouped_rdm': [],
203
+ 'hetro_in_means': [],
204
+ 'transformations': ['no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no', 'no'],
205
+ 'dispersion': 1
206
+ }
207
+
208
+ # Search Arguments
209
+ arguments = {
210
+ 'algorithm': 'hs',
211
+ 'test_percentage': 0,
212
+ 'test_complexity': 6,
213
+ 'instance_name': 'name',
214
+ 'Manual_Fit': manual_fit_spec
215
+ }
216
+ obj_fun = ObjectiveFunction(X, y, **arguments)
217
+ print('completed')
218
+
191
219
 
192
- args = {'algorithm': 'hs', 'test_percentage': 0.15, 'test_complexity': 6, 'instance_number': 1,
193
- 'val_percentage': 0.15, 'obj_1': 'bic', '_obj_2': 'RMSE_TEST', "MAX_TIME": 600, 'desicions':a_des, 'is_multi': 1}
194
220
  else:
195
221
 
196
222
  args = {'algorithm': 'hs', 'test_percentage': 0, 'test_complexity': 2, 'instance_number': 1,
@@ -19,7 +19,7 @@ import numpy as np
19
19
  import pandas as pd
20
20
  import psutil
21
21
  import scipy.special as sc
22
-
22
+ import shutil
23
23
  import statsmodels.api as sm
24
24
  from scipy.integrate import quad
25
25
  from scipy.optimize import minimize
@@ -123,6 +123,14 @@ class ObjectiveFunction(object):
123
123
  """
124
124
 
125
125
  def __init__(self, x_data, y_data, **kwargs):
126
+ """
127
+ Initialie with dynatmic kwargs
128
+ Keyword Args:
129
+
130
+ """
131
+
132
+
133
+
126
134
  self.gbl_best = 1e5
127
135
  self.non_sig_prints = kwargs.get('non_sig_prints', False)
128
136
  self.run_numerical_hessian = kwargs.get('r_nu_hess', False)
@@ -227,6 +235,16 @@ class ObjectiveFunction(object):
227
235
  print(
228
236
  'Making a Directory, if you want to stop from storing the files to this directory set argumet: make_directory:False')
229
237
  os.makedirs(self.instance_name)
238
+ else:
239
+ if kwargs.get('delete_directory', False):
240
+ print('Clearing the directory...')
241
+ # Remove all contents of the directory
242
+ shutil.rmtree(self.instance_name)
243
+ # Recreate the directory
244
+ os.makedirs(self.instance_name)
245
+ else:
246
+ print('Directory exists. To clear it, set the argument: delete_directory=True')
247
+
230
248
  else:
231
249
  self.save_state = False
232
250
  if not hasattr(self, '_obj_1'):
@@ -574,7 +592,7 @@ class ObjectiveFunction(object):
574
592
  # Harmony search parameters
575
593
  self.algorithm = kwargs.get('algorithm', 'hs')
576
594
  self._hms = 20
577
- self._max_time = kwargs.get('_max_time', 0.8 * 60 * 60 * 24)
595
+ # self._max_time = kwargs.get('_max_time', 0.8 * 60 * 60 * 24)
578
596
  self._hmcr = kwargs.get('_hmcr', 0.5)
579
597
  self._par = 0.3
580
598
  self._mpai = 1
@@ -724,7 +742,7 @@ class ObjectiveFunction(object):
724
742
  # defalt paramaters for hs #TODO unpack into harmony search class
725
743
  self.algorithm = kwargs.get('algorithm', 'hs') # 'sa' 'de' also avialable
726
744
  self._hms = 20
727
- self._max_time = self._max_time = kwargs.get('_max_time', kwargs.get('MAX_TIME', 0.8 * 60 * 60 * 24))
745
+ self._max_time = kwargs.get('_max_time', kwargs.get('MAX_TIME', 0.8 * 60 * 60 * 24))
728
746
  self._hmcr = kwargs.get('_hmcr', .5)
729
747
  self._par = 0.3 #dont think this gets useted
730
748
  self._mpai = 1
@@ -1472,7 +1490,12 @@ class ObjectiveFunction(object):
1472
1490
  if dispersion == 0:
1473
1491
  return None
1474
1492
  if dispersion == 1:
1475
- return np.clip(np.exp(betas[-1]),None, 2)
1493
+ if betas[-1] <0:
1494
+ #transforming
1495
+ return np.clip(np.exp(betas[-1]-1),None, 600)
1496
+ else: #transforming
1497
+ return np.clip(betas[-1],None, 600)
1498
+
1476
1499
 
1477
1500
 
1478
1501
  elif dispersion == 2 or dispersion == 1:
@@ -1789,7 +1812,10 @@ class ObjectiveFunction(object):
1789
1812
  signif_list = self.pvalue_asterix_add(self.pvalues)
1790
1813
  if model == 1:
1791
1814
  # raise to the exponential
1792
- self.coeff_[-1] = np.maximum(np.exp(self.coeff_[-1]),5)
1815
+ if self.coeff_[-1] < 0:
1816
+ #transform if negative
1817
+ self.coeff_[-1] = np.maximum(np.exp(self.coeff_[-1]-1),600)
1818
+
1793
1819
  if self.no_extra_param:
1794
1820
  self.coeff_ = np.append(self.coeff_, self.nb_parma)
1795
1821
  self.stderr = np.append(self.stderr, 0.00001)
@@ -2083,6 +2109,7 @@ class ObjectiveFunction(object):
2083
2109
 
2084
2110
  # Calculate dispersion
2085
2111
  dispersion = residual_deviance / degrees_of_freedom
2112
+
2086
2113
  except:
2087
2114
  dispersion =1
2088
2115
 
@@ -2092,7 +2119,7 @@ class ObjectiveFunction(object):
2092
2119
  #disp = sm.OLS(ab.ravel(), bb.ravel()).fit()
2093
2120
  #gamma = disp.params[0]
2094
2121
  #print(f'dispersion is {gamma}')
2095
- gamma = np.min([dispersion,1])
2122
+ gamma = np.min([dispersion,1000])
2096
2123
  if gamma < 0.05:
2097
2124
  gamma = 0.05
2098
2125
  return gamma
@@ -4245,11 +4272,15 @@ class ObjectiveFunction(object):
4245
4272
  '''
4246
4273
  alpha = gamma
4247
4274
  size = 1.0 / alpha * mu ** Q
4248
-
4275
+ r = 1/gamma
4276
+ p = gamma/(gamma+lam)
4277
+ pmf = nbinom.pmf(y, r, p)
4278
+ #pmf = self.nbinom_pmf(y, lam, gamma)
4279
+ #p = lam/(lam+r)
4249
4280
  prob = size/(size+mu)
4250
4281
 
4251
-
4252
-
4282
+ #binom_coeff = math.comb(int(y +r - 1), y)
4283
+ #ff = binom_coeff * ((1 - p) ** r) * (p ** y)
4253
4284
  '''test'''
4254
4285
 
4255
4286
 
@@ -4283,7 +4314,7 @@ class ObjectiveFunction(object):
4283
4314
  y + alpha) * np.log(mu + alpha))
4284
4315
  gg[np.isnan(gg)] = 1
4285
4316
  '''
4286
- gg_alt = nbinom.pmf(y ,1/alpha, prob)
4317
+ gg_alt = nbinom.pmf(y ,alpha, prob)
4287
4318
  #gg_alt_2 = (gammaln(size + y) - gammaln(y + 1) -
4288
4319
  #gammaln(size)) + size * np.log(prob) + y * np.log(1 - prob)
4289
4320
  #print('check theses')
@@ -4293,7 +4324,8 @@ class ObjectiveFunction(object):
4293
4324
 
4294
4325
  except Exception as e:
4295
4326
  print("Neg Binom error.")
4296
- return gg_alt
4327
+ return pmf
4328
+ #return gg_alt
4297
4329
 
4298
4330
  def lindley_pmf(self, x, r, theta, k=50):
4299
4331
  """
@@ -6404,6 +6436,7 @@ class ObjectiveFunction(object):
6404
6436
  0, False, 0, None, None, None, None, None, mod),
6405
6437
  method=method2, tol=tol['ftol'], options={'gtol': tol['gtol']})
6406
6438
 
6439
+
6407
6440
  if initial_beta is not None and not np.isnan(initial_beta['fun']):
6408
6441
  self._no_random_paramaters = 1
6409
6442
  if initial_beta['success'] != 0:
@@ -6875,19 +6908,46 @@ class ObjectiveFunction(object):
6875
6908
  if dispersion ==0:
6876
6909
  model = sm.GLM(y.squeeze(axis=-1), XX.squeeze(axis=1), family=sm.families.Poisson())
6877
6910
  else:
6878
- model = sm.NegativeBinomial(y.squeeze(axis=-1), XX.squeeze(axis=1))
6911
+ model = sm.GLM(y.squeeze(axis=-1), XX.squeeze(axis=1), family =sm.families.NegativeBinomial())
6912
+ #model = sm.NegativeBinomial(y.squeeze(axis=-1), XX.squeeze(axis=1))
6879
6913
  result = model.fit()
6880
- initial_params = result.params # then exten to num_coefficients
6914
+ #initial_params = result.params # then exten to num_coefficients
6915
+ if result.converged:
6916
+ initial_params = result.params
6917
+
6918
+ if len(initial_params) < num_coefficients:
6919
+ pearson_residuals = result.resid_pearson
6920
+ alpha = (pearson_residuals ** 2).sum() / result.df_resid
6921
+ if alpha > 0:
6922
+ alpha = np.log(alpha)
6923
+ initial_params = np.concatenate([
6924
+ initial_params.ravel(), np.array([alpha])])
6925
+ '''
6926
+ initial_params = np.concatenate([
6927
+ initial_params,
6928
+ np.random.uniform(-0.01, 0.03, size=num_coefficients - len(initial_params))
6929
+ ])
6930
+ '''
6881
6931
  if len(initial_params) < num_coefficients:
6882
- initial_params = np.concatenate([
6883
- initial_params,
6884
- np.random.uniform(-0.01, 0.03, size=num_coefficients - len(initial_params))
6885
- ])
6932
+
6933
+ #how to insert this into the second last position
6934
+
6935
+ # Assuming initial_params and num_coefficients are already defined
6936
+ new_elements = np.random.uniform(-0.01, 0.03, size=num_coefficients - len(initial_params))
6937
+
6938
+ # Second-to-last position is at index `-1` in NumPy
6939
+ initial_params = np.insert(initial_params, -dispersion, new_elements)
6940
+
6941
+
6942
+
6943
+
6944
+
6886
6945
 
6887
6946
  else:
6947
+ #print('failed taking random fit')
6888
6948
  initial_params = np.random.uniform(-0.01, 0.3, size=num_coefficients)
6889
6949
  except:
6890
- print('pre fit failed')
6950
+ #print('pre fit failed, continue')
6891
6951
  initial_params = np.random.uniform(-0.01, 0.01, size=num_coefficients)
6892
6952
 
6893
6953
 
@@ -6897,8 +6957,17 @@ class ObjectiveFunction(object):
6897
6957
  initial_params[parma_sum:-dispersion] =0.0001
6898
6958
 
6899
6959
  # Add dispersion parameter if applicable
6960
+ if dispersion == 1:
6961
+ #print('checking for dispersion')
6962
+ calculated_dispersion = self.poisson_mean_get_dispersion(initial_params[:-1], XX, y)
6963
+ #print('init', initial_params)
6964
+ #print('calculated dispersion', calculated_dispersion)
6965
+ #print('alpha ', 1/calculated_dispersion)
6966
+ #print('estimated dispersion', initial_params[-1])
6967
+
6968
+
6900
6969
  if dispersion > 0:
6901
- initial_params[-1] = 0.0
6970
+ initial_params[-1] = calculated_dispersion
6902
6971
  #initial_params[0] =3
6903
6972
 
6904
6973
  return initial_params
@@ -6940,6 +7009,7 @@ class ObjectiveFunction(object):
6940
7009
 
6941
7010
 
6942
7011
  # Run optimization
7012
+ #initial_params = [2.82, 1.11]
6943
7013
  optimization_result = self._run_optimization(
6944
7014
  XX, y, dispersion, initial_params, bounds, tol, mod, maxiter=maxiter
6945
7015
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: metacountregressor
3
- Version: 1.0.13
3
+ Version: 1.0.19
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, Alexander Paz
@@ -4,18 +4,18 @@ metacountregressor/app_main.py,sha256=vY3GczTbGbBRalbzMkl_9jVW7RMgEOc6z2Dr1IZJv9
4
4
  metacountregressor/data_split_helper.py,sha256=M2fIMdIO8znUaYhx5wlacRyNWdQjNYu1z1wkE-kFUYU,3373
5
5
  metacountregressor/halton.py,sha256=jhovA45UBoZYU9g-hl6Lb2sBIx_ZBTNdPrpgkzR9fng,9463
6
6
  metacountregressor/helperprocess.py,sha256=wW45-i31zy6rwaXt5PZt0GyR83PzF30jc9Wl4SQtnUI,26372
7
- metacountregressor/main.py,sha256=UFx9FONEjrVGIdoHdkwCejbi62OmIcM3_vGW0FSoXP4,24363
7
+ metacountregressor/main.py,sha256=9Pot00gDKJCM5n5CkT19yJYf1USK6FL8cxqWmQoDQr8,25234
8
8
  metacountregressor/main_old.py,sha256=eTS4ygq27MnU-dZ_j983Ucb-D5XfbVF8OJQK2hVVLZc,24123
9
9
  metacountregressor/metaheuristics.py,sha256=gVqJRNiHOa48-dHZxaJNgu2OLiYOpSYvWHJ1VFPqFWY,107817
10
10
  metacountregressor/pareto_file.py,sha256=whySaoPAUWYjyI8zo0hwAOa3rFk6SIUlHSpqZiLur0k,23096
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=Tzi7ZojYS8H4ZGJzKrmfqohnvpOtLtPqqhpWbW4Zo74,346466
14
+ metacountregressor/solution.py,sha256=48q4eeQyKDDEIk52ypCEGGBzmIhEH9VNNGGJxpxzsWc,349143
15
15
  metacountregressor/test_code.py,sha256=_7Emm2JbhK_NVhxoqMhshN2JeHZtihZuSDl3Jpe7Ajk,1641
16
16
  metacountregressor/test_generated_paper2.py,sha256=pwOoRzl1jJIIOUAAvbkT6HmmTQ81mwpsshn9SLdKOg8,3927
17
- metacountregressor-1.0.13.dist-info/licenses/LICENSE.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
18
- metacountregressor-1.0.13.dist-info/METADATA,sha256=gByJzUWaUNb16lb5htmQ3TfW85_DQa5SipENnAtsGFI,23635
19
- metacountregressor-1.0.13.dist-info/WHEEL,sha256=DnLRTWE75wApRYVsjgc6wsVswC54sMSJhAEd4xhDpBk,91
20
- metacountregressor-1.0.13.dist-info/top_level.txt,sha256=zGG7UC5WIpr76gsFUpwJ4En2aCcoNTONBaS3OewwjR0,19
21
- metacountregressor-1.0.13.dist-info/RECORD,,
17
+ metacountregressor-1.0.19.dist-info/licenses/LICENSE.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
18
+ metacountregressor-1.0.19.dist-info/METADATA,sha256=YeuBfnVUTsj_KckhWP7N9wtxWriCyqlDxdJBxP6FBgQ,23635
19
+ metacountregressor-1.0.19.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
20
+ metacountregressor-1.0.19.dist-info/top_level.txt,sha256=zGG7UC5WIpr76gsFUpwJ4En2aCcoNTONBaS3OewwjR0,19
21
+ metacountregressor-1.0.19.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (80.4.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5