{ "cells": [ { "cell_type": "markdown", "metadata": { "id": "7SZxKPcyDbw_" }, "source": [ "# Stock Market Analysis And Forecasting\n", "A stock market, equity market, or share market is the aggregation of buyers and sellers of stocks (also called shares), which represent ownership claims on businesses; these may include securities listed on a public stock exchange, as well as stock that is only traded privately, such as shares of private companies which are sold to investors through equity crowdfunding platforms. Investment in the stock market is most often done via stockbrokerages and electronic trading platforms. Investment is usually made with an investment strategy in mind.\n", "\n", "The task of stock prediction has always been a challenging problem for statistics experts and finance. The main reason behind this prediction is buying stocks that are likely to increase in price and then selling stocks that are probably to fall. Generally, there are two ways for stock market prediction.\n", "\n", "Fundamental analysis is one of them and relies on a company's technique and fundamental information like market position, expenses and annual growth rates. The second one is the technical analysis method, which concentrates on previous stock prices and values.\n", "\n", "In the first part of our project, we will try to analyze the data. and in the second part, we will forecast the stock market.\n", "\n", "## Dataset\n", "We will be using stock data from 2006-2018 for the following publicly traded companies:\n", " 1. Google\n", " 2. Microsoft\n", " 3. IBM\n", " 4. Amazon\n", "\n", "## Analysis\n", "We will find the distribution of close and open. Then we will find the correlation between close and open. After that, we will visualize the attributes [Open, High, Low, Close, volume] of our datasets. Then we compare the \"High\" and \"Close\" of each datasets. At last, we will find the trend and seasonality in the dataset." ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "id": "aiCB-iASNjYf" }, "outputs": [], "source": [ "import numpy as np \n", "import pandas as pd\n", "import matplotlib.pyplot as plt\n", "import seaborn as sns\n", "import plotly.express as px\n", "import pickle" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "Cf8CvSh6OGay", "outputId": "7ce041bf-0827-415c-8af8-a5f97b028c22" }, "outputs": [], "source": [ "google = pd.read_csv('./data/GOOGL_2006-01-01_to_2018-01-01.csv', index_col='Date', parse_dates=['Date'])\n", "google.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "gYLCppXiTrK0", "outputId": "5be584f8-e797-402c-89b3-2c6505a44c02" }, "outputs": [], "source": [ "microsoft = pd.read_csv('./data/MSFT_2006-01-01_to_2018-01-01.csv', index_col='Date', parse_dates=['Date'])\n", "microsoft.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "iPKqlK-ZOOIx", "outputId": "96f140cd-ace1-4dd7-948c-c53dbffd8394" }, "outputs": [], "source": [ "amazon = pd.read_csv('./data/AMZN_2006-01-01_to_2018-01-01.csv', index_col='Date', parse_dates=['Date'])\n", "amazon.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 235 }, "id": "7D6zjAx2OZRp", "outputId": "594067ed-8438-4def-9f23-a19f2edc23d6" }, "outputs": [], "source": [ "ibm = pd.read_csv('./data/IBM_2006-01-01_to_2018-01-01.csv', index_col='Date', parse_dates=['Date'])\n", "ibm.head()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "id": "_DsuOfcPOfDZ", "outputId": "e2856039-3928-4ba8-8990-7206eac91a64" }, "outputs": [], "source": [ "google.describe()" ] }, { "cell_type": "markdown", "metadata": { "id": "VOn--fTaHv9u" }, "source": [ "After describing the google dataset, there is a high difference between the minimum and maximum values. And 75% of the value is close to the mean." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "NlCJYoSlOkTC", "outputId": "1e6c5e8e-c477-44e8-c8e1-0d04d0cae615" }, "outputs": [], "source": [ "google.columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "afaJu5juPvVM", "outputId": "d02c39e2-6673-4045-d73b-3492354eddb2" }, "outputs": [], "source": [ "google.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "YJO2obUyQJ-D", "outputId": "ebe42795-4831-4a6c-c7d0-509bf538878c" }, "outputs": [], "source": [ "google.isna().sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "id": "u9e-C3xCUFoG", "outputId": "d0f68684-bc60-41d5-8248-69714753da9e" }, "outputs": [], "source": [ "microsoft.describe()" ] }, { "cell_type": "markdown", "metadata": { "id": "AiEKBLpTIhDN" }, "source": [ "Also same for the Microsoft dataset, there is a high difference between the minimum and maximum values. And 75% of the value is close to the mean." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "B1XbVBsaUFqs", "outputId": "f3c2d8fd-5e19-4903-e9b1-77f39eeb2a4b" }, "outputs": [], "source": [ "microsoft.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "ZGeHejlzUFtb", "outputId": "46ad34ee-9e98-4f77-c604-7c3b02fffa1a" }, "outputs": [], "source": [ "microsoft.isna().sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "_lAPBgC3QTP0", "outputId": "8b1cb52c-45c2-4f77-e9f1-9eafed9fdcf6" }, "outputs": [], "source": [ "amazon.info()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "id": "e7ReAgz7QXC6", "outputId": "a083e744-86b4-42b4-d57f-a5b092e5f052" }, "outputs": [], "source": [ "amazon.describe()" ] }, { "cell_type": "markdown", "metadata": { "id": "nphzU3gkI09M" }, "source": [ "Also same for the Amazon dataset, there is a high difference between the minimum and maximum values. And 75% of the value is close to the mean." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "kmBZCcnvQeOT", "outputId": "58b0405f-4dfe-4ff2-a7f3-9138a85da3a4" }, "outputs": [], "source": [ "amazon.columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "oxbvY--YQjbz", "outputId": "084b0ef8-806c-4fd5-d3ec-fb27e4a7e008" }, "outputs": [], "source": [ "amazon.isna().sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 297 }, "id": "ubBizjDgQ9vT", "outputId": "ec4b39cf-b198-47be-884a-e5a93c823c16" }, "outputs": [], "source": [ "ibm.describe()" ] }, { "cell_type": "markdown", "metadata": { "id": "idAqf3mEJHnN" }, "source": [ "But for the IBM dataset, we can see all the value is nearly close to the mean." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "JX0WUfEERBFr", "outputId": "c989995c-3523-4276-d21e-342f129af016" }, "outputs": [], "source": [ "ibm.columns" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "gF2Vp6xpRGgL", "outputId": "2c184665-5108-465c-ed7e-f08e24e72a14" }, "outputs": [], "source": [ "ibm.isna().sum()" ] }, { "cell_type": "code", "execution_count": 20, "metadata": { "id": "wcMiz2pDRUJT" }, "outputs": [], "source": [ "ibm.dropna(inplace=True)\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "bJDp1qMTSMGU", "outputId": "444b03d8-27cf-4989-f13d-6bd36d8659f2" }, "outputs": [], "source": [ "ibm.isna().sum()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 542 }, "id": "vdy-INO5tRTM", "outputId": "aeb6a77f-9074-4828-ccdc-0d84c10b5a8f" }, "outputs": [], "source": [ "fig = px.histogram(google, \n", " x='Close', \n", " marginal='box', \n", " nbins=47, \n", " title='Distribution of Close')\n", "fig.update_layout(bargap=0.1)\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 542 }, "id": "5UVmGagftpjt", "outputId": "fb0cf4cb-9260-4219-b74c-f3c33caf827d" }, "outputs": [], "source": [ "fig = px.histogram(google, \n", " x='Open', \n", " marginal='box', \n", " color_discrete_sequence=['red'], \n", " title='Distribution of open')\n", "fig.update_layout(bargap=0.1)\n", "fig.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 542 }, "id": "YysvtUmIuAqi", "outputId": "7ef51f06-fde5-45bb-ee5c-c782fbae23b1" }, "outputs": [], "source": [ "fig = px.scatter(google, \n", " x='Open', \n", " y='Close', \n", " opacity=0.8,\n", " title='Open vs. Close')\n", "fig.update_traces(marker_size=5)\n", "fig.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "kNq5XracJazF" }, "source": [ "There is a very high correlation between \"Open\" and \"Close\". As we can see from the scatter plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 204 }, "id": "pM8o7QEdulPc", "outputId": "235f2ad4-7493-4bbc-b97b-534eb238165b" }, "outputs": [], "source": [ "google.corr(numeric_only=True)" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 661 }, "id": "AiT3r-2kSRGj", "outputId": "cca7e3cd-6baf-4031-8fed-137248ec8909" }, "outputs": [], "source": [ "google['2008':'2018'].plot(subplots=True, figsize=(10,12))\n", "plt.title('Google stock attributes from 2008 to 2018')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 661 }, "id": "-8QaSCwIUaCd", "outputId": "0dbeb672-dbff-40ea-f0fd-49dfd90b17ef" }, "outputs": [], "source": [ "microsoft['2008':'2018'].plot(subplots=True, figsize=(10,12))\n", "plt.title('Microsoft stock attributes from 2008 to 2018')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 661 }, "id": "caMqi4-hSw-0", "outputId": "3de05802-e399-4148-b254-fe230ed98e7a" }, "outputs": [], "source": [ "amazon['2008':'2018'].plot(subplots=True, figsize=(10,12))\n", "plt.title('Amazon stock attributes from 2008 to 2018')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 661 }, "id": "jc-ay-okS5Ek", "outputId": "aa61d916-3d67-4e56-a324-7f99bb23042d" }, "outputs": [], "source": [ "ibm['2008':'2018'].plot(subplots=True, figsize=(10,12))\n", "plt.title('IBM stock attributes from 2008 to 2018')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "oEyemEBUJ7Hd" }, "source": [ "### High plot" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 514 }, "id": "n91_-c-vTGYr", "outputId": "89755429-2cfa-4548-8199-509c800faa7d" }, "outputs": [], "source": [ "# Plotting before normalization\n", "google.High.plot()\n", "microsoft.High.plot()\n", "amazon.High.plot()\n", "ibm.High.plot()\n", "plt.legend(['Google','Microsoft','Amazon','IBM'])\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "uNEvX-eNKG_u" }, "source": [ "As we can see here Microsoft's \"High\" value is very slowly increasing straight line. IBM's \"High\" value and Amazon's \"High\" value started from the approx same stage, even Amazon's \"High\" value was a bit lower but after 2012 Amazon's \"High\" value started to exponentially increase and slight drop for IBM's \"High\" value. Since 2016 there is a high fight going between Google's \"High\" value and Amazon's \"High\" value at 2018 Amazon's \"High\" value also beat Google's \"High\" value." ] }, { "cell_type": "code", "execution_count": 31, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 69 }, "id": "At_n7sgAVHOQ", "outputId": "8df3470a-c038-4cfe-ba37-d9a1043d2a30" }, "outputs": [], "source": [ "# Normalizing and comparison\n", "# Both stocks start from 100\n", "# normalized_google = google.High.div(google.High.iloc[0]).mul(100)\n", "# normalized_microsoft = microsoft.High.div(microsoft.High.iloc[0]).mul(100)\n", "# normalized_amazon = amazon.High.div(google.High.iloc[0]).mul(100)\n", "# normalized_ibm = ibm.High.div(microsoft.High.iloc[0]).mul(100)\n", "# normalized_google.plot()\n", "# normalized_microsoft.plot()\n", "# normalized_amazon.plot()\n", "# normalized_ibm.plot()\n", "# plt.legend(['Google','Microsoft','Amazon','IBM'])\n", "# plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 530 }, "id": "p5Mb1sq8WU84", "outputId": "b16ec5b1-e160-45cf-e541-d279f62c347b" }, "outputs": [], "source": [ "# Expanding window functions\n", "microsoft_mean = microsoft.High.expanding().mean()\n", "microsoft_std = microsoft.High.expanding().std()\n", "microsoft.High.plot()\n", "microsoft_mean.plot()\n", "microsoft_std.plot()\n", "plt.legend(['High','Expanding Mean','Expanding Standard Deviation'])\n", "plt.title('Microsoft')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "F_uazrtGMsMQ" }, "source": [ "In Microsoft data, we can see in 2009 \"High\" value was under mean for a long time, so we can say there was some loss." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 530 }, "id": "HYJVphVxWYeV", "outputId": "3584d0c6-b486-4f75-b9fa-3c9372f6813b" }, "outputs": [], "source": [ "# Expanding window functions\n", "google_mean = google.High.expanding().mean()\n", "google_std = google.High.expanding().std()\n", "google.High.plot()\n", "google_mean.plot()\n", "google_std.plot()\n", "plt.legend(['High','Expanding Mean','Expanding Standard Deviation'])\n", "plt.title('Google')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "XqflMuOONQbe" }, "source": [ "Same for Google data, we can see in 2009 \"High\" value was under mean for a long time, so we can say there was some loss. But it was not an as huge loss as Amazon." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 530 }, "id": "hdwNPs0qW9nn", "outputId": "8e9d23af-827f-4513-95b5-c186b44a0221" }, "outputs": [], "source": [ "# Expanding window functions\n", "ibm_mean = ibm.High.expanding().mean()\n", "ibm_std = ibm.High.expanding().std()\n", "ibm.High.plot()\n", "ibm_mean.plot()\n", "ibm_std.plot()\n", "plt.legend(['High','Expanding Mean','Expanding Standard Deviation'])\n", "plt.title('IBM')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "wxBYR8shNm3n" }, "source": [ "Same for IBM data, we can see in 2009 \"High\" value was under mean for a long time, so we can say there was some loss. And after 2013 again a drop then in 2016 there was a huge loss but after that, they were doing well. After just some profit again in between 2017, there was another drop." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 530 }, "id": "1N2XLLVfXaN4", "outputId": "08fef5e2-78fb-44bf-c301-f3e17f7dce97" }, "outputs": [], "source": [ "# Expanding window functions\n", "amazon_mean = amazon.High.expanding().mean()\n", "amazon_std = amazon.High.expanding().std()\n", "amazon.High.plot()\n", "amazon_mean.plot()\n", "amazon_std.plot()\n", "plt.legend(['High','Expanding Mean','Expanding Standard Deviation'])\n", "plt.title('Amazon')\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "BKFOLIFQNku_" }, "source": [ "For Amazon's \"High\" value the case is different they also face loss in 2009 but for a very little margin. And after that their growth is in exponential order." ] }, { "cell_type": "markdown", "metadata": { "id": "6-_5BaUEPN7u" }, "source": [ "## Here we can see every company faced a loss in 2009, maybe that's because of the economic slowdown." ] }, { "cell_type": "markdown", "metadata": { "id": "kF6fwLdhPE3-" }, "source": [ "### Close" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 530 }, "id": "EUTiQTKXXtPH", "outputId": "faa3a26a-0c01-4f83-9c89-77bbeb7b3b77" }, "outputs": [], "source": [ "# Expanding window functions\n", "google_mean = google.Close.expanding().mean()\n", "google_std = google.Close.expanding().std()\n", "google.High.plot()\n", "google_mean.plot()\n", "google_std.plot()\n", "plt.legend(['Close','Expanding Mean','Expanding Standard Deviation'])\n", "plt.title('Google')\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": 37, "metadata": { "id": "iyr745YnYdOG" }, "outputs": [], "source": [ "from pylab import rcParams\n", "import statsmodels.api as sm" ] }, { "cell_type": "markdown", "metadata": { "id": "NStUk9DAPuCO" }, "source": [ "## Trend and Seasonality" ] }, { "cell_type": "markdown", "metadata": { "id": "xyc1m22DP9vf" }, "source": [ "Google data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "nCEPW4zHYMVO", "outputId": "9d1e681b-f6af-46ae-d2c4-8440e1eec48f" }, "outputs": [], "source": [ "# Now, for decomposition...\n", "rcParams['figure.figsize'] = 11, 9\n", "decomposed_google_volume = sm.tsa.seasonal_decompose(google[\"High\"],period=360) # The frequncy is annual\n", "figure = decomposed_google_volume.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "-BKKfxqAQBu3" }, "source": [ "There is a very slow increasing trend until 2012, but after 2012 there was an exponential high trend. And very high seasonality." ] }, { "cell_type": "markdown", "metadata": { "id": "bb1MA8O7QmFs" }, "source": [ "Microsoft data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "JZR6or5ZYi8m", "outputId": "271ee237-4836-43a2-a94c-b11f6f0ed652" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_microsoft_volume = sm.tsa.seasonal_decompose(microsoft[\"High\"],period=360) # The frequncy is annual\n", "figure = decomposed_microsoft_volume.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "C4iQFevgQ3Px" }, "source": [ "Same for Microsoft data, there is a very slow increasing trend until 2012, but after 2012 there was an exponential high trend. And very high seasonality." ] }, { "cell_type": "markdown", "metadata": { "id": "std7HguHQ7jw" }, "source": [ "IBM data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "jfuFqS80Yxlu", "outputId": "ba038348-c4b5-43d1-b03f-334c0d21bb89" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_ibm_volume = sm.tsa.seasonal_decompose(ibm[\"High\"],period=360) # The frequncy is annual\n", "figure = decomposed_ibm_volume.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "9ZVHA6zuRE-L" }, "source": [ "IBM data has a very slow increasing trend until 2008, but after 2009 there was an exponential high trend until 2013, then a high drop until 2016 then a very slow increasing trend. And very high seasonality." ] }, { "cell_type": "markdown", "metadata": { "id": "95ZBbTHoRzNS" }, "source": [ "Amazon data" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 659 }, "id": "2578pdviY_Wo", "outputId": "07ebbf3f-2944-4bc5-fac6-cbe522b25a6d" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_amazon_volume = sm.tsa.seasonal_decompose(amazon[\"High\"],period=360) # The frequncy is annual\n", "figure = decomposed_amazon_volume.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "uo4KfDAkSQF3" }, "source": [ "Amazon data is similar to Google data." ] }, { "cell_type": "markdown", "metadata": { "id": "3i01zQQHSf-x" }, "source": [ "### Close" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "Oqx57vd1ZQVW", "outputId": "55e0bb86-f451-461b-c231-b917547cb06a" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_google_volume = sm.tsa.seasonal_decompose(google[\"Close\"],period=360) # The frequncy is annual\n", "figure = decomposed_google_volume.plot()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "cMSuInsuZWRn", "outputId": "ac1532ea-0c48-4808-c82c-c6fa1c5ff349" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_microsoft_volume = sm.tsa.seasonal_decompose(microsoft[\"Close\"],period=360) # The frequncy is annual\n", "figure = decomposed_microsoft_volume.plot()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 661 }, "id": "Yc19oD9ZZi-B", "outputId": "8aa64728-5317-475c-d56a-4224c16c0b09" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_amazon_volume = sm.tsa.seasonal_decompose(amazon[\"Close\"],period=360) # The frequncy is annual\n", "figure = decomposed_amazon_volume.plot()\n", "plt.show()" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 657 }, "id": "HnfVt4A3Zvp_", "outputId": "8ede7386-e1a6-4740-ae69-efd6f9661890" }, "outputs": [], "source": [ "rcParams['figure.figsize'] = 11, 9\n", "decomposed_ibm_volume = sm.tsa.seasonal_decompose(ibm[\"Close\"],period=360) # The frequncy is annual\n", "figure = decomposed_ibm_volume.plot()\n", "plt.show()" ] }, { "cell_type": "markdown", "metadata": { "id": "HpnfOK0GSo9z" }, "source": [ "Because there have a very strong correlation between Close and High, we can see the trend and seasonality is very similar." ] } ], "metadata": { "colab": { "collapsed_sections": [], "name": "Stock_market.ipynb", "provenance": [] }, "kernelspec": { "display_name": "Python 3", "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.3" } }, "nbformat": 4, "nbformat_minor": 0 }