def pet_activity_simple_simulate(set_random_seed, pets_merged, pet_spec): # choosers: the choice model will be applied to each row of the choosers table (a pandas.DataFrame) choosers = pets_merged.to_frame() # spec: table of variable specifications and coefficient values of alternatives (a pandas.DataFrame table spec = pet_spec # locals whose values will be accessible to the execution context when the expressions in spec are applied to choosers locals_d=None # eval_variables evaluates each of the expressions in spec in the context of each row in of the choosers dataframe model_design = asim.eval_variables(spec.index, choosers, locals_d) print "\n### model_design - results of the expressions for each row in choosers" print model_design utilities = model_design.dot(spec) print "\n### utilities - the net utility of each alternative for each row in choosers" print utilities probs = asim.utils_to_probs(utilities) print "\n### probs - utilities normalized as relative choice probablities (summing to 1)" print probs # Make choices for each chooser from among the set of alternatives based on probability choices = asim.make_choices(probs) print "\n### choices - choices expressed as zero-based index into set of alternatives" print choices
def test_eval_variables(spec, data): result = asim.eval_variables(spec.index, data) pdt.assert_frame_equal( result, pd.DataFrame([ [True, False, 4, 1], [False, True, 4, 1], [False, True, 5, 1]], index=data.index, columns=spec.index), check_names=False)