Esempio n. 1
0
def candidate_regrets(voters, candidates, weights=None, order=1):
    """Calculate the voter regret for each candidate or winner.

    Parameters
    ----------
    voters : array (a, n)
        Voter preferences; n-dimensional voter cardinal preferences for n issues.
    candidates : array (b, n)
        Candidate preferences for `b` candidates and `n`-dimensional issues.

    Returns
    -------
    out : (b,) array
        Average preference distance of voters from each candidate numbering `b`.
    """
    voters = np.atleast_2d(voters)
    candidates = np.atleast_2d(candidates)
    num_voters = len(voters)

    # distance shape (a, b) for `a` num voters, `b` num candidates.
    distances = vcalcs.voter_distances(voters,
                                       candidates,
                                       weights=weights,
                                       order=order)

    avg_distances = np.sum(distances, axis=0) / num_voters
    return avg_distances
Esempio n. 2
0
    def set_raw(self,
                voters=None,
                weights=None,
                order=1,
                candidates=None,
                winners=None,
                distances=None,
                ballots=None,
                ties=None):

        vstat = VoterStats(pref=voters, weights=weights, order=order)
        vdata = VoterData(pref=voters,
                          weights=weights,
                          order=order,
                          stats=vstat,
                          tol=None,
                          base='linear')

        distances = vcalcs.voter_distances(voters=voters,
                                           candidates=candidates,
                                           weights=weights,
                                           order=order)

        cstats = CandidateStats(pref=candidates, distances=distances)
        cdata = CandidateData(pref=candidates,
                              distances=distances,
                              stats=cstats)

        edata = ElectionData(ballots=ballots,
                             winners=winners,
                             ties=ties,
                             group_index=None)
        self.set_data(voters=vdata, candidates=cdata, election=edata)
        return
Esempio n. 3
0
def regret_std(voters, meanvoter=None, weights=None):
    if meanvoter is None:
        v_mean = np.mean(voters, axis=0)
    else:
        v_mean = meanvoter
    v_dist = vcalcs.voter_distances(voters, v_mean[None, :], weights=weights)
    std = np.std(v_dist)
    return std
Esempio n. 4
0
 def nearest_winner_distances(self):
     """array shaped (a,)
         Preference distances of nearest winner for `a` voters. 
     """
     distances = vcalcs.voter_distances(self.voters, self.winners,
                                        self.weights)
     index = np.arange(self._num_voters)
     return distances[index, self.nearest_winners]
Esempio n. 5
0
 def test2(self):
     voters = [[-1, 0, 2]]*5
     candidates = [[0,0,0]]*10
     
     voters = np.array(voters)
     candidates = np.array(candidates)
     d = vcalcs.voter_distances(voters, candidates)
     print(d)    
     self.assertTrue(np.all(d==3))
Esempio n. 6
0
    def _regret_std(voters, meanvoter, weights=None):

        #v_mean = np.mean(voters, axis=0)
        v_mean = meanvoter
        v_dist = vcalcs.voter_distances(voters,
                                        v_mean[None, :],
                                        weights=weights)
        std = np.std(v_dist)
        return std
Esempio n. 7
0
 def test1(self):
     voters = [1,2,3]
     candidates = [1,2,3]
     
     voters = np.array(voters).T
     candidates = np.array(candidates).T
     d = vcalcs.voter_distances(voters, candidates)
     print(d)
     
     correct = [[0, 1, 2],
                [1, 0, 1],
                [2, 1, 0],]
     correct = np.array(correct)
     self.assertTrue(np.all(d==correct))
     return
Esempio n. 8
0
    def calculate_distances(self, candidates):
        """Preference distances of candidates from voters.

        
        Parameters
        ----------
        candidates : array shaped (a, b)
            Candidate preference data
        """        
        pref = self.pref
        try:
            weights = self.weights
        except AttributeError:
            weights = None
            
        distances = vcalcs.voter_distances(voters=pref,
                                           candidates=candidates,
                                           weights=weights,
                                           order=self.order)
        return distances
Esempio n. 9
0
 def set(self,
         voters=None,
         weights=-1,
         candidates=None,
         winners=None,
         ballots=None):
     """Set voters, weights, candidates or winners to recalculate"""
     
     if voters is not None:
         self._voters = voters
         self._cache_voters = {}
         self._cache_candidate = {}
         self._cache_result = {}
         
         
     if weights != -1:
         self._weights = weights
         self._cache_voters = {}
         self._cache_candidate = {}
         self._cache_result = {}            
         
     if candidates is not None:
         self._candidates = candidates
         self._cache_candidate = {}
         self._cache_result = {}
     
     if winners is not None:
         self._winners = winners
         self._cache_result = {}
         
     if ballots is not None:
         self._ballots = ballots
         
         
     self._distances = vcalcs.voter_distances(voters=self._voters,
                                              candidates=self._candidates,
                                              weights=self._weights,
                                              order=1
                                              )
Esempio n. 10
0
 def calculate_distances(self, candidates):
     """Calculate regret distances.
     
     Parameters
     ----------
     candidates : array shaped (a, b)
         Candidate preference data
     """        
     pref = self._pref
     error = self.voter_error
     rs = self._randomstate        
     
     try:
         weights = self.weights
     except AttributeError:
         weights = None
         
     distances = vcalcs.voter_distances(voters=pref,
                                        candidates=candidates,
                                        weights=weights)
     distances = vcalcs.voter_distance_error(distances, error, rstate=rs)
     return distances