{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Election" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.424997Z", "start_time": "2023-09-28T09:17:32.776871Z" } }, "outputs": [], "source": [ "import svvamp" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Create a population of 9 voters with preferences over 5 candidates, using the Spheroid model (which extends Impartial Culture to utilities):" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.451807Z", "start_time": "2023-09-28T09:17:34.427746Z" } }, "outputs": [], "source": [ "random_profile = svvamp.GeneratorProfileSpheroid(n_v=9, n_c=5)\n", "profile = random_profile()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Preference rankings of the voters:" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.483948Z", "start_time": "2023-09-28T09:17:34.451807Z" } }, "outputs": [ { "data": { "text/plain": [ "array([[0, 1, 4, 2, 3],\n", " [3, 0, 4, 1, 2],\n", " [4, 2, 3, 1, 0],\n", " [2, 3, 0, 4, 1],\n", " [0, 1, 4, 3, 2],\n", " [2, 1, 4, 3, 0],\n", " [4, 2, 0, 3, 1],\n", " [4, 2, 0, 1, 3],\n", " [1, 4, 0, 2, 3]])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "profile.preferences_rk" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Define a voting rule:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.499827Z", "start_time": "2023-09-28T09:17:34.483948Z" } }, "outputs": [], "source": [ "rule = svvamp.RulePlurality()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Load the profile into the voting rule:" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.515938Z", "start_time": "2023-09-28T09:17:34.499827Z" } }, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule(profile)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ballots:" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.531822Z", "start_time": "2023-09-28T09:17:34.515938Z" } }, "outputs": [ { "data": { "text/plain": [ "array([0, 3, 4, 2, 0, 2, 4, 4, 1])" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.ballots_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Scores:" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.548084Z", "start_time": "2023-09-28T09:17:34.532952Z" } }, "outputs": [ { "data": { "text/plain": [ "array([2, 1, 2, 1, 3])" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.scores_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Order the candidates according to their scores:" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.564088Z", "start_time": "2023-09-28T09:17:34.548084Z" } }, "outputs": [ { "data": { "text/plain": [ "array([4, 0, 2, 1, 3], dtype=int64)" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.candidates_by_scores_best_to_worst_" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.585641Z", "start_time": "2023-09-28T09:17:34.564808Z" } }, "outputs": [ { "data": { "text/plain": [ "array([3, 2, 2, 1, 1])" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.scores_best_to_worst_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The winner, her score and her total utility:" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.601764Z", "start_time": "2023-09-28T09:17:34.585641Z" } }, "outputs": [ { "data": { "text/plain": [ "4" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.w_" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.618155Z", "start_time": "2023-09-28T09:17:34.602521Z" } }, "outputs": [ { "data": { "text/plain": [ "3" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.score_w_" ] }, { "cell_type": "code", "execution_count": 12, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.634047Z", "start_time": "2023-09-28T09:17:34.618155Z" } }, "outputs": [ { "data": { "text/plain": [ "0.5038515767548695" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.total_utility_w_" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Decide whether the winner of the election is a Condorcet winner:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "ExecuteTime": { "end_time": "2023-09-28T09:17:34.650174Z", "start_time": "2023-09-28T09:17:34.634047Z" } }, "outputs": [ { "data": { "text/plain": [ "True" ] }, "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "rule.w_is_condorcet_winner_ut_abs_" ] } ], "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 }