Skip to content

Commit d20aceb

Browse files
committed
black format repo
1 parent 443abac commit d20aceb

File tree

12 files changed

+5187
-1216
lines changed

12 files changed

+5187
-1216
lines changed

notebooks/Binomial_GLM.ipynb

Lines changed: 656 additions & 53 deletions
Large diffs are not rendered by default.

notebooks/Gaussian_GLM.ipynb

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,14 @@
1717
"metadata": {},
1818
"outputs": [],
1919
"source": [
20-
"#Load sample dataset - columbus dataset \n",
21-
"db = ps.open(ps.get_path('columbus.dbf'),'r')\n",
20+
"# Load sample dataset - columbus dataset\n",
21+
"db = ps.open(ps.get_path(\"columbus.dbf\"), \"r\")\n",
2222
"\n",
23-
"#Set dependent variable\n",
23+
"# Set dependent variable\n",
2424
"y = np.array(db.by_col(\"HOVAL\"))\n",
25-
"y = np.reshape(y, (49,1))\n",
25+
"y = np.reshape(y, (49, 1))\n",
2626
"\n",
27-
"#Set indepdent varibLES\n",
27+
"# Set indepdent varibLES\n",
2828
"X = []\n",
2929
"X.append(db.by_col(\"INC\"))\n",
3030
"X.append(db.by_col(\"CRIME\"))\n",
@@ -37,12 +37,14 @@
3737
"metadata": {},
3838
"outputs": [],
3939
"source": [
40-
"#Estimate Gaussian GLM\n",
40+
"# Estimate Gaussian GLM\n",
4141
"\n",
42-
"#First instantiate a GLM model object\n",
43-
"model = GLM(y, X) #Gaussian is the default family parameter so it doesn't need to be set\n",
42+
"# First instantiate a GLM model object\n",
43+
"model = GLM(\n",
44+
" y, X\n",
45+
") # Gaussian is the default family parameter so it doesn't need to be set\n",
4446
"\n",
45-
"#Then use the fit method to estimate coefficients and compute diagnostics\n",
47+
"# Then use the fit method to estimate coefficients and compute diagnostics\n",
4648
"results = model.fit()"
4749
]
4850
},
@@ -60,7 +62,7 @@
6062
}
6163
],
6264
"source": [
63-
"#Estimated prameters, intercept is always the first column on the left\n",
65+
"# Estimated prameters, intercept is always the first column on the left\n",
6466
"print(results.params)"
6567
]
6668
},
@@ -78,7 +80,7 @@
7880
}
7981
],
8082
"source": [
81-
"#Parameter standard errors\n",
83+
"# Parameter standard errors\n",
8284
"print(results.bse)"
8385
]
8486
},
@@ -96,7 +98,7 @@
9698
}
9799
],
98100
"source": [
99-
"#Parameter t-values\n",
101+
"# Parameter t-values\n",
100102
"print(results.tvalues)"
101103
]
102104
},
@@ -114,7 +116,7 @@
114116
}
115117
],
116118
"source": [
117-
"#Model AIC\n",
119+
"# Model AIC\n",
118120
"print(results.aic)"
119121
]
120122
}

notebooks/Poisson_GLM.ipynb

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,16 @@
1818
"metadata": {},
1919
"outputs": [],
2020
"source": [
21-
"#Load sample dataset - columbus dataset \n",
22-
"db = ps.open(ps.get_path('columbus.dbf'),'r')\n",
21+
"# Load sample dataset - columbus dataset\n",
22+
"db = ps.open(ps.get_path(\"columbus.dbf\"), \"r\")\n",
2323
"\n",
24-
"#Set dependent variable\n",
24+
"# Set dependent variable\n",
2525
"y = np.array(db.by_col(\"HOVAL\"))\n",
26-
"y = np.reshape(y, (49,1))\n",
27-
"#Round dependent variable and convert to integer for the example since Poisson is for discrete data\n",
26+
"y = np.reshape(y, (49, 1))\n",
27+
"# Round dependent variable and convert to integer for the example since Poisson is for discrete data\n",
2828
"y = np.round(y).astype(int)\n",
2929
"\n",
30-
"#Set indepdent varibLES\n",
30+
"# Set indepdent varibLES\n",
3131
"X = []\n",
3232
"X.append(db.by_col(\"INC\"))\n",
3333
"X.append(db.by_col(\"CRIME\"))\n",
@@ -40,12 +40,14 @@
4040
"metadata": {},
4141
"outputs": [],
4242
"source": [
43-
"#Estimate Poisson GLM\n",
43+
"# Estimate Poisson GLM\n",
4444
"\n",
45-
"#First instantiate a GLM model object\n",
46-
"model = GLM(y, X, family=Poisson()) #Set family to Poisson family object for Poisson GLM\n",
45+
"# First instantiate a GLM model object\n",
46+
"model = GLM(\n",
47+
" y, X, family=Poisson()\n",
48+
") # Set family to Poisson family object for Poisson GLM\n",
4749
"\n",
48-
"#Then use the fit method to estimate coefficients and compute diagnostics\n",
50+
"# Then use the fit method to estimate coefficients and compute diagnostics\n",
4951
"results = model.fit()"
5052
]
5153
},
@@ -63,7 +65,7 @@
6365
}
6466
],
6567
"source": [
66-
"#Estimated prameters, intercept is always the first column on the left\n",
68+
"# Estimated prameters, intercept is always the first column on the left\n",
6769
"print(results.params)"
6870
]
6971
},
@@ -81,7 +83,7 @@
8183
}
8284
],
8385
"source": [
84-
"#Parameter standard errors\n",
86+
"# Parameter standard errors\n",
8587
"print(results.bse)"
8688
]
8789
},
@@ -99,7 +101,7 @@
99101
}
100102
],
101103
"source": [
102-
"#Parameter t-values\n",
104+
"# Parameter t-values\n",
103105
"print(results.tvalues)"
104106
]
105107
},
@@ -117,7 +119,7 @@
117119
}
118120
],
119121
"source": [
120-
"#Model AIC\n",
122+
"# Model AIC\n",
121123
"print(results.aic)"
122124
]
123125
}

spglm/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
__version__ = '1.0.8'
1+
__version__ = "1.0.8"
22

33
from . import glm
44
from . import family

spglm/base.py

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
from __future__ import print_function
32
import numpy as np
43
from scipy import stats
@@ -24,9 +23,10 @@ def __init__(self, model, params, **kwd):
2423
def initialize(self, model, params, **kwd):
2524
self.params = params
2625
self.model = model
27-
if hasattr(model, 'k_constant'):
26+
if hasattr(model, "k_constant"):
2827
self.k_constant = model.k_constant
2928

29+
3030
# TODO: public method?
3131

3232

@@ -176,61 +176,69 @@ class LikelihoodModelResults(Results):
176176
True: converged. False: did not converge.
177177
allvecs : list
178178
Results at each iteration.
179-
"""
179+
"""
180180

181181
# by default we use normal distribution
182182
# can be overwritten by instances or subclasses
183183
use_t = False
184184

185-
def __init__(self, model, params, normalized_cov_params=None, scale=1.,
186-
**kwargs):
185+
def __init__(self, model, params, normalized_cov_params=None, scale=1.0, **kwargs):
187186
super(LikelihoodModelResults, self).__init__(model, params)
188187
self.normalized_cov_params = normalized_cov_params
189188
self.scale = scale
190189

191190
# robust covariance
192191
# We put cov_type in kwargs so subclasses can decide in fit whether to
193192
# use this generic implementation
194-
if 'use_t' in kwargs:
195-
use_t = kwargs['use_t']
193+
if "use_t" in kwargs:
194+
use_t = kwargs["use_t"]
196195
if use_t is not None:
197196
self.use_t = use_t
198-
if 'cov_type' in kwargs:
199-
cov_type = kwargs.get('cov_type', 'nonrobust')
200-
cov_kwds = kwargs.get('cov_kwds', {})
201-
202-
if cov_type == 'nonrobust':
203-
self.cov_type = 'nonrobust'
204-
self.cov_kwds = {'description': 'Standard Errors assume that the ' +
205-
'covariance matrix of the errors is correctly ' +
206-
'specified.'}
197+
if "cov_type" in kwargs:
198+
cov_type = kwargs.get("cov_type", "nonrobust")
199+
cov_kwds = kwargs.get("cov_kwds", {})
200+
201+
if cov_type == "nonrobust":
202+
self.cov_type = "nonrobust"
203+
self.cov_kwds = {
204+
"description": "Standard Errors assume that the "
205+
+ "covariance matrix of the errors is correctly "
206+
+ "specified."
207+
}
207208
else:
208209
from statsmodels.base.covtype import get_robustcov_results
210+
209211
if cov_kwds is None:
210212
cov_kwds = {}
211213
use_t = self.use_t
212214
# TODO: we shouldn't need use_t in get_robustcov_results
213-
get_robustcov_results(self, cov_type=cov_type, use_self=True,
214-
use_t=use_t, **cov_kwds)
215+
get_robustcov_results(
216+
self, cov_type=cov_type, use_self=True, use_t=use_t, **cov_kwds
217+
)
215218

216219
def normalized_cov_params(self):
217220
raise NotImplementedError
218221

219-
def _get_robustcov_results(self, cov_type='nonrobust', use_self=True,
220-
use_t=None, **cov_kwds):
222+
def _get_robustcov_results(
223+
self, cov_type="nonrobust", use_self=True, use_t=None, **cov_kwds
224+
):
221225
from statsmodels.base.covtype import get_robustcov_results
226+
222227
if cov_kwds is None:
223228
cov_kwds = {}
224229

225-
if cov_type == 'nonrobust':
226-
self.cov_type = 'nonrobust'
227-
self.cov_kwds = {'description': 'Standard Errors assume that the ' +
228-
'covariance matrix of the errors is correctly ' +
229-
'specified.'}
230+
if cov_type == "nonrobust":
231+
self.cov_type = "nonrobust"
232+
self.cov_kwds = {
233+
"description": "Standard Errors assume that the "
234+
+ "covariance matrix of the errors is correctly "
235+
+ "specified."
236+
}
230237
else:
231238
# TODO: we shouldn't need use_t in get_robustcov_results
232-
get_robustcov_results(self, cov_type=cov_type, use_self=True,
233-
use_t=use_t, **cov_kwds)
239+
get_robustcov_results(
240+
self, cov_type=cov_type, use_self=True, use_t=use_t, **cov_kwds
241+
)
234242

235243
@cache_readonly
236244
def llf(self):
@@ -250,13 +258,14 @@ def tvalues(self):
250258
@cache_readonly
251259
def pvalues(self):
252260
if self.use_t:
253-
df_resid = getattr(self, 'df_resid_inference', self.df_resid)
261+
df_resid = getattr(self, "df_resid_inference", self.df_resid)
254262
return stats.t.sf(np.abs(self.tvalues), df_resid) * 2
255263
else:
256264
return stats.norm.sf(np.abs(self.tvalues)) * 2
257265

258-
def cov_params(self, r_matrix=None, column=None, scale=None, cov_p=None,
259-
other=None):
266+
def cov_params(
267+
self, r_matrix=None, column=None, scale=None, cov_p=None, other=None
268+
):
260269
"""
261270
Returns the variance/covariance matrix.
262271
The variance/covariance matrix can be of a linear contrast
@@ -293,24 +302,30 @@ def cov_params(self, r_matrix=None, column=None, scale=None, cov_p=None,
293302
OR
294303
``(scale) * (X.T X)^(-1)[column][:,column]`` if column is 1d
295304
"""
296-
if (hasattr(self, 'mle_settings') and
297-
self.mle_settings['optimizer'] in ['l1', 'l1_cvxopt_cp']):
305+
if hasattr(self, "mle_settings") and self.mle_settings["optimizer"] in [
306+
"l1",
307+
"l1_cvxopt_cp",
308+
]:
298309
dot_fun = nan_dot
299310
else:
300311
dot_fun = np.dot
301312

302-
if (cov_p is None and self.normalized_cov_params is None and
303-
not hasattr(self, 'cov_params_default')):
304-
raise ValueError('need covariance of parameters for computing '
305-
'(unnormalized) covariances')
313+
if (
314+
cov_p is None
315+
and self.normalized_cov_params is None
316+
and not hasattr(self, "cov_params_default")
317+
):
318+
raise ValueError(
319+
"need covariance of parameters for computing "
320+
"(unnormalized) covariances"
321+
)
306322
if column is not None and (r_matrix is not None or other is not None):
307-
raise ValueError('Column should be specified without other '
308-
'arguments.')
323+
raise ValueError("Column should be specified without other " "arguments.")
309324
if other is not None and r_matrix is None:
310-
raise ValueError('other can only be specified with r_matrix')
325+
raise ValueError("other can only be specified with r_matrix")
311326

312327
if cov_p is None:
313-
if hasattr(self, 'cov_params_default'):
328+
if hasattr(self, "cov_params_default"):
314329
cov_p = self.cov_params_default
315330
else:
316331
if scale is None:
@@ -337,7 +352,7 @@ def cov_params(self, r_matrix=None, column=None, scale=None, cov_p=None,
337352
else: # if r_matrix is None and column is None:
338353
return cov_p
339354

340-
def conf_int(self, alpha=.05, cols=None, method='default'):
355+
def conf_int(self, alpha=0.05, cols=None, method="default"):
341356
"""
342357
Returns the confidence interval of the fitted parameters.
343358
@@ -396,7 +411,7 @@ def conf_int(self, alpha=.05, cols=None, method='default'):
396411

397412
if self.use_t:
398413
dist = stats.t
399-
df_resid = getattr(self, 'df_resid_inference', self.df_resid)
414+
df_resid = getattr(self, "df_resid_inference", self.df_resid)
400415
q = dist.ppf(1 - alpha / 2, df_resid)
401416
else:
402417
dist = stats.norm

0 commit comments

Comments
 (0)