Hazard Function (Hazard)

A Hazard object represents a mapping from a quantity, t, to a conditional probability: of all values in the distribution that are at least t, what fraction are exactly t?

Hazard is a subclass of a Pandas Series, so it has all Series methods, although some are overridden to change their behavior.

Bases: Distribution

Represents a Hazard function.

Source code in empiricaldist/empiricaldist.py
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
class Hazard(Distribution):
    """Represents a Hazard function."""

    def copy(self, deep=True):
        """Make a copy.

        Args:
            deep: whether to make a deep copy

        Returns: new Pmf
        """
        return Hazard(self, copy=deep)

    # Hazard inherits __call__ from Distribution

    def normalize(self):
        """Normalize the hazard function (modifies self).

        Returns: normalizing constant
        """
        old_total = getattr(self, "total", 1.0)
        self.total = 1.0
        return old_total

    def make_surv(self, **kwargs):
        """Make a Surv from the Hazard.

        Args:
            kwargs: passed to the Surv constructor

        Returns: Surv
        """
        normalize = kwargs.pop("normalize", False)
        ps = (1 - self).cumprod()
        total = getattr(self, "total", 1.0)
        surv = Surv(ps * total, **kwargs)
        surv.total = total

        if normalize:
            surv.normalize()
        return surv

    def make_cdf(self, **kwargs):
        """Make a Cdf from the Hazard.

        Args:
            kwargs: passed to the Cdf constructor

        Returns: Cdf
        """
        return self.make_surv().make_cdf(**kwargs)

    def make_pmf(self, **kwargs):
        """Make a Pmf from the Hazard.

        Args:
            kwargs: passed to the Pmf constructor

        Returns: Pmf
        """
        return self.make_surv().make_cdf().make_pmf(**kwargs)

    def make_same(self, dist):
        """Convert the given dist to Hazard.

        Args:
            dist: Distribution

        Returns: Hazard
        """
        return dist.make_hazard()

    @staticmethod
    def from_seq(seq, **kwargs):
        """Make a Hazard from a sequence of values.

        Args:
            seq: iterable
            normalize: whether to normalize the Pmf, default True
            sort: whether to sort the Pmf by values, default True
            kwargs: passed to Pmf.from_seq

        Returns: Hazard object
        """
        pmf = Pmf.from_seq(seq, **kwargs)
        return pmf.make_hazard()

copy(deep=True)

Make a copy.

Parameters:
  • deep

    whether to make a deep copy

Returns: new Pmf

Source code in empiricaldist/empiricaldist.py
1277
1278
1279
1280
1281
1282
1283
1284
1285
def copy(self, deep=True):
    """Make a copy.

    Args:
        deep: whether to make a deep copy

    Returns: new Pmf
    """
    return Hazard(self, copy=deep)

from_seq(seq, **kwargs) staticmethod

Make a Hazard from a sequence of values.

Parameters:
  • seq

    iterable

  • normalize

    whether to normalize the Pmf, default True

  • sort

    whether to sort the Pmf by values, default True

  • kwargs

    passed to Pmf.from_seq

Returns: Hazard object

Source code in empiricaldist/empiricaldist.py
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
@staticmethod
def from_seq(seq, **kwargs):
    """Make a Hazard from a sequence of values.

    Args:
        seq: iterable
        normalize: whether to normalize the Pmf, default True
        sort: whether to sort the Pmf by values, default True
        kwargs: passed to Pmf.from_seq

    Returns: Hazard object
    """
    pmf = Pmf.from_seq(seq, **kwargs)
    return pmf.make_hazard()

make_cdf(**kwargs)

Make a Cdf from the Hazard.

Parameters:
  • kwargs

    passed to the Cdf constructor

Returns: Cdf

Source code in empiricaldist/empiricaldist.py
1316
1317
1318
1319
1320
1321
1322
1323
1324
def make_cdf(self, **kwargs):
    """Make a Cdf from the Hazard.

    Args:
        kwargs: passed to the Cdf constructor

    Returns: Cdf
    """
    return self.make_surv().make_cdf(**kwargs)

make_pmf(**kwargs)

Make a Pmf from the Hazard.

Parameters:
  • kwargs

    passed to the Pmf constructor

Returns: Pmf

Source code in empiricaldist/empiricaldist.py
1326
1327
1328
1329
1330
1331
1332
1333
1334
def make_pmf(self, **kwargs):
    """Make a Pmf from the Hazard.

    Args:
        kwargs: passed to the Pmf constructor

    Returns: Pmf
    """
    return self.make_surv().make_cdf().make_pmf(**kwargs)

make_same(dist)

Convert the given dist to Hazard.

Parameters:
  • dist

    Distribution

Returns: Hazard

Source code in empiricaldist/empiricaldist.py
1336
1337
1338
1339
1340
1341
1342
1343
1344
def make_same(self, dist):
    """Convert the given dist to Hazard.

    Args:
        dist: Distribution

    Returns: Hazard
    """
    return dist.make_hazard()

make_surv(**kwargs)

Make a Surv from the Hazard.

Parameters:
  • kwargs

    passed to the Surv constructor

Returns: Surv

Source code in empiricaldist/empiricaldist.py
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
def make_surv(self, **kwargs):
    """Make a Surv from the Hazard.

    Args:
        kwargs: passed to the Surv constructor

    Returns: Surv
    """
    normalize = kwargs.pop("normalize", False)
    ps = (1 - self).cumprod()
    total = getattr(self, "total", 1.0)
    surv = Surv(ps * total, **kwargs)
    surv.total = total

    if normalize:
        surv.normalize()
    return surv

normalize()

Normalize the hazard function (modifies self).

Returns: normalizing constant

Source code in empiricaldist/empiricaldist.py
1289
1290
1291
1292
1293
1294
1295
1296
def normalize(self):
    """Normalize the hazard function (modifies self).

    Returns: normalizing constant
    """
    old_total = getattr(self, "total", 1.0)
    self.total = 1.0
    return old_total