diff --git a/.gitignore b/.gitignore index 58fd37be..71d1c244 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +myenv # Byte-compiled / optimized / DLL files __pycache__/ *.py[cod] diff --git a/Brent_v3.M.xlsx b/Brent_v3.M.xlsx new file mode 100644 index 00000000..694dff96 Binary files /dev/null and b/Brent_v3.M.xlsx differ diff --git a/docs/examples/forecaster-quickstart.ipynb b/docs/examples/forecaster-quickstart.ipynb index 6239177a..7bb7053d 100644 --- a/docs/examples/forecaster-quickstart.ipynb +++ b/docs/examples/forecaster-quickstart.ipynb @@ -247,15 +247,296 @@ ] }, { - "cell_type": "markdown", - "id": "6a8ae523", + "cell_type": "code", + "execution_count": 1, + "id": "9d08da3e", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " See https://github.com/google-research/timesfm/blob/master/README.md for updated APIs.\n", + "h = 2 \n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:p-29740:t-25856:modeling_flowstate.py:__init__:Number of encoder parameters: 7885.8240000000005k\n", + "INFO:p-29740:t-25856:modeling_flowstate.py:__init__:Number of dencoder parameters: 1181.952k (14.99%)\n", + "100%|██████████| 1/1 [00:00<00:00, 1.74it/s]\n" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " unique_id ds FlowState\n", + "0 brent 2025-09-14 -10.250992\n", + "1 brent 2025-09-21 -7.985748\n", + " h = 1 \n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "INFO:p-29740:t-25856:modeling_flowstate.py:__init__:Number of encoder parameters: 7885.8240000000005k\n", + "INFO:p-29740:t-25856:modeling_flowstate.py:__init__:Number of dencoder parameters: 1181.952k (14.99%)\n", + "100%|██████████| 1/1 [00:00<00:00, 2.07it/s]" + ] + }, + { + "name": "stdout", + "output_type": "stream", + "text": [ + " unique_id ds FlowState\n", + "0 brent 2025-09-14 -10.250992\n" + ] + }, + { + "name": "stderr", + "output_type": "stream", + "text": [ + "\n" + ] + } + ], + "source": [ + "import pandas as pd\n", + "from timecopilot import TimeCopilotForecaster\n", + "from timecopilot.models.foundation.flowstate import FlowState\n", + "\n", + "\n", + "\n", + "df = pd.read_excel(\n", + " # \"https://timecopilot.s3.amazonaws.com/public/data/events_pageviews.csv\",\n", + " 'D:/AO/timecopilot/Brent_v3.M.xlsx',\n", + " parse_dates=[\"ds\"],\n", + ")\n", + "\n", + "df_w = (\n", + " df\n", + " .set_index(\"ds\")\n", + " .groupby(\"unique_id\")[\"y\"]\n", + " .resample(\"W\")\n", + " .sum()\n", + " .reset_index()\n", + ")\n", + "\n", + "\n", + "\n", + "tcf = TimeCopilotForecaster(\n", + " models=[\n", + " FlowState(), \n", + " ]\n", + ")\n", + "\n", + "print(\"h = 2 \")\n", + "fcst_h2 = tcf.forecast(\n", + " df=df_w,\n", + " h=2,\n", + " freq=\"W\",\n", + ")\n", + "print(fcst_h2.head())\n", + "\n", + "print(\" h = 1 \")\n", + "fcst_h1 = tcf.forecast(\n", + " df=df_w,\n", + " h=1,\n", + " freq=\"W\",\n", + ")\n", + "print(fcst_h1.head())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 9, + "id": "37ba8935", + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + " unique_id ds y\n", + "0 brent 2005-09-01 63.48\n", + "1 brent 2005-10-01 58.10\n", + "2 brent 2005-11-01 55.05\n", + "3 brent 2005-12-01 58.98\n", + "4 brent 2006-01-01 65.99\n", + ".. ... ... ...\n", + "236 brent 2025-05-01 63.90\n", + "237 brent 2025-06-01 67.61\n", + "238 brent 2025-07-01 72.53\n", + "239 brent 2025-08-01 68.12\n", + "240 brent 2025-09-01 66.75\n", + "\n", + "[241 rows x 3 columns]\n", + " unique_id ds y\n", + "0 Oktoberfest 2020-01-31 25376\n", + "1 Oktoberfest 2020-02-29 28470\n", + "2 Oktoberfest 2020-03-31 23816\n", + "3 Oktoberfest 2020-04-30 46186\n", + "4 Oktoberfest 2020-05-31 31213\n", + ".. ... ... ...\n", + "403 Thanksgiving 2025-04-30 47655\n", + "404 Thanksgiving 2025-05-31 48667\n", + "405 Thanksgiving 2025-06-30 56856\n", + "406 Thanksgiving 2025-07-31 38935\n", + "407 Thanksgiving 2025-08-31 50620\n", + "\n", + "[408 rows x 3 columns]\n" + ] + } + ], + "source": [ + "import pandas as pd\n", + "df = pd.read_excel('D:/AO/timecopilot/Brent_v3.M.xlsx')\n", + "print(df)\n", + "\n", + "\n", + "import pandas as pd\n", + "df = pd.read_csv('https://timecopilot.s3.amazonaws.com/public/data/events_pageviews.csv')\n", + "print(df)\n" + ] + }, + { + "cell_type": "code", + "execution_count": 8, + "id": "0fb3f02c", "metadata": {}, - "source": [] + "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", + "
unique_iddsy
0Oktoberfest2020-01-3125376
1Oktoberfest2020-02-2928470
2Oktoberfest2020-03-3123816
3Oktoberfest2020-04-3046186
4Oktoberfest2020-05-3131213
............
403Thanksgiving2025-04-3047655
404Thanksgiving2025-05-3148667
405Thanksgiving2025-06-3056856
406Thanksgiving2025-07-3138935
407Thanksgiving2025-08-3150620
\n", + "

408 rows × 3 columns

\n", + "
" + ], + "text/plain": [ + " unique_id ds y\n", + "0 Oktoberfest 2020-01-31 25376\n", + "1 Oktoberfest 2020-02-29 28470\n", + "2 Oktoberfest 2020-03-31 23816\n", + "3 Oktoberfest 2020-04-30 46186\n", + "4 Oktoberfest 2020-05-31 31213\n", + ".. ... ... ...\n", + "403 Thanksgiving 2025-04-30 47655\n", + "404 Thanksgiving 2025-05-31 48667\n", + "405 Thanksgiving 2025-06-30 56856\n", + "406 Thanksgiving 2025-07-31 38935\n", + "407 Thanksgiving 2025-08-31 50620\n", + "\n", + "[408 rows x 3 columns]" + ] + }, + "execution_count": 8, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "df" + ] } ], "metadata": { "kernelspec": { - "display_name": ".venv", + "display_name": "myenv", "language": "python", "name": "python3" }, @@ -269,7 +550,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.12" + "version": "3.10.11" } }, "nbformat": 4, diff --git a/timecopilot/models/foundation/flowstate.py b/timecopilot/models/foundation/flowstate.py index 172bd0be..e3d40e09 100644 --- a/timecopilot/models/foundation/flowstate.py +++ b/timecopilot/models/foundation/flowstate.py @@ -167,7 +167,8 @@ def _predict_batch( batch_first=False, ).prediction_outputs fcst = fcst.squeeze(-1).transpose(-1, -2) # now shape is (batch, h, quantiles) - fcst_mean = fcst[..., supported_quantiles.index(0.5)].squeeze() + # fcst_mean = fcst[..., supported_quantiles.index(0.5)].squeeze() + fcst_mean = fcst[..., supported_quantiles.index(0.5)] fcst_mean_np = fcst_mean.detach().numpy() fcst_quantiles_np = fcst.detach().numpy() if quantiles is not None else None return fcst_mean_np, fcst_quantiles_np