-
Notifications
You must be signed in to change notification settings - Fork 2.8k
/
ordinal_model.py
339 lines (269 loc) · 11.5 KB
/
ordinal_model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 22 20:24:42 2015
Author: Josef Perktold
License: BSD-3
"""
import numpy as np
import pandas as pd
from pandas.api.types import CategoricalDtype
from scipy import stats
from statsmodels.base.model import GenericLikelihoodModel, \
GenericLikelihoodModelResults
from statsmodels.compat.pandas import Appender
class OrderedModel(GenericLikelihoodModel):
"""Ordinal Model based on logistic or normal distribution
The parameterization corresponds to the proportional odds model.
The mode assumes that the endogenous variable is ordered but that the
labels have no numeric interpretation besides the ordering.
The model is based on a latent linear variable, where we observe only
y_latent = X beta + u
The observed variable is defined by the interval
y = {0 if y_latent <= cut_0
1 of cut_0 < y_latent <= cut_1
...
K if cut_K < y_latent
The probability of observing y=k conditional on the explanatory variables
X is given by
prob(y = k | x) = Prob(cut_k < y_latent <= cut_k+1)
= Prob(cut_k - x beta < u <= cut_k+1 - x beta
= F(cut_k+1 - x beta) - F(cut_k - x beta)
Where F is the cumulative distribution of u which is either the normal
or the logistic distribution, but can be set to any other continuous
distribution. We use standardized distributions to avoid identifiability
problems.
Parameters
----------
endog : array_like
endogenous or dependent ordered categorical variable with k levels.
Labels or values of endog will internally transformed to consecutive
integers, 0, 1, 2, ...
pd.Series with Categorical as dtype should be preferred as it gives
the order relation between the levels.
exog : array_like
exogenous explanatory variables. This should not include an intercept.
(TODO: verify)
pd.DataFrame are also accepted.
distr : string 'probit' or 'logit', or a distribution instance
The default is currently 'probit' which uses the normal distribution
and corresponds to an ordered Probit model. The distribution is
assumed to have the main methods of scipy.stats distributions, mainly
cdf, pdf and ppf. The inverse cdf, ppf, is only use to calculate
starting values.
Status: initial version, subclasses `GenericLikelihoodModel`
"""
def __init__(self, endog, exog, offset=None, distr='probit', **kwds):
if distr == 'probit':
self.distr = stats.norm
elif distr == 'logit':
self.distr = stats.logistic
else:
self.distr = distr
if offset is not None:
offset = np.asarray(offset)
# TODO: check if super can handle offset
self.offset = offset
endog, labels, is_pandas = self._check_inputs(endog, exog)
super(OrderedModel, self).__init__(endog, exog, **kwds)
if not is_pandas:
unique, index = np.unique(self.endog, return_inverse=True)
self.endog = index
labels = unique
self.labels = labels
self.k_levels = len(labels)
if self.exog is not None:
self.nobs, self.k_vars = self.exog.shape
else: # no exog in model
self.nobs, self.k_vars = self.endog.shape[0], 0
threshold_names = [str(x) + '/' + str(y)
for x, y in zip(labels[:-1], labels[1:])]
# from GenericLikelihoodModel.fit
if self.exog is not None:
self.exog_names.extend(threshold_names)
else:
self.data.xnames = threshold_names
self.results_class = OrderedResults
def _check_inputs(self, endog, exog):
"""
checks if self.distrib is legal and does the Pandas
support for endog and exog. Also retrieves columns & categories
names for .summary() of the results class.
"""
if not isinstance(self.distr, stats.rv_continuous):
msg = (
f"{self.distr.name} must be a scipy.stats distribution."
)
raise ValueError(msg)
labels = None
is_pandas = False
if isinstance(endog, pd.Series):
if isinstance(endog.dtypes, CategoricalDtype):
if not endog.dtype.ordered:
import warnings
warnings.warn("the endog has ordered == False, "
"risk of capturing a wrong order for the "
"categories. ordered == True preferred.",
Warning)
endog_name = endog.name
labels = endog.values.categories
endog = endog.cat.codes
if endog.min() == -1: # means there is a missing value
raise ValueError("missing values in categorical endog are "
"not supported")
endog.name = endog_name
is_pandas = True
else:
msg = ("If endog is a pandas.Series, "
"it must be of CategoricalDtype.")
raise ValueError(msg)
return endog, labels, is_pandas
def cdf(self, x):
"""cdf evaluated at x
"""
return self.distr.cdf(x)
def pdf(self, x):
"""pdf evaluated at x
"""
return self.distr.pdf(x)
def prob(self, low, upp):
"""interval probability
"""
return np.maximum(self.cdf(upp) - self.cdf(low), 0)
def transform_threshold_params(self, params):
"""transformation of the parameters in the optimization
Parameters
----------
params : nd_array
contains (exog_coef, transformed_thresholds) where exog_coef are
the coefficient for the explanatory variables in the linear term,
transformed threshold or cutoff points. The first, lowest threshold
is unchanged, all other thresholds are in terms of exponentiated
increments
Returns
-------
thresh : nd_array
thresh are the thresholds or cutoff constants for the intervals.
"""
th_params = params[-(self.k_levels - 1):]
thresh = np.concatenate((th_params[:1],
np.exp(th_params[1:]))).cumsum()
thresh = np.concatenate(([-np.inf], thresh, [np.inf]))
return thresh
def transform_reverse_threshold_params(self, params):
"""obtain transformed thresholds from original thresholds, cutoff
constants.
"""
start_ppf = params
thresh_params = np.concatenate((start_ppf[:1],
np.log(np.diff(start_ppf[:-1]))))
return thresh_params
def predict(self, params, exog=None):
"""predicted probabilities for each level of the ordinal endog.
"""
if exog is None:
exog = self.exog
# structure of params = [beta, constants_or_thresholds]
# explicit in several steps to avoid bugs
th_params = params[-(self.k_levels - 1):]
thresh = np.concatenate((th_params[:1],
np.exp(th_params[1:]))).cumsum()
thresh = np.concatenate(([-np.inf], thresh, [np.inf]))
xb = exog.dot(params[:-(self.k_levels - 1)])[:, None]
low = thresh[:-1] - xb
upp = thresh[1:] - xb
prob = self.prob(low, upp)
return prob
def _linpred(self, params, exog=None, offset=None):
"""linear prediction of latent variable `x b`
currently only for exog from estimation sample (in-sample)
"""
if exog is None:
exog = self.exog
if offset is None:
offset = self.offset
if exog is not None:
linpred = self.exog.dot(params[:-(self.k_levels - 1)])
else: # means self.exog is also None
linpred = np.zeros(self.nobs)
if offset is not None:
linpred += offset
return linpred
def _bounds(self, params):
thresh = self.transform_threshold_params(params)
thresh_i_low = thresh[self.endog]
thresh_i_upp = thresh[self.endog + 1]
xb = self._linpred(params)
low = thresh_i_low - xb
upp = thresh_i_upp - xb
return low, upp
def loglike(self, params):
thresh = self.transform_threshold_params(params)
thresh_i_low = thresh[self.endog]
thresh_i_upp = thresh[self.endog + 1]
xb = self._linpred(params)
low = thresh_i_low - xb
upp = thresh_i_upp - xb
prob = self.prob(low, upp)
return np.log(prob + 1e-20).sum()
def loglikeobs(self, params):
low, upp = self._bounds(params)
prob = self.prob(low, upp)
return np.log(prob + 1e-20)
def score_obs_(self, params):
"""score, first derivative of loglike for each observations
This currently only implements the derivative with respect to the
exog parameters, but not with respect to threshold-constant parameters.
"""
low, upp = self._bounds(params)
prob = self.prob(low, upp)
pdf_upp = self.pdf(upp)
pdf_low = self.pdf(low)
# TODO the following doesn't work yet because of the incremental exp
# parameterization. The following was written base on Greene for the
# simple non-incremental parameterization.
# k = self.k_levels - 1
# idx = self.endog
# score_factor = np.zeros((self.nobs, k + 1 + 2)) #+2 avoids idx bounds
#
# rows = np.arange(self.nobs)
# shift = 1
# score_factor[rows, shift + idx-1] = -pdf_low
# score_factor[rows, shift + idx] = pdf_upp
# score_factor[:, 0] = pdf_upp - pdf_low
score_factor = (pdf_upp - pdf_low)[:, None]
score_factor /= prob[:, None]
so = np.column_stack((-score_factor[:, :1] * self.exog,
score_factor[:, 1:]))
return so
@property
def start_params(self):
# start params based on model without exog
freq = np.bincount(self.endog) / len(self.endog)
start_ppf = self.distr.ppf(np.clip(freq.cumsum(), 0, 1))
start_threshold = self.transform_reverse_threshold_params(start_ppf)
start_params = np.concatenate((np.zeros(self.k_vars), start_threshold))
return start_params
@Appender(GenericLikelihoodModel.fit.__doc__)
def fit(self, start_params=None, method='nm', maxiter=500, full_output=1,
disp=1, callback=None, retall=0, **kwargs):
fit_method = super(OrderedModel, self).fit
mlefit = fit_method(start_params=start_params,
method=method, maxiter=maxiter,
full_output=full_output,
disp=disp, callback=callback, **kwargs)
# use the proper result class
ordmlefit = OrderedResults(self, mlefit)
return ordmlefit
class OrderedResults(GenericLikelihoodModelResults):
def pred_table(self):
"""prediction table
returns pandas DataFrame
"""
# todo: add category labels
categories = np.arange(self.model.k_levels)
observed = pd.Categorical(self.model.endog,
categories=categories, ordered=True)
predicted = pd.Categorical(self.predict().argmax(1),
categories=categories, ordered=True)
table = pd.crosstab(observed, predicted, margins=True, dropna=False)
return table