{ "cells": [ { "cell_type": "markdown", "metadata": { "cell_id": "00000-d24dfd04-2d72-490d-bc6a-f7268335fd1d", "deepnote_cell_type": "text-cell-h1", "is_collapsed": false, "tags": [] }, "source": [ "# `model_wrapper.py`" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00001-e06e94c7-16c6-4d05-a75a-b0521b1dd613", "deepnote_cell_type": "text-cell-h2", "is_collapsed": false, "tags": [] }, "source": [ "## Initialize" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "cell_id": "00002-4aec7776-a894-4ce1-aef6-f4be41ab9afd", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 1526, "execution_start": 1636396371284, "source_hash": "50e43dfb", "tags": [] }, "outputs": [], "source": [ "# Run dependencies\n", "%run ./model_python_lib_utils.ipynb\n", "%run ./model_python_lib_event_counts.ipynb\n", "%run ./model_python_lib_decision_functions.ipynb\n", "%run ./python_lib_visualization.ipynb" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00001-8e64b728-54a4-4fff-9c01-0954b5b9d8db", "deepnote_cell_type": "text-cell-h2", "is_collapsed": false, "tags": [] }, "source": [ "## Read data" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "cell_id": "00000-3d202dcb-853a-4886-9724-47c9a98dc76f", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 993, "execution_start": 1636396374710, "source_hash": "91679174", "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
game_idversionis_sona_autocreditsona_experiment_idsona_credit_tokensona_survey_coderound_indexplayer_idis_botbot_strategybot_move_probabilitiesround_begin_tsplayer_moveplayer_rtplayer_outcomeplayer_outcome_viewtimeplayer_pointsplayer_total
030aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320281c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency{'win': {'0': {'rock': {'rock': 0.049999999999...1583904283047rock2091tie395100
130aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320281f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency{'win': {'0': {'rock': {'rock': 0.049999999999...1583904283047rock0tie000
230aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320282c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency{'win': {'0': {'rock': {'rock': 0.049999999999...1583904289171paper2886loss1741-10
330aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320282f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency{'win': {'0': {'rock': {'rock': 0.049999999999...1583904289171scissors0win030
430aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320283c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency{'win': {'0': {'rock': {'rock': 0.049999999999...1583904293847scissors1676tie20400-1
\n", "
" ], "text/plain": [ " game_id version is_sona_autocredit \\\n", "0 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "1 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "2 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "3 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "4 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "\n", " sona_experiment_id sona_credit_token sona_survey_code \\\n", "0 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "1 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "2 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "3 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "4 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "\n", " round_index player_id is_bot \\\n", "0 1 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "1 1 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "2 2 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "3 2 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "4 3 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "\n", " bot_strategy \\\n", "0 outcome_transition_dual_dependency \n", "1 outcome_transition_dual_dependency \n", "2 outcome_transition_dual_dependency \n", "3 outcome_transition_dual_dependency \n", "4 outcome_transition_dual_dependency \n", "\n", " bot_move_probabilities round_begin_ts \\\n", "0 {'win': {'0': {'rock': {'rock': 0.049999999999... 1583904283047 \n", "1 {'win': {'0': {'rock': {'rock': 0.049999999999... 1583904283047 \n", "2 {'win': {'0': {'rock': {'rock': 0.049999999999... 1583904289171 \n", "3 {'win': {'0': {'rock': {'rock': 0.049999999999... 1583904289171 \n", "4 {'win': {'0': {'rock': {'rock': 0.049999999999... 1583904293847 \n", "\n", " player_move player_rt player_outcome player_outcome_viewtime \\\n", "0 rock 2091 tie 3951 \n", "1 rock 0 tie 0 \n", "2 paper 2886 loss 1741 \n", "3 scissors 0 win 0 \n", "4 scissors 1676 tie 2040 \n", "\n", " player_points player_total \n", "0 0 0 \n", "1 0 0 \n", "2 -1 0 \n", "3 3 0 \n", "4 0 -1 " ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df = read_rps_data(os.path.join(\"data\", DEFAULT_FILE))\n", "df.head()" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00005-e210b328-ade5-4c7b-a264-58fdfb285ce1", "deepnote_cell_type": "text-cell-h2", "is_collapsed": false, "tags": [] }, "source": [ "## Add bot predictors" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00006-638c831c-9254-4a53-a1af-138c7abaf82e", "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "*Initialize columns*" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "cell_id": "00006-f43b4014-0727-42db-867e-5635fe4e0a18", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 96, "execution_start": 1636396378090, "source_hash": "df4f7c42", "tags": [] }, "outputs": [], "source": [ "# TODO may want to move these to `utils.py` as globals so that functions in `event_counts.py` can reference them there\n", "# Add columns corresponding to opponent move counts\n", "supplementary_cols = [\n", " 'opponent_move', \n", " 'previous_move', 'opponent_previous_move', \n", " 'previous_outcome', 'opponent_previous_outcome',\n", " 'previous_transition', 'opponent_previous_transition',\n", " 'current_transition',\n", " 'opponent_prev2_transition'\n", "]\n", "\n", "event_counts = [\n", " # transition counts\n", " 'up_transition_count', 'down_transition_count', 'stay_transition_count',\n", " 'cournot_up_transition_count', 'cournot_down_transition_count', 'cournot_stay_transition_count',\n", " # outcome-dependent transition counts\n", " 'win_up_count', 'win_down_count', 'win_stay_count', \n", " 'loss_up_count', 'loss_down_count', 'loss_stay_count',\n", " 'tie_up_count', 'tie_down_count', 'tie_stay_count', \n", " # dual transition outcome counts\n", " 'up_win_up_count', 'up_win_down_count', 'up_win_stay_count', \n", " 'up_loss_up_count', 'up_loss_down_count', 'up_loss_stay_count',\n", " 'up_tie_up_count', 'up_tie_down_count', 'up_tie_stay_count',\n", " 'down_win_up_count', 'down_win_down_count', 'down_win_stay_count',\n", " 'down_loss_up_count', 'down_loss_down_count', 'down_loss_stay_count',\n", " 'down_tie_up_count', 'down_tie_down_count', 'down_tie_stay_count',\n", " 'stay_win_up_count', 'stay_win_down_count', 'stay_win_stay_count',\n", " 'stay_loss_up_count', 'stay_loss_down_count', 'stay_loss_stay_count',\n", " 'stay_tie_up_count', 'stay_tie_down_count', 'stay_tie_stay_count'\n", "]\n", "\n", "opponent_counts = [\n", " # base move counts\n", " 'opponent_rock_count', 'opponent_paper_count', 'opponent_scissors_count',\n", " # transition counts\n", " 'opponent_up_transition_count', 'opponent_down_transition_count', 'opponent_stay_transition_count', \n", " 'opponent_cournot_up_transition_count', 'opponent_cournot_down_transition_count', 'opponent_cournot_stay_transition_count',\n", " # outcome-dependent transition counts \n", " 'opponent_win_up_count', 'opponent_win_down_count', 'opponent_win_stay_count',\n", " 'opponent_loss_up_count', 'opponent_loss_down_count', 'opponent_loss_stay_count',\n", " 'opponent_tie_up_count', 'opponent_tie_down_count', 'opponent_tie_stay_count',\n", " # dual transition outcome counts\n", " 'opponent_up_win_up_count', 'opponent_up_win_down_count', 'opponent_up_win_stay_count', \n", " 'opponent_up_loss_up_count', 'opponent_up_loss_down_count', 'opponent_up_loss_stay_count',\n", " 'opponent_up_tie_up_count', 'opponent_up_tie_down_count', 'opponent_up_tie_stay_count',\n", " 'opponent_down_win_up_count', 'opponent_down_win_down_count', 'opponent_down_win_stay_count',\n", " 'opponent_down_loss_up_count', 'opponent_down_loss_down_count', 'opponent_down_loss_stay_count',\n", " 'opponent_down_tie_up_count', 'opponent_down_tie_down_count', 'opponent_down_tie_stay_count',\n", " 'opponent_stay_win_up_count', 'opponent_stay_win_down_count', 'opponent_stay_win_stay_count',\n", " 'opponent_stay_loss_up_count', 'opponent_stay_loss_down_count', 'opponent_stay_loss_stay_count',\n", " 'opponent_stay_tie_up_count', 'opponent_stay_tie_down_count', 'opponent_stay_tie_stay_count'\n", "]\n", "\n", "\n", "df = add_col(df, supplementary_cols, value = '')\n", "df = add_col(df, event_counts, value = 1)\n", "df = add_col(df, opponent_counts, value = 1)\n", "\n", "# df.head()\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00008-81c86be8-9b78-443f-ac33-1e42050a76ce", "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "*Populate new columns with counts*" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "cell_id": "00008-2d92d9b3-5042-426c-9991-f96ea6a0e2e6", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 630750, "execution_start": 1636396385614, "source_hash": "67f2d5f2", "tags": [] }, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
game_idversionis_sona_autocreditsona_experiment_idsona_credit_tokensona_survey_coderound_indexplayer_idis_botbot_strategy...opponent_down_tie_stay_countopponent_stay_win_up_countopponent_stay_win_down_countopponent_stay_win_stay_countopponent_stay_loss_up_countopponent_stay_loss_down_countopponent_stay_loss_stay_countopponent_stay_tie_up_countopponent_stay_tie_down_countopponent_stay_tie_stay_count
030aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320281c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency...1111111111
130aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320281f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency...1111111111
230aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320282c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency...1111111111
330aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320282f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency...1111111111
430aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320283c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency...1111111111
530aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320283f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency...1111111111
630aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320284c82739a2-9e24-4c1b-840f-9eb00d10cd9b0outcome_transition_dual_dependency...1111111111
730aea025-4c7b-41c0-bd03-363cc73bf58321176819421bc286424246b6b1e873e7a55a8e320284f1c3a029-e381-4f79-afbe-677ef9416f041outcome_transition_dual_dependency...1111111111
\n", "

8 rows × 114 columns

\n", "
" ], "text/plain": [ " game_id version is_sona_autocredit \\\n", "0 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "1 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "2 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "3 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "4 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "5 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "6 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "7 30aea025-4c7b-41c0-bd03-363cc73bf583 2 1 \n", "\n", " sona_experiment_id sona_credit_token sona_survey_code \\\n", "0 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "1 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "2 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "3 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "4 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "5 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "6 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "7 1768 19421bc286424246b6b1e873e7a55a8e 32028 \n", "\n", " round_index player_id is_bot \\\n", "0 1 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "1 1 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "2 2 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "3 2 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "4 3 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "5 3 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "6 4 c82739a2-9e24-4c1b-840f-9eb00d10cd9b 0 \n", "7 4 f1c3a029-e381-4f79-afbe-677ef9416f04 1 \n", "\n", " bot_strategy ... opponent_down_tie_stay_count \\\n", "0 outcome_transition_dual_dependency ... 1 \n", "1 outcome_transition_dual_dependency ... 1 \n", "2 outcome_transition_dual_dependency ... 1 \n", "3 outcome_transition_dual_dependency ... 1 \n", "4 outcome_transition_dual_dependency ... 1 \n", "5 outcome_transition_dual_dependency ... 1 \n", "6 outcome_transition_dual_dependency ... 1 \n", "7 outcome_transition_dual_dependency ... 1 \n", "\n", " opponent_stay_win_up_count opponent_stay_win_down_count \\\n", "0 1 1 \n", "1 1 1 \n", "2 1 1 \n", "3 1 1 \n", "4 1 1 \n", "5 1 1 \n", "6 1 1 \n", "7 1 1 \n", "\n", " opponent_stay_win_stay_count opponent_stay_loss_up_count \\\n", "0 1 1 \n", "1 1 1 \n", "2 1 1 \n", "3 1 1 \n", "4 1 1 \n", "5 1 1 \n", "6 1 1 \n", "7 1 1 \n", "\n", " opponent_stay_loss_down_count opponent_stay_loss_stay_count \\\n", "0 1 1 \n", "1 1 1 \n", "2 1 1 \n", "3 1 1 \n", "4 1 1 \n", "5 1 1 \n", "6 1 1 \n", "7 1 1 \n", "\n", " opponent_stay_tie_up_count opponent_stay_tie_down_count \\\n", "0 1 1 \n", "1 1 1 \n", "2 1 1 \n", "3 1 1 \n", "4 1 1 \n", "5 1 1 \n", "6 1 1 \n", "7 1 1 \n", "\n", " opponent_stay_tie_stay_count \n", "0 1 \n", "1 1 \n", "2 1 \n", "3 1 \n", "4 1 \n", "5 1 \n", "6 1 \n", "7 1 \n", "\n", "[8 rows x 114 columns]" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "game_split = separate_df(df)\n", "game_split = get_event_counts(df, game_split)\n", "df = pd.concat(game_split).reset_index(drop = True)\n", "\n", "df.head(8)" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00008-b6e8e569-27ba-42cf-b3c3-a6d0976852a5", "deepnote_cell_type": "text-cell-h2", "is_collapsed": false, "tags": [] }, "source": [ "## Compute probabilities" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "cell_id": "00008-434d6546-4492-4ec9-b56f-d799a75a5f4b", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 109, "execution_start": 1636397022950, "source_hash": "ae0f9e46", "tags": [] }, "outputs": [], "source": [ "# TODO make these programmatic (esp. final set)\n", "\n", "# Move baserate probabilities\n", "df = add_prob(\n", " df, \n", " 'opponent_rock_count', 'opponent_paper_count', 'opponent_scissors_count', \n", " 'p_opponent_rock', 'p_opponent_paper', 'p_opponent_scissors'\n", ")\n", "# Transition probabilities\n", "df = add_prob(\n", " df, \n", " 'opponent_up_transition_count', 'opponent_down_transition_count', 'opponent_stay_transition_count', \n", " 'p_opponent_transition_up', 'p_opponent_transition_down', 'p_opponent_transition_stay'\n", ")\n", "df = add_prob(\n", " df, \n", " 'opponent_cournot_up_transition_count', 'opponent_cournot_down_transition_count', 'opponent_cournot_stay_transition_count', \n", " 'p_opponent_cournot_transition_up', 'p_opponent_cournot_transition_down', 'p_opponent_cournot_transition_stay'\n", ")\n", "# Outcome-transition probabilities\n", "df = add_prob(\n", " df,\n", " 'opponent_win_up_count', 'opponent_win_down_count', 'opponent_win_stay_count',\n", " 'p_opponent_win_up', 'p_opponent_win_down', 'p_opponent_win_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_loss_up_count', 'opponent_loss_down_count', 'opponent_loss_stay_count',\n", " 'p_opponent_loss_up', 'p_opponent_loss_down', 'p_opponent_loss_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_tie_up_count', 'opponent_tie_down_count', 'opponent_tie_stay_count',\n", " 'p_opponent_tie_up', 'p_opponent_tie_down', 'p_opponent_tie_stay'\n", ")\n", "\n", "# Dual transition outcome probabilities\n", "df = add_prob(\n", " df,\n", " 'opponent_up_win_up_count', 'opponent_up_win_down_count', 'opponent_up_win_stay_count',\n", " 'p_opponent_up_win_up', 'p_opponent_up_win_down', 'p_opponent_up_win_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_up_loss_up_count', 'opponent_up_loss_down_count', 'opponent_up_loss_stay_count',\n", " 'p_opponent_up_loss_up', 'p_opponent_up_loss_down', 'p_opponent_up_loss_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_up_tie_up_count', 'opponent_up_tie_down_count', 'opponent_up_tie_stay_count',\n", " 'p_opponent_up_tie_up', 'p_opponent_up_tie_down', 'p_opponent_up_tie_stay'\n", ")\n", "\n", "df = add_prob(\n", " df,\n", " 'opponent_down_win_up_count', 'opponent_down_win_down_count', 'opponent_down_win_stay_count',\n", " 'p_opponent_down_win_up', 'p_opponent_down_win_down', 'p_opponent_down_win_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_down_loss_up_count', 'opponent_down_loss_down_count', 'opponent_down_loss_stay_count',\n", " 'p_opponent_down_loss_up', 'p_opponent_down_loss_down', 'p_opponent_down_loss_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_down_tie_up_count', 'opponent_down_tie_down_count', 'opponent_down_tie_stay_count',\n", " 'p_opponent_down_tie_up', 'p_opponent_down_tie_down', 'p_opponent_down_tie_stay'\n", ")\n", "\n", "df = add_prob(\n", " df,\n", " 'opponent_stay_win_up_count', 'opponent_stay_win_down_count', 'opponent_stay_win_stay_count',\n", " 'p_opponent_stay_win_up', 'p_opponent_stay_win_down', 'p_opponent_stay_win_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_stay_loss_up_count', 'opponent_stay_loss_down_count', 'opponent_stay_loss_stay_count',\n", " 'p_opponent_stay_loss_up', 'p_opponent_stay_loss_down', 'p_opponent_stay_loss_stay'\n", ")\n", "df = add_prob(\n", " df,\n", " 'opponent_stay_tie_up_count', 'opponent_stay_tie_down_count', 'opponent_stay_tie_stay_count',\n", " 'p_opponent_stay_tie_up', 'p_opponent_stay_tie_down', 'p_opponent_stay_tie_stay'\n", ")\n", "\n", "\n", "# df.head(8)" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00012-ebb890e5-572e-497c-bf47-6946d37674e9", "deepnote_cell_type": "text-cell-h2", "is_collapsed": false, "tags": [] }, "source": [ "## Calculate Expected Value" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00013-c7fe1395-86f6-412e-91d2-1e5b57b1e95d", "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "*Filter bot rows, any rows without interpretable values for previous move, etc.*" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "cell_id": "00013-4619e71c-16fe-4bf8-b59c-fa03340eb40b", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 549, "execution_start": 1636397032454, "source_hash": "be2813bd", "tags": [] }, "outputs": [], "source": [ "# Filter out non-nan, agent-only rows and add expected value calculations\n", "df_agent = drop_bot_rows(df)\n", "\n", "\n", "df_agent = df_agent.dropna(subset=['previous_move']) # TODO why is this necessary?\n", "# NB: need this for cournot transition tracking\n", "df_agent = df_agent[df_agent['previous_move'] != 'none']\n", "df_agent = df_agent[df_agent['previous_move'] != '']\n", "df_agent = df_agent[df_agent['opponent_previous_move'] != '']\n", "\n", "# outcome-transition\n", "df_agent = df_agent[df_agent['opponent_previous_transition'] != '']\n", "df_agent = df_agent[df_agent['opponent_prev2_transition'] != '']\n", "\n", "\n", "# df_agent.head(25)\n", "# df_agent.shape\n" ] }, { "cell_type": "markdown", "metadata": { "cell_id": "00015-c7067e8c-b882-425a-a15b-1834cfae2710", "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "*Compute expected value*" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "cell_id": "00015-75f861eb-5cfe-48a9-af69-dcbc458890f9", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 8775, "execution_start": 1636397043287, "source_hash": "9023dd3f", "tags": [] }, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ ":75: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['filler_1'] = points_lookup(agent_move, transition_move_lookup(\"transition_up\", df_agent[move])).tolist()\n", ":76: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['filler_2'] = points_lookup(agent_move, transition_move_lookup(\"transition_down\", df_agent[move])).tolist()\n", ":77: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['filler_3'] = points_lookup(agent_move, transition_move_lookup(\"transition_stay\", df_agent[move])).tolist()\n", ":78: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['prod_1'] = df_agent[prob1] * df_agent.filler_1\n", ":79: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['prod_2'] = df_agent[prob2] * df_agent.filler_2\n", ":80: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['prod_3'] = df_agent[prob3] * df_agent.filler_3\n", ":81: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent['ev_transitions'] = df_agent['prod_1'] + df_agent['prod_2'] + df_agent['prod_3']\n", ":83: SettingWithCopyWarning: \n", "A value is trying to be set on a copy of a slice from a DataFrame.\n", "Try using .loc[row_indexer,col_indexer] = value instead\n", "\n", "See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy\n", " df_agent[ev_col_name] = df_agent['ev_transitions']\n" ] } ], "source": [ "# EV for move base rates\n", "df_agent = ev_move_baserate(df_agent)\n", "\n", "# EV for transitions\n", "df_agent = ev_transitions(df_agent)\n", "df_agent = ev_cournot(df_agent)\n", "\n", "# EV for outcome-transitions\n", "df_agent = ev_previous_outcome(df_agent)\n", "\n", "# EV for dual outcome-transitions\n", "df_agent = ev_previous_outcome_previous_transition(df_agent)\n", "\n", "\n", "# df_agent.head(6)\n", "# df_agent.shape" ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "cell_id": "00019-4781a375-63cf-4687-8fac-427a9f39bb95", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 148955, "execution_start": 1636397058553, "source_hash": "7e520c2f", "tags": [] }, "outputs": [], "source": [ "# Test\n", "# model7 = df_agent.copy()\n", "\n", "\n", "# model7['ev_rock'] = model7['ev_outcome_dual_depend_rock']\n", "# model7['ev_paper'] = model7['ev_outcome_dual_depend_paper']\n", "# model7['ev_scissors'] = model7['ev_outcome_dual_depend_scissors']\n", "\n", "# # Compute softmax distribution (1 min.)\n", "# m7_softmax = get_softmax_probabilities(\n", "# model7, \n", "# ['ev_rock', 'ev_paper', 'ev_scissors']\n", "# )\n", "\n", "# # Select agent move based on softmax computed above (1 min.)\n", "# model7 = pick_move(model7, m7_softmax)\n", "\n", "# # Evaluate outcome of agent move choices in simulation above\n", "# model7 = assign_agent_outcomes(model7)\n" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "cell_id": "00020-417aa27a-1a93-4e4f-9034-969a322b70ef", "deepnote_cell_type": "code", "deepnote_to_be_reexecuted": false, "execution_millis": 448, "execution_start": 1636397215017, "source_hash": "72f4d14a", "tags": [] }, "outputs": [], "source": [ "# plot_summary_coarse = win_summary(groupby_f_data(model7, 'agent_outcome', 5), 'agent_outcome')\n", "# plot_summary_fine = win_summary(groupby_f_data(model7, 'agent_outcome', 60), 'agent_outcome')\n", "# plot_summary_fine = plot_summary_fine[plot_summary_fine['bin'] <= '50']\n", "\n" ] }, { "cell_type": "code", "execution_count": 10, "metadata": { "cell_id": "00021-970541fb-edd0-4d05-ab90-1b2e09a4e8ab", "deepnote_cell_type": "code", "deepnote_output_heights": [ 21, 575.390625 ], "deepnote_to_be_reexecuted": false, "execution_millis": 875, "execution_start": 1636397220785, "source_hash": "b9dbc1", "tags": [] }, "outputs": [], "source": [ "# plot_win_rates(plot_summary_coarse)\n", "# plot_win_rates(plot_summary_fine)" ] }, { "cell_type": "markdown", "metadata": { "created_in_deepnote_cell": true, "deepnote_cell_type": "markdown", "tags": [] }, "source": [ "\n", "Created in deepnote.com \n", "Created in Deepnote" ] } ], "metadata": { "deepnote": { "is_reactive": false }, "deepnote_execution_queue": [], "deepnote_notebook_id": "8fb43daf-8125-4927-aa6f-9b19e8ee2760", "kernelspec": { "display_name": "Python 3", "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.8.5" } }, "nbformat": 4, "nbformat_minor": 2 }