{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# First Steps with SVVAMP" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Welcome to this series of tutorials! The objective here is to get you up and running with the package *SVVAMP*, but not to present all its features in detail. For more exhaustive information, see the *Reference* section of the documentation." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.281839Z", "start_time": "2023-09-28T09:17:50.849848Z" } }, "outputs": [], "source": [ "import svvamp" ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2021-07-12T08:09:38.305008Z", "start_time": "2021-07-12T08:09:38.297029Z" } }, "source": [ "## Basic Usage" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a profile of preferences:" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.298014Z", "start_time": "2023-09-28T09:17:52.281839Z" } }, "outputs": [], "source": [ "profile = svvamp.Profile(preferences_rk=[\n", " [0, 1, 2],\n", " [0, 1, 2],\n", " [1, 0, 2],\n", " [1, 0, 2],\n", " [2, 1, 0]\n", "])" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Each row represents a voter. For example, the first row represents a voter who prefers candidate 0, then candidate 1, then candidate 2." ] }, { "cell_type": "markdown", "metadata": { "ExecuteTime": { "end_time": "2021-07-12T04:28:49.470908Z", "start_time": "2021-07-12T04:28:49.463802Z" } }, "source": [ "Define a voting rule:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.306493Z", "start_time": "2023-09-28T09:17:52.302484Z" } }, "outputs": [], "source": [ "rule = svvamp.RulePlurality()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the profile into the rule:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.322521Z", "start_time": "2023-09-28T09:17:52.307493Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule(profile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute the winner of the election:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.338974Z", "start_time": "2023-09-28T09:17:52.322521Z" } }, "outputs": [ { "data": { "text/plain": [ "0" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.w_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Here, the winner is candidate 0. Observe that SVVAMP always uses \"candidate tie-breaking\": in case of a tie, candidate 0 is favored over candidate 1, etc." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Compute whether the election is coalitionally manipulable:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.354957Z", "start_time": "2023-09-28T09:17:52.338974Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.is_cm_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Demo of SVVAMP's Features" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Properties of the profile:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.386951Z", "start_time": "2023-09-28T09:17:52.354957Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "************************\n", "* Basic properties *\n", "************************\n", "n_v = 5\n", "n_c = 3\n", "labels_candidates = ['0', '1', '2']\n", "preferences_ut =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "preferences_borda_ut =\n", "[[2. 1. 0.]\n", " [2. 1. 0.]\n", " [1. 2. 0.]\n", " [1. 2. 0.]\n", " [0. 1. 2.]]\n", "preferences_borda_rk =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "preferences_rk =\n", "[[0 1 2]\n", " [0 1 2]\n", " [1 0 2]\n", " [1 0 2]\n", " [2 1 0]]\n", "v_has_same_ordinal_preferences_as_previous_voter =\n", "[False True False True False]\n", "\n", "************************\n", "* Plurality scores *\n", "************************\n", "preferences_rk (reminder) =\n", "[[0 1 2]\n", " [0 1 2]\n", " [1 0 2]\n", " [1 0 2]\n", " [2 1 0]]\n", "plurality_scores_rk = [2 2 1]\n", "majority_favorite_rk = nan\n", "majority_favorite_rk_ctb = nan\n", "\n", "preferences_borda_ut (reminder) =\n", "[[2. 1. 0.]\n", " [2. 1. 0.]\n", " [1. 2. 0.]\n", " [1. 2. 0.]\n", " [0. 1. 2.]]\n", "plurality_scores_ut = [2 2 1]\n", "majority_favorite_ut = nan\n", "majority_favorite_ut_ctb = nan\n", "\n", "********************\n", "* Borda scores *\n", "********************\n", "preferences_borda_rk (reminder) =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "borda_score_c_rk =\n", "[6 7 2]\n", "Remark: Borda scores above are computed with the matrix of duels.\n", "Check: np.sum(self.preferences_borda_rk, 0) =\n", "[6 7 2]\n", "decreasing_borda_scores_rk =\n", "[7 6 2]\n", "candidates_by_decreasing_borda_score_rk =\n", "[1 0 2]\n", "\n", "preferences_borda_ut (reminder) =\n", "[[2. 1. 0.]\n", " [2. 1. 0.]\n", " [1. 2. 0.]\n", " [1. 2. 0.]\n", " [0. 1. 2.]]\n", "borda_score_c_ut =\n", "[6. 7. 2.]\n", "decreasing_borda_scores_ut =\n", "[7. 6. 2.]\n", "candidates_by_decreasing_borda_score_ut =\n", "[1 0 2]\n", "\n", "*****************\n", "* Utilities *\n", "*****************\n", "preferences_ut (reminder) =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "total_utility_c = \n", "[6 7 2]\n", "total_utility_min = 2\n", "total_utility_max = 7\n", "total_utility_mean = 5.0\n", "total_utility_std = 2.160246899469287\n", "\n", "*******************************************\n", "* Condorcet notions based on rankings *\n", "*******************************************\n", "preferences_rk (reminder) =\n", "[[0 1 2]\n", " [0 1 2]\n", " [1 0 2]\n", " [1 0 2]\n", " [2 1 0]]\n", "matrix_duels_rk =\n", "[[0 2 4]\n", " [3 0 4]\n", " [1 1 0]]\n", "matrix_victories_rk =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_winner_rk = 1\n", "exists_condorcet_order_rk = True\n", "matrix_victories_rk_ctb =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_winner_rk_ctb = 1\n", "exists_condorcet_order_rk_ctb = True\n", "\n", "***************************************\n", "* Relative Condorcet notions (ut) *\n", "***************************************\n", "preferences_borda_ut (reminder) =\n", "[[2. 1. 0.]\n", " [2. 1. 0.]\n", " [1. 2. 0.]\n", " [1. 2. 0.]\n", " [0. 1. 2.]]\n", "matrix_duels_ut =\n", "[[0 2 4]\n", " [3 0 4]\n", " [1 1 0]]\n", "matrix_victories_ut_rel =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_winner_ut_rel = 1\n", "exists_condorcet_order_ut_rel = True\n", "matrix_victories_ut_rel_ctb =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_winner_ut_rel_ctb = 1\n", "exists_condorcet_order_ut_rel_ctb = True\n", "\n", "***************************************\n", "* Absolute Condorcet notions (ut) *\n", "***************************************\n", "matrix_duels_ut (reminder) =\n", "[[0 2 4]\n", " [3 0 4]\n", " [1 1 0]]\n", "matrix_victories_ut_abs =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_admissible_candidates = \n", "[False True False]\n", "nb_condorcet_admissible = 1\n", "weak_condorcet_winners =\n", "[False True False]\n", "nb_weak_condorcet_winners = 1\n", "condorcet_winner_ut_abs = 1\n", "exists_condorcet_order_ut_abs = True\n", "resistant_condorcet_winner = nan\n", "threshold_c_prevents_w_Condorcet_ut_abs =\n", "[[0 1 1]\n", " [2 0 1]\n", " [0 0 0]]\n", "matrix_victories_ut_abs_ctb =\n", "[[0. 0. 1.]\n", " [1. 0. 1.]\n", " [0. 0. 0.]]\n", "condorcet_winner_ut_abs_ctb = 1\n", "exists_condorcet_order_ut_abs_ctb = True\n", "\n", "**********************************************\n", "* Implications between Condorcet notions *\n", "**********************************************\n", "maj_fav_ut (False) ==> maj_fav_ut_ctb (False)\n", " || || || ||\n", " || V V ||\n", " || maj_fav_rk (False) ==> maj_fav_rk_ctb (False) ||\n", " V || || ||\n", "Resistant Condorcet (False) ||\n", " || || || ||\n", " V || || V\n", "Condorcet_ut_abs (True) ==> Condorcet_ut_abs_ctb (True) \n", " || || || || || ||\n", " || V V V V ||\n", " || Condorcet_rk (True) ==> Condorcet_rk_ctb (True) ||\n", " V V\n", "Condorcet_ut_rel (True) ==> Condorcet_ut_rel_ctb (True) \n", " ||\n", " V\n", "Weak Condorcet (True) \n", " ||\n", " V\n", "Condorcet-admissible (True) \n" ] } ], "source": [ "profile.demo(log_depth=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Results of the election:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.419336Z", "start_time": "2023-09-28T09:17:52.386951Z" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "************************\n", "* *\n", "* Election Results *\n", "* *\n", "************************\n", "\n", "***************\n", "* Results *\n", "***************\n", "profile_.preferences_ut (reminder) =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "profile_.preferences_rk (reminder) =\n", "[[0 1 2]\n", " [0 1 2]\n", " [1 0 2]\n", " [1 0 2]\n", " [2 1 0]]\n", "ballots =\n", "[0 0 1 1 2]\n", "scores =\n", "[2 2 1]\n", "candidates_by_scores_best_to_worst\n", "[0 1 2]\n", "scores_best_to_worst\n", "[2 2 1]\n", "w = 0\n", "score_w = 2\n", "total_utility_w = 6\n", "\n", "*********************************\n", "* Condorcet efficiency (rk) *\n", "*********************************\n", "w (reminder) = 0\n", "\n", "condorcet_winner_rk_ctb = 1\n", "w_is_condorcet_winner_rk_ctb = False\n", "w_is_not_condorcet_winner_rk_ctb = True\n", "w_missed_condorcet_winner_rk_ctb = True\n", "\n", "condorcet_winner_rk = 1\n", "w_is_condorcet_winner_rk = False\n", "w_is_not_condorcet_winner_rk = True\n", "w_missed_condorcet_winner_rk = True\n", "\n", "***************************************\n", "* Condorcet efficiency (relative) *\n", "***************************************\n", "w (reminder) = 0\n", "\n", "condorcet_winner_ut_rel_ctb = 1\n", "w_is_condorcet_winner_ut_rel_ctb = False\n", "w_is_not_condorcet_winner_ut_rel_ctb = True\n", "w_missed_condorcet_winner_ut_rel_ctb = True\n", "\n", "condorcet_winner_ut_rel = 1\n", "w_is_condorcet_winner_ut_rel = False\n", "w_is_not_condorcet_winner_ut_rel = True\n", "w_missed_condorcet_winner_ut_rel = True\n", "\n", "***************************************\n", "* Condorcet efficiency (absolute) *\n", "***************************************\n", "w (reminder) = 0\n", "\n", "condorcet_admissible_candidates =\n", "[False True False]\n", "w_is_condorcet_admissible = False\n", "w_is_not_condorcet_admissible = True\n", "w_missed_condorcet_admissible = True\n", "\n", "weak_condorcet_winners =\n", "[False True False]\n", "w_is_weak_condorcet_winner = False\n", "w_is_not_weak_condorcet_winner = True\n", "w_missed_weak_condorcet_winner = True\n", "\n", "condorcet_winner_ut_abs_ctb = 1\n", "w_is_condorcet_winner_ut_abs_ctb = False\n", "w_is_not_condorcet_winner_ut_abs_ctb = True\n", "w_missed_condorcet_winner_ut_abs_ctb = True\n", "\n", "condorcet_winner_ut_abs = 1\n", "w_is_condorcet_winner_ut_abs = False\n", "w_is_not_condorcet_winner_ut_abs = True\n", "w_missed_condorcet_winner_ut_abs = True\n", "\n", "resistant_condorcet_winner = nan\n", "w_is_resistant_condorcet_winner = False\n", "w_is_not_resistant_condorcet_winner = True\n", "w_missed_resistant_condorcet_winner = False\n" ] } ], "source": [ "rule.demo_results_(log_depth=0)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Manipulation of the election:" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:52.460088Z", "start_time": "2023-09-28T09:17:52.419336Z" }, "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "*****************************\n", "* *\n", "* Election Manipulation *\n", "* *\n", "*****************************\n", "\n", "*********************************************\n", "* Basic properties of the voting system *\n", "*********************************************\n", "with_two_candidates_reduces_to_plurality = True\n", "is_based_on_rk = True\n", "is_based_on_ut_minus1_1 = False\n", "meets_iia = False\n", "\n", "****************************************************\n", "* Manipulation properties of the voting system *\n", "****************************************************\n", "Condorcet_c_ut_rel_ctb (False) ==> Condorcet_c_ut_rel (False)\n", " || ||\n", " || Condorcet_c_rk_ctb (False) ==> Condorcet_c_rk (False) ||\n", " || || || || || ||\n", " V V || || V V\n", "Condorcet_c_ut_abs_ctb (False) ==> Condorcet_ut_abs_c (False)\n", " || || || ||\n", " || V V ||\n", " || maj_fav_c_rk_ctb (True) ==> maj_fav_c_rk (True) ||\n", " || || || ||\n", " V V V V\n", "majority_favorite_c_ut_ctb (True) ==> majority_favorite_c_ut (True) \n", " || ||\n", " V V\n", "IgnMC_c_ctb (True) ==> IgnMC_c (True) \n", " || ||\n", " V V\n", "InfMC_c_ctb (True) ==> InfMC_c (True) \n", "\n", "*****************************************************\n", "* Independence of Irrelevant Alternatives (IIA) *\n", "*****************************************************\n", "w (reminder) = 0\n", "is_iia = False\n", "log_iia: iia_subset_maximum_size = 2.0\n", "example_winner_iia = 1\n", "example_subset_iia = [ True True False]\n", "\n", "**********************\n", "* c-Manipulators *\n", "**********************\n", "w (reminder) = 0\n", "preferences_ut (reminder) =\n", "[[2 1 0]\n", " [2 1 0]\n", " [1 2 0]\n", " [1 2 0]\n", " [0 1 2]]\n", "v_wants_to_help_c = \n", "[[False False False]\n", " [False False False]\n", " [False True False]\n", " [False True False]\n", " [False True True]]\n", "\n", "************************************\n", "* Individual Manipulation (IM) *\n", "************************************\n", "is_im = True\n", "log_im: im_option = exact\n", "candidates_im =\n", "[0. 1. 0.]\n", "\n", "*********************************\n", "* Trivial Manipulation (TM) *\n", "*********************************\n", "is_tm = True\n", "log_tm: tm_option = exact\n", "candidates_tm =\n", "[0. 1. 0.]\n", "\n", "********************************\n", "* Unison Manipulation (UM) *\n", "********************************\n", "is_um = True\n", "log_um: um_option = exact\n", "candidates_um =\n", "[0. 1. 0.]\n", "\n", "*********************************************\n", "* Ignorant-Coalition Manipulation (ICM) *\n", "*********************************************\n", "is_icm = True\n", "log_icm: icm_option = exact\n", "candidates_icm =\n", "[0. 1. 0.]\n", "necessary_coalition_size_icm =\n", "[0. 3. 5.]\n", "sufficient_coalition_size_icm =\n", "[0. 3. 5.]\n", "\n", "***********************************\n", "* Coalition Manipulation (CM) *\n", "***********************************\n", "is_cm = True\n", "log_cm: cm_option = exact\n", "candidates_cm =\n", "[0. 1. 0.]\n", "necessary_coalition_size_cm =\n", "[0. 3. 3.]\n", "sufficient_coalition_size_cm =\n", "[0. 3. 3.]\n" ] } ], "source": [ "rule.demo_manipulation_(log_depth=0)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" }, "toc": { "base_numbering": 1, "nav_menu": {}, "number_sections": false, "sideBar": true, "skip_h1_title": true, "title_cell": "Table of Contents", "title_sidebar": "Contents", "toc_cell": false, "toc_position": {}, "toc_section_display": true, "toc_window_display": false } }, "nbformat": 4, "nbformat_minor": 4 }