# Probabilities¶

[1]:

from matplotlib import pyplot as plt
from fractions import Fraction
import poisson_approval as pa


## Random Factories¶

The package provides a large variety of random factories to generate profiles, strategies or tau-vectors. For example, define a random factory of tau-vectors:

[2]:

rand_tau = pa.RandTauVectorUniform()


Then use this random factory to generate a tau-vector:

[3]:

tau = rand_tau()
tau

[3]:

<a: 0.13742516708602215, ab: 0.6199484833440477, ac: 0.011448359176042144, b: 0.027954897800712586, bc: 0.10541250539939295, c: 0.09781058719378244> ==> a


Most random factories of the package also have a “grid” counterpart where the coefficients are fractions of a given denominator. This is convenient to generate less “messy” examples:

[4]:

rand_tau = pa.RandTauVectorGridUniform(denominator=100)
tau = rand_tau()
tau

[4]:

<a: 23/100, ab: 1/100, ac: 29/50, b: 1/100, bc: 3/50, c: 11/100> ==> a


Also, most random factories have options that enable to finely tune the distribution:

[5]:

rand_tau = pa.RandTauVectorUniform(
ballots=['a', 'b'],
d_ballot_fixed_share={'c': 0.1},
voting_rule=pa.PLURALITY
)
tau = rand_tau()
tau

[5]:

<a: 0.48571900227732984, b: 0.4142809977226702, c: 0.1> ==> a (Plurality)


## Conditional Random Factory¶

RandConditional is a random factory implementing rejection sampling. It needs:

• Another random factory that is responsible to generate the objects,
• A test that the objects must meet,
• A maximum number of trials before renouncing (which can be None if you want to draw objects forever, until finding one that meets the test).

Here, we use a conditional random factory to generate an example of TauVector with a direct focus:

[6]:

def test_direct_focus(tau):
return tau.focus == pa.Focus.DIRECT

[7]:

rand_tau = pa.RandConditional(
factory=pa.RandTauVectorGridUniform(denominator=100),
test=test_direct_focus,
n_trials_max=None
)
tau = rand_tau()
tau

[7]:

<a: 3/25, ab: 6/25, ac: 21/100, b: 13/50, bc: 1/100, c: 4/25> ==> a


Alternatively, RandConditional accepts a tuple of factories, and a test on the tuple of the results. Here is an example of a ProfileNoisyDiscrete and a StrategyOrdinal, such that the strategy is an equilibrium for the profile:

[8]:

rand_profile = pa.RandProfileNoisyDiscreteGridUniform(
denominator=100,
types=[('abc', 0.4, 0.01),
('bac', 0.2, 0.01),
('cab', 0.7, 0.01)]
)
rand_strategy = pa.RandStrategyOrdinalUniform()
def test_is_equilibrium(profile, strategy):
return profile.is_equilibrium(strategy) == pa.EquilibriumStatus.EQUILIBRIUM

[9]:

rand_example = pa.RandConditional(
factory=(rand_profile, rand_strategy),
test=test_is_equilibrium,
n_trials_max=None
)
profile, strategy = rand_example()
print(profile)
print(strategy)

<abc 0.4 ± 0.01: 2/5, bac 0.2 ± 0.01: 11/50, cab 0.7 ± 0.01: 19/50> (Condorcet winner: a)
<abc: a, acb: ac, bac: ab, bca: bc, cab: c, cba: bc>


## Probability Estimation¶

To compute a probability, use the function probability.

For example, estimate the probability that a TauVector drawn uniformly at random has a direct focus:

[10]:

def test_direct_focus(tau):
return tau.focus == pa.Focus.DIRECT

[11]:

pa.probability(
factory=pa.RandTauVectorUniform(),
n_samples=100,
test=test_direct_focus
)

[11]:

0.54


To avoid the definition of an auxiliary function, you can use a “lambda”. For example:

[12]:

pa.probability(
factory=pa.RandTauVectorUniform(),
n_samples=100,
test=lambda tau: tau.focus == pa.Focus.DIRECT
)

[12]:

0.45


You can also compute a conditional probability. For example, the probability that a ProfileNoisyDiscrete (generated by the random factory defined below) has an ordinal equilibrium, conditionally on having a strict Condorcet winner:

[13]:

rand_profile = pa.RandProfileNoisyDiscreteUniform(
types=[('abc', 0.4, 0.01),
('bac', 0.2, 0.01),
('cab', 0.7, 0.01)]
)
def test_exists_ordinal_equilibrium(profile):
return len(profile.analyzed_strategies_ordinal.equilibria) > 0
def test_is_strictly_condorcet(profile):
return profile.is_profile_condorcet == 1.0

[14]:

pa.probability(
factory=rand_profile,
n_samples=100,
test=test_exists_ordinal_equilibrium,
conditional_on=test_is_strictly_condorcet
)

[14]:

1.0


You can also use a tuple of random factories, and tests on the tuple of the results. For example, estimate the probability that a random StrategyOrdinal is an equilibrium for a random ProfileNoisyDiscrete (generated by the random factory defined below), conditionally on the fact that the profile has a strict Condorcet winner and that the initial strategy elects the Condorcet winner:

[15]:

rand_profile = pa.RandProfileNoisyDiscreteUniform(
types=[('abc', 0.4, 0.01),
('bac', 0.2, 0.01),
('cab', 0.7, 0.01)]
)
rand_strategy = pa.RandStrategyOrdinalUniform()
def test_is_equilibrium(profile, strategy):
return profile.is_equilibrium(strategy) == pa.EquilibriumStatus.EQUILIBRIUM
def test_elect_condorcet_winner(profile, strategy):
return (profile.is_profile_condorcet == 1.
and profile.tau(strategy).winners == profile.condorcet_winners)

[16]:

pa.probability(
factory=(rand_profile, rand_strategy),
n_samples=100,
test=test_is_equilibrium,
conditional_on=test_elect_condorcet_winner
)

[16]:

0.36


## Image Distribution Estimation¶

To compute an image distribution, use the function image_distribution.

For example, when drawing a ProfileNoisyDiscrete (with the random factory defined below) and an initial StrategyOrdinal at random, and when applying iterated voting, let us estimate the distribution of the length of the cycle to which it converges:

[17]:

rand_profile = pa.RandProfileNoisyDiscreteUniform(
types=[('abc', 0.4, 0.01),
('bca', 0.2, 0.01),
('cab', 0.7, 0.01)]
)
rand_strategy = pa.RandStrategyOrdinalUniform()

[18]:

def len_cycle(profile, strategy_ini):
cycle = profile.iterated_voting(init=strategy_ini, n_max_episodes=100)['cycle_taus_actual']
return len(cycle)

[19]:

d_len_occurrences = pa.image_distribution(
factory=(rand_profile, rand_strategy),
n_samples=100, f=len_cycle)
d_len_occurrences

[19]:

{1: 0.19, 2: 0.01, 3: 0.8}

[20]:

plt.bar(d_len_occurrences.keys(), d_len_occurrences.values())

[20]:

<BarContainer object of 3 artists>