{
  "nbformat": 4,
  "nbformat_minor": 0,
  "metadata": {
    "colab": {
      "name": "Swiss health insurance.ipynb",
      "provenance": [],
      "collapsed_sections": []
    },
    "kernelspec": {
      "name": "python3",
      "display_name": "Python 3"
    }
  },
  "cells": [
    {
      "cell_type": "code",
      "metadata": {
        "id": "Blq8CHGm4xIH",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Set up logic\n",
        "\n",
        "class Regulation:\n",
        "\n",
        "  def c(self):\n",
        "    return 0.10\n",
        "\n",
        "  def p_c_max(self):\n",
        "    return 700\n",
        "  \n",
        "  def d_min(self):\n",
        "    return 300\n",
        "\n",
        "  def d_max(self):\n",
        "    return 2500\n",
        "\n",
        "\n",
        "class InsuranceProvider:\n",
        "\n",
        "  def __init__(self, monthly_premium):\n",
        "    self.monthly_premium = monthly_premium\n",
        "\n",
        "  def issue_policy(self, deductible):\n",
        "    return InsurancePolicy(\n",
        "        deductible=deductible,\n",
        "        monthly_premium=self.monthly_premium[deductible])\n",
        "\n",
        "  def deductibles(self):\n",
        "    return sorted(self.monthly_premium.keys())\n",
        "\n",
        "  def monthly_premiums(self):\n",
        "    return [self.monthly_premium[d] for d in self.deductibles()]\n",
        "\n",
        "\n",
        "class InsurancePolicy:\n",
        "\n",
        "  def __init__(self, deductible, monthly_premium):\n",
        "    self.deductible = deductible\n",
        "    self.monthly_premium = monthly_premium\n",
        "    self.regulation = Regulation()\n",
        "    assert(deductible >= self.regulation.d_min())\n",
        "    assert(deductible <= self.regulation.d_max())\n",
        "    assert(monthly_premium >= 0)\n",
        "\n",
        "  def d(self):\n",
        "    return self.deductible\n",
        "\n",
        "  def m(self):\n",
        "    return self.monthly_premium \n",
        "\n",
        "  def c(self):\n",
        "    return self.regulation.c()\n",
        "  \n",
        "  def p_c_max(self):\n",
        "    return self.regulation.p_c_max()\n",
        "\n",
        "\n",
        "class Scenario:\n",
        "\n",
        "  def __init__(self,\n",
        "               policy,\n",
        "               healthcare_services):\n",
        "    assert(healthcare_services >= 0)\n",
        "    self.policy = policy\n",
        "    self.healthcare_services = healthcare_services\n",
        "\n",
        "  def m(self):\n",
        "    return self.policy.m()\n",
        "\n",
        "  def d(self):\n",
        "    return self.policy.d()\n",
        "\n",
        "  def h(self):\n",
        "    return self.healthcare_services\n",
        "\n",
        "  def p(self):\n",
        "    return self.p_d() + self.p_c()\n",
        "\n",
        "  def p_c(self):\n",
        "    return min(\n",
        "        self.policy.c() * max(self.h() - self.d(), 0),\n",
        "        self.policy.p_c_max())\n",
        "\n",
        "  def p_d(self):\n",
        "    return min(self.d(), self.h())\n",
        "\n",
        "  def i(self):\n",
        "    return self.h() - self.p()\n",
        "\n",
        "  def e(self):\n",
        "    return 12 * self.m() + self.p()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "8fG0ekOh4kfM",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Create sample insurance provider\n",
        "\n",
        "insurer = InsuranceProvider({\n",
        "            300: 418,\n",
        "            500: 407,\n",
        "            1000: 379,\n",
        "            1500: 352,\n",
        "            2000: 325,\n",
        "            2500: 298,\n",
        "          })\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "5buDfr4N_cfy",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Set up plotting\n",
        "\n",
        "from matplotlib import pyplot as plt\n",
        "import seaborn\n",
        "\n",
        "_width = 700.0 / (936.0 / 13.0)\n",
        "_output_count = 1\n",
        "\n",
        "def savefigure():\n",
        "  global _output_count\n",
        "  plt.savefig('output%02d' % _output_count)\n",
        "  _output_count = _output_count + 1\n",
        "\n",
        "hs = range(0, 10000, 10)"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "-ggIdFid8EVF",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Consumed health services vs. policyholder contribution\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "d = min(insurer.deductibles())\n",
        "ps = [Scenario(policy=insurer.issue_policy(deductible=d),\n",
        "                healthcare_services=h).p()\n",
        "      for h in hs]\n",
        "ax.set_title('Consumed health services vs. policyholder contribution')\n",
        "ax.set_xlabel('consumed health services [CHF]')\n",
        "ax.set_ylabel('policyholder contributions [CHF]')\n",
        "seaborn.lineplot(hs, ps, label='%d CHF deductible' % d);\n",
        "savefigure()\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "ax.set_title('Consumed health services vs. policyholder contribution')\n",
        "ax.set_xlabel('consumed health services [CHF]')\n",
        "ax.set_ylabel('policyholder contributions [CHF]')\n",
        "for d in insurer.deductibles():\n",
        "  ps = [Scenario(policy=insurer.issue_policy(deductible=d),\n",
        "                 healthcare_services=h).p()\n",
        "        for h in hs]\n",
        "  seaborn.lineplot(hs, ps, label='%d CHF deductible' % d)\n",
        "savefigure()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "qQvmRM_PKnBU",
        "colab_type": "code",
        "cellView": "form",
        "colab": {}
      },
      "source": [
        "#@title Consumed health services vs. net expense\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "d = min(insurer.deductibles())\n",
        "es = [Scenario(policy=insurer.issue_policy(deductible=d),\n",
        "                healthcare_services=h).e()\n",
        "      for h in hs]\n",
        "ax.set_title('Consumed health services vs. net expense')\n",
        "ax.set_xlabel('consumed health services [CHF]')\n",
        "ax.set_ylabel('net expense [CHF]')\n",
        "seaborn.lineplot(hs,\n",
        "                 es,\n",
        "                 label='%d CHF deductible' % d);\n",
        "savefigure()\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "ax.set_title('Consumed health services vs. net expense')\n",
        "ax.set_xlabel('consumed health services [CHF]')\n",
        "ax.set_ylabel('net expense [CHF]')\n",
        "for d in insurer.deductibles():\n",
        "  es = [Scenario(policy=insurer.issue_policy(deductible=d),\n",
        "                 healthcare_services=h).e()\n",
        "        for h in hs]\n",
        "  seaborn.lineplot(hs, es, label='%d CHF deductible' % d)\n",
        "savefigure()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "Nr1y-wyCKWYl",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Impact of deductible on net expenses\n",
        "\n",
        "res = []\n",
        "for h in hs:\n",
        "  es = [Scenario(policy=insurer.issue_policy(deductible=d),\n",
        "                 healthcare_services=h).e()\n",
        "        for d in insurer.deductibles()]\n",
        "  res.append(max(es) - min(es))\n",
        "\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "ax.set_title('Impact of deductible on net expenses')\n",
        "ax.set_xlabel('consumed health services [CHF]')\n",
        "ax.set_ylabel('impact [CHF]')\n",
        "seaborn.lineplot(hs, res)\n",
        "savefigure()\n"
      ],
      "execution_count": 0,
      "outputs": []
    },
    {
      "cell_type": "code",
      "metadata": {
        "id": "yegd6aoV7c2a",
        "colab_type": "code",
        "colab": {},
        "cellView": "form"
      },
      "source": [
        "#@title Deductible s. monthly premium\n",
        "\n",
        "_, ax = plt.subplots(figsize=(_width, 5))\n",
        "ax.set_title('Deductible vs. monthly premium')\n",
        "ax.set_xlabel('deductible [CHF]')\n",
        "ax.set_ylabel('monthly premium [CHF]')\n",
        "seaborn.pointplot(insurer.deductibles(),\n",
        "                  insurer.monthly_premiums(),\n",
        "                  linestyles='--')\n",
        "savefigure()"
      ],
      "execution_count": 0,
      "outputs": []
    }
  ]
}