<commit_before>import bayes
import math
from payoff import *
from recognition import *
from disclosuregame.Util import shuffled

class ProspectTheorySignaller(bayes.BayesianSignaller):
    """
    A responder which makes decision using cumulative prospect theory.
    """
    def __init__(self, player_type=1, signals=[0, 1, 2], responses=[0, 1], 
        alpha=.88, beta=.88, l=2.25, gamma=.61, delta=.69, seed=None):
        self.alpha = alpha
        self.gamma = gamma
        self.l = l
        self.delta = delta
        self.beta = beta
        super(ProspectTheorySignaller, self).__init__(player_type, signals, responses, seed=seed)

    def weighting(self, probability, power):
        return pow(probability, power) / pow(pow(probability, power) + pow(1. - probability, power), 1. / power)


    def value(self, outcome):
        """
        Return the relative value of an outcome.
        """
        if outcome > 0:
            return self.gain_value(outcome)
        elif outcome < 0:
            return self.l * self.loss_value(outcome)
        return 0

    def gain_value(self, payoff):
        if self.alpha > 0:
            payoff = pow(payoff, self.alpha)
        elif self.alpha < 0:
            payoff = math.log(payoff)
        else:
            payoff = 1 - pow(1 + payoff, self.alpha)
        return payoff

    def loss_value(self, payoff):
        if self.beta > 0:
            payoff = -pow(-payoff, self.beta)
        elif self.beta < 0:
            payoff = pow(1 - payoff, self.beta) - 1
        else:
            payoff = -math.log(-payoff)
        return payoff

    ##@profile
    def collect_prospects(self, signal):
        """
        Compute the cumulative prospect theory value of this signal
        based on the estimated probalities at this appointment.
        """
        prospects = []
        for player_type, type_belief in self.type_distribution.items():
            for response, response_belief in self.response_belief[signal].items():
                total_belief = response_belief*type_belief
                payoff = self.baby_payoffs[response] + self.social_payoffs[player_type][signal]
                prospects.append((payoff, total_belief))
        prospects.sort()
        prospects.reverse()
        return prospects

    ##@profile
    def cpt_value(self, prospects):
        """
        Compute the cumulative prospect theory value of this set of
        prospects.
        """
        payoffs, probs = zip(*prospects)
        signal_risk = 0.
        for i in range(len(prospects)):
            type_belief = probs[i]
            payoff = payoffs[i]
            power = self.gamma
            if payoff < 0:
                power = self.delta
            if i == 0 and payoff < 0 or i == (len(prospects) - 1) and payoff >= 0:
                weight =  (1 - self.weighting(1 - type_belief, power))
                #print "1 - w(1 - ", type_belief,")"
            elif payoff < 0:
                #print "sum(",probs[i:],"-sum(",probs[i + 1:],")"
                weight = self.weighting(sum(probs[i:]), power) - self.weighting(sum(probs[i + 1:]), power)
            else:
                weight = self.weighting(sum(probs[:i + 1]), power) - self.weighting(sum(probs[:i]), power)
                #print "sum(",probs[:i+1],"-sum(",probs[:i],")"
            #print "Weighting for outcome %f: %f" % (payoff, weight)
            signal_risk += self.value(payoff) * weight
        return signal_risk

    def signal_search(self, signals):
        best = (-1, float('inf'))
        for signal in signals:
            act_risk = self.cpt_value(self.collect_prospects(signal))
            #self.risk_log[signal].append(act_risk)
            #self.risk_log_general[signal].append(act_risk)
            if act_risk > best[1]:
                best = (signal, act_risk)
        return best

    def __str__(self):
        return "prospect"

class PayoffProspectSignaller(ProspectTheorySignaller, BayesianPayoffSignaller):
    def __str__(self):
        return "cpt_payoff"

    def collect_prospects(self, signal):
        """
        Compute the cumulative prospect theory value of this signal
        based on the estimated probalities at this appointment.
        """
        prospects = []
        for payoff, belief in self.payoff_belief[signal].items():
            #belief = belief[len(belief) - 1]
            prospects.append((payoff, belief))<commit_msg>Inf sign in CPT<commit_after>import bayes
import math
from payoff import *
from recognition import *
from disclosuregame.Util import shuffled

class ProspectTheorySignaller(bayes.BayesianSignaller):
    """
    A responder which makes decision using cumulative prospect theory.
    """
    def __init__(self, player_type=1, signals=[0, 1, 2], responses=[0, 1], 
        alpha=.88, beta=.88, l=2.25, gamma=.61, delta=.69, seed=None):
        self.alpha = alpha
        self.gamma = gamma
        self.l = l
        self.delta = delta
        self.beta = beta
        super(ProspectTheorySignaller, self).__init__(player_type, signals, responses, seed=seed)

    def weighting(self, probability, power):
        return pow(probability, power) / pow(pow(probability, power) + pow(1. - probability, power), 1. / power)


    def value(self, outcome):
        """
        Return the relative value of an outcome.
        """
        if outcome > 0:
            return self.gain_value(outcome)
        elif outcome < 0:
            return self.l * self.loss_value(outcome)
        return 0

    def gain_value(self, payoff):
        if self.alpha > 0:
            payoff = pow(payoff, self.alpha)
        elif self.alpha < 0:
            payoff = math.log(payoff)
        else:
            payoff = 1 - pow(1 + payoff, self.alpha)
        return payoff

    def loss_value(self, payoff):
        if self.beta > 0:
            payoff = -pow(-payoff, self.beta)
        elif self.beta < 0:
            payoff = pow(1 - payoff, self.beta) - 1
        else:
            payoff = -math.log(-payoff)
        return payoff

    ##@profile
    def collect_prospects(self, signal):
        """
        Compute the cumulative prospect theory value of this signal
        based on the estimated probalities at this appointment.
        """
        prospects = []
        for player_type, type_belief in self.type_distribution.items():
            for response, response_belief in self.response_belief[signal].items():
                total_belief = response_belief*type_belief
                payoff = self.baby_payoffs[response] + self.social_payoffs[player_type][signal]
                prospects.append((payoff, total_belief))
        prospects.sort()
        prospects.reverse()
        return prospects

    ##@profile
    def cpt_value(self, prospects):
        """
        Compute the cumulative prospect theory value of this set of
        prospects.
        """
        payoffs, probs = zip(*prospects)
        signal_risk = 0.
        for i in range(len(prospects)):
            type_belief = probs[i]
            payoff = payoffs[i]
            power = self.gamma
            if payoff < 0:
                power = self.delta
            if i == 0 and payoff < 0 or i == (len(prospects) - 1) and payoff >= 0:
                weight =  (1 - self.weighting(1 - type_belief, power))
                #print "1 - w(1 - ", type_belief,")"
            elif payoff < 0:
                #print "sum(",probs[i:],"-sum(",probs[i + 1:],")"
                weight = self.weighting(sum(probs[i:]), power) - self.weighting(sum(probs[i + 1:]), power)
            else:
                weight = self.weighting(sum(probs[:i + 1]), power) - self.weighting(sum(probs[:i]), power)
                #print "sum(",probs[:i+1],"-sum(",probs[:i],")"
            #print "Weighting for outcome %f: %f" % (payoff, weight)
            signal_risk += self.value(payoff) * weight
        return signal_risk

    def signal_search(self, signals):
        best = (-1, float('-inf'))
        for signal in signals:
            act_risk = self.cpt_value(self.collect_prospects(signal))
            #self.risk_log[signal].append(act_risk)
            #self.risk_log_general[signal].append(act_risk)
            if act_risk > best[1]:
                best = (signal, act_risk)
        return best

    def __str__(self):
        return "prospect"

class PayoffProspectSignaller(ProspectTheorySignaller, BayesianPayoffSignaller):
    def __str__(self):
        return "cpt_payoff"

    def collect_prospects(self, signal):
        """
        Compute the cumulative prospect theory value of this signal
        based on the estimated probalities at this appointment.
        """
        prospects = []
        for payoff, belief in self.payoff_belief[signal].items():
            #belief = belief[len(belief) - 1]
            prospects.append((payoff, belief))
{'fraction_non_alphanumeric': 0.11272232921717458, 'fraction_numerical': 0.010875722215928401, 'mean_word_length': 2.070608695652174, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4525776', 'n_tokens_mistral': 2716, 'n_tokens_neox': 2575, 'n_words': 668}