metacountregressor 1.0.12__py3-none-any.whl → 1.0.18__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.
- metacountregressor/main.py +3 -3
- metacountregressor/solution.py +117 -156
- {metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/METADATA +2 -2
- {metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/RECORD +7 -7
- {metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/WHEEL +1 -1
- {metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/licenses/LICENSE.txt +0 -0
- {metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/top_level.txt +0 -0
metacountregressor/main.py
CHANGED
@@ -188,19 +188,19 @@ def main(args, **kwargs):
|
|
188
188
|
# some example argument, these are defualt so the following line is just for claritity
|
189
189
|
AMALAN = False
|
190
190
|
if AMALAN:
|
191
|
-
|
191
|
+
print('testing code')
|
192
192
|
args = {'algorithm': 'hs', 'test_percentage': 0.15, 'test_complexity': 6, 'instance_number': 1,
|
193
193
|
'val_percentage': 0.15, 'obj_1': 'bic', '_obj_2': 'RMSE_TEST', "MAX_TIME": 600, 'desicions':a_des, 'is_multi': 1}
|
194
194
|
else:
|
195
195
|
|
196
196
|
args = {'algorithm': 'hs', 'test_percentage': 0, 'test_complexity': 2, 'instance_number': 1,
|
197
197
|
'val_percentage': 0, 'obj_1': 'bic', '_obj_2': 'RMSE_TEST', "MAX_TIME": 600, 'desicions': a_des,
|
198
|
-
'is_multi': False, 'grad_est':
|
198
|
+
'is_multi': False, 'grad_est': True, 'non_sig_prints':True, 'model_types': [[0]], 'run_bootstrap':0, 'r_nu_hess':0, '_transformations': ["no", "no", "nil", 'log']}
|
199
199
|
# Fit the model with metacountregressor
|
200
200
|
# Step 5: Transform the dataset based on the configuration
|
201
201
|
#data_new = helperprocess.transform_dataframe(dataset, config)
|
202
202
|
y = df[['Y']]
|
203
|
-
X = df.drop(columns=['Y'])
|
203
|
+
X = df.drop(columns=['Y', 'ID', 'TRAIN', 'MXMEDSH', 'DECLANES', 'DOUBLE', 'INTECHAG', 'MINRAD', 'PEAKHR', 'AVESNOW', 'FC', 'SINGLE', 'WIDTH', 'MEDWIDTH', 'CURVES', 'URB', 'ADTLANE', 'GRADEBR', 'SLOPE', 'MIMEDSH', 'TANGENT', 'AVEPRE', 'ACCESS'])
|
204
204
|
obj_fun = ObjectiveFunction(X, y, **args)
|
205
205
|
# replace with other metaheuristics if desired
|
206
206
|
results = harmony_search(obj_fun)
|
metacountregressor/solution.py
CHANGED
@@ -128,7 +128,7 @@ class ObjectiveFunction(object):
|
|
128
128
|
self.run_numerical_hessian = kwargs.get('r_nu_hess', False)
|
129
129
|
self.run_bootstrap = kwargs.get('run_bootstrap', False)
|
130
130
|
self.linear_regression = kwargs.get('linear_model', False)
|
131
|
-
self.reg_penalty = kwargs.get('reg_penalty',
|
131
|
+
self.reg_penalty = kwargs.get('reg_penalty', 0)
|
132
132
|
self.power_up_ll = False
|
133
133
|
self.nb_parma = 1
|
134
134
|
self.bic = None
|
@@ -227,6 +227,11 @@ class ObjectiveFunction(object):
|
|
227
227
|
print(
|
228
228
|
'Making a Directory, if you want to stop from storing the files to this directory set argumet: make_directory:False')
|
229
229
|
os.makedirs(self.instance_name)
|
230
|
+
else:
|
231
|
+
if not kwargs.get('delete_directory', False):
|
232
|
+
print('to clear the directory clear')
|
233
|
+
os.d
|
234
|
+
|
230
235
|
else:
|
231
236
|
self.save_state = False
|
232
237
|
if not hasattr(self, '_obj_1'):
|
@@ -435,7 +440,7 @@ class ObjectiveFunction(object):
|
|
435
440
|
self.pvalue_sig_value = kwargs.get('pvalue_sig_value', .1)
|
436
441
|
self.observations = self._x_data.shape[0]
|
437
442
|
self.minimize_scaler = 1 / self.observations # scale the minimization function to the observations
|
438
|
-
|
443
|
+
self.minimize_scaler =1
|
439
444
|
self.batch_size = None
|
440
445
|
# open the file in the write mode
|
441
446
|
self.grab_transforms = 0
|
@@ -574,7 +579,7 @@ class ObjectiveFunction(object):
|
|
574
579
|
# Harmony search parameters
|
575
580
|
self.algorithm = kwargs.get('algorithm', 'hs')
|
576
581
|
self._hms = 20
|
577
|
-
|
582
|
+
# self._max_time = kwargs.get('_max_time', 0.8 * 60 * 60 * 24)
|
578
583
|
self._hmcr = kwargs.get('_hmcr', 0.5)
|
579
584
|
self._par = 0.3
|
580
585
|
self._mpai = 1
|
@@ -724,7 +729,7 @@ class ObjectiveFunction(object):
|
|
724
729
|
# defalt paramaters for hs #TODO unpack into harmony search class
|
725
730
|
self.algorithm = kwargs.get('algorithm', 'hs') # 'sa' 'de' also avialable
|
726
731
|
self._hms = 20
|
727
|
-
self._max_time
|
732
|
+
self._max_time = kwargs.get('_max_time', kwargs.get('MAX_TIME', 0.8 * 60 * 60 * 24))
|
728
733
|
self._hmcr = kwargs.get('_hmcr', .5)
|
729
734
|
self._par = 0.3 #dont think this gets useted
|
730
735
|
self._mpai = 1
|
@@ -2083,6 +2088,7 @@ class ObjectiveFunction(object):
|
|
2083
2088
|
|
2084
2089
|
# Calculate dispersion
|
2085
2090
|
dispersion = residual_deviance / degrees_of_freedom
|
2091
|
+
|
2086
2092
|
except:
|
2087
2093
|
dispersion =1
|
2088
2094
|
|
@@ -3610,68 +3616,7 @@ class ObjectiveFunction(object):
|
|
3610
3616
|
print(exc_type, fname, exc_tb.tb_lineno)
|
3611
3617
|
raise Exception
|
3612
3618
|
|
3613
|
-
def NB_score_lindley(self, params, y, mu, X, Q=0, obs_specific=False):
|
3614
|
-
"""
|
3615
|
-
Calculate the score (gradient) vector of the Negative Binomial-Lindley log-likelihood
|
3616
|
-
Parameters
|
3617
|
-
----------
|
3618
|
-
params : array_like
|
3619
|
-
The parameters of the model
|
3620
|
-
params[-1]: is the dispersion parameter
|
3621
|
-
y: array_like
|
3622
|
-
Vector of true counts N long
|
3623
|
-
mu: array_like
|
3624
|
-
Vector of predicted counts N long
|
3625
|
-
X: array_like
|
3626
|
-
Matrix of explanatory variables len N* (D-1)
|
3627
|
-
a: float or None, optional
|
3628
|
-
Optional parameter, if not None the function calculates the score for the NB-Lindley model with Lindley parameter a,
|
3629
|
-
otherwise, it calculates the score for the Negative Binomial model.
|
3630
|
-
Returns
|
3631
|
-
-------
|
3632
|
-
score : ndarray, 1-D
|
3633
|
-
The score vector of the model, i.e. the first derivative of the
|
3634
|
-
loglikelihood function, evaluated at `params`
|
3635
|
-
"""
|
3636
|
-
|
3637
|
-
alpha = params[-1]
|
3638
|
-
a = params[-2]
|
3639
|
-
a1 = 1 / alpha * mu
|
3640
|
-
prob = a1 / (a1 + mu)
|
3641
|
-
exog = X
|
3642
|
-
|
3643
|
-
# Calculate the score of the Negative Binomial model
|
3644
|
-
dgpart = sc.digamma(y + alpha * mu) - sc.digamma(alpha * mu)
|
3645
|
-
dparams = exog * alpha * (np.log(prob) + dgpart)
|
3646
|
-
dalpha = ((alpha * (y - mu * np.log(prob) -
|
3647
|
-
mu * (dgpart + 1)) -
|
3648
|
-
mu * (np.log(prob) +
|
3649
|
-
dgpart)) /
|
3650
|
-
(alpha ** 2 * (alpha + 1)))
|
3651
|
-
|
3652
|
-
# If a is not None, calculate the score of the NB-Lindley model
|
3653
|
-
if a is not None:
|
3654
|
-
a1 = (1 + a) / (alpha + a + mu)
|
3655
|
-
prob = a1 / (a1 + mu)
|
3656
|
-
dgpart = sc.digamma(y + alpha * mu + a) - \
|
3657
|
-
sc.digamma(alpha * mu + a)
|
3658
|
-
dparams_lindley = exog * (alpha + a) * (np.log(prob) + dgpart)
|
3659
|
-
dalpha_lindley = (((alpha + a) * (y - mu * np.log(prob) -
|
3660
|
-
mu * (dgpart + 1)) -
|
3661
|
-
mu * (np.log(prob) +
|
3662
|
-
dgpart)) /
|
3663
|
-
((alpha + a) ** 2 * (alpha + a + 1)))
|
3664
|
-
|
3665
|
-
if obs_specific is False:
|
3666
|
-
return np.r_[dparams.sum(0), dalpha_lindley.sum(), dalpha.sum()]
|
3667
|
-
# return np.r_[dparams.sum(0) + dparams_lindley.sum(0), dalpha_lindley.sum(), dalpha.sum()]
|
3668
|
-
else:
|
3669
|
-
return np.concatenate((dparams, dalpha_lindley, dalpha), axis=1)
|
3670
|
-
# return np.concatenate((dparams + dparams_lindley, dalpha_lindley, dalpha), axis=1)
|
3671
|
-
# return np.r_[dparams.sum(0), dalpha, dparams_lindley.sum(0), dalpha_lindley]
|
3672
3619
|
|
3673
|
-
else:
|
3674
|
-
return np.r_[dparams.sum(0), dalpha]
|
3675
3620
|
|
3676
3621
|
def PoissonNegLogLikelihood(self, lam, y, penalty=0, X=None):
|
3677
3622
|
"""computers the negative log-likelihood for a poisson random variable"""
|
@@ -3763,71 +3708,7 @@ class ObjectiveFunction(object):
|
|
3763
3708
|
p = np.exp(-ltheta)
|
3764
3709
|
return r, p
|
3765
3710
|
|
3766
|
-
def negative_binomial_lindley_pmf(self, y, r, theta2, mu):
|
3767
|
-
"""
|
3768
|
-
Calculate the probability mass function (PMF) of the Negative Binomial Lindley (NB-L) distribution
|
3769
|
-
for a given count y, mean lambda, dispersion r, shape alpha, and scale beta.
|
3770
|
-
|
3771
|
-
Parameters:
|
3772
|
-
y (int or array-like): The count(s) of interest.
|
3773
|
-
mu (float): The mean parameter of the Negative Binomial distribution.
|
3774
|
-
r (float): The dispersion parameter of the Negative Binomial distribution.
|
3775
|
-
theta2: The shape parameter of the Lindley distribution.
|
3776
|
-
|
3777
|
-
|
3778
|
-
Returns:
|
3779
|
-
pmf (float or ndarray): The probability mass function evaluated at the count(s) y.
|
3780
|
-
"""
|
3781
|
-
|
3782
|
-
theta = self.my_lindley(y, theta2)
|
3783
|
-
mu1 = mu * theta
|
3784
|
-
|
3785
|
-
var = mu1 + 1 / r * mu1 ** 2
|
3786
|
-
p = (var - mu1) / var
|
3787
|
-
numerator = math.comb(r + y.ravel() - 1.0, y.ravel()
|
3788
|
-
) * ((theta ** 2) / (theta + 1))
|
3789
|
-
denominator = 0
|
3790
|
-
for j in range(y + 1):
|
3791
|
-
denominator += math.comb(y, j) * ((-1) ** j) * \
|
3792
|
-
((theta + r + j + 1) / ((theta + r + j) ** 2))
|
3793
|
-
|
3794
|
-
please = numerator / denominator * p ** y * (1 - p) ** r
|
3795
|
-
return please
|
3796
|
-
|
3797
|
-
def negative_binomial_lindley_pmf_gradient(self, y, r, theta2, mu):
|
3798
|
-
"""
|
3799
|
-
Calculate the gradient of the probability mass function (PMF) of the Negative Binomial Lindley (NB-L)
|
3800
|
-
distribution for a given count y, mean lambda, dispersion r, shape alpha, and scale beta.
|
3801
|
-
|
3802
|
-
Parameters:
|
3803
|
-
y (int or array-like): The count(s) of interest.
|
3804
|
-
mu (float): The mean parameter of the Negative Binomial distribution.
|
3805
|
-
r (float): The dispersion parameter of the Negative Binomial distribution.
|
3806
|
-
theta2: The shape parameter of the Lindley distribution.
|
3807
|
-
|
3808
|
-
|
3809
|
-
Returns:
|
3810
|
-
gradient (ndarray): The gradient of the probability mass function evaluated at the count(s) y.
|
3811
|
-
"""
|
3812
3711
|
|
3813
|
-
theta = self.my_lindley(y, theta2)
|
3814
|
-
mu = mu * mu + theta
|
3815
|
-
var = mu + 1 / r * mu ** 2
|
3816
|
-
p = (var - mu) / var
|
3817
|
-
numerator = math.comb(r + y - 1, y) * ((theta ** 2) / (theta + 1))
|
3818
|
-
denominator = 0
|
3819
|
-
for j in range(y + 1):
|
3820
|
-
denominator += math.comb(y, j) * ((-1) ** j) * \
|
3821
|
-
((theta + r + j + 1) / ((theta + r + j) ** 2))
|
3822
|
-
|
3823
|
-
dtheta = numerator * (y * (2 * theta + 1) - theta * (theta + 1)) / denominator ** 2
|
3824
|
-
dmu = (y - mu) * p / (1 - p)
|
3825
|
-
dr = -r ** 2 / var + r / var * (y - r * mu / (1 - p))
|
3826
|
-
dtheta2 = theta * (y * (theta + 1) / (theta + 1 + mu) -
|
3827
|
-
(theta2 + 1) / (theta2 + mu)) / denominator
|
3828
|
-
|
3829
|
-
gradient = np.array([dtheta2, dmu, dr])
|
3830
|
-
return gradient
|
3831
3712
|
|
3832
3713
|
def dnbl(self, x, r, theta):
|
3833
3714
|
|
@@ -4754,6 +4635,43 @@ class ObjectiveFunction(object):
|
|
4754
4635
|
pch[pch == 0] = 0.00001
|
4755
4636
|
return pch
|
4756
4637
|
|
4638
|
+
|
4639
|
+
|
4640
|
+
|
4641
|
+
|
4642
|
+
|
4643
|
+
def compute_gradient_central(self, betas, Xd, y, draws=None, Xf=None, Xr=None, batch_size=None, return_gradient=False,
|
4644
|
+
return_gradient_n=False, dispersion=0, test_set=0, return_EV=False, verbose=0,
|
4645
|
+
corr_list=None, zi_list=None, exog_infl=None, draws_grouped=None, Xgroup=None,
|
4646
|
+
model_nature=None, kwarg=None, **kwargs)->np.ndarray:
|
4647
|
+
# {
|
4648
|
+
params = np.array(betas)
|
4649
|
+
|
4650
|
+
delta = np.ones_like(params) * 1e-5 #
|
4651
|
+
gradient = np.zeros_like(params) # create an array
|
4652
|
+
for i in range(len(params)):
|
4653
|
+
# {
|
4654
|
+
orig = params[i]
|
4655
|
+
params[i] = orig + delta[i]
|
4656
|
+
case_1 = self._loglik_gradient(
|
4657
|
+
params, Xd, y, draws=draws, Xf=Xf, Xr=Xr, batch_size=batch_size, return_gradient=return_gradient,
|
4658
|
+
return_gradient_n=return_gradient_n, dispersion=dispersion, test_set=test_set, return_EV=return_EV,
|
4659
|
+
verbose=verbose, corr_list=corr_list, zi_list=zi_list, exog_infl=exog_infl, draws_grouped=draws_grouped,
|
4660
|
+
Xgroup=Xgroup, model_nature=model_nature, kwarg=kwarg, **kwargs
|
4661
|
+
)
|
4662
|
+
params[i] = orig - delta[i]
|
4663
|
+
case_2 = self._loglik_gradient(
|
4664
|
+
params, Xd, y, draws=draws, Xf=Xf, Xr=Xr, batch_size=batch_size, return_gradient=return_gradient,
|
4665
|
+
return_gradient_n=return_gradient_n, dispersion=dispersion, test_set=test_set, return_EV=return_EV,
|
4666
|
+
verbose=verbose, corr_list=corr_list, zi_list=zi_list, exog_infl=exog_infl, draws_grouped=draws_grouped,
|
4667
|
+
Xgroup=Xgroup, model_nature=model_nature, kwarg=kwarg, **kwargs
|
4668
|
+
)
|
4669
|
+
params[i] = orig # restore value
|
4670
|
+
gradient[i] = (case_1 - case_2) / (2.0 * delta[i])
|
4671
|
+
# }
|
4672
|
+
return gradient
|
4673
|
+
|
4674
|
+
|
4757
4675
|
def gradient_calc_est(self, N, Kf, Kr, Kchol, dispersion, proba_n, eVd, br, brstd, draws_, Xdf, Xdr, y, R, lik,
|
4758
4676
|
alpha=0.5, betas=None, Br=None, panels=None, model_nature=None, br_h=None, br_hs=None):
|
4759
4677
|
|
@@ -5000,7 +4918,7 @@ class ObjectiveFunction(object):
|
|
5000
4918
|
(self._group_Y[key].ravel() - sub_eVd[i].ravel())[:, None] * Xd[key])
|
5001
4919
|
|
5002
4920
|
# todo make dummies with grouped
|
5003
|
-
|
4921
|
+
grad_g = np.concatenate(der_list, axis=0)
|
5004
4922
|
grad_n = np.concatenate(der_n, axis=1)
|
5005
4923
|
grad = grad_n.sum(axis=0)
|
5006
4924
|
# grad = grad_n.sum(axis = 1)
|
@@ -5016,17 +4934,22 @@ class ObjectiveFunction(object):
|
|
5016
4934
|
der = grad_n.sum(axis=0)
|
5017
4935
|
# to do prob product arcross panel
|
5018
4936
|
|
5019
|
-
return np.nan_to_num(der, nan=
|
5020
|
-
posinf=
|
5021
|
-
neginf=-
|
4937
|
+
return np.nan_to_num(der, nan=200, posinf=200, neginf=-200), np.nan_to_num(grad_n, nan=200,
|
4938
|
+
posinf=200,
|
4939
|
+
neginf=-200)
|
5022
4940
|
|
5023
4941
|
if obs_specific:
|
5024
4942
|
grad_n_p = (y - eVd)[:, :, :] * Xd
|
5025
4943
|
grad_n = self._prob_product_across_panels(grad_n_p, self.panel_info)
|
5026
4944
|
der = grad_n.sum(axis=0)
|
5027
|
-
return np.nan_to_num(der, nan=
|
4945
|
+
return np.nan_to_num(der, nan=200, posinf=200, neginf=-200)
|
5028
4946
|
else:
|
4947
|
+
n, p, k = Xd.shape
|
5029
4948
|
grad_n_p = (y - eVd)[:, :, :] * Xd
|
4949
|
+
#residual = (y - eVd).squeeze(axis = -1)
|
4950
|
+
# grad = np.zeros(k)
|
4951
|
+
#for j in range(p):
|
4952
|
+
# grad += Xd[:, j, :].T @ residual[:, j] # Shape: (k,)
|
5030
4953
|
grad_n = self._prob_product_across_panels(grad_n_p, self.panel_info)
|
5031
4954
|
der = grad_n.sum(axis=0)
|
5032
4955
|
|
@@ -5046,11 +4969,7 @@ class ObjectiveFunction(object):
|
|
5046
4969
|
y, eVd, Xd, obs_specific=True)
|
5047
4970
|
return np.nan_to_num(der, nan=200, posinf=200, neginf=-200), np.nan_to_num(grad_n, nan=140, posinf=140,
|
5048
4971
|
neginf=-140)
|
5049
|
-
elif dispersion == 3:
|
5050
4972
|
|
5051
|
-
der, grad_n = self.poisson_lindley_gradient(betas, Xd, y)
|
5052
|
-
|
5053
|
-
return der, grad_n
|
5054
4973
|
|
5055
4974
|
|
5056
4975
|
elif dispersion == 'poisson_lognormal':
|
@@ -5058,7 +4977,7 @@ class ObjectiveFunction(object):
|
|
5058
4977
|
der, grad_n = self.poisson_lognormal_glm_score(betas, y, Xd, sig)
|
5059
4978
|
return der, grad_n
|
5060
4979
|
|
5061
|
-
return np.nan_to_num(der, nan=
|
4980
|
+
return np.nan_to_num(der, nan=200, posinf=200, neginf=-200)
|
5062
4981
|
|
5063
4982
|
def prob_obs_draws(self, eVi, y, disp, dispersion=0.0, disp2=0):
|
5064
4983
|
|
@@ -5072,12 +4991,10 @@ class ObjectiveFunction(object):
|
|
5072
4991
|
|
5073
4992
|
proba_r = self.general_poisson_pmf(eVi, y, disp)
|
5074
4993
|
|
5075
|
-
|
5076
|
-
proba_r = self.poisson_lindley_pmf(eVi, disp2, y)
|
4994
|
+
|
5077
4995
|
# proba_r = self.dpoisl(y, eVi)
|
5078
4996
|
|
5079
|
-
|
5080
|
-
proba_r = self.dnegbimonli(y, eVi, disp)
|
4997
|
+
|
5081
4998
|
|
5082
4999
|
else:
|
5083
5000
|
raise Exception
|
@@ -5359,13 +5276,13 @@ class ObjectiveFunction(object):
|
|
5359
5276
|
if self.is_dispersion(dispersion):
|
5360
5277
|
penalty, main_disper = self._penalty_dispersion(dispersion, main_disper, eVd, y, penalty,
|
5361
5278
|
model_nature)
|
5362
|
-
b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5363
|
-
penalty = self.regularise_l2(betas) + self.regularise_l1(betas)
|
5364
|
-
penalty = self.custom_penalty(betas, penalty)
|
5279
|
+
#b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5280
|
+
#penalty = self.regularise_l2(betas) + self.regularise_l1(betas)
|
5281
|
+
#penalty = self.custom_penalty(betas, penalty)
|
5365
5282
|
|
5366
5283
|
betas[-1] = main_disper
|
5367
5284
|
|
5368
|
-
b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5285
|
+
#b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5369
5286
|
penalty = self.regularise_l2(betas) + self.regularise_l1(betas)
|
5370
5287
|
penalty = self.custom_penalty(betas, penalty)
|
5371
5288
|
|
@@ -5390,7 +5307,7 @@ class ObjectiveFunction(object):
|
|
5390
5307
|
loglik += 2*loglik
|
5391
5308
|
print('am i powering up')
|
5392
5309
|
|
5393
|
-
b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5310
|
+
#b_pen = self.custom_betas_to_penalise(betas, dispersion)
|
5394
5311
|
penalty = self.regularise_l2(betas) + self.regularise_l1(betas)
|
5395
5312
|
penalty = self.custom_penalty(betas, penalty)
|
5396
5313
|
|
@@ -5404,13 +5321,26 @@ class ObjectiveFunction(object):
|
|
5404
5321
|
der, grad_n = self.simple_score_grad(
|
5405
5322
|
betas, y, eVd, Xd, dispersion, both=True)
|
5406
5323
|
#return (-loglik + penalty, -der, grad_n)*self.minimize_scaler
|
5407
|
-
scaled_tuple = tuple(x * self.minimize_scaler for x in (-loglik + penalty,
|
5324
|
+
scaled_tuple = tuple(x * self.minimize_scaler for x in (-loglik + penalty, der.ravel(), grad_n))
|
5325
|
+
|
5408
5326
|
return scaled_tuple
|
5409
5327
|
else:
|
5410
5328
|
der = self.simple_score_grad(
|
5411
5329
|
betas, y, eVd, Xd, dispersion, both=False)
|
5330
|
+
#arguments = locals() # Capture all current arguments
|
5331
|
+
#arguments["return_gradient"] = False # Change `dispersion` to 1
|
5332
|
+
#del arguments["self"] # Remove `self` from arguments (not needed in the call)
|
5333
|
+
'''
|
5334
|
+
der_alt = self.compute_gradient_central(betas, Xd, y, draws=draws, Xf=Xf, Xr=Xr, batch_size=batch_size, return_gradient=False,
|
5335
|
+
return_gradient_n=False, dispersion=dispersion, test_set=test_set, return_EV=return_EV, verbose=verbose, corr_list=corr_list,
|
5336
|
+
zi_list=zi_list, exog_infl=exog_infl, draws_grouped=draws_grouped, Xgroup=Xgroup, model_nature=model_nature, kwarg=kwarg,
|
5337
|
+
**kwargs)
|
5338
|
+
'''
|
5412
5339
|
scaled_tuple = tuple(
|
5413
5340
|
x * self.minimize_scaler for x in (-loglik + penalty, -der.ravel()))
|
5341
|
+
|
5342
|
+
|
5343
|
+
|
5414
5344
|
return scaled_tuple
|
5415
5345
|
#return (-loglik + penalty, -der.ravel())*self.minimize_scaler
|
5416
5346
|
else:
|
@@ -5752,9 +5682,7 @@ class ObjectiveFunction(object):
|
|
5752
5682
|
traceback.print_exc()
|
5753
5683
|
print(e, 'where loglik')
|
5754
5684
|
|
5755
|
-
|
5756
|
-
r'Takes the logliklihood function and tranforms it to a more handed minimization function'
|
5757
|
-
return loglike/self.n_obs
|
5685
|
+
|
5758
5686
|
def print_chol_mat(self, betas):
|
5759
5687
|
print(self.chol_mat)
|
5760
5688
|
self.get_br_and_bstd(betas)
|
@@ -6623,6 +6551,12 @@ class ObjectiveFunction(object):
|
|
6623
6551
|
tol=tol.get('ftol', 1e-6), # Use 'ftol' as the default tolerance
|
6624
6552
|
options=options
|
6625
6553
|
)
|
6554
|
+
|
6555
|
+
#print(result.summary())
|
6556
|
+
|
6557
|
+
|
6558
|
+
#i want to compare this to stats model.s
|
6559
|
+
|
6626
6560
|
if optimization_result.message == 'NaN result encountered.':
|
6627
6561
|
optimization_result = self._minimize(self._loglik_gradient,
|
6628
6562
|
initial_params,
|
@@ -6930,7 +6864,7 @@ class ObjectiveFunction(object):
|
|
6930
6864
|
total = sum(self.get_num_params()) + dispersion_param
|
6931
6865
|
return total
|
6932
6866
|
|
6933
|
-
def _build_initial_params(self, num_coefficients, dispersion):
|
6867
|
+
def _build_initial_params(self, num_coefficients, dispersion, XX, y):
|
6934
6868
|
"""
|
6935
6869
|
Build the initial parameter array for optimization.
|
6936
6870
|
|
@@ -6942,13 +6876,40 @@ class ObjectiveFunction(object):
|
|
6942
6876
|
Initial parameter array.
|
6943
6877
|
"""
|
6944
6878
|
# Generate random initial coefficients
|
6945
|
-
|
6879
|
+
# call in statsmodels
|
6880
|
+
try:
|
6881
|
+
if dispersion ==0:
|
6882
|
+
model = sm.GLM(y.squeeze(axis=-1), XX.squeeze(axis=1), family=sm.families.Poisson())
|
6883
|
+
else:
|
6884
|
+
model = sm.NegativeBinomial(y.squeeze(axis=-1), XX.squeeze(axis=1))
|
6885
|
+
result = model.fit()
|
6886
|
+
initial_params = result.params # then exten to num_coefficients
|
6887
|
+
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
|
+
])
|
6892
|
+
|
6893
|
+
else:
|
6894
|
+
initial_params = np.random.uniform(-0.01, 0.3, size=num_coefficients)
|
6895
|
+
except:
|
6896
|
+
print('pre fit failed, continie')
|
6897
|
+
initial_params = np.random.uniform(-0.01, 0.01, size=num_coefficients)
|
6898
|
+
|
6899
|
+
|
6946
6900
|
parma_sum = sum(self.get_num_params()[:2])
|
6947
6901
|
|
6948
6902
|
|
6949
6903
|
initial_params[parma_sum:-dispersion] =0.0001
|
6950
6904
|
|
6951
6905
|
# Add dispersion parameter if applicable
|
6906
|
+
if dispersion == 1:
|
6907
|
+
print('checking for dispersion')
|
6908
|
+
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])
|
6911
|
+
|
6912
|
+
|
6952
6913
|
if dispersion > 0:
|
6953
6914
|
initial_params[-1] = 0.0
|
6954
6915
|
#initial_params[0] =3
|
@@ -6972,7 +6933,7 @@ class ObjectiveFunction(object):
|
|
6972
6933
|
try:
|
6973
6934
|
dispersion = mod.get('dispersion', dispersion)
|
6974
6935
|
# Preprocessing
|
6975
|
-
tol = {'ftol': 1e-
|
6936
|
+
tol = {'ftol': 1e-10, 'gtol': 1e-6, 'xtol': 1e-7}
|
6976
6937
|
y, X, Xr, XG, XH = mod.get('y'), mod.get('X'), mod.get('Xr'), mod.get('XG'), mod.get('XH')
|
6977
6938
|
|
6978
6939
|
# Validate input data
|
@@ -6987,7 +6948,7 @@ class ObjectiveFunction(object):
|
|
6987
6948
|
num_coefficients = self._calculate_num_coefficients(mod, dispersion)
|
6988
6949
|
|
6989
6950
|
# Build initial parameters and bounds
|
6990
|
-
initial_params = self._build_initial_params(num_coefficients, dispersion)
|
6951
|
+
initial_params = self._build_initial_params(num_coefficients, dispersion, XX, y)
|
6991
6952
|
bounds = self._set_bounds(initial_params, dispersion)
|
6992
6953
|
|
6993
6954
|
|
@@ -1,9 +1,9 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: metacountregressor
|
3
|
-
Version: 1.0.
|
3
|
+
Version: 1.0.18
|
4
4
|
Summary: Extensive Testing for Estimation of Data Count Models
|
5
5
|
Home-page: https://github.com/zahern/CountDataEstimation
|
6
|
-
Author: Zeke Ahern
|
6
|
+
Author: Zeke Ahern, Alexander Paz
|
7
7
|
Author-email: z.ahern@qut.edu.au
|
8
8
|
License: MIT
|
9
9
|
Requires-Python: >=3.10
|
@@ -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=
|
7
|
+
metacountregressor/main.py,sha256=v1W9XuXkvOr5SXHdOBuQ3KUSFW6mpGR3ydO0DqAjxqY,24396
|
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=
|
14
|
+
metacountregressor/solution.py,sha256=ufrKyd1MOVi9SnWmvNKh9h913UAPeLNjJuyBEEvUsXk,346926
|
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
|
-
metacountregressor-1.0.
|
19
|
-
metacountregressor-1.0.
|
20
|
-
metacountregressor-1.0.
|
21
|
-
metacountregressor-1.0.
|
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,,
|
{metacountregressor-1.0.12.dist-info → metacountregressor-1.0.18.dist-info}/licenses/LICENSE.txt
RENAMED
File without changes
|
File without changes
|