metacountregressor 0.1.34__py3-none-any.whl → 0.1.36__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.
@@ -144,7 +144,8 @@ class ObjectiveFunction(object):
144
144
  self.MAE = None
145
145
  self.best_obj_1 = 100000000
146
146
  self._obj_1 = 'bic'
147
- self._obj_2 = 'MAE'
147
+ self._obj_2 = 'MSE'
148
+ self.numerical_hessian_calc = 1 # calculates hessian by statsmodels otherwise scipy
148
149
  self.full_model = None
149
150
  self.GP_pararameter = 0
150
151
  self.is_multi = 0
@@ -518,8 +519,8 @@ class ObjectiveFunction(object):
518
519
 
519
520
  for_testing_purposes = 0
520
521
  if "Manual_Fit" in kwargs and kwargs['Manual_Fit'] is not None:
521
- for_testing_purposes = 0
522
- print('off for now turn back on')
522
+ for_testing_purposes = 1
523
+
523
524
  if for_testing_purposes:
524
525
  self.initial_sig = 1 # pass the test of a single model
525
526
  self.pvalue_sig_value = 1
@@ -1055,7 +1056,7 @@ class ObjectiveFunction(object):
1055
1056
  except Exception as e:
1056
1057
  print(e)
1057
1058
 
1058
- def summary_alternative(self, long_print=0, model=0, solution=None):
1059
+ def summary_alternative(self, long_print=0, model=0, solution=None, save_state = 0):
1059
1060
  fmt = "{:19} {:13} {:13.10f} {:13.10f}{:13.10f} {:13.3g} {:3}"
1060
1061
  coeff_name_str_length = 19
1061
1062
 
@@ -1075,17 +1076,17 @@ class ObjectiveFunction(object):
1075
1076
  raise Exception
1076
1077
 
1077
1078
 
1078
- self.pvalues= [self.round_with_padding(x, 2) for x in self.pvalues]
1079
+ #self.pvalues= [self.round_with_padding(x, 2) for x in self.pvalues]
1079
1080
  #self.pvalues= [self.round_with_scientific(x, 2) for x in self.pvalues]
1080
1081
 
1081
1082
  for i in range(len(self.coeff_)):
1082
1083
  signif = ""
1083
1084
 
1084
- if self.pvalues[i] < 0.01:
1085
+ if float(self.pvalues[i]) < 0.01:
1085
1086
  signif = "***"
1086
- elif self.pvalues[i] < 0.05:
1087
+ elif float(self.pvalues[i]) < 0.05:
1087
1088
  signif = "**"
1088
- elif self.pvalues[i] < 0.1:
1089
+ elif float(self.pvalues[i]) < 0.1:
1089
1090
  signif = "*"
1090
1091
 
1091
1092
  '''
@@ -1180,8 +1181,10 @@ class ObjectiveFunction(object):
1180
1181
  file_name = self.instance_number + "/sln" + \
1181
1182
  str(solution['sol_num']) + \
1182
1183
  "_with_BIC_"+str(self.bic)+".tex"
1183
- self.save_to_file(latextable.draw_latex(
1184
- table, caption=caption, caption_above = True), file_name)
1184
+
1185
+ if save_state:
1186
+ self.save_to_file(latextable.draw_latex(
1187
+ table, caption=caption, caption_above = True), file_name)
1185
1188
 
1186
1189
 
1187
1190
  #print('change this')
@@ -1419,11 +1422,20 @@ class ObjectiveFunction(object):
1419
1422
  if np.size(y) != np.size(eVy):
1420
1423
  y = np.tile(y, self.Ndraws).ravel()
1421
1424
  eVy = eVy.ravel()
1425
+
1426
+ # y_avg = np.mean(y, axis = (1,2))
1427
+ # eVy_avg = np.mean(eVy, axis = (1,2))
1428
+ #mspe1 = np.nan_to_num(MSPE(np.squeeze(y_avg), np.squeeze(eVy_avg)), nan=100000, posinf=100000)
1422
1429
  eVy = np.nan_to_num(eVy, nan=100000, posinf=100000)
1423
1430
  eVy = np.clip(eVy, None, 1000)
1424
1431
  mae = np.nan_to_num(MAE(np.squeeze(y), np.squeeze(eVy)), nan=100000, posinf=100000)
1425
1432
  mspe = np.nan_to_num(MSPE(np.squeeze(y), np.squeeze(eVy)), nan=100000, posinf=100000)
1426
-
1433
+ if self._obj_2 == 'MAE':
1434
+ return mae
1435
+ elif self._obj_2 == 'MSE':
1436
+ return mspe
1437
+ elif self._obj_2 == "MAD":
1438
+ raise Exception
1427
1439
  return mspe
1428
1440
 
1429
1441
  def get_solution_vector(self, fixed_vars, random_vars, random_var_cor, distribution_vars, dispersion=None):
@@ -1603,7 +1615,7 @@ class ObjectiveFunction(object):
1603
1615
 
1604
1616
  if (self.get_num_discrete_values(get_rdm_i) - 1) == 0:
1605
1617
  # TODO: must be a better way to avoid selecting this
1606
- print('ignore replacemenet')
1618
+ print('repair constraint violated, skipping over..')
1607
1619
 
1608
1620
 
1609
1621
  else:
@@ -3455,15 +3467,22 @@ class ObjectiveFunction(object):
3455
3467
  return covariance
3456
3468
 
3457
3469
  def _numerical_hessian(self, betas, args, jac):
3458
- Xd, y, draws, Xf, Xr, corr_list, dispersion = args
3470
+ #Xd, y, draws, Xf, Xr, corr_list, dispersion = args
3459
3471
  def loglike(p): return self._loglik_gradient(
3460
- p, Xd, y, draws, Xf, Xr, None, False, False, dispersion, corr_list=corr_list)
3472
+ p, *args)
3473
+ robust = False
3461
3474
  hess = approx_hess(betas, loglike)
3475
+ hess /= self.N
3462
3476
  hess_inv1 = np.linalg.pinv(hess)
3463
- hess = self._hessian_prot(betas, loglike)
3464
- hess = np.nan_to_num(hess)
3465
- hess_inv = np.linalg.pinv(hess)
3466
- return hess_inv
3477
+ if robust:
3478
+
3479
+ scores = approx_fprime(betas, loglike)
3480
+ score_cov = np.cov(scores.T)
3481
+ inv_hess = hess_inv1.dot(score_cov).dot(hess_inv1) / self.N
3482
+ else:
3483
+ inv_hess = hess_inv1 / self.N
3484
+
3485
+ return inv_hess
3467
3486
 
3468
3487
  def _chol_mat(self, correlationLength, br, Br_w, correlation):
3469
3488
  # if correlation = True correlation pos is randpos, if list get correct pos
@@ -5940,65 +5959,26 @@ class ObjectiveFunction(object):
5940
5959
 
5941
5960
  if dispersion ==1:
5942
5961
  mod['dispersion_penalty'] = abs(b[-1])
5943
-
5944
- betas_est = self._minimize(self._loglik_gradient, b, args=(X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod),
5962
+ grad_args = (X, y, draws, X, Xr, self.batch_size,False, False, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod)
5963
+ betas_est = self._minimize(self._loglik_gradient, b, args=(X, y, draws, X, Xr, self.batch_size,False, False, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod),
5945
5964
  method=method2, tol=tol['ftol'],
5946
5965
  options={'gtol': tol['gtol']}, bounds = bounds)
5966
+
5967
+ if self.numerical_hessian_calc:
5968
+ try:
5969
+ bb_hess = self._numerical_hessian(betas_est.x, grad_args, False)
5970
+ except Exception as e:
5971
+ bb_hess = None
5972
+ else:
5973
+ bb_hess = None
5947
5974
  # betas_est = self._minimize(self._loglik_gradient, b, args=(X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod),
5948
5975
  # method=method2, tol=tol['ftol'], options={'gtol': tol['gtol']})
5949
5976
  # betas_est = self._minimize(self._loglik_gradient, b, args=(X, y, draws, X, Xr, self.batch_size, False, False, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod),
5950
5977
  # method=method2, tol=tol['ftol'], options={'gtol': tol['gtol']})
5951
5978
 
5952
- '''
5953
-
5954
- print('detlet the fuck out of this ...')
5955
-
5956
- if dispersion:
5957
- bb = b[0:-1]
5958
- dispersion_coef = b[-1]
5959
- constant_coef = None
5960
- else:
5961
- bb = b[1:]
5962
- dispersion_coef = None
5963
- constant_coef = b[0]
5964
-
5965
-
5979
+
5966
5980
 
5967
- beta_est_alt = self._minimize(self._loglik_gradient_wrapper, bb, args=(constant_coef, dispersion_coef, X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl, draws_grouped, XG, mod),
5968
- method=method, tol=tol['ftol'],
5969
- options={'gtol': tol['gtol']})
5970
- print(beta_est_alt)
5971
- '''
5972
5981
 
5973
- old_code = 0
5974
- if old_code:
5975
- gb_best = betas_est.copy()
5976
- if betas_est['success'] == False:
5977
- #b[0] += b[0] -len(b)/5
5978
- # for i in range(1, len(bb) -self.is_dispersion(dispersion)):
5979
- # b[i] += b[i]/5 +.5
5980
- # for i, j in enumerate(b):
5981
- # b[i] = 0
5982
- start_time = time.time()
5983
- #print('starint slow')
5984
- betas_est = self._minimize(self._loglik_gradient, b, args=(X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl),
5985
- method=method2, tol=tol['ftol'], options={'gtol': tol['gtol']})
5986
- print("--- %s seconds ---" %
5987
- (time.time() - start_time))
5988
- #ll, grad, hess = self._loglik_gradient(betas_est['x'] , X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl)
5989
- #hess_inv = np.linalg.pinv(np.dot(hess.T, hess))
5990
- #betas_est['hess_inv'] = hess_inv
5991
- if betas_est['fun'] <= gb_best['fun']:
5992
- gb_best = betas_est.copy()
5993
- bb = gb_best['x'].copy()
5994
-
5995
- betas_est = gb_best.copy()
5996
-
5997
- ll, grad, hess = self._loglik_gradient(
5998
- betas_est['x'], X, y, draws, X, Xr, self.batch_size, True, True, dispersion, 0, False, 0, self.rdm_cor_fit, self.zi_fit, exog_infl)
5999
- hess_inv = np.linalg.pinv(np.dot(hess.T, hess))
6000
- betas_est['hess_inv'] = hess_inv
6001
- # this is causing more problems, lets reconsider
6002
5982
 
6003
5983
  if betas_est['message'] == 'NaN result encountered.':
6004
5984
 
@@ -6015,7 +5995,8 @@ class ObjectiveFunction(object):
6015
5995
  betas_est['x'], 0, dispersion, zi_fit=sub_zi, model_nature = mod)
6016
5996
  if hasattr(betas_est.hess_inv, 'todense'):
6017
5997
  betas_est['hess_inv'] = betas_est.hess_inv.todense() if hasattr(betas_est.hess_inv, 'todense') else np.array([betas_est.hess_inv(np.eye(len(b))[i]) for i in range(len(b))])
6018
-
5998
+ if bb_hess is not None:
5999
+ betas_est['hess_inv'] = bb_hess
6019
6000
  if betas_est['hess_inv'] is None:
6020
6001
 
6021
6002
  self.convergance = 1
@@ -6540,11 +6521,14 @@ class ObjectiveFunction(object):
6540
6521
  if self.is_multi:
6541
6522
 
6542
6523
  if self.pareto_printer.check_if_dominance(self._pareto_population, obj_1):
6524
+ print('dominate soltuion')
6543
6525
  try:
6544
6526
  self.summary_alternative(
6545
6527
  long_print=1, model=dispersion, solution=obj_1)
6546
6528
  except Exception as e:
6547
6529
  print('e', obj_1)
6530
+ else:
6531
+ print('non_dominant solution.')
6548
6532
  if obj_1['layout'] is None:
6549
6533
  print('no layout??')
6550
6534
  else: