metacountregressor 1.0.18__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:
191
193
  print('testing code')
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
+ 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
+
219
+
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)
@@ -228,9 +236,14 @@ class ObjectiveFunction(object):
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)
230
238
  else:
231
- if not kwargs.get('delete_directory', False):
232
- print('to clear the directory clear')
233
- os.d
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')
234
247
 
235
248
  else:
236
249
  self.save_state = False
@@ -1477,7 +1490,12 @@ class ObjectiveFunction(object):
1477
1490
  if dispersion == 0:
1478
1491
  return None
1479
1492
  if dispersion == 1:
1480
- 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
+
1481
1499
 
1482
1500
 
1483
1501
  elif dispersion == 2 or dispersion == 1:
@@ -1794,7 +1812,10 @@ class ObjectiveFunction(object):
1794
1812
  signif_list = self.pvalue_asterix_add(self.pvalues)
1795
1813
  if model == 1:
1796
1814
  # raise to the exponential
1797
- 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
+
1798
1819
  if self.no_extra_param:
1799
1820
  self.coeff_ = np.append(self.coeff_, self.nb_parma)
1800
1821
  self.stderr = np.append(self.stderr, 0.00001)
@@ -2098,7 +2119,7 @@ class ObjectiveFunction(object):
2098
2119
  #disp = sm.OLS(ab.ravel(), bb.ravel()).fit()
2099
2120
  #gamma = disp.params[0]
2100
2121
  #print(f'dispersion is {gamma}')
2101
- gamma = np.min([dispersion,1])
2122
+ gamma = np.min([dispersion,1000])
2102
2123
  if gamma < 0.05:
2103
2124
  gamma = 0.05
2104
2125
  return gamma
@@ -4251,11 +4272,15 @@ class ObjectiveFunction(object):
4251
4272
  '''
4252
4273
  alpha = gamma
4253
4274
  size = 1.0 / alpha * mu ** Q
4254
-
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)
4255
4280
  prob = size/(size+mu)
4256
4281
 
4257
-
4258
-
4282
+ #binom_coeff = math.comb(int(y +r - 1), y)
4283
+ #ff = binom_coeff * ((1 - p) ** r) * (p ** y)
4259
4284
  '''test'''
4260
4285
 
4261
4286
 
@@ -4289,7 +4314,7 @@ class ObjectiveFunction(object):
4289
4314
  y + alpha) * np.log(mu + alpha))
4290
4315
  gg[np.isnan(gg)] = 1
4291
4316
  '''
4292
- gg_alt = nbinom.pmf(y ,1/alpha, prob)
4317
+ gg_alt = nbinom.pmf(y ,alpha, prob)
4293
4318
  #gg_alt_2 = (gammaln(size + y) - gammaln(y + 1) -
4294
4319
  #gammaln(size)) + size * np.log(prob) + y * np.log(1 - prob)
4295
4320
  #print('check theses')
@@ -4299,7 +4324,8 @@ class ObjectiveFunction(object):
4299
4324
 
4300
4325
  except Exception as e:
4301
4326
  print("Neg Binom error.")
4302
- return gg_alt
4327
+ return pmf
4328
+ #return gg_alt
4303
4329
 
4304
4330
  def lindley_pmf(self, x, r, theta, k=50):
4305
4331
  """
@@ -6410,6 +6436,7 @@ class ObjectiveFunction(object):
6410
6436
  0, False, 0, None, None, None, None, None, mod),
6411
6437
  method=method2, tol=tol['ftol'], options={'gtol': tol['gtol']})
6412
6438
 
6439
+
6413
6440
  if initial_beta is not None and not np.isnan(initial_beta['fun']):
6414
6441
  self._no_random_paramaters = 1
6415
6442
  if initial_beta['success'] != 0:
@@ -6881,19 +6908,46 @@ class ObjectiveFunction(object):
6881
6908
  if dispersion ==0:
6882
6909
  model = sm.GLM(y.squeeze(axis=-1), XX.squeeze(axis=1), family=sm.families.Poisson())
6883
6910
  else:
6884
- 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))
6885
6913
  result = model.fit()
6886
- 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
+ '''
6887
6931
  if len(initial_params) < num_coefficients:
6888
- initial_params = np.concatenate([
6889
- initial_params,
6890
- np.random.uniform(-0.01, 0.03, size=num_coefficients - len(initial_params))
6891
- ])
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
+
6892
6945
 
6893
6946
  else:
6947
+ #print('failed taking random fit')
6894
6948
  initial_params = np.random.uniform(-0.01, 0.3, size=num_coefficients)
6895
6949
  except:
6896
- print('pre fit failed, continie')
6950
+ #print('pre fit failed, continue')
6897
6951
  initial_params = np.random.uniform(-0.01, 0.01, size=num_coefficients)
6898
6952
 
6899
6953
 
@@ -6904,14 +6958,16 @@ class ObjectiveFunction(object):
6904
6958
 
6905
6959
  # Add dispersion parameter if applicable
6906
6960
  if dispersion == 1:
6907
- print('checking for dispersion')
6961
+ #print('checking for dispersion')
6908
6962
  calculated_dispersion = self.poisson_mean_get_dispersion(initial_params[:-1], XX, y)
6909
- print('calculated dispersion', calculated_dispersion)
6910
- print('estimated dispersion', initial_params[-1])
6963
+ #print('init', initial_params)
6964
+ #print('calculated dispersion', calculated_dispersion)
6965
+ #print('alpha ', 1/calculated_dispersion)
6966
+ #print('estimated dispersion', initial_params[-1])
6911
6967
 
6912
6968
 
6913
6969
  if dispersion > 0:
6914
- initial_params[-1] = 0.0
6970
+ initial_params[-1] = calculated_dispersion
6915
6971
  #initial_params[0] =3
6916
6972
 
6917
6973
  return initial_params
@@ -6953,6 +7009,7 @@ class ObjectiveFunction(object):
6953
7009
 
6954
7010
 
6955
7011
  # Run optimization
7012
+ #initial_params = [2.82, 1.11]
6956
7013
  optimization_result = self._run_optimization(
6957
7014
  XX, y, dispersion, initial_params, bounds, tol, mod, maxiter=maxiter
6958
7015
  )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: metacountregressor
3
- Version: 1.0.18
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=v1W9XuXkvOr5SXHdOBuQ3KUSFW6mpGR3ydO0DqAjxqY,24396
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=ufrKyd1MOVi9SnWmvNKh9h913UAPeLNjJuyBEEvUsXk,346926
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.18.dist-info/licenses/LICENSE.txt,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
18
- metacountregressor-1.0.18.dist-info/METADATA,sha256=9l4G8HZtBunSQYosC6TXH99IxlXbfATdC2LXGY6cJCA,23635
19
- metacountregressor-1.0.18.dist-info/WHEEL,sha256=zaaOINJESkSfm_4HQVc5ssNzHCPXhJm0kEUakpsEHaU,91
20
- metacountregressor-1.0.18.dist-info/top_level.txt,sha256=zGG7UC5WIpr76gsFUpwJ4En2aCcoNTONBaS3OewwjR0,19
21
- metacountregressor-1.0.18.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.8.0)
2
+ Generator: setuptools (80.9.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5