Source code for utility_modules.insurance_choice

#!/usr/bin/env python

[docs]def fam_struct_create(): import numpy as np import pandas as pd from utility_modules.math_functions import expand_grid #begin with a 6n vector #p1 = parent 1 #p2 = parent 2 #p3 = doc underage child #p4 = undoc underage child #p5 = doc adult child #p6 = doc adult child #p7 = doc adult child #p8 = doc adult child #defined family structure hieu_self = [1] hieu_sp = [0,1] hieu_ch1 = [0,1] hieu_ch2 = [0,1] hieu_ak1 = [0,1] hieu_ak2 = [0,1] hieu_ak3 = [0,1] hieu_ak4 = [0,1] fam_struct = expand_grid(hieu_self, hieu_sp, hieu_ch1, hieu_ch2, hieu_ak1, hieu_ak2, hieu_ak3, hieu_ak4) fam_struct.columns = ["adult_1", "adult_2", "child_1", "child_2", "adult_child_1", "adult_child_2", "adult_child_3", "adult_child_4"] return(fam_struct)
[docs]def insur_choice_enumerate(curr_fam_strct, elig_lst): import numpy as np from utility_modules.math_functions import expand_grid #use insur_lst as a list of eligibilities #0's here represent that the peson is not in the family insur_lst = np.array(curr_fam_strct) #make an array to put insurance options into expand_lst = np.repeat(None, 8).tolist() #case of 0 for i in np.where(insur_lst == 0)[1]: expand_lst[i] = [0] #case of 1 for i in np.where(insur_lst == 1)[1]: expand_lst[i] = elig_lst[i] enum_fam = expand_grid(*expand_lst) return(enum_fam)
[docs]def cut_eligs(curr_enum_fam, curr_fam_strct, fam_rltn): import numpy as np import pandas as pd #only need to cut for actual people in family ppl2trim = np.where(curr_enum_fam)[1] fam_mtx = np.matrix(curr_enum_fam) #initialize empty array to store bad plans bad_plans = np.empty(0) for i in np.unique(ppl2trim): #list indirect offers spousal_plans = np.array(np.where(fam_mtx[:,i] == 3)[0]) par1_plans = np.array(np.where(fam_mtx[:,i] == 4)[0]) par2_plans = np.array(np.where(fam_mtx[:,i] == 5)[0]) #return plans to lists of the plans which are not valid if spousal_plans.size: bad_plans = np.append(bad_plans, spousal_plans[np.array(fam_mtx[spousal_plans,fam_rltn['spouseid'][i]] != 2).T[0]]) if par1_plans.size: bad_plans = np.append(bad_plans, par1_plans[np.array(fam_mtx[par1_plans,fam_rltn['par1id'][i]] != 2).T[0]]) if par2_plans.size: bad_plans = np.append(bad_plans, par2_plans[np.array(fam_mtx[par2_plans,fam_rltn['par2id'][i]] != 2).T[0]]) #return unique rows to be trimmed plans2trim = np.unique(bad_plans).tolist() #result matrix poss_plans = np.delete(fam_mtx, plans2trim, 0) return(poss_plans)
[docs]def make_choices(input, names, fam_rltn, elig_lst): import pandas as pd import numpy as np fam_struct_list = input[names] for fam_idx in range(0, len(fam_struct_list)): #return current family structure curr_fam_strct = fam_struct_list[0+fam_idx:1+fam_idx] #enumerate options curr_enum_fam = insur_choice_enumerate(curr_fam_strct, elig_lst) #cut bad plans poss_plans = cut_eligs(curr_enum_fam, curr_fam_strct, fam_rltn) #return to temp data frame temp_choices = pd.DataFrame(poss_plans) temp_choices.columns = names #return to choices df if fam_idx == 0: choices = temp_choices else: choices = choices.append(temp_choices) return(choices)
[docs]def match_fam_type(choices, names): from utility_modules.math_functions import expand_grid import pandas as pd import numpy as np #convert to 0/1 choices_bool = pd.DataFrame(np.matrix(choices[names]).astype('bool').astype('int')) choices_bool.columns = names #defined family structure hieu_self = [1] hieu_sp = [0,1] hieu_ch1 = [0,1] hieu_ch2 = [0,1] hieu_ak1 = [0,1] hieu_ak2 = [0,1] hieu_ak3 = [0,1] hieu_ak4 = [0,1] fam_struct = expand_grid(hieu_self, hieu_sp, hieu_ch1, hieu_ch2, hieu_ak1, hieu_ak2, hieu_ak3, hieu_ak4) fam_struct.columns = names fam_struct['family_type'] = fam_struct.index f_types = pd.merge(choices_bool, fam_struct, how = 'left')['family_type'].values return(f_types)
[docs]def insur_choice(input): import numpy as np import pandas as pd #create insurance eligabilities #1 = unisured #2 = self ESI #3 = spouse ESI #4 = parent 1 ESI #5 = parent 2 ESI #6 = ex_silver #7 = ex_bronze #8 = ex_cat #9 = mcaid #10 = mcare p1 = [1,2,3,6,7,8,9,10] #parent 1 p2 = [1,2,3,6,7,8,9,10] #parent 2 p3 = [1,4,5,6,7,8,9] #doc underage child p4 = [1,4,5,6,7,8,9] #undoc underage child p5 = [1,2,4,5,6,7,8,9] #doc adult child p6 = [1,2,4,5,6,7,8,9] #doc adult child p7 = [1,2,4,5,6,7,8,9] #doc adult child p8 = [1,2,4,5,6,7,8,9] #doc adult child #return to list elig_lst = [p1,p2,p3,p4,p5,p6,p7,p8] fam_rltn = { "spouseid": np.array([1,0,None,None,None,None,None,None]), "par1id": np.array([None,None,0,0,0,0,0,0]), "par2id": np.array([None,None,1,1,1,1,1,1]) } names = ['adult_1','adult_2','child_1','child_2', 'adult_child_1','adult_child_2','adult_child_3','adult_child_4'] choices = make_choices(input, names, fam_rltn, elig_lst) choices['family_type'] = match_fam_type(choices, names) choices = choices.reset_index() return(choices)
if __name__ == "__main__": insur_choice()