{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Nonlinear Models, Regimes, Market Microstructure, and Ordered Outcomes\n",
        "\n**Opening question:** What if the same lag has a different effect in calm and stressed regimes, or the observed price change is an ordered category?\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "\n",
        "rng = np.random.default_rng(23)\n",
        "x = np.zeros(300)\n",
        "for t in range(1, len(x)):\n",
        "    phi = -1.2 if x[t-1] < 0 else 0.55\n",
        "    x[t] = phi*x[t-1] + rng.normal()\n",
        "print(round(x.mean(), 3), round((x < 0).mean(), 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. The unconditional mean is positive even though neither regime includes an intercept, illustrating nonlinear asymmetry.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "import pandas as pd\n",
        "from statsmodels.miscmodels.ordinal_model import OrderedModel\n",
        "\n",
        "rng = np.random.default_rng(2301)\n",
        "x = rng.normal(size=700)\n",
        "latent = 0.8*x + rng.normal(size=700)\n",
        "y = pd.cut(latent, [-np.inf, -0.7, 0.7, np.inf], labels=[0,1,2], ordered=True)\n",
        "model = OrderedModel(y, pd.DataFrame({\"x\": x}), distr=\"probit\").fit(method=\"bfgs\", disp=False)\n",
        "print(round(model.params[\"x\"], 3))\n",
        "print(np.round(model.model.predict(model.params, exog=pd.DataFrame({\"x\": [0.0]}))[0], 3))\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "**Interpretation check:** Interpretation. At x=0, the central category is most probable. The probabilities sum to one and depend on both slope and thresholds.\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "rng = np.random.default_rng(23)\n",
        "x = np.zeros(300)\n",
        "x[t] = phi*x[t-1] + rng.normal()\n",
        "print(round(x.mean(), 3), round((x < 0).mean(), 3))\n",
        "import numpy as np\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## Verified source output\n",
        "\n",
        "```text\n0.726 0.263\n```\n\n```text\n0.805 [0.282 0.518 0.2 ]\n```\n\n```text\n0.805\n[0.282 0.518 0.2  ]\n```\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    },
    "ceteris_lab": {
      "course_slug": "fundamentals-python-econometrics",
      "source_derived": true,
      "course_title": "Fundamentals of Python for Financial Econometrics",
      "chapter": 42,
      "source_origin": "source-derived"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
