{ "cells": [ { "cell_type": "code", "execution_count": null, "id": "1bbf544a-2be3-4579-ade3-404f4d850ef1", "metadata": {}, "outputs": [], "source": [ "from pandas import DataFrame\n", "import matplotlib.pyplot as plt\n", "from DATA225utils import make_connection, dataframe_query\n", "\n", "%matplotlib inline" ] }, { "cell_type": "code", "execution_count": null, "id": "2dcedb2f-42ab-4b8a-bc71-8c9bdeb59793", "metadata": {}, "outputs": [], "source": [ "conn = make_connection(config_file='stock-prices.ini')" ] }, { "cell_type": "code", "execution_count": null, "id": "fc76337a-e795-4e0d-833e-3d36b20350a4", "metadata": {}, "outputs": [], "source": [ "def compute_exponential_smoothing(conn, alpha, label):\n", " \"\"\"\n", " @cursor the database cursor\n", " @alpha the alpha value for exponential smoothing\n", " @label label for the exponentially smoothed values\n", " \"\"\"\n", " conn.cursor().execute('SET @smoothed = NULL')\n", " \n", " _, df = dataframe_query(conn,\n", " f'SELECT days_ago, price, '\n", " f' @smoothed := ({alpha}*price '\n", " f' + (1-{alpha})*(IF (@smoothed IS NULL, price, @smoothed))) '\n", " f' AS {label} '\n", " f'FROM recent_prices '\n", " f'ORDER BY days_ago'\n", " )\n", " \n", " return df" ] }, { "cell_type": "code", "execution_count": null, "id": "9a30b7c5-2426-4694-93e2-4d2d3984c720", "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(15, 10))\n", "\n", "first = True\n", "\n", "for smoother in [5, 2]:\n", " graph_label = f'zero_point_{smoother}_exponentially_smoothed'\n", " alpha = smoother/10\n", " \n", " df = compute_exponential_smoothing(conn, alpha, graph_label)\n", " display(df)\n", "\n", " rows = df.values.tolist()\n", " xs = [row[0] for row in rows]\n", " ys = [row[1] for row in rows]\n", " exps = [row[2] for row in rows]\n", " \n", " # Initialize the graph.\n", " if first:\n", " plt.xticks(xs)\n", " plt.plot(xs, ys, linewidth=5)\n", " plt.title('Exponentially Smoothed Stock Prices')\n", " plt.xlabel('Days ago')\n", " plt.ylabel('Price')\n", " first = False\n", "\n", " # Plot the moving average line.\n", " plt.plot(xs, exps, label=graph_label)\n", " \n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "84062204-fd04-4dfb-8de9-b20073459435", "metadata": {}, "outputs": [], "source": [ "plt.figure(figsize=(15, 10))\n", "\n", "first = True\n", "\n", "for smoother in [5, 2]:\n", " graph_label = f'zero_point_{smoother}_exponentially_smoothed'\n", " alpha = smoother/10\n", " \n", " df = compute_exponential_smoothing(conn, alpha, graph_label)\n", " display(df)\n", "\n", " rows = df.values.tolist()\n", " xs = [row[0] for row in rows]\n", " ys = [row[1] for row in rows]\n", " exps = [row[2] for row in rows]\n", " \n", " # Initialize the graph.\n", " if first:\n", " plt.xticks(xs)\n", " plt.plot(xs, ys, linewidth=5)\n", " plt.title('Exponentially Smoothed Stock Prices')\n", " plt.xlabel('Days ago')\n", " plt.ylabel('Price')\n", " first = False\n", "\n", " # Plot the moving average line.\n", " plt.plot(xs, exps, label=graph_label)\n", " \n", "plt.legend()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "id": "b750ca51-c26e-43df-98b3-ec64a65eff16", "metadata": {}, "outputs": [], "source": [ "conn.close()" ] }, { "cell_type": "markdown", "id": "6044e8e5-2c26-4ff4-919f-093576be8e91", "metadata": {}, "source": [ "#### (c) 2023 by Ronald Mak" ] }, { "cell_type": "code", "execution_count": null, "id": "8e9ace8a-3452-456d-aa22-2c88df721822", "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.18" } }, "nbformat": 4, "nbformat_minor": 5 }