Survival Function (Surv)

A Surv object represents a mapping from a quantity to the probability that a value from the distribution exceeds the quantity.

Surv 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 survival function (complementary CDF).

Source code in empiricaldist/empiricaldist.py
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
class Surv(Distribution):
    """Represents a survival function (complementary CDF)."""

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

        Args:
            deep: whether to make a deep copy

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

    @staticmethod
    def from_seq(seq, normalize=True, sort=True, **options):
        """Make a Surv from a sequence of values.

        Args:
            seq: iterable
            normalize: whether to normalize the Surv, default True
            sort: whether to sort the Surv by values, default True
            options: passed to the pd.Series constructor

        Returns: Surv object
        """
        cdf = Cdf.from_seq(seq, normalize=normalize, sort=sort, **options)
        return cdf.make_surv()

    def step(self, **options):
        """Plot the Surv as a step function.

        Args:
            options: passed to pd.Series.plot
        """
        underride(options, drawstyle="steps-post")
        self.plot(**options)

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

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

    @property
    def forward(self, **kwargs):
        """Make a function that computes the forward survival function.

        Args:
            kwargs: keyword arguments passed to interp1d

        Returns: array of probabilities
        """
        total = getattr(self, "total", 1.0)
        underride(
            kwargs,
            kind="previous",
            copy=False,
            assume_sorted=True,
            bounds_error=False,
            fill_value=(total, 0),
        )
        interp = interp1d(self.qs, self.ps, **kwargs)
        return interp

    @property
    def inverse(self, **kwargs):
        """Make a function that computes the inverse survival function.

        Args:
            kwargs: keyword arguments passed to interp1d

        Returns: interpolation function from ps to qs
        """
        total = getattr(self, "total", 1.0)
        underride(
            kwargs,
            kind="previous",
            copy=False,
            assume_sorted=True,
            bounds_error=False,
            fill_value=(np.nan, np.nan),
        )
        # sort in descending order
        # I don't remember why
        rev = self.sort_values()

        # If the reversed Surv doesn't get all the way to total
        # add a fake entry at -inf
        if rev.iloc[-1] != total:
            rev[-np.inf] = total

        interp = interp1d(rev, rev.index, **kwargs)
        return interp

    # calling a Surv like a function does forward lookup
    __call__ = forward

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

        Args:
            kwargs: passed to the Cdf constructor

        Returns: Cdf
        """
        normalize = kwargs.pop("normalize", False)
        total = getattr(self, "total", 1.0)
        cdf = Cdf(total - self, **kwargs)
        if normalize:
            cdf.normalize()
        return cdf

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

        Args:
            kwargs: passed to the Pmf constructor

        Returns: Pmf
        """
        cdf = self.make_cdf()
        pmf = cdf.make_pmf(**kwargs)
        return pmf

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

        Args:
            kwargs: passed to the Hazard constructor

        Returns: Hazard
        """
        pmf = self.make_pmf()
        at_risk = self + pmf
        haz = Hazard(pmf / at_risk, **kwargs)
        haz.total = getattr(self, "total", 1.0)
        haz.name = self.name
        return haz

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

        Args:
            dist: Distribution

        Returns: Surv
        """
        return dist.make_surv()

forward property

Make a function that computes the forward survival function.

Parameters:
  • kwargs

    keyword arguments passed to interp1d

Returns: array of probabilities

inverse property

Make a function that computes the inverse survival function.

Parameters:
  • kwargs

    keyword arguments passed to interp1d

Returns: interpolation function from ps to qs

copy(deep=True)

Make a copy.

Parameters:
  • deep

    whether to make a deep copy

Returns: new Surv

Source code in empiricaldist/empiricaldist.py
1123
1124
1125
1126
1127
1128
1129
1130
1131
def copy(self, deep=True):
    """Make a copy.

    Args:
        deep: whether to make a deep copy

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

from_seq(seq, normalize=True, sort=True, **options) staticmethod

Make a Surv from a sequence of values.

Parameters:
  • seq

    iterable

  • normalize

    whether to normalize the Surv, default True

  • sort

    whether to sort the Surv by values, default True

  • options

    passed to the pd.Series constructor

Returns: Surv object

Source code in empiricaldist/empiricaldist.py
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
@staticmethod
def from_seq(seq, normalize=True, sort=True, **options):
    """Make a Surv from a sequence of values.

    Args:
        seq: iterable
        normalize: whether to normalize the Surv, default True
        sort: whether to sort the Surv by values, default True
        options: passed to the pd.Series constructor

    Returns: Surv object
    """
    cdf = Cdf.from_seq(seq, normalize=normalize, sort=sort, **options)
    return cdf.make_surv()

make_cdf(**kwargs)

Make a Cdf from the Surv.

Parameters:
  • kwargs

    passed to the Cdf constructor

Returns: Cdf

Source code in empiricaldist/empiricaldist.py
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
def make_cdf(self, **kwargs):
    """Make a Cdf from the Surv.

    Args:
        kwargs: passed to the Cdf constructor

    Returns: Cdf
    """
    normalize = kwargs.pop("normalize", False)
    total = getattr(self, "total", 1.0)
    cdf = Cdf(total - self, **kwargs)
    if normalize:
        cdf.normalize()
    return cdf

make_hazard(**kwargs)

Make a Hazard from the Surv.

Parameters:
  • kwargs

    passed to the Hazard constructor

Returns: Hazard

Source code in empiricaldist/empiricaldist.py
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
def make_hazard(self, **kwargs):
    """Make a Hazard from the Surv.

    Args:
        kwargs: passed to the Hazard constructor

    Returns: Hazard
    """
    pmf = self.make_pmf()
    at_risk = self + pmf
    haz = Hazard(pmf / at_risk, **kwargs)
    haz.total = getattr(self, "total", 1.0)
    haz.name = self.name
    return haz

make_pmf(**kwargs)

Make a Pmf from the Surv.

Parameters:
  • kwargs

    passed to the Pmf constructor

Returns: Pmf

Source code in empiricaldist/empiricaldist.py
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
def make_pmf(self, **kwargs):
    """Make a Pmf from the Surv.

    Args:
        kwargs: passed to the Pmf constructor

    Returns: Pmf
    """
    cdf = self.make_cdf()
    pmf = cdf.make_pmf(**kwargs)
    return pmf

make_same(dist)

Convert the given dist to Surv.

Parameters:
  • dist

    Distribution

Returns: Surv

Source code in empiricaldist/empiricaldist.py
1263
1264
1265
1266
1267
1268
1269
1270
1271
def make_same(self, dist):
    """Convert the given dist to Surv.

    Args:
        dist: Distribution

    Returns: Surv
    """
    return dist.make_surv()

normalize()

Normalize the survival function (modifies self).

Returns: normalizing constant

Source code in empiricaldist/empiricaldist.py
1157
1158
1159
1160
1161
1162
1163
1164
1165
def normalize(self):
    """Normalize the survival function (modifies self).

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

step(**options)

Plot the Surv as a step function.

Parameters:
  • options

    passed to pd.Series.plot

Source code in empiricaldist/empiricaldist.py
1148
1149
1150
1151
1152
1153
1154
1155
def step(self, **options):
    """Plot the Surv as a step function.

    Args:
        options: passed to pd.Series.plot
    """
    underride(options, drawstyle="steps-post")
    self.plot(**options)