def kl_measures(sender_pop, receiver_pop, n=2): msgs = list(itertools.product(range(n), range(n))) states = list(itertools.product(range(n), range(n))) state_probs = [1. / float(len(states))] * len(states) all_cprobs_msg_given_state = collections.defaultdict(list) all_cprobs_state_given_msg = collections.defaultdict(list) information_contents = collections.defaultdict(list) for i, msg in enumerate(msgs): cprobs_msg_given_state = [] for j, state in enumerate(states): pr = 0. for (sender, sender_prob) in sender_pop: if simulation.sender_matrix(sender)[j][i] == 1: pr += sender_prob cprobs_msg_given_state.append(pr) all_cprobs_msg_given_state[i] = cprobs_msg_given_state for j, state in enumerate(states): if math.fsum(cprobs_msg_given_state) > 0.: prob_state_given_msg = ((state_probs[j] * cprobs_msg_given_state[j]) / math.fsum(state_probs[k] * cprobs_msg_given_state[k] for k in xrange(len(states)))) else: prob_state_given_msg = float('inf') all_cprobs_state_given_msg[j].append(prob_state_given_msg) if prob_state_given_msg > 0. and not math.isinf(prob_state_given_msg): information_contents[i].append(math.log(prob_state_given_msg / state_probs[j])) else: information_contents[i].append(- float('inf')) return (information_contents, all_cprobs_state_given_msg, all_cprobs_msg_given_state)
def output_klstats(this, out, duplications, r_payoffs, s_payoffs, comparative=False, splitat=None): options = this.options times_misinformation = 0 times_sender_hdeception = 0 times_receiver_hdeception = 0 times_full_deception = 0 pcts_senders_deceptive = [] pcts_senders_hdeceptive = [] pcts_interactions_hdeceptive_potential = [] pcts_interactions_deceptive_potential = [] pcts_interactions_hdeceptive = [] pcts_interactions_deceptive = [] n = 2 all_states = list(itertools.product(range(n), range(n))) state_probs = [1. / float(len(all_states))] * len(all_states) for dup_i, (final_sender, final_receiver, generations) in enumerate(duplications): misinfo_msgs = [] (kls, all_cprobs_state_given_msg, all_cprobs_msg_given_state) = kl_measures(final_sender, final_receiver, n) if not options.quiet: print >> out, "Conditional Probabilities (msg: [pr(msg | state) for state in states]):" for msg, cprobs_msg_given_state in all_cprobs_msg_given_state.iteritems(): print >> out, "\t{0:>5}: {1}".format(msg, cprobs_msg_given_state) print >> out, "Conditional Probabilities (msg: [pr(state | msg) for state in states]):" for msg, cprobs_state_given_msg in all_cprobs_state_given_msg.iteritems(): print >> out, "\t{0:>5}: {1}".format(msg, cprobs_state_given_msg) print >> out, "KL Information Measures (msg: I(msg): [I(msg, state) for state in states]):" for msg, information_content in kls.iteritems(): print >> out, "\t{0:>5}: {1:>15}: {2}".format(msg, math.fsum(state_content * all_cprobs_state_given_msg[msg][state] for state, state_content in enumerate(information_content) if not math.isinf(state_content) and\ not math.isinf(all_cprobs_state_given_msg[msg][state]) ), information_content) misinfo_msgs = [msg for msg, information_content in kls.iteritems() if misinfo(msg, information_content)] if len(misinfo_msgs) > 0: times_misinformation += 1 # sum of population proportions that are deceptive against some receiver percent_senders_deceptive = 0. # sum of population proportions that are almost deceptive against some receiver (no receiver detriment) percent_senders_halfdeceptive = 0. # percent of interactions that are deceptive percent_interactions_deceptive_potential = 0. # percent of interactions that are almost deceptive percent_interactions_halfdeceptive_potential = 0. # percent_interactions_deceptive = 0. # percent_interactions_halfdeceptive = 0. s_decept = False r_decept = False f_decept = False for (sender, sender_prob) in final_sender: s_matrix = simulation.sender_matrix(sender) s_hdeceptive = False s_fdeceptive = False for msg in misinfo_msgs: states_msg_sent = [state for state, row in enumerate(s_matrix) if row[msg] == 1] for (receiver, receiver_prob) in final_receiver: sri_hdeceptive = False sri_fdeceptive = False if comparative and receiver < splitat: r_matrix = simulation.sender_matrix(receiver) elif comparative: r_matrix = simulation.receiver_matrix(receiver - splitat) elif this._result_options.combinatorial: r_matrix = simulation.receiver_matrix(receiver) else: r_matrix = simulation.sender_matrix(receiver) action = r_matrix[msg].index(1) for state in states_msg_sent: if comparative and receiver < splitat: receiver_payoff_actual = r_payoffs[0][state][action] receiver_payoff_ifknew = max(r_payoffs[0][state]) actions_ifknew = [act for act, payoff in enumerate(r_payoffs[0][state]) if payoff == receiver_payoff_ifknew] elif comparative: receiver_payoff_actual = r_payoffs[1][state][action] receiver_payoff_ifknew = max(r_payoffs[1][state]) actions_ifknew = [act for act, payoff in enumerate(r_payoffs[1][state]) if payoff == receiver_payoff_ifknew] else: receiver_payoff_actual = r_payoffs[state][action] receiver_payoff_ifknew = max(r_payoffs[state]) actions_ifknew = [act for act, payoff in enumerate(r_payoffs[state]) if payoff == receiver_payoff_ifknew] sender_payoff_actual = s_payoffs[state][action] sender_payoffs_ifknew = [s_payoffs[state][act] for act in actions_ifknew] if any(sender_payoff_actual > spk for spk in sender_payoffs_ifknew): percent_interactions_halfdeceptive += sender_prob * receiver_prob * state_probs[state] sender_benefit = True if not s_decept: times_sender_hdeception += 1 s_decept = True if not s_hdeceptive: percent_senders_halfdeceptive += sender_prob s_hdeceptive = True if not sri_hdeceptive: percent_interactions_halfdeceptive_potential += sender_prob * receiver_prob sri_hdeceptive = True else: sender_benefit = False if receiver_payoff_actual < receiver_payoff_ifknew: receiver_detriment = True if not r_decept: times_receiver_hdeception += 1 r_decept = True else: receiver_detriment = False if sender_benefit and receiver_detriment: percent_interactions_deceptive += sender_prob * receiver_prob * state_probs[state] if not f_decept: times_full_deception += 1 f_decept = True if not s_fdeceptive: percent_senders_deceptive += sender_prob s_fdeceptive = True if not sri_fdeceptive: percent_interactions_deceptive_potential += sender_prob * receiver_prob sri_fdeceptive = True pcts_senders_deceptive.append(percent_senders_deceptive) pcts_senders_hdeceptive.append(percent_senders_halfdeceptive) pcts_interactions_hdeceptive_potential.append(percent_interactions_halfdeceptive_potential) pcts_interactions_deceptive_potential.append(percent_interactions_deceptive_potential) pcts_interactions_hdeceptive.append(percent_interactions_halfdeceptive) pcts_interactions_deceptive.append(percent_interactions_deceptive) print >> out, "Number of duplications with misinformation: {0}".format(times_misinformation) print >> out, "Number of duplications with senders benefitting: {0}".format(times_sender_hdeception) print >> out, "Number of duplications with receivers losing out: {0}".format(times_receiver_hdeception) print >> out, "Number of duplications with deception: {0}".format(times_full_deception) print >> out if options.gphx is not None: fname_s_hdecept = options.gphx.format("s_hdecept") fname_s_decept = options.gphx.format("s_decept") fname_i_hdecept_pos = options.gphx.format("i_hdecept_pos") fname_i_decept_pos = options.gphx.format("i_decept_pos") fname_i_hdecept = options.gphx.format("i_hdecept") fname_i_decept = options.gphx.format("i_decept") else: fname_s_hdecept = None fname_s_decept = None fname_i_hdecept_pos = None fname_i_decept_pos = None fname_i_hdecept = None fname_i_decept = None print >> out, "Percent of Senders Benefitting:" print >> out, format_stats(pcts_senders_hdeceptive, "\t", fname_s_hdecept) print >> out, "Percent of Senders Deceptive:" print >> out, format_stats(pcts_senders_deceptive, "\t", fname_s_decept) print >> out, "Percent of Interactions with Potential Sender Benefit:" print >> out, format_stats(pcts_interactions_hdeceptive_potential, "\t", fname_i_hdecept_pos) print >> out, "Percent of Interactions with Potential Deception:" print >> out, format_stats(pcts_interactions_deceptive_potential, "\t", fname_i_decept_pos) print >> out, "Percent of Interactions with Sender Benefit:" print >> out, format_stats(pcts_interactions_hdeceptive, "\t", fname_i_hdecept) print >> out, "Percent of Interactions with Deception:" print >> out, format_stats(pcts_interactions_deceptive, "\t", fname_i_decept) print >> out, "=" * 72