Skip to content
Snippets Groups Projects
Analysis_25JUN2019_modular.ipynb 49.8 KiB
Newer Older
  • Learn to ignore specific revisions
  •    "metadata": {
        "scrolled": true
       },
       "outputs": [],
       "source": [
        "import pystan\n",
        "\n",
        "code = \"\"\"\n",
        "functions{\n",
        "  // below taken from https://discourse.mc-stan.org/t/quantile-function-in-stan/3642/13\n",
        "  // as Stan doesn't have a quantile function nor supports real-to-int conversion.\n",
        "  int ub(real x) {\n",
        "    int ub = 1;\n",
        "    while (ub < x) ub *= 2;\n",
        "    return ub;\n",
        "  }\n",
        "\n",
        "  int closest(real x, int a, int b) {\n",
        "    return fabs(x - a) < fabs(x - b) ? a : b;\n",
        "  }\n",
        "\n",
        "  // L <= x <= U\n",
        "  int to_int_bsearch(real x, int L, int U);\n",
        "\n",
        "  int to_int_bsearch(real x, int L, int U) {\n",
        "    int mid = (L + U) / 2;\n",
        "    if (L == U) return L;\n",
        "    if (L + 1 == U) return closest(x, L, U);\n",
        "    return x <= mid? to_int_bsearch(x, L, mid) : to_int_bsearch(x, mid, U);\n",
        "  }\n",
        "\n",
        "  int to_int(real x);\n",
        "\n",
        "  int to_int(real x) {\n",
        "    if (fabs(x) >= 2^31) reject(\"to_int arugment must be < 2^31, found x = \", x);\n",
        "    if (x < 0) return -to_int(-x);\n",
        "    return to_int_bsearch(x, 0, ub(x));\n",
        "  }\n",
        "}\n",
        "\n",
        "data {\n",
        "  int<lower=0> N;\n",
        "  int<lower=0> N_quantiles;\n",
        "  real<lower=0, upper=1> r;\n",
        "  int<lower=0, upper=1> decision[N];\n",
        "  real X[N];\n",
        "  real<lower=0, upper=1> quantiles[N_quantiles];\n",
        "}\n",
        "\n",
        "parameters {\n",
        "  real Z[N];\n",
        "}\n",
        "\n",
        "model {\n",
        "  Z ~ normal(0, 1);\n",
        "  \n",
        "  for(i in 1:N){\n",
        "    decision ~ bernoulli(inv_logit(X[i] + Z[i]) >= quantiles[to_int(r*N_quantiles)] ? 1 : 0);\n",
        "  }\n",
        "}\n",
        "\"\"\"\n",
        "\n",
        "dat = dict()\n",
        "\n",
        "sm = pystan.StanModel(model_code=code)\n",
        "fit = sm.sampling(data=dat, iter=4000, chains=4)"
       ]
      }
     ],
     "metadata": {
      "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.7.3"
      },
      "toc": {
       "base_numbering": 1,
       "nav_menu": {},
       "number_sections": true,
       "sideBar": true,
       "skip_h1_title": true,
       "title_cell": "Table of Contents",
       "title_sidebar": "Contents",
       "toc_cell": true,
       "toc_position": {
        "height": "1084px",
        "left": "228px",
        "top": "111.133px",
        "width": "300.7px"
       },
       "toc_section_display": true,
       "toc_window_display": true
      },
      "varInspector": {
       "cols": {
        "lenName": 16,
        "lenType": 16,
        "lenVar": 40
       },
       "kernels_config": {
        "python": {
         "delete_cmd_postfix": "",
         "delete_cmd_prefix": "del ",
         "library": "var_list.py",
         "varRefreshCmd": "print(var_dic_list())"
        },
        "r": {
         "delete_cmd_postfix": ") ",
         "delete_cmd_prefix": "rm(",
         "library": "var_list.r",
         "varRefreshCmd": "cat(var_dic_list()) "
        }
       },
       "position": {
        "height": "352.85px",
        "left": "1070px",
        "right": "20px",
        "top": "120px",
        "width": "350px"
       },
       "types_to_exclude": [
        "module",
        "function",
        "builtin_function_or_method",
        "instance",
        "_Feature"
       ],
       "window_display": false
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }