Beispiel #1
0
  def __init__(self, n):

    # initialise population - time of death only 
    self.population = pd.DataFrame(data={"TimeOfDeath": neworder.first_arrival(data.mortality_rate, neworder.timestep, n, 0.0),
                                         "TimeOfPregnancy": np.full(n, neworder.never()),
                                         "Parity": np.full(n, Parity.CHILDLESS),
                                         "Unions": np.zeros(n, dtype=int),
                                        })

    # Construct a timeline of unions for each person
    # first union - probabilities start at 15, so we add this on afterwards
    self.population["T_Union1Start"] = neworder.first_arrival(data.p_u1f, data.delta_t, len(self.population)) + data.min_age
    self.population["T_Union1End"] = neworder.next_arrival(self.population["T_Union1Start"].values, data.r_diss2[0], data.delta_t_u, True, data.min_u1) 

    # second union
    self.population["T_Union2Start"] = neworder.next_arrival(self.population["T_Union1End"].values, data.r_u2f, data.delta_t, True) 
    # no mimimum time of 2nd union 
    self.population["T_Union2End"] = neworder.next_arrival(self.population["T_Union2Start"].values, data.r_diss2[1], data.delta_t_u, True) 

    # and discard events happening after death
    self.population.loc[self.population["T_Union1Start"] > self.population["TimeOfDeath"], "T_Union1Start"] = neworder.never()
    self.population.loc[self.population["T_Union1End"] > self.population["TimeOfDeath"], "T_Union1End"] = neworder.never()
    self.population.loc[self.population["T_Union2Start"] > self.population["TimeOfDeath"], "T_Union2Start"] = neworder.never()
    self.population.loc[self.population["T_Union2End"] > self.population["TimeOfDeath"], "T_Union2End"] = neworder.never()

    # count unions entered into
    self.population.Unions = (~neworder.isnever(self.population["T_Union1Start"].values)).astype(int) \
                           + (~neworder.isnever(self.population["T_Union2Start"].values)).astype(int)

    neworder.log("RiskPaths initialised")
Beispiel #2
0
def test():
    t = test_.Test()

    # Exp.value = p +/- 1/sqrt(N)
    h = neworder.hazard(0.2, 10000)
    t.check(isinstance(h, np.ndarray))
    t.check(len(h) == 10000)
    t.check(abs(np.mean(h) - 0.2) < 0.01)

    hv = neworder.hazard(np.array([0.1, 0.2, 0.3, 0.4, 0.5]))
    t.check(isinstance(hv, np.ndarray))
    t.check(len(hv) == 5)

    # Exp.value = 1/p +/- 1/sqrt(N)
    s = neworder.stopping(0.1, 10000)
    t.check(isinstance(s, np.ndarray))
    t.check(len(s) == 10000)
    t.check(abs(np.mean(s) / 10 - 1.0) < 0.03)

    sv = neworder.stopping(np.array([0.1, 0.2, 0.3, 0.4, 0.5]))
    t.check(isinstance(sv, np.ndarray))
    t.check(len(sv) == 5)

    # Non-homogeneous Poisson process (time-dependent hazard)
    nhpp = neworder.first_arrival(np.array([0.1, 0.2, 0.3, 0.4, 0.5]), 1.0, 10)
    t.check(isinstance(nhpp, np.ndarray))
    t.check(len(nhpp) == 10)

    return not t.any_failed
Beispiel #3
0
  def pregnancy(self):
    # We're interested in the first pregnancy that occurs for each individual
    # fmin ignores nan (np.minimum is a problem as it doesnt deal with nan well)

    # pre-union1 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.NEVER_IN_UNION.value]
    # sample  
    t_pregnancy1 = neworder.first_arrival(p_preg, data.delta_t, len(self.population)) + data.min_age
    # remove pregnancies that happen after union1 formation
    t_pregnancy1[t_pregnancy1 > self.population["T_Union1Start"]] = neworder.never()

    # union1 phase1 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.FIRST_UNION_PERIOD1.value]
    # sample 
    t_pregnancy1_u1a = neworder.next_arrival(self.population["T_Union1Start"].values, p_preg, data.delta_t)
    # discard those that happen after union1 transition
    t_pregnancy1_u1a[t_pregnancy1_u1a > self.population["T_Union1Start"] + data.min_u1] = neworder.never()                    
    t_pregnancy1 = np.fmin(t_pregnancy1, t_pregnancy1_u1a)

    # union1 phase2 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.FIRST_UNION_PERIOD2.value]
    # sample
    t_pregnancy1_u1b = neworder.next_arrival(self.population["T_Union1Start"].values + data.min_u1, p_preg, data.delta_t)
    # discard those that happen after union1 
    t_pregnancy1_u1b[t_pregnancy1_u1b > self.population["T_Union1End"]] = neworder.never()                    
    t_pregnancy1 = np.fmin(t_pregnancy1, t_pregnancy1_u1b)

    # post union1 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.AFTER_FIRST_UNION.value]
    # sample
    t_pregnancy1_postu1 = neworder.next_arrival(self.population["T_Union1End"].values, p_preg, data.delta_t)
    # discard those that happen after union2 formation
    t_pregnancy1_postu1[t_pregnancy1_postu1 > self.population["T_Union2Start"]] = neworder.never()                    
    t_pregnancy1 = np.fmin(t_pregnancy1, t_pregnancy1_postu1)

    # union2 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.SECOND_UNION.value]
    # sample
    t_pregnancy1_u2 = neworder.next_arrival(self.population["T_Union2Start"].values, p_preg, data.delta_t)
    # discard those that happen after union2 dissolution
    t_pregnancy1_u2[t_pregnancy1_u2 > self.population["T_Union2End"]] = neworder.never()                    
    t_pregnancy1 = np.fmin(t_pregnancy1, t_pregnancy1_u2)

    # # post union2 pregnancy
    p_preg = data.p_preg * data.r_preg[UnionState.AFTER_SECOND_UNION.value]
    t_pregnancy1_postu2 = neworder.next_arrival(self.population["T_Union2End"].values, p_preg, data.delta_t)
    t_pregnancy1 = np.fmin(t_pregnancy1, t_pregnancy1_postu2)

    # add the times to pregnancy1 to the population, removing those pregnancies that occur after death
    self.population["TimeOfPregnancy"] = t_pregnancy1
    self.population.loc[self.population["TimeOfPregnancy"] > self.population["TimeOfDeath"], "TimeOfPregnancy"] = neworder.never()
    # and update parity column
    self.population.loc[~neworder.isnever(self.population["TimeOfPregnancy"].values), "Parity"] = Parity.PREGNANT

    # save population
    self.population.to_csv("./population.csv", index=False)
Beispiel #4
0
  def age(self):
    # sample times

    self.population["TimeOfDeath"] = neworder.first_arrival(self.mortality_hazard.Rate.values, neworder.timestep, len(self.population))

    births = neworder.arrivals(self.fertility_hazard.Rate.values, neworder.timestep, 0.75, len(self.population))
    
    #neworder.log(births)
    for i in range(births.shape[1]):
      col = "TimeOfBaby" + str(i+1)
      self.population[col] = births[:,i]
      # remove births that would have occured after death
      self.population.loc[self.population[col] > self.population.TimeOfDeath, col] = neworder.never() 
      self.population.Parity = self.population.Parity + ~np.isnan(self.population[col])
Beispiel #5
0
    def calc_life_expectancy(self):
        # ensure all people have died
        assert np.sum(self.population.Alive) == 0
        #self.dump("./population.csv")

        # in this case we can just compute the mortality directly by modelling a non-homogeneous Poisson process and
        # using the Lewis-Shedler algorithm
        self.population["TimeOfDeathNHPP"] = neworder.first_arrival(
            self.mortality_hazard.Rate.values, neworder.timestep,
            len(self.population))

        neworder.log("%f vs %f" % (np.mean(self.population.TimeOfDeath),
                                   np.mean(self.population.TimeOfDeathNHPP)))
        return np.mean(self.population.TimeOfDeath)
Beispiel #6
0
def test():
  t = test_.Test()

  n = 10000
  # 10% constant hazard for 10 time units, followed by zero
  dt = 1.0
  p = np.full(11, 0.1)
  p[-1] = 0
  a = no.first_arrival(p, dt, n)
  t.check(np.nanmin(a) > 0.0)
  t.check(np.nanmax(a) < 10.0)
  no.log("%f - %f" % (np.nanmin(a), np.nanmax(a)))

  # now set a to all 8.0
  a = np.full(n, 8.0)
  # next arrivals (absolute) only in range 8-10, if they happen
  b = no.next_arrival(a, p, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > 8.0)
  t.check(np.nanmax(b) < 10.0)

  # next arrivals with gap dt (absolute) only in range 9-10, if they happen
  b = no.next_arrival(a, p, dt, False, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > 9.0)
  t.check(np.nanmax(b) < 10.0)

  # next arrivals (relative) only in range 8-18, if they happen
  b = no.next_arrival(a, p, dt, True)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > 8.0)
  t.check(np.nanmax(b) < 18.0)

  # next arrivals with gap dt (relative) only in range 9-19, if they happen
  b = no.next_arrival(a, p, dt, True, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > 9.0)
  t.check(np.nanmax(b) < 19.0)

  # now set a back to random arrivals
  a = no.first_arrival(p, dt, n)
  # next arrivals (absolute) only in range (min(a), 10), if they happen
  b = no.next_arrival(a, p, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > np.nanmin(a))
  t.check(np.nanmax(b) < 10.0)

  # next arrivals with gap dt (absolute) only in range (min(a)+dt, 10), if they happen
  b = no.next_arrival(a, p, dt, False, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > np.nanmin(a) + dt)
  t.check(np.nanmax(b) < 10.0)

  # next arrivals (relative) only in range (min(a), max(a)+10), if they happen
  b = no.next_arrival(a, p, dt, True)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > np.nanmin(a))
  t.check(np.nanmax(b) < np.nanmax(a) + 10.0)

  # next arrivals with gap dt (relative) only in range (min(a)+dt, max(a)+dt+10), if they happen
  b = no.next_arrival(a, p, dt, True, dt)
  #no.log("%f - %f" % (np.nanmin(b), np.nanmax(b)))
  t.check(np.nanmin(b) > np.nanmin(a) + dt)
  t.check(np.nanmax(b) < np.nanmax(a) + dt + 10.0)

  return not t.any_failed