{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "import matplotlib.pyplot as plt\n", "import pandas as pd\n", "import plotly.express as px\n", "from scipy import stats" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "df = {}\n", "\n", "csv_file = \"coal_price_data.csv\"\n", "csv_date = \"date\"\n", "csv_file = csv_file.split(\".\")[0]\n", "df[csv_file] = pd.read_csv(f\"../coal-price-data/{csv_file}.csv\")\n", "df[csv_file][\"datetime\"] = pd.to_datetime(df[csv_file][csv_date], format=\"%b-%y\")\n", "\n", "csv_path = \"../coal-price-data/fred/\"\n", "csv_date = \"DATE\"\n", "csv_date_start = \"2011-11-01\"\n", "csv_date_end = \"2023-12-01\"\n", "csv_list = os.listdir(csv_path)\n", "for csv_file in csv_list:\n", " csv_file = csv_file.split(\".\")[0]\n", " df[csv_file] = pd.read_csv(f\"../coal-price-data/fred/{csv_file}.csv\")\n", " df[csv_file][\"datetime\"] = pd.to_datetime(df[csv_file][csv_date], format=\"%Y-%m-%d\")\n", " df_m2_filtered = df[csv_file].loc[\n", " (df[csv_file][\"datetime\"] >= csv_date_start) & (df[csv_file][\"datetime\"] < csv_date_end)\n", " ]" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DATEM2SLdatetime
01959-01-01286.61959-01-01
11959-02-01287.71959-02-01
21959-03-01289.21959-03-01
31959-04-01290.11959-04-01
41959-05-01292.21959-05-01
............
7752023-08-0120825.62023-08-01
7762023-09-0120755.42023-09-01
7772023-10-0120725.72023-10-01
7782023-11-0120767.52023-11-01
7792023-12-0120865.22023-12-01
\n", "

780 rows × 3 columns

\n", "
" ], "text/plain": [ " DATE M2SL datetime\n", "0 1959-01-01 286.6 1959-01-01\n", "1 1959-02-01 287.7 1959-02-01\n", "2 1959-03-01 289.2 1959-03-01\n", "3 1959-04-01 290.1 1959-04-01\n", "4 1959-05-01 292.2 1959-05-01\n", ".. ... ... ...\n", "775 2023-08-01 20825.6 2023-08-01\n", "776 2023-09-01 20755.4 2023-09-01\n", "777 2023-10-01 20725.7 2023-10-01\n", "778 2023-11-01 20767.5 2023-11-01\n", "779 2023-12-01 20865.2 2023-12-01\n", "\n", "[780 rows x 3 columns]" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df[\"M2SL\"]" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "year 2011\n", "date Apr-12\n", "newcastle 48.8\n", "HBA 49.42\n", "ICI_1 52.93\n", "datetime 2011-12-01 00:00:00\n", "dtype: object\n", "year 2023\n", "date Sep-23\n", "newcastle 433.7\n", "HBA 330.97\n", "ICI_1 277.62\n", "datetime 2023-12-01 00:00:00\n", "dtype: object\n", "DATE 1959-01-01\n", "M2SL 286.6\n", "datetime 1959-01-01 00:00:00\n", "dtype: object\n", "DATE 2023-12-01\n", "M2SL 21703.5\n", "datetime 2023-12-01 00:00:00\n", "dtype: object\n" ] } ], "source": [ "print(df[\"coal_price_data\"].min())\n", "print(df[\"coal_price_data\"].max())\n", "print(df[\"M2SL\"].min())\n", "print(df[\"M2SL\"].max())" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = \"newcastle\"\n", "fig = px.line(df_coal, x=\"datetime\", y=y, labels={\"Month\": \"Date\"})" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "fig.update_layout(\n", " template=\"simple_white\",\n", " font=dict(size=18),\n", " title_text=\"Coal Price\",\n", " width=650,\n", " title_x=0.5,\n", " height=400,\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "y = \"M2SL\"\n", "fig = px.line(df_m2_filtered, x=\"datetime\", y=y, labels={\"Month\": \"Date\"})\n", "fig.update_layout(\n", " template=\"simple_white\",\n", " font=dict(size=18),\n", " title_text=y,\n", " width=650,\n", " title_x=0.5,\n", " height=400,\n", ")\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_coal" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "df_m2_filtered" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(len(df_coal.newcastle))\n", "print(len(df_m2_filtered[\"M2SL\"]))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = df_coal.newcastle\n", "y = df_coal.ICI_1\n", "\n", "slope, intercept, r, p, std_err = stats.linregress(x, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"slope: {slope}\")\n", "print(f\"intercept: {intercept}\")\n", "print(f\"r: {r}\")\n", "print(f\"p: {p}\")\n", "print(f\"std_err: {std_err}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "x = df_coal.newcastle\n", "y = df_m2_filtered[\"M2SL\"]\n", "\n", "slope, intercept, r, p, std_err = stats.linregress(x, y)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(f\"slope: {slope}\")\n", "print(f\"intercept: {intercept}\")\n", "print(f\"r: {r}\")\n", "print(f\"p: {p}\")\n", "print(f\"std_err: {std_err}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def myfunc(x):\n", " return slope * x + intercept\n", "\n", "\n", "mymodel = list(map(myfunc, x))\n", "\n", "plt.scatter(x, y)\n", "plt.plot(x, mymodel, color=\"orange\")\n", "plt.xlabel(\"Newcastle\")\n", "plt.ylabel(\"ICI 1\")\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "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.12.2" } }, "nbformat": 4, "nbformat_minor": 4 }