#NEWLINE# def initial_household_utilities( utilities , people , hh_id_col ) : #NEWLINE# #INDENT# '\n    Create initial household utilities by grouping and summing utilities\n    from individual household members.\n\n    Parameters\n    ----------\n    utilities : pandas.DataFrame\n        Should have the index of `people` and columns for each alternative.\n    people : pandas.DataFrame\n        DataFrame of individual people data.\n    hh_id_col : str\n        Name of the column in `people` that has their household ID.\n\n    Returns\n    -------\n    hh_util : dict of pandas.Series\n        Keys will be household IDs and values will be Series\n        mapping alternative choices to their utility.\n\n    ' #NEWLINE# hh_util = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# alts = utilities . columns #NEWLINE# combo_cache = { #NEWLINE# #INDENT# #NEWLINE# #UNINDENT# } #NEWLINE# for ( hh_id , df ) in people . groupby ( hh_id_col , sort = False ) : #NEWLINE# #INDENT# hh_size = len ( df ) #NEWLINE# utils = utilities . loc [ df . index ] . as_matrix ( ) #NEWLINE# if ( hh_size in combo_cache ) : #NEWLINE# #INDENT# ( ncombos , combos , flat_combos , tiled ) = combo_cache [ hh_size ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# combos = list ( itertools . product ( alts , repeat = hh_size ) ) #NEWLINE# flat_combos = list ( tz . concat ( itertools . product ( range ( len ( alts ) ) , repeat = hh_size ) ) ) #NEWLINE# ncombos = len ( combos ) #NEWLINE# tiled = np . tile ( np . arange ( hh_size ) , ncombos ) #NEWLINE# combo_cache [ hh_size ] = ( ncombos , combos , flat_combos , hh_id_col ) #NEWLINE# #UNINDENT# u = utils [ ( tiled , flat_combos ) ] . reshape ( ( ncombos , hh_size ) ) . sum ( axis = 1 ) #NEWLINE# hh_util [ hh_id ] = pd . Series ( u , index = combos ) #NEWLINE# #UNINDENT# return hh_util