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")
def calc_fuelwood_usage_probability(household, time): """ Calculates the probability of fuelwood usage (not quantity of usage, but probability of using any wood at all) at the household-level. """ hhsize = household.num_members() if hhsize == 0: # Households may be empty but still in the model if they have # out-migrants currently away, but that will be returning. return 0 inner = rcParams['fw_usageprob.coef.intercept'] ###################################################################### # Household level vars inner += rcParams['fw_usageprob.coef.hhsize'] * hhsize hh_ethnicity = household.get_hh_head().get_ethnicity() if hh_ethnicity == "HighHindu": # This was the reference class pass elif hh_ethnicity == "LowHindu": inner += rcParams['fw_usageprob.coef.ethnicLowHindu'] elif hh_ethnicity == "Newar": inner += rcParams['fw_usageprob.coef.ethnicNewar'] elif hh_ethnicity == "HillTibeto": inner += rcParams['fw_usageprob.coef.ethnicHillTibeto'] elif hh_ethnicity == "TeraiTibeto": inner += rcParams['fw_usageprob.coef.ethnicTeraiTibeto'] else: raise StatisticsError("No coefficient was specified for ethnicity '%s'"%hh_ethnicity) inner += rcParams['fw_usageprob.coef.meangender'] * household.mean_gender() ###################################################################### # Neighborhood level vars neighborhood = household.get_parent_agent() inner += rcParams['fw_usageprob.coef.elecavail'] * \ neighborhood._elec_available inner += rcParams['fw_usageprob.coef.distnara_km'] * \ neighborhood._distnara if neighborhood._forest_closest_type == "BZ": # Reference level pass elif neighborhood._forest_closest_type == "CNP": inner += rcParams['fw_usageprob.coef.closest_typeCNP'] else: raise StatisticsError("No coefficient was specified for closest forest type '%s'"%neighborhood._forest_closest_type) prob = 1./(1 + np.exp(-inner)) if rcParams['log_stats_probabilities']: logger.debug("Household %s fuelwood usage probability %.6f (size: %s)"%(household.get_ID(), prob, household.num_members())) return prob
def calc_first_birth_prob_zvoleff(person, time): """ Calculates the probability of a first birth in a given month for an agent, using the results of Zvoleff's empirical analysis, following the analysis of Ghimire and Axinn (2010). """ ######################################################################### # Intercept inner = rcParams['firstbirth.zv.coef.(Intercept)'] ######################################################################### # Adult community context neighborhood = person.get_parent_agent().get_parent_agent() # Convert nbh_area from square meters to square kilometers nbh_area = neighborhood._land_total / 1000000 inner += rcParams['firstbirth.zv.coef.total_t1'] * nbh_area percent_agveg = (neighborhood._land_agveg / neighborhood._land_total) * 100 inner += rcParams['firstbirth.zv.coef.percagveg_t1'] * percent_agveg inner += rcParams['firstbirth.zv.coef.dist_nara'] * neighborhood._distnara inner += rcParams['firstbirth.zv.coef.elec_avail'] * neighborhood._elec_available inner += rcParams['firstbirth.zv.coef.avg_yrs_services_lt15'] * neighborhood._avg_yrs_services_lt15 ######################################################################### # Parents characteristics inner += rcParams['firstbirth.zv.coef.mother_num_children'] * person.get_mother_num_children() if person.get_mother_years_schooling() > 1: inner += rcParams['firstbirth.zv.coef.mother_school'] inner += rcParams['firstbirth.zv.coef.mother_work'] * person.get_mother_work() if person.get_father_years_schooling() > 1: inner += rcParams['firstbirth.zv.coef.father_school'] inner += rcParams['firstbirth.zv.coef.father_work'] * person.get_father_work() inner += rcParams['firstbirth.zv.coef.parents_contracep_ever'] * person._parents_contracep_ever ######################################################################### # Other personal controls ethnicity = person.get_ethnicity() assert ethnicity!=None, "Ethnicity must be defined" if ethnicity == "HighHindu": # This was the reference level pass elif ethnicity == "HillTibeto": inner += rcParams['firstbirth.zv.coef.ethnicHillTibeto'] elif ethnicity == "LowHindu": inner += rcParams['firstbirth.zv.coef.ethnicLowHindu'] elif ethnicity == "Newar": inner += rcParams['firstbirth.zv.coef.ethnicNewar'] elif ethnicity == "TeraiTibeto": inner += rcParams['firstbirth.zv.coef.ethnicTeraiTibeto'] inner += rcParams['firstbirth.zv.coef.age_at_first_marr'] * person.get_marriage_age_years(time) #inner += rcParams['firstbirth.zv.coef.mths_marr_pre_1997'] ######################################################################### # Hazard duration marriage_time = time - person._marriage_time if marriage_time <= 6: inner += rcParams['firstbirth.zv.coef.marr_duration[0,6)'] elif marriage_time <= 12: inner += rcParams['firstbirth.zv.coef.marr_duration[6,12)'] elif marriage_time <= 18: inner += rcParams['firstbirth.zv.coef.marr_duration[12,18)'] elif marriage_time <= 24: inner += rcParams['firstbirth.zv.coef.marr_duration[18,24)'] elif marriage_time <= 30: inner += rcParams['firstbirth.zv.coef.marr_duration[24,30)'] elif marriage_time <= 36: inner += rcParams['firstbirth.zv.coef.marr_duration[30,36)'] elif marriage_time > 36: inner += rcParams['firstbirth.zv.coef.marr_duration[36,42)'] ######################################################################### # Education level of individual assert person._schooling !=None, "schoolinging must be defined" if person._schooling < 4: # This was the reference level pass elif person._schooling < 8: inner += rcParams['firstbirth.zv.coef.schooling_yrs_cat[4,7)'] elif person._schooling < 11: inner += rcParams['firstbirth.zv.coef.schooling_yrs_cat[7,11)'] elif person._schooling >= 11: inner += rcParams['firstbirth.zv.coef.schooling_yrs_cat[11,99)'] prob = 1./(1 + np.exp(-inner)) if rcParams['log_stats_probabilities']: logger.debug("Person %s first birth probability %.6f (marriage_time: %s)"%(person.get_ID(), prob, person._marriage_time)) return prob
def calc_probability_LL_migration_zvoleff(person, time): """ Calculates the probability of local-local migration for an agent, using the results of Alex Zvoleff's empirical analysis of the CVFS data, as presented in chapter 3 of his dissertation. """ household = person.get_parent_agent() neighborhood = household.get_parent_agent() ######################################################################### # Intercept inner = rcParams['migration.ll.zv.coef.(Intercept)'] ####################################################################### # Neighborhood level variables # # Note that the EVI measures are based off 2 year mean EVI for the change, # so calculate the 2 year mean EVI. EVI_2yr_mean = np.mean(neighborhood._EVI_ts[-2:]) # Note that the EVI coefficients are expressed for EVI/1000 (given the need # to get smaller betas for lmer to converge when estimating the model) inner += rcParams['migration.ll.zv.coef.mean_Sinteg_500m_24mth_2002_div_1000'] * (neighborhood._EVI_t0/1000) inner += rcParams['migration.ll.zv.coef.mean_Sinteg_500m_24mth_chg_2002_div_1000'] * ((EVI_2yr_mean - neighborhood._EVI_t0)/1000) inner += rcParams['migration.ll.zv.coef.NEAR_R_EVD_reversed'] * neighborhood._elevation_above_river inner += rcParams['migration.ll.zv.coef.SCHLFT_2001'] * neighborhood.NFOs['school_min_ft'] inner += rcParams['migration.ll.zv.coef.MARFT_2001'] * neighborhood.NFOs['market_min_ft'] inner += rcParams['migration.ll.zv.coef.EMPFT_2001'] * neighborhood.NFOs['employer_min_ft'] inner += rcParams['migration.ll.zv.coef.num_groups_2001'] * neighborhood._num_groups ####################################################################### # Household level variables inner += rcParams['migration.ll.zv.coef.own_total_2001'] * household._total_possessions inner += rcParams['migration.ll.zv.coef.any_farming_2001TRUE'] * household._any_farming inner += rcParams['migration.ll.zv.coef.TLU_livestock_2001'] * household._TLU_livestock ######################################################################### # Individual level variables if person.get_sex() == "female": # Male is the reference class inner += rcParams['migration.ll.zv.coef.genderfemale'] age_decades = person.get_age_years() / 10. inner += age_decades * rcParams['migration.ll.zv.coef.agedecades'] inner += (age_decades**2) * rcParams['migration.ll.zv.coef.I(agedecades^2)'] ######################################################################### # Baseline hazard month_num = int(np.mod(np.round(time*12, 0), 12) + 1) if month_num in [1, 2, 3, 4]: # Reference class pass elif month_num in [5, 6, 7, 8]: inner += rcParams['migration.ll.zv.coef.SeasonMonsoon (MJJA)'] elif month_num in [9, 10, 11, 12]: inner += rcParams['migration.ll.zv.coef.SeasonWinter (SOND)'] else: raise StatisticsError("Month number is %s. Should not be outside range of [1, 12]"%month_num) ethnicity = person.get_ethnicity() assert ethnicity!=None, "Ethnicity must be defined" if ethnicity == "HighHindu": # This was the reference level pass elif ethnicity == "HillTibeto": inner += rcParams['migration.ll.zv.coef.ethnicHillTibeto'] elif ethnicity == "LowHindu": inner += rcParams['migration.ll.zv.coef.ethnicLowHindu'] elif ethnicity == "Newar": inner += rcParams['migration.ll.zv.coef.ethnicNewar'] elif ethnicity == "TeraiTibeto": inner += rcParams['migration.ll.zv.coef.ethnicTeraiTibeto'] prob = 1./(1 + np.exp(-inner)) if rcParams['log_stats_probabilities']: logger.debug("Person %s local-local migration probability %.6f (age: %s)"%(person.get_ID(), prob, person.get_age_years())) return prob
def calc_probability_LD_migration_zvoleff(person, time): """ Calculates the probability of local-distant migration for an agent, using the results of Alex Zvoleff's empirical analysis of the CVFS data, following the results of the analysis conducted by Massey et al. (2010). """ ######################################################################### # Intercept inner = rcParams['migration.ld.zv.coef.intercept'] if person.is_in_school(): inner += rcParams['migration.ld.zv.coef.in_school'] inner += person.get_years_schooling() * rcParams['migration.ld.zv.coef.years_schooling'] ####################################################################### # Household level variables household = person.get_parent_agent() inner += rcParams['migration.ld.zv.coef.own_farmland'] * household._own_land ####################################################################### # Neighborhood level variables neighborhood = household.get_parent_agent() inner += rcParams['migration.ld.zv.coef.log_market_min_ft'] * np.log(neighborhood.NFOs['market_min_ft'] + 1) ######################################################################### # Other controls if person.get_sex() == "female": inner += rcParams['migration.ld.zv.coef.female'] ethnicity = person.get_ethnicity() assert ethnicity!=None, "Ethnicity must be defined" if ethnicity == "HighHindu": # This was the reference level pass elif ethnicity == "HillTibeto": inner += rcParams['migration.ld.zv.coef.ethnicHillTibeto'] elif ethnicity == "LowHindu": inner += rcParams['migration.ld.zv.coef.ethnicLowHindu'] elif ethnicity == "Newar": inner += rcParams['migration.ld.zv.coef.ethnicNewar'] elif ethnicity == "TeraiTibeto": inner += rcParams['migration.ld.zv.coef.ethnicTeraiTibeto'] age = person.get_age_years() if (age >= 15) & (age <= 24): inner += rcParams['migration.ld.zv.coef.age15-24'] elif (age > 24) & (age <= 34): inner += rcParams['migration.ld.zv.coef.age24-34'] elif (age > 34) & (age <= 44): inner += rcParams['migration.ld.zv.coef.age34-44'] elif (age > 44) & (age <= 55): inner += rcParams['migration.ld.zv.coef.age45-55'] elif (age > 55): # Reference class pass prob = 1./(1 + np.exp(-inner)) if rcParams['log_stats_probabilities']: logger.debug("Person %s local-distant migration probability %.6f (age: %s)"%(person.get_ID(), prob, person.get_age_years())) return prob
def calc_probability_marriage_zvoleff(person, time): """ Calculates the probability of marriage for an agent, using the results of Alex Zvoleff's empirical analysis of the CVFS data, following the results of the analysis conducted by Yabiku (2006). """ inner = rcParams['marrtime.zv.coef.(Intercept)'] ethnicity = person.get_ethnicity() assert ethnicity!=None, "Ethnicity must be defined" if ethnicity == "HighHindu": # This was the reference level pass elif ethnicity == "HillTibeto": inner += rcParams['marrtime.zv.coef.ethnicHillTibeto'] elif ethnicity == "LowHindu": inner += rcParams['marrtime.zv.coef.ethnicLowHindu'] elif ethnicity == "Newar": inner += rcParams['marrtime.zv.coef.ethnicNewar'] elif ethnicity == "TeraiTibeto": inner += rcParams['marrtime.zv.coef.ethnicTeraiTibeto'] # Gender if person.get_sex() == "female": inner += rcParams['marrtime.zv.coef.genderfemale'] age = person.get_age_years() inner += rcParams['marrtime.zv.coef.age'] * age inner += rcParams['marrtime.zv.coef.I(age^2)'] * (age ** 2) # Neighborhood characteristics neighborhood = person.get_parent_agent().get_parent_agent() log_percent_agveg = np.log((neighborhood._land_agveg / neighborhood._land_total)*100 + 1) inner += rcParams['marrtime.zv.coef.interp_logpercagveg'] * log_percent_agveg inner += rcParams['marrtime.zv.coef.SCHLFT'] * neighborhood.NFOs['school_min_ft'] inner += rcParams['marrtime.zv.coef.HLTHFT'] * neighborhood.NFOs['health_min_ft'] inner += rcParams['marrtime.zv.coef.BUSFT'] * neighborhood.NFOs['bus_min_ft'] inner += rcParams['marrtime.zv.coef.MARFT'] * neighborhood.NFOs['market_min_ft'] inner += rcParams['marrtime.zv.coef.EMPFT'] * neighborhood.NFOs['employer_min_ft'] # Schooling inner += rcParams['marrtime.zv.coef.schooling_yrs'] * person.get_years_schooling() if person.is_in_school(): inner += rcParams['marrtime.zv.coef.in_school'] # Account for monthly differences in marriage rates - some days (and # months) are more auspicious for marriage than others. month_num = int(np.mod(np.round(time*12, 0), 12) + 1) if month_num == 1: # This was the reference level pass elif month_num == 2: inner += rcParams['marrtime.zv.coef.month2'] elif month_num == 3: inner += rcParams['marrtime.zv.coef.month3'] elif month_num == 4: inner += rcParams['marrtime.zv.coef.month4'] elif month_num == 5: inner += rcParams['marrtime.zv.coef.month5'] elif month_num == 6: inner += rcParams['marrtime.zv.coef.month6'] elif month_num == 7: inner += rcParams['marrtime.zv.coef.month7'] elif month_num == 8: inner += rcParams['marrtime.zv.coef.month8'] elif month_num == 9: inner += rcParams['marrtime.zv.coef.month9'] elif month_num == 10: inner += rcParams['marrtime.zv.coef.month10'] elif month_num == 11: inner += rcParams['marrtime.zv.coef.month11'] elif month_num == 12: inner += rcParams['marrtime.zv.coef.month12'] prob = 1./(1 + np.exp(-inner)) if rcParams['log_stats_probabilities']: logger.debug("Person %s marriage probability %.6f (age: %s)"%(person.get_ID(), prob, person.get_age_years())) return prob