{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "8752a8c4",
   "metadata": {},
   "outputs": [],
   "source": [
    "# -*- coding: utf-8 -*- mini Batch\n",
    "\"\"\"\n",
    "Mini-batch Gradient Descent on f(x)=x^2 — Smooth Video Animation (NO SAVING)\n",
    "\"\"\"\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "matplotlib.use(\"Qt5Agg\")   # IMPORTANT for real animations\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Target function\n",
    "def f(x):\n",
    "    return x**2\n",
    "\n",
    "# Visualization grid\n",
    "xx = np.linspace(-100, 100, 800)\n",
    "yy = f(xx)\n",
    "\n",
    "# Hyperparameters\n",
    "x = 80.0                       # starting point\n",
    "eta = 0.01                     # learning rate\n",
    "steps = 600                    # number of iterations\n",
    "m = 32                         # mini-batch size\n",
    "rng = np.random.default_rng(0)\n",
    "\n",
    "# Enable interactive mode\n",
    "plt.ion()\n",
    "\n",
    "# Create persistent figure ONCE\n",
    "fig, ax = plt.subplots(figsize=(8, 6))\n",
    "ax.plot(xx, yy, linewidth=2, label=r\"$f(x)=x^2$\")\n",
    "point, = ax.plot([], [], \"ro\", markersize=10, label=\"Current $x_t$\")\n",
    "path,  = ax.plot([], [], \"b--\", alpha=0.4)  # trail (history)\n",
    "\n",
    "ax.set_xlim(-100, 100)\n",
    "ax.set_ylim(0, 10000)\n",
    "ax.set_xlabel(\"x\")\n",
    "ax.set_ylabel(\"f(x)\")\n",
    "ax.legend()\n",
    "\n",
    "# To store trajectory for smoother visual effect\n",
    "xs, ys = [], []\n",
    "\n",
    "# Main animation loop\n",
    "for t in range(steps):\n",
    "\n",
    "    # Sample mini-batch Z_1,...,Z_m ~ N(0,1)\n",
    "    z_batch = rng.normal(0.0, 1.0, size=m)\n",
    "\n",
    "    # Gradient: g = 2(a_i - z_j) mean → g = 2(x - mean(z_j))\n",
    "    g = 2.0 * (x - z_batch.mean())\n",
    "\n",
    "    # Update\n",
    "    x = x - eta * g\n",
    "\n",
    "    # Store for trail\n",
    "    xs.append(x)\n",
    "    ys.append(f(x))\n",
    "\n",
    "    # Update moving point + trail\n",
    "    point.set_data([x], [f(x)])\n",
    "    path.set_data(xs, ys)\n",
    "\n",
    "    ax.set_title(\n",
    "        f\"Mini-batch GD on $x^2$  |  m={m}  |  iter={t}  |  x={x:.2f}  |  g=2(x - mean(z))\"\n",
    "    )\n",
    "\n",
    "    # Smooth animation\n",
    "    fig.canvas.draw()\n",
    "    fig.canvas.flush_events()\n",
    "    plt.pause(0.01)\n",
    "\n",
    "plt.ioff()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "628a8260",
   "metadata": {},
   "outputs": [],
   "source": [
    "#Stochastic\n",
    "# -*- coding: utf-8 -*-\n",
    "\"\"\"\n",
    "Stochastic Gradient Descent on f(x)=x^2 — Smooth Video Animation (NO SAVING)\n",
    "\"\"\"\n",
    "\n",
    "import numpy as np\n",
    "import matplotlib\n",
    "matplotlib.use(\"Qt5Agg\")     # <<< IMPORTANT for REAL animation\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "# Target function\n",
    "def f(x):\n",
    "    return x**2\n",
    "\n",
    "# Visualization grid\n",
    "xx = np.linspace(-100, 100, 800)\n",
    "yy = f(xx)\n",
    "\n",
    "# SGD hyperparameters\n",
    "x = 80.0                         # initial point\n",
    "eta = 0.7                       # learning rate\n",
    "steps = 800                      # number of updates\n",
    "rng = np.random.default_rng(0)   # reproducible randomness\n",
    "\n",
    "# Activate interactive mode\n",
    "plt.ion()\n",
    "\n",
    "# Create persistent figure\n",
    "fig, ax = plt.subplots(figsize=(8, 6))\n",
    "ax.plot(xx, yy, linewidth=2, label=r\"$f(x)=x^2$\")\n",
    "point, = ax.plot([], [], \"ro\", markersize=10, label=\"Current $x_t$\")\n",
    "path,  = ax.plot([], [], \"b--\", alpha=0.4, label=\"Trajectory\")\n",
    "\n",
    "ax.set_xlim(-100, 100)\n",
    "ax.set_ylim(0, 10000)\n",
    "ax.set_xlabel(\"x\")\n",
    "ax.set_ylabel(\"f(x)\")\n",
    "ax.legend()\n",
    "\n",
    "# Store trajectory\n",
    "xs, ys = [], []\n",
    "\n",
    "# Animation loop\n",
    "for t in range(steps):\n",
    "\n",
    "    # Draw one sample Z ~ N(0,1)\n",
    "    z = rng.normal(0.0, 1.0)\n",
    "\n",
    "    # Stochastic gradient: g = 2(x - z)\n",
    "    g = 2.0 * (x - z)\n",
    "\n",
    "    # Update\n",
    "    x = x - eta * g\n",
    "\n",
    "    # Record trajectory\n",
    "    xs.append(x)\n",
    "    ys.append(f(x))\n",
    "\n",
    "    # Update point + trail\n",
    "    point.set_data([x], [f(x)])\n",
    "    path.set_data(xs, ys)\n",
    "\n",
    "    ax.set_title(\n",
    "        f\"Stochastic GD on $x^2$  |  iter={t}  |  x={x:.2f}  |  g = 2(x - z)\"\n",
    "    )\n",
    "\n",
    "    # Smooth animation\n",
    "    fig.canvas.draw()\n",
    "    fig.canvas.flush_events()\n",
    "    plt.pause(0.05)\n",
    "\n",
    "plt.ioff()\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "82d4c527",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "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.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
