@@ -79,7 +79,6 @@ class IdentityLink(Link):
79
79
def __eq__ (self , other ): # noqa D
80
80
return isinstance (other , self .__class__ )
81
81
82
- # unnecessary type hint for consistency with other methods
83
82
def link (self , mu ):
84
83
"""Return mu (identity link).
85
84
@@ -102,7 +101,6 @@ def derivative(self, mu):
102
101
"""
103
102
return 1.0 if np .isscalar (mu ) else np .ones_like (mu )
104
103
105
- # unnecessary type hint for consistency with other methods
106
104
def inverse (self , lin_pred ):
107
105
"""Compute the inverse link function ``h(lin_pred)``.
108
106
@@ -156,7 +154,7 @@ def link(self, mu):
156
154
-------
157
155
numpy.ndarray
158
156
"""
159
- return np .log (mu )
157
+ return np .log (_asanyarray ( mu ) )
160
158
161
159
def derivative (self , mu ):
162
160
"""Get the derivative of the log link, one over ``mu``.
@@ -169,7 +167,7 @@ def derivative(self, mu):
169
167
-------
170
168
numpy.ndarray
171
169
"""
172
- return 1 / mu
170
+ return 1 / _asanyarray ( mu )
173
171
174
172
def inverse (self , lin_pred ):
175
173
"""Get the inverse of the log link, the exponential function.
@@ -184,7 +182,7 @@ def inverse(self, lin_pred):
184
182
-------
185
183
numpy.ndarray
186
184
"""
187
- return np .exp (lin_pred )
185
+ return np .exp (_asanyarray ( lin_pred ) )
188
186
189
187
def inverse_derivative (self , lin_pred ):
190
188
"""Compute the derivative of the inverse link function ``h'(lin_pred)``.
@@ -194,17 +192,17 @@ def inverse_derivative(self, lin_pred):
194
192
lin_pred : array-like, shape (n_samples,)
195
193
Usually the (fitted) linear predictor.
196
194
"""
197
- return np .exp (lin_pred )
195
+ return np .exp (_asanyarray ( lin_pred ) )
198
196
199
197
def inverse_derivative2 (self , lin_pred ):
200
- """Compute 2nd derivative of the inverse link function ``h''(lin_pred)``.
198
+ """Compute second derivative of the inverse link function ``h''(lin_pred)``.
201
199
202
200
Parameters
203
201
----------
204
202
lin_pred : array-like, shape (n_samples,)
205
203
Usually the (fitted) linear predictor.
206
204
"""
207
- return np .exp (lin_pred )
205
+ return np .exp (_asanyarray ( lin_pred ) )
208
206
209
207
210
208
class LogitLink (Link ):
@@ -282,7 +280,7 @@ def inverse_derivative(self, lin_pred):
282
280
return ep * (1.0 - ep )
283
281
284
282
def inverse_derivative2 (self , lin_pred ):
285
- """Compute 2nd derivative of the inverse link function ``h''(lin_pred)``.
283
+ """Compute second derivative of the inverse link function ``h''(lin_pred)``.
286
284
287
285
Parameters
288
286
----------
@@ -324,61 +322,56 @@ def _to_return(*args, **kwargs):
324
322
class TweedieLink (Link ):
325
323
"""The Tweedie link function ``x^(1-p)`` if ``p≠1`` and ``log(x)`` if ``p=1``."""
326
324
327
- def __new__ (cls , p : float ):
328
- """
329
- Create a new ``TweedieLink`` object.
330
-
331
- Parameters
332
- ----------
333
- p: scalar
334
- """
335
- if p == 0 :
336
- return IdentityLink ()
337
- if p == 1 :
338
- return LogLink ()
339
- return super ().__new__ (cls )
340
-
341
325
def __init__ (self , p ):
342
326
self .p = p
343
327
344
328
def __eq__ (self , other ): # noqa D
345
329
return isinstance (other , self .__class__ ) and (self .p == other .p )
346
330
347
331
def link (self , mu ):
348
- """
349
- Get the Tweedie canonical link.
332
+ """Get the Tweedie canonical link.
350
333
351
334
See superclass documentation.
352
335
353
336
Parameters
354
337
----------
355
338
mu: array-like
356
339
"""
340
+ if self .p == 0 :
341
+ return _asanyarray (mu )
342
+ if self .p == 1 :
343
+ return np .log (_asanyarray (mu ))
357
344
return _asanyarray (mu ) ** (1 - self .p )
358
345
359
346
def derivative (self , mu ):
360
- """
361
- Get the derivative of the Tweedie link.
347
+ """Get the derivative of the Tweedie link.
362
348
363
349
See superclass documentation.
364
350
365
351
Parameters
366
352
----------
367
353
mu: array-like
368
354
"""
355
+ if self .p == 0 :
356
+ return 1.0 if np .isscalar (mu ) else np .ones_like (mu )
357
+ if self .p == 1 :
358
+ return 1 / _asanyarray (mu )
369
359
return (1 - self .p ) * _asanyarray (mu ) ** (- self .p )
370
360
371
361
@catch_p
372
362
def inverse (self , lin_pred ):
373
- """
374
- Get the inverse of the Tweedie link.
363
+ """Get the inverse of the Tweedie link.
375
364
376
365
See superclass documentation.
377
366
378
367
Parameters
379
368
----------
380
369
mu: array-like
381
370
"""
371
+ if self .p == 0 :
372
+ return _asanyarray (lin_pred )
373
+ if self .p == 1 :
374
+ return np .exp (_asanyarray (lin_pred ))
382
375
return _asanyarray (lin_pred ) ** (1 / (1 - self .p ))
383
376
384
377
@catch_p
@@ -390,20 +383,30 @@ def inverse_derivative(self, lin_pred):
390
383
lin_pred : array-like, shape (n_samples,)
391
384
Usually the (fitted) linear predictor.
392
385
"""
386
+ if self .p == 0 :
387
+ return 1.0 if np .isscalar (lin_pred ) else np .ones_like (lin_pred )
388
+ if self .p == 1 :
389
+ return np .exp (_asanyarray (lin_pred ))
393
390
return (1 / (1 - self .p )) * _asanyarray (lin_pred ) ** (self .p / (1 - self .p ))
394
391
395
392
@catch_p
396
393
def inverse_derivative2 (self , lin_pred ):
397
- """Compute secondnd derivative of the inverse Tweedie link function \
394
+ """Compute second derivative of the inverse Tweedie link function \
398
395
``h''(lin_pred)``.
399
396
400
397
Parameters
401
398
----------
402
399
lin_pred : array, shape (n_samples,)
403
400
Usually the (fitted) linear predictor.
404
401
"""
402
+ if self .p == 0 :
403
+ return 0.0 if np .isscalar (lin_pred ) else np .zeros_like (lin_pred )
404
+ if self .p == 1 :
405
+ return np .exp (_asanyarray (lin_pred ))
406
+
405
407
result = _asanyarray (lin_pred ) ** ((2 * self .p - 1 ) / (1 - self .p ))
406
408
result *= self .p / (1 - self .p ) ** 2
409
+
407
410
return result
408
411
409
412
@@ -484,7 +487,7 @@ def inverse_derivative(self, lin_pred):
484
487
return np .exp (lin_pred - np .exp (lin_pred ))
485
488
486
489
def inverse_derivative2 (self , lin_pred ):
487
- """Compute 2nd derivative of the inverse link function ``h''(lin_pred)``.
490
+ """Compute second derivative of the inverse link function ``h''(lin_pred)``.
488
491
489
492
Parameters
490
493
----------
0 commit comments