{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "2KoygnSiDYjc"
   },
   "source": [
    "### **Setting**\n",
    "\n",
    "Assume $P_{X|0} ~conforms~ N(0,1)$, $P_I ~conforms~ N(0.5,1)$, $P_{X|1} ~conforms~ N(1,1)$\n",
    "\n",
    "First let $ϵ$ be 0\n",
    "\n",
    "Assume $C_S - C_I = N(0,0.25)$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "5bI16UC5Et6w"
   },
   "source": [
    "#### **1. Get the cdf, pdf, and visualize the distributions**"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "id": "vwz_zyQtC--o"
   },
   "outputs": [],
   "source": [
    "import scipy.stats\n",
    "from scipy.stats import norm\n",
    "from scipy.stats import beta\n",
    "from scipy.stats import uniform\n",
    "from matplotlib import pyplot as plt\n",
    "plt.style.use('seaborn-paper')\n",
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 123,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 339
    },
    "id": "uo73nY76F9Rq",
    "outputId": "b1730a47-2a2d-41d8-fa7b-cc0b83ad8753"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/1742868580.py:8: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"r-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.pdf(x, mu_0, sd),'r-', linestyle = 'solid', label='unqualified')\n",
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/1742868580.py:9: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"g-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.pdf(x, mu_1, sd),'g-', linestyle = 'dashed',label='fail to improve')\n",
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/1742868580.py:10: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"b-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.pdf(x, mu_2, sd),'b-', linestyle = 'dotted', label='qualified')\n",
      "The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 30856 characters]\n",
      "text/plain": [
       "<Figure size 400x250 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize the pdf\n",
    "mu_0 = 0\n",
    "mu_1 = 0.5\n",
    "mu_2 = 1\n",
    "sd = 1\n",
    "x = np.linspace(-4,4,1000)\n",
    "plt.figure(figsize = (4,2.5))\n",
    "plt.plot(x, norm.pdf(x, mu_0, sd),'r-', linestyle = 'solid', label='unqualified')\n",
    "plt.plot(x, norm.pdf(x, mu_1, sd),'g-', linestyle = 'dashed',label='fail to improve')\n",
    "plt.plot(x, norm.pdf(x, mu_2, sd),'b-', linestyle = 'dotted', label='qualified')\n",
    "plt.xticks(fontsize=12)\n",
    "plt.yticks(fontsize=12)\n",
    "plt.xlabel('x',size=12)\n",
    "plt.ylabel('pdf',size=12)\n",
    "plt.legend()\n",
    "plt.savefig('gaussian.eps',bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 339
    },
    "id": "f3nZQof4HRw3",
    "outputId": "9cc67568-fcc1-42f3-b1ca-ecbb4e3004fd"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/4098113835.py:8: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"r-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.cdf(x, mu_0, sd),'r-',linestyle = 'solid' ,label='unqualified')\n",
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/4098113835.py:9: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"g-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.cdf(x, mu_1, sd),'g-', linestyle = 'dashed',label='fail to improve')\n",
      "/var/folders/sd/fg0lfp196c38z2cl19tc1sc00000gn/T/ipykernel_91873/4098113835.py:10: UserWarning: linestyle is redundantly defined by the 'linestyle' keyword argument and the fmt string \"b-\" (-> linestyle='-'). The keyword argument will take precedence.\n",
      "  plt.plot(x, norm.cdf(x, mu_2, sd),'b-', linestyle = 'dotted',label='qualified')\n",
      "The PostScript backend does not support transparency; partially transparent artists will be rendered opaque.\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 26368 characters]\n",
      "text/plain": [
       "<Figure size 400x250 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize the cdf\n",
    "mu_0 = 0\n",
    "mu_1 = 0.5\n",
    "mu_2 = 1\n",
    "sd = 1\n",
    "x = np.linspace(-4,4,1000)\n",
    "plt.figure(figsize = (4,2.5))\n",
    "plt.plot(x, norm.cdf(x, mu_0, sd),'r-',linestyle = 'solid' ,label='unqualified')\n",
    "plt.plot(x, norm.cdf(x, mu_1, sd),'g-', linestyle = 'dashed',label='fail to improve')\n",
    "plt.plot(x, norm.cdf(x, mu_2, sd),'b-', linestyle = 'dotted',label='qualified')\n",
    "plt.xticks(fontsize=12)\n",
    "plt.yticks(fontsize=12)\n",
    "plt.xlabel('x',size=12)\n",
    "plt.ylabel('cdf',size=12)\n",
    "plt.legend()\n",
    "plt.savefig('gaussian_cdf.eps',bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 339
    },
    "id": "VIcI0WeuJ3WB",
    "outputId": "047d4fc8-b89c-463b-d616-be574d1d41fd"
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7fa4d9a00c40>"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 33444 characters]\n",
      "text/plain": [
       "<Figure size 800x500 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# Visualize Ci-Cs\n",
    "mu_c = 0\n",
    "std_c = 0.25\n",
    "x = np.linspace(-1,1,1000)\n",
    "plt.figure(figsize = (8,5))\n",
    "plt.plot(x, norm.pdf(x, mu_c, std_c),'r-', lw=3, alpha=0.6, label='cost-pdf')\n",
    "plt.plot(x, norm.cdf(x, mu_c, std_c),'b-', lw=3, alpha=0.6, label='cost-cdf')\n",
    "plt.legend()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "EUvsDmmqINj1"
   },
   "source": [
    "#### 2. Optimal Decision Maker vs Strategic Optimal Decision Maker\n",
    "\n",
    "For non-strategic decision maker, we assume $u^+ = u^- = 1$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {
    "id": "1JS54d7UIZxj"
   },
   "outputs": [],
   "source": [
    "eps = 0.2\n",
    "u = 1\n",
    "q = 0.5\n",
    "alpha = 0.6\n",
    "\n",
    "# Optimal Threshold without strategy\n",
    "def get_opt_theta_no_strategic():\n",
    "    \"\"\"\n",
    "    get optimal threshold under non-strategic situation\n",
    "    \"\"\"\n",
    "    val = u*(1-alpha)/(u*alpha)\n",
    "    from scipy.optimize import fsolve\n",
    "    func = lambda theta: norm.pdf(theta,mu_2,sd)/norm.pdf(theta,mu_0,sd) - val\n",
    "    root = fsolve(func,0.2)\n",
    "    return root[0]\n",
    "\n",
    "opt_no = get_opt_theta_no_strategic()\n",
    "\n",
    "def get_nonstrat_utility(theta):\n",
    "    return u*alpha*(1-norm.cdf(theta,mu_2,sd))-u*(1-alpha)*(1-norm.cdf(theta,mu_0,sd))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "CKmn89yZyanh",
    "outputId": "7362dd33-2d3f-4e50-d24f-e756c7369b89"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "non-strategic optimal threshold is 0.30549743564359466\n",
      "non-strategic utility is 0.09453489189183595\n"
     ]
    }
   ],
   "source": [
    "U_no = get_nonstrat_utility(opt_no)\n",
    "print('non-strategic optimal threshold is', U_no)\n",
    "print('non-strategic utility is', opt_no)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "HKpfQBC2yqpj"
   },
   "source": [
    "### Strategic Utility\n",
    "\n",
    "We decompose strategic utility into two parts:\n",
    "\n",
    "Non-strategic utility and $\\Phi(\\theta)$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get manipulation Prob\n",
    "def get_manip_prob(theta, q, eps):\n",
    "    \"\"\"\n",
    "    function to get manip prob under q and eps\n",
    "    \"\"\"\n",
    "    value = (1-q)*(norm.cdf(theta, mu_1, sd) - norm.cdf(theta, mu_2, sd)) - eps*(1 - norm.cdf(theta, mu_2, sd))\n",
    "    return norm.cdf(value, mu_c, std_c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Get Phi, which is strategic - non-strategic\n",
    "def get_Phi(theta, q, eps, k1 = 1, k2 = 1, k3 = 1):\n",
    "    \"\"\"\n",
    "    Get Phi, which is strategic - non-strategic\n",
    "    k1, k2, k3 measure our preferences for 3 kinds of behaviors\n",
    "    larger k1 means we prefer improvement\n",
    "    larger k2 means we do not prefer failure of improvement\n",
    "    larger k3 means we do not prefer manipulation\n",
    "    k1=k2=k3=1 is the naive difference\n",
    "    \"\"\"\n",
    "    # manipulation probability\n",
    "    pm = get_manip_prob(theta, q, eps)\n",
    "    # Reverse Benefit\n",
    "    b1 = k1*(1-pm)*q*(2 - norm.cdf(theta, mu_0, sd) - norm.cdf(theta, mu_2, sd))\n",
    "    # Loss of Failure of Improvement \n",
    "    l1 = k2*(1-pm)*(1-q)*(norm.cdf(theta, mu_0, sd) - norm.cdf(theta, mu_1, sd))\n",
    "    # Loss of manipulation\n",
    "    l2 = k3*pm*((1-eps)*(1 - norm.cdf(theta,mu_2,sd))-(1-norm.cdf(theta,mu_0,sd)))\n",
    "    return u*(1-alpha)*(b1-l1-l2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "id": "Y5NtbTg2WUVW"
   },
   "outputs": [],
   "source": [
    "def get_strat_optimal(q, eps, k1=1, k2=1, k3=1):\n",
    "    def get_strat_utility(theta, k10 = k1, k20 = k2, k30 = k3, q0 = q, eps0 = eps):\n",
    "        phi = get_Phi(theta,q0,eps0,k10,k20,k30)\n",
    "        U = get_nonstrat_utility(theta) + phi\n",
    "        return -U\n",
    "\n",
    "    from scipy.optimize import minimize_scalar\n",
    "    res = minimize_scalar(get_strat_utility)\n",
    "    return res.x, -get_strat_utility(res.x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(-15.32623793723726, 0.7076289202833207)"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "get_strat_optimal(0.5,0.2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "metadata": {
    "id": "DEU1pohsbFO6"
   },
   "outputs": [],
   "source": [
    "# for fixed q, vary eps\n",
    "def vary_eps(q_list = [0,0.3,0.6,1], eps_list = np.linspace(0,1,100)):\n",
    "  for i in range(len(q_list)):\n",
    "      q = q_list[i]\n",
    "      x = np.zeros(100)\n",
    "      u = np.zeros(100)\n",
    "      m = np.zeros(100)\n",
    "      j = 0\n",
    "      for eps in eps_list:\n",
    "        x1,u1 = get_strat_optimal(q,eps)\n",
    "        x[j] = x1\n",
    "        u[j] = u1\n",
    "        m[j] = get_manip_prob(x1,q,eps)\n",
    "        j += 1\n",
    "      df = pd.DataFrame({'eps': np.linspace(0,1,100), 'theta_max': x, 'max utility': u, 'manipulation prob': m})\n",
    "      axes[int(i/2)][int(i%2)].tick_params(axis='both', which='major', labelsize=12)\n",
    "      axes[int(i/2)][int(i%2)].plot(df['eps'], df['theta_max'],'r-', lw=3, alpha=0.6, label=r'$\\theta^*$')\n",
    "      axes[int(i/2)][int(i%2)].plot(np.linspace(0,1,100), [opt_no]*100, 'b-', label = r'$\\widehat{\\theta^*}$')\n",
    "      axes[int(i/2)][int(i%2)].set_xlabel(r'$\\epsilon$',fontsize = 12)\n",
    "      axes[int(i/2)][int(i%2)].set_ylabel(r'$\\theta$',fontsize=12)\n",
    "      axes[int(i/2)][int(i%2)].legend(fontsize = 11)\n",
    "      axes[int(i/2)][int(i%2)].set_title('q = %.2f'%q, fontsize=12)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 119,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 1000
    },
    "id": "Cv9Qg9eskCp_",
    "outputId": "b4ff6dbf-0917-4324-8994-f4815406c09c",
    "scrolled": false
   },
   "outputs": [],
   "source": [
    "fig, axes = plt.subplots(2,2)\n",
    "fig.set_size_inches(8, 6)\n",
    "fig.tight_layout(pad=3.0)\n",
    "vary_eps()\n",
    "fig.savefig('threshold.eps',bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 120,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 50196 characters]\n",
      "text/plain": [
       "<Figure size 800x600 with 4 Axes>"
      ]
     },
     "execution_count": 120,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "fig"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Illustration of different parameter combinations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "metadata": {},
   "outputs": [],
   "source": [
    "def strat_u(theta, q, eps):\n",
    "    value = (1-q)*(norm.cdf(theta, mu_1, sd) - norm.cdf(theta, mu_2, sd)) - eps*(1 - norm.cdf(theta, mu_2, sd))\n",
    "    pm = norm.cdf(value, mu_c, std_c)\n",
    "    U = u_p*(alpha + (1-alpha)*(1-pm)*q)*(1-norm.cdf(theta, mu_2, sd))-u_n*(1-alpha)*((1-eps)*pm*(1-norm.cdf(theta, mu_2, sd)) + (1-pm)*(1-q)*(1-norm.cdf(theta, mu_1, sd)))\n",
    "    return U\n",
    "\n",
    "def non_strat_u(theta):\n",
    "    return u_p*alpha*(1-norm.cdf(theta,mu_2,sd))-u_n*(1-alpha)*(1-norm.cdf(theta,mu_0,sd))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 131,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22492 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "u_p = u_n = 1\n",
    "alpha = 0.6\n",
    "eps = 0\n",
    "q = 0.5\n",
    "theta = np.linspace(-5,5,1000)\n",
    "func1 = np.vectorize(strat_u, excluded = ['q','eps'])\n",
    "value = func1(theta,q,eps)\n",
    "plt.figure(figsize = (4,3))\n",
    "plt.plot(theta, value,color = 'blue')\n",
    "plt.xlabel(r'$\\theta$', fontsize = 12)\n",
    "plt.ylabel('Strategic utility', fontsize = 12)\n",
    "plt.xticks(fontsize = 12)\n",
    "plt.yticks(fontsize = 12)\n",
    "plt.savefig('0050.eps', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22056 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "u_p = u_n = 1\n",
    "alpha = 0.6\n",
    "eps = 0.75\n",
    "q = 0.25\n",
    "theta = np.linspace(-5,5,1000)\n",
    "func1 = np.vectorize(strat_u, excluded = ['q','eps'])\n",
    "value = func1(theta,q,eps)\n",
    "plt.figure(figsize = (4,3))\n",
    "plt.plot(theta, value, color = 'blue')\n",
    "plt.xlabel(r'$\\theta$', fontsize = 12)\n",
    "plt.ylabel('Strategic utility', fontsize = 12)\n",
    "plt.xticks(fontsize = 12)\n",
    "plt.yticks(fontsize = 12)\n",
    "plt.savefig('7525.eps', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 133,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 19644 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "u_p = u_n = 1\n",
    "alpha = 0.3\n",
    "eps = 0\n",
    "q = 0.5\n",
    "theta = np.linspace(-5,5,1000)\n",
    "func1 = np.vectorize(strat_u, excluded = ['q','eps'])\n",
    "value = func1(theta,q,eps)\n",
    "plt.figure(figsize = (4,3))\n",
    "plt.plot(theta, value,color = 'blue')\n",
    "plt.xlabel(r'$\\theta$', fontsize = 12)\n",
    "plt.ylabel('Strategic utility', fontsize = 12)\n",
    "plt.xticks(fontsize = 12)\n",
    "plt.yticks(fontsize = 12)\n",
    "plt.savefig('0050n.eps', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 134,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 22380 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "u_p = u_n = 1\n",
    "alpha = 0.3\n",
    "eps = 0.75\n",
    "q = 0.25\n",
    "theta = np.linspace(-5,5,1000)\n",
    "func1 = np.vectorize(strat_u, excluded = ['q','eps'])\n",
    "value = func1(theta,q,eps)\n",
    "plt.figure(figsize = (4,3))\n",
    "plt.plot(theta, value,color = 'blue')\n",
    "plt.xlabel(r'$\\theta$', fontsize = 12)\n",
    "plt.ylabel('Strategic utility', fontsize = 12)\n",
    "plt.xticks(fontsize = 12)\n",
    "plt.yticks(fontsize = 12)\n",
    "plt.savefig('7525n.eps', bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Different Policies\n",
    "\n",
    "Visualize the influences of different $k_1,k_2$\n",
    "\n",
    "\n",
    "#### k1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 138,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for fixed q, vary eps\n",
    "\n",
    "def vary_eps_k1(q, eps_list = np.linspace(0,1,100), k_list = [100], filename = 'k1.eps'):\n",
    "    fig, ax1 = plt.subplots(1, 1)\n",
    "    fig.set_size_inches(4,3)\n",
    "    ax1.plot(eps_list, [opt_no]*100, 'b-', label = r'$\\widehat{\\theta^*}$')\n",
    "    for k in k_list:\n",
    "        x = np.zeros(100)\n",
    "        u = np.zeros(100)\n",
    "        us = np.zeros(100)\n",
    "        m = np.zeros(100)\n",
    "        i = 0  \n",
    "        for eps in eps_list:\n",
    "            x1,u1 = get_strat_optimal(q,eps, k1 = k, k2 = 1, k3 = 1)\n",
    "            if abs(x1) > 10:\n",
    "                if x1 != abs(x1):\n",
    "                    x1 = -10\n",
    "                else:\n",
    "                    x1 = 10\n",
    "            x[i] = x1\n",
    "            phi = get_Phi(x1,q,eps,1,1,1)\n",
    "            U = get_nonstrat_utility(x1) + phi\n",
    "            u[i] = U\n",
    "            us[i] = u1\n",
    "            m[i] = get_manip_prob(x1,q,eps)\n",
    "            i += 1\n",
    "    \n",
    "        df = pd.DataFrame({'eps': np.linspace(0,1,100), 'theta_max': x, 'max utility': u, 'manipulation prob': m})\n",
    "        ax1.plot(df['eps'], df['theta_max'], lw=2, alpha=0.6, label=r'$\\theta^*(k_1 = %.2f)$'%k, color = 'red')\n",
    "\n",
    "    ax1.legend(fontsize=12)\n",
    "    ax1.tick_params(axis='both', which='major', labelsize=14)\n",
    "    ax1.set_xlabel(r'$\\epsilon$',fontsize = 14)\n",
    "    ax1.set_ylabel(r'$\\theta$',fontsize = 14)\n",
    "#     fig.savefig('k2069.eps',bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 139,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 17952 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 17952 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha = 0.6\n",
    "opt_no = get_opt_theta_no_strategic()\n",
    "U_no = get_nonstrat_utility(opt_no)\n",
    "vary_eps_k1(q = 0.01, k_list = [10], filename = 'k1utility.eps')\n",
    "vary_eps_k1(q = 0.01, k_list = [10], filename = 'k1utility2.eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 140,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15364 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 15364 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha = 0.3\n",
    "opt_no = get_opt_theta_no_strategic()\n",
    "U_no = get_nonstrat_utility(opt_no)\n",
    "vary_eps_k1(q = 0.01, k_list = [100], filename = 'k1031.eps')\n",
    "vary_eps_k1(q = 0.01, k_list = [100], filename = 'k1032.eps')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### k2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 135,
   "metadata": {},
   "outputs": [],
   "source": [
    "# for fixed q, vary eps\n",
    "def vary_eps_k2(q, eps_list = np.linspace(0,1,100), k_list = [100], filename = 'k2.eps'):\n",
    "    fig, ax1 = plt.subplots(1, 1)\n",
    "    fig.set_size_inches(4,3)\n",
    "    ax1.plot(eps_list, [opt_no]*100, 'b-', label = r'$\\widehat{\\theta^*}$')\n",
    "    for k in k_list:\n",
    "        x = np.zeros(100)\n",
    "        u = np.zeros(100)\n",
    "        us = np.zeros(100)\n",
    "        m = np.zeros(100)\n",
    "        i = 0  \n",
    "        for eps in eps_list:\n",
    "            x1,u1 = get_strat_optimal(q,eps, k1 = 1, k2 = k, k3 = 1)\n",
    "            if abs(x1) > 10:\n",
    "                if x1 != abs(x1):\n",
    "                    x1 = -10\n",
    "                else:\n",
    "                    x1 = 10\n",
    "            x[i] = x1\n",
    "            phi = get_Phi(x1,q,eps,1,1,1)\n",
    "            U = get_nonstrat_utility(x1) + phi\n",
    "            u[i] = U\n",
    "            us[i] = u1\n",
    "            m[i] = get_manip_prob(x1,q,eps)\n",
    "            i += 1\n",
    "    \n",
    "        df = pd.DataFrame({'eps': np.linspace(0,1,100), 'theta_max': x, 'max utility': u, 'manipulation prob': m})\n",
    "        ax1.plot(df['eps'], df['theta_max'], lw=2, alpha=0.6, label=r'$\\theta^*(k_2 = %.2f)$'%k, color = 'red')\n",
    "\n",
    "    ax1.legend(fontsize=12)\n",
    "    ax1.tick_params(axis='both', which='major', labelsize=14)\n",
    "    ax1.set_xlabel(r'$\\epsilon$',fontsize = 14)\n",
    "    ax1.set_ylabel(r'$\\theta$',fontsize = 14)\n",
    "#     fig.savefig('k2069.eps',bbox_inches='tight')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 136,
   "metadata": {
    "scrolled": false
   },
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 15672 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 15460 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha = 0.3\n",
    "opt_no = get_opt_theta_no_strategic()\n",
    "U_no = get_nonstrat_utility(opt_no)\n",
    "vary_eps_k2(q = 0.01, k_list = [100], filename = 'k2031.eps')\n",
    "vary_eps_k2(q = 0.99, k_list = [100], filename = 'k2039.eps')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 137,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 21076 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "image/png": "[encoded data, 15456 characters]\n",
      "text/plain": [
       "<Figure size 400x300 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "alpha = 0.6\n",
    "opt_no = get_opt_theta_no_strategic()\n",
    "U_no = get_nonstrat_utility(opt_no)\n",
    "vary_eps_k2(q = 0.01, k_list = [100], filename = 'k2061.eps')\n",
    "vary_eps_k2(q = 0.99, k_list = [100], filename = 'k2069.eps')"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
