def choose_spouse(person, eligible_mates): """ Once lists of marrying men and women are created, this function chooses a wife for a particular male based on the age differential between the man and each woman, based on observed data. """ sp_probs = [] for eligible_mate in eligible_mates: if person.get_sex == "male": agediff = person.get_age_years() - eligible_mate.get_age_years() else: agediff = eligible_mate.get_age_years() - person.get_age_years() if person.get_sex() == eligible_mate.get_sex() or \ person.get_ethnicity() != eligible_mate.get_ethnicity() or \ person.is_sibling(eligible_mate): sp_probs.append(0) else: sp_probs.append(calc_prob_from_prob_dist(rcParams['spousechoice.male.agediff'], agediff)) #print "f", eligible_mate.get_age_years(), #print "m", male.get_age_years(), "|", if sum(sp_probs) == 0: # In this case NONE of the eligible_mates are eligible (all of different # ethnicities than the person). return None num = np.random.rand() * np.sum(sp_probs) sp_probs = np.cumsum(sp_probs) n = 0 for problim in sp_probs[0:-1]: if num <= problim: break n += 1 return eligible_mates[n]
def calc_education_level(person): """ Calculate education level for person, based on results of empirical analysis of CVFS panel data. """ levels = rcParams['education.depvar_levels'] intercepts = [rcParams['education.coef.y>=gt0lt4'], rcParams['education.coef.y>=gt4lt8'], rcParams['education.coef.y>=gt8lt11'], rcParams['education.coef.y>=gt11']] prob_y_gte_j = np.zeros(len(levels) - 1) # probability y >= j for n in np.arange(len(prob_y_gte_j)): intercept = intercepts[n] xb_sum = 0 # Individual-level characteristics if person.get_sex() == "female": xb_sum += rcParams['education.coef.gender=female'] if person.get_ethnicity() == "HighHindu": # This was the reference class pass elif person.get_ethnicity() == "HillTibeto": xb_sum += rcParams['education.coef.ethnic=HillTibeto'] elif person.get_ethnicity() == "LowHindu": xb_sum += rcParams['education.coef.ethnic=LowHindu'] elif person.get_ethnicity() == "Newar": xb_sum += rcParams['education.coef.ethnic=Newar'] elif person.get_ethnicity() == "TeraiTibeto": xb_sum += rcParams['education.coef.ethnic=TeraiTibeto'] else: raise StatisticsError("No coefficient was specified for ethnicity '%s'"%person.get_ethnicity()) # Neighborhood-level characteristics neighborhood = person.get_parent_agent().get_parent_agent() xb_sum += rcParams['education.coef.avg_yrs_services_lt15'] * \ neighborhood._avg_yrs_services_lt15 prob_y_gte_j[n] = 1. / (1 + np.exp(-(intercept + xb_sum))) prob_y_eq_j = np.zeros(4) # probability y == j prob_y_eq_j[0] = 1 - prob_y_gte_j[0] # Loop over all but the first cell of prob_y_eq_j for j in np.arange(1, len(prob_y_gte_j)): prob_y_lt_j = np.sum(prob_y_eq_j[0:j]) prob_y_eq_j[j] = 1 - prob_y_gte_j[j] - prob_y_lt_j prob_cutoffs = np.cumsum(prob_y_eq_j) prob_cutoffs[-1] = 1 # Code for testing only: #print prob_cutoffs rand = np.random.rand() for n in np.arange(len(prob_cutoffs)): if rand <= prob_cutoffs[n]: return levels[n] # Should never reach the next line. raise StatisticsError("Check level calculation - no class predicted")