{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyOWkwExTrIVbjBZVZJUZvSd"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","execution_count":1,"metadata":{"id":"7BjPn_5DUW9c","executionInfo":{"status":"ok","timestamp":1785051145831,"user_tz":-330,"elapsed":306,"user":{"displayName":"Raunak Bhattacharyya","userId":"05940311940370151459"}}},"outputs":[],"source":["import numpy as np\n","import matplotlib.pyplot as plt\n","from matplotlib.animation import FuncAnimation\n","from scipy.special import erfinv\n","from IPython.display import HTML"]},{"cell_type":"code","source":["# ============================================\n","# Parameters\n","# ============================================\n","\n","N = 5000\n","batch_size = 100\n","\n","# Uniform samples\n","u = np.random.rand(N)\n","\n","# Inverse transform\n","g = np.sqrt(2) * erfinv(2*u - 1)\n","\n","# True Gaussian PDF\n","xx = np.linspace(-4, 4, 500)\n","pdf = (1/np.sqrt(2*np.pi))*np.exp(-xx**2/2)"],"metadata":{"id":"tA6MsA3zUxJD","executionInfo":{"status":"ok","timestamp":1785051825689,"user_tz":-330,"elapsed":5,"user":{"displayName":"Raunak Bhattacharyya","userId":"05940311940370151459"}}},"execution_count":8,"outputs":[]},{"cell_type":"code","source":["# ============================================\n","# Figure\n","# ============================================\n","\n","fig = plt.figure(figsize=(13,4))\n","\n","ax_left  = fig.add_axes([0.05,0.18,0.30,0.70])\n","ax_mid   = fig.add_axes([0.38,0.18,0.12,0.70])\n","ax_right = fig.add_axes([0.55,0.18,0.40,0.70])\n","\n","ax_mid.axis(\"off\")\n","\n","# ============================================\n","# Animation\n","# ============================================\n","\n","def update(frame):\n","\n","    ax_left.clear()\n","    ax_mid.clear()\n","    ax_right.clear()\n","\n","    ax_mid.axis(\"off\")\n","\n","    start = frame * batch_size\n","    end = min(start + batch_size, N)\n","\n","    # ---------------------------------------\n","    # LEFT: Uniform samples\n","    # ---------------------------------------\n","\n","    if start > 0:\n","        ax_left.scatter(\n","            u[:start],\n","            np.zeros(start),\n","            s=12,\n","            color=\"lightgrey\",\n","            alpha=0.5\n","        )\n","\n","    ax_left.scatter(\n","        u[start:end],\n","        np.zeros(end-start),\n","        s=28,\n","        color=\"darkorange\",\n","        edgecolors=\"black\",\n","        linewidths=0.3,\n","        zorder=10\n","    )\n","\n","    ax_left.set_xlim(0,1)\n","    ax_left.set_ylim(-0.1,0.1)\n","    ax_left.set_yticks([])\n","    ax_left.set_xlabel(\"Uniform samples\")\n","    ax_left.set_title(f\"New batch: {start+1}–{end}\")\n","\n","    # ---------------------------------------\n","    # MIDDLE: Transformation arrow\n","    # ---------------------------------------\n","\n","    ax_mid.annotate(\n","        \"\",\n","        xy=(0.95,0.5),\n","        xytext=(0.05,0.5),\n","        arrowprops=dict(\n","            arrowstyle=\"->\",\n","            lw=3\n","        )\n","    )\n","\n","    ax_mid.text(\n","        0.5,\n","        0.62,\n","        r\"$F^{-1}$\",\n","        ha=\"center\",\n","        fontsize=22\n","    )\n","\n","    ax_mid.text(\n","        0.5,\n","        0.35,\n","        \"Inverse\\nCDF\",\n","        ha=\"center\",\n","        fontsize=12\n","    )\n","\n","    # ---------------------------------------\n","    # RIGHT: Histogram\n","    # ---------------------------------------\n","\n","    ax_right.hist(\n","        g[:end],\n","        bins=40,\n","        density=True,\n","        color=\"steelblue\",\n","        edgecolor=\"black\",\n","        alpha=0.75\n","    )\n","\n","    ax_right.plot(xx, pdf, 'r', lw=2)\n","\n","    ax_right.set_xlim(-4,4)\n","    ax_right.set_ylim(0,0.45)\n","\n","    ax_right.set_xlabel(\"Gaussian samples\")\n","    ax_right.set_title(f\"Histogram ({end} samples)\")\n","\n","ani = FuncAnimation(\n","    fig,\n","    update,\n","    frames=int(np.ceil(N/batch_size)),\n","    interval=300,\n","    repeat=False\n",")\n","\n","plt.close(fig)\n","HTML(ani.to_jshtml())"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":498,"output_embedded_package_id":"1VAZ0fLeLnWc8x8TXFzVTAxlNqMicIoqE"},"id":"qPZM_hJ2U6bZ","executionInfo":{"status":"ok","timestamp":1785051849053,"user_tz":-330,"elapsed":15280,"user":{"displayName":"Raunak Bhattacharyya","userId":"05940311940370151459"}},"outputId":"b390f471-161d-446d-cdae-29e6188531cb"},"execution_count":9,"outputs":[{"output_type":"display_data","data":{"text/plain":"Output hidden; open in https://colab.research.google.com to view."},"metadata":{}}]}]}