Skip to content

Commit

Permalink
v0.18.3 (#627)
Browse files Browse the repository at this point in the history
* bump version

* make this test more robust
  • Loading branch information
CamDavidsonPilon authored Feb 7, 2019
1 parent ca931a6 commit 6f3d825
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
### Changelogs

### 0.18.3 (dev)
### 0.18.3
- Some performance improvements to parametric univariate models.
- Suppressing some irrelevant NumPy and autograd warnings, so lifeline warnings are more noticeable.
- Improved some warning and error messages.
Expand Down
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@
#
# The short X.Y version.

version = "0.18.2"
version = "0.18.3"
# The full version, including dev info
release = version

Expand Down
35 changes: 23 additions & 12 deletions lifelines/fitters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
from functools import wraps
import sys
import warnings
#pylint: disable=wrong-import-position

# pylint: disable=wrong-import-position

warnings.simplefilter(action="ignore", category=FutureWarning)
from textwrap import dedent
Expand Down Expand Up @@ -238,13 +239,13 @@ def __init__(self, *args, **kwargs):
if "alpha" in self._fitted_parameter_names:
raise NameError("'alpha' in _fitted_parameter_names is a lifelines reserved word. Try 'alpha_' instead.")

def _check_cumulative_hazard_is_monotone_and_positive(self):
test_times = np.linspace(0.01, 100, 15)
def _check_cumulative_hazard_is_monotone_and_positive(self, durations):
class_name = self.__class__.__name__

cumulative_hazard = self._cumulative_hazard(self._initial_values, test_times)
cumulative_hazard = self._cumulative_hazard(self._initial_values, durations)
if not np.all(cumulative_hazard > 0):
raise StatisticalWarning(dedent(
warnings.warn(
dedent(
"""\
Cumulative hazard is not strictly positive. For example, try:
Expand All @@ -253,11 +254,17 @@ def _check_cumulative_hazard_is_monotone_and_positive(self):
>>> fitter._cumulative_hazard(fitter._initial_values, test_times)
This may harm convergence, or return nonsensical results.
""".format(class_name)))
""".format(
class_name
)
),
StatisticalWarning,
)

derivative_of_cumulative_hazard = self._hazard(self._initial_values, test_times)
derivative_of_cumulative_hazard = self._hazard(self._initial_values, durations)
if not np.all(derivative_of_cumulative_hazard >= 0):
raise StatisticalWarning(dedent(
warnings.warn(
dedent(
"""\
Cumulative hazard is not strictly non-decreasing. For example, try:
Expand All @@ -266,7 +273,12 @@ def _check_cumulative_hazard_is_monotone_and_positive(self):
>>> fitter._hazard(fitter._initial_values, test_times)
This may harm convergence, or return nonsensical results.
""".format(class_name)))
""".format(
class_name
)
),
StatisticalWarning,
)

def _initial_values_from_bounds(self):
for (lb, ub) in self._bounds:
Expand Down Expand Up @@ -472,16 +484,15 @@ def fit(
if event_observed is not None:
check_nans_or_infs(event_observed)

self._check_cumulative_hazard_is_monotone_and_positive()


self.durations = np.asarray(durations, dtype=float)
# check for negative or 0 durations - these are not allowed in a weibull model.
if np.any(self.durations <= 0):
raise ValueError(
"This model does not allow for non-positive durations. Suggestion: add a small positive value to zero elements."
)

self._check_cumulative_hazard_is_monotone_and_positive(self.durations)

self.event_observed = (
np.asarray(event_observed, dtype=int) if event_observed is not None else np.ones_like(self.durations)
)
Expand Down
2 changes: 1 addition & 1 deletion lifelines/version.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

__version__ = "0.18.2"
__version__ = "0.18.3"
25 changes: 23 additions & 2 deletions tests/test_estimation.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
StatisticalWarning,
)

from lifelines.fitters import BaseFitter
from lifelines.fitters import BaseFitter, ParametericUnivariateFitter

from lifelines import (
WeibullFitter,
Expand Down Expand Up @@ -180,6 +180,27 @@ def test_parametric_univarite_fitters_can_print_summary(
f.summary
f.print_summary()

def test_warnings_for_problematic_cumulative_hazards(self):
class NegativeFitter(ParametericUnivariateFitter):

_fitted_parameter_names = ["a"]

def _cumulative_hazard(self, params, times):
return params[0] * (times - 0.4)

class DecreasingFitter(ParametericUnivariateFitter):

_fitted_parameter_names = ["a"]

def _cumulative_hazard(self, params, times):
return params[0] * 1 / times

with pytest.warns(StatisticalWarning, match="positive") as w:
NegativeFitter().fit([0.01, 0.5, 10.0, 20.0])

with pytest.warns(StatisticalWarning, match="non-decreasing") as w:
DecreasingFitter().fit([0.01, 0.5, 10.0, 20])


class TestUnivariateFitters:
def test_univarite_fitters_with_survival_function_have_conditional_time_to_(
Expand Down Expand Up @@ -451,7 +472,7 @@ def test_lnf_inference(self, lnf):

assert abs(mu - lnf.mu_) < 0.05
assert abs(sigma - lnf.sigma_) < 0.05
assert abs(lnf.median_ - np.percentile(X, 50)) < 0.05
assert abs(lnf.median_/np.percentile(X, 50) - 1) < 0.05

def test_lnf_inference_with_large_sigma(self, lnf):
N = 250000
Expand Down

0 comments on commit 6f3d825

Please sign in to comment.