class_docstr: Class for fitting the Aalen-Johansen estimate for the cumulative incidence function in a competing risks framework.
  Treating competing risks as censoring can result in over-estimated cumulative density functions. Using the Kaplan
  Meier estimator with competing risks as censored is akin to estimating the cumulative density if all competing risks
  had been prevented.
  
  Aalen-Johansen cannot deal with tied times. We can get around this by randomly jittering the event times
  slightly. This will be done automatically and generates a warning.
  
  
  Parameters
  ----------
  alpha: float, option (default=0.05)
      The alpha value associated with the confidence intervals.
  
  jitter_level: float, option (default=0.00001)
      If tied event times are detected, event times are randomly changed by this factor.
  
  seed: int, option (default=None)
      To produce replicate results with tied event times, the numpy.random.seed can be specified in the function.
  
  calculate_variance: bool, option (default=True)
      By default, AalenJohansenFitter calculates the variance and corresponding confidence intervals. Due to how the
      variance is calculated, the variance must be calculated for each event time individually. This is
      computationally intensive. For some procedures, like bootstrapping, the variance is not necessary. To reduce
      computation time during these procedures, `calculate_variance` can be set to `False` to skip the variance
      calculation.
  
  Example
  -------
  
  .. code:: python
  
      from lifelines import AalenJohansenFitter
      from lifelines.datasets import load_waltons
      T, E = load_waltons()['T'], load_waltons()['E']
      ajf = AalenJohansenFitter(calculate_variance=True)
      ajf.fit(T, E, event_of_interest=1)
      ajf.cumulative_density_
      ajf.plot()
  
  
  References
  ----------
  If you are interested in learning more, we recommend the following open-access
  paper; Edwards JK, Hester LL, Gokhale M, Lesko CR. Methodologic Issues When Estimating Risks in
  Pharmacoepidemiology. Curr Epidemiol Rep. 2016;3(4):285-296.
docstr: Bounds are based on pg 411 of "Modelling Survival Data in Medical Research" David Collett 3rd Edition, which
  is derived from Greenwood's variance estimator. Confidence intervals are obtained using the delta method
  transformation of SE(log(-log(F_j))). This ensures that the confidence intervals all lie between 0 and 1.
  
  Formula for the variance follows:
  
  .. math::
  
      Var(F_j) = sum((F_j(t) - F_j(t_i))**2 * d/(n*(n-d) + S(t_i-1)**2 * ((d*(n-d))/n**3) +
                  -2 * sum((F_j(t) - F_j(t_i)) * S(t_i-1) * (d/n**2)
  
  Delta method transformation:
  
  .. math::
  
      SE(log(-log(F_j) = SE(F_j) / (F_j * |log(F_j)|)
  
  More information can be found at: https://support.sas.com/documentation/onlinedoc/stat/141/lifetest.pdf
  There is also an alternative method (Aalen) but this is not currently implemented
docstr_tok: ["Bounds","are","based","on","pg","411","of","``","Modelling","Survival","Data","in","Medical","Research","''","David","Collett","3rd","Edition",",","which","is","derived","from","Greenwood","'s","variance","estimator",".","Confidence","intervals","are","obtained","using","the","delta","method","transformation","of","SE","(","log","(","-log","(","F_j",")",")",")",".","This","ensures","that","the","confidence","intervals","all","lie","between","0","and","1",".","Formula","for","the","variance","follows",":","..","math",":",":","Var","(","F_j",")","=","sum","(","(","F_j","(","t",")","-","F_j","(","t_i",")",")","*","*","2","*","d\/","(","n","*","(","n-d",")","+","S","(","t_i-1",")","*","*","2","*","(","(","d","*","(","n-d",")",")","\/n","*","*","3",")","+","-2","*","sum","(","(","F_j","(","t",")","-","F_j","(","t_i",")",")","*","S","(","t_i-1",")","*","(","d\/n","*","*","2",")","Delta","method","transformation",":","..","math",":",":","SE","(","log","(","-log","(","F_j",")","=","SE","(","F_j",")","\/","(","F_j","*","|log","(","F_j",")","|",")","More","information","can","be","found","at",":","https",":","\/\/support.sas.com\/documentation\/onlinedoc\/stat\/141\/lifetest.pdf","There","is","also","an","alternative","method","(","Aalen",")","but","this","is","not","currently","implemented"]
code: def _bounds(self, lagged_survival, alpha, ci_labels):
      """Bounds are based on pg 411 of "Modelling Survival Data in Medical Research" David Collett 3rd Edition, which
      is derived from Greenwood's variance estimator. Confidence intervals are obtained using the delta method
      transformation of SE(log(-log(F_j))). This ensures that the confidence intervals all lie between 0 and 1.
  
      Formula for the variance follows:
  
      .. math::
  
          Var(F_j) = sum((F_j(t) - F_j(t_i))**2 * d/(n*(n-d) + S(t_i-1)**2 * ((d*(n-d))/n**3) +
                      -2 * sum((F_j(t) - F_j(t_i)) * S(t_i-1) * (d/n**2)
  
      Delta method transformation:
  
      .. math::
  
          SE(log(-log(F_j) = SE(F_j) / (F_j * |log(F_j)|)
  
      More information can be found at: https://support.sas.com/documentation/onlinedoc/stat/141/lifetest.pdf
      There is also an alternative method (Aalen) but this is not currently implemented
      """
      # Preparing environment
      ci = 1 - alpha
      df = self.event_table.copy()
      df["Ft"] = self.cumulative_density_
      df["lagS"] = lagged_survival.fillna(1)
      if ci_labels is None:
          ci_labels = [
              "%s_upper_%g" % (self._label, ci),
              "%s_lower_%g" % (self._label, ci),
          ]
      assert (
          len(ci_labels) == 2
      ), "ci_labels should be a length 2 array."
  
      # Have to loop through each time independently. Don't think there is a faster way
      all_vars = []
      for _, r in df.iterrows():
          sf = df.loc[df.index <= r.name].copy()
          F_t = float(r["Ft"])
          first_term = np.sum(
              (F_t - sf["Ft"]) ** 2
              * sf["observed"]
              / sf["at_risk"]
              / (sf["at_risk"] - sf["observed"])
          )
          second_term = np.sum(
              sf["lagS"] ** 2
              / sf["at_risk"]
              * sf[self.label_cmprisk]
              / sf["at_risk"]
              * (sf["at_risk"] - sf[self.label_cmprisk])
              / sf["at_risk"]
          )
          third_term = np.sum(
              (F_t - sf["Ft"])
              / sf["at_risk"]
              * sf["lagS"]
              * sf[self.label_cmprisk]
              / sf["at_risk"]
          )
          variance = first_term + second_term - 2 * third_term
          all_vars.append(variance)
      df["variance"] = all_vars
  
      # Calculating Confidence Intervals
      df["F_transformed"] = np.log(-np.log(df["Ft"]))
      df["se_transformed"] = np.sqrt(df["variance"]) / (
          df["Ft"] * np.absolute(np.log(df["Ft"]))
      )
      zalpha = inv_normal_cdf(1 - alpha / 2)
      df[ci_labels[0]] = np.exp(
          -np.exp(df["F_transformed"] + zalpha * df["se_transformed"])
      )
      df[ci_labels[1]] = np.exp(
          -np.exp(df["F_transformed"] - zalpha * df["se_transformed"])
      )
      return df["variance"], df[ci_labels]
  
file_path: lifelines/lifelines/fitters/aalen_johansen_fitter.py
imports: from textwrap import dedent
  import numpy
  import pandas
  import warnings
  from lifelines.fitters import NonParametricUnivariateFitter
  from lifelines.utils import _preprocess_inputs, inv_normal_cdf, CensoringType, coalesce
  from lifelines import KaplanMeierFitter
  from lifelines.plotting import _plot_estimate
label_desc: Use image node_id 4 for calling the AalenJohansenFitter obj's underlying member method code with example usage: obj._bounds(lagged_survival, alpha, ci_labels) and returns: df,  df