{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "chXPUtUHvC4D"
   },
   "source": [
    "### Case Study : Stock market data from yahoo-finance for a few stocks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "id": "ku-lqsB-u9cy"
   },
   "outputs": [],
   "source": [
    "import yfinance as yf\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "id": "N2AOyHAgvMu2"
   },
   "outputs": [],
   "source": [
    "# Function to fetch stock data from Yahoo Finance for multiple tickers\n",
    "def fetch_multiple_stock_data(tickers, start_date, end_date):\n",
    "    stock_data = pd.DataFrame()\n",
    "    for ticker in tickers:\n",
    "        data = yf.download(ticker, start=start_date, end=end_date)\n",
    "        data['Ticker'] = ticker  # Add ticker as a column\n",
    "        stock_data = pd.concat([stock_data, data])\n",
    "    return stock_data\n",
    "\n",
    "# Function to calculate returns for multiple stocks\n",
    "def calculate_returns(stock_data):\n",
    "    stock_data['Daily_Returns'] = stock_data.groupby('Ticker')['Adj Close'].pct_change()\n",
    "    return stock_data\n",
    "\n",
    "# Function to calculate risk measures for multiple stocks\n",
    "def calculate_risk(stock_data):\n",
    "    risk_measures = stock_data.groupby('Ticker')['Daily_Returns'].agg(['std', 'var'])\n",
    "    return risk_measures\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "id": "4-i7IOR3vSpj"
   },
   "outputs": [],
   "source": [
    "# Main function\n",
    "def stocks(ticker_list,start_date,end_date):\n",
    "    ''' Input : Pass a list of stocks, start date and end date\n",
    "    Output: Stock data including the daily returns data frame\n",
    "    and  the risk measures'''\n",
    "\n",
    "    # Input parameters\n",
    "    tickers = ticker_list  # Example list of stock tickers\n",
    "    start_date = start_date\n",
    "    end_date = end_date\n",
    "\n",
    "    # Fetch stock data\n",
    "    stock_data = fetch_multiple_stock_data(tickers, start_date, end_date)\n",
    "\n",
    "   # Calculate returns\n",
    "    stock_data = calculate_returns(stock_data)\n",
    "\n",
    "   # Calculate risk measures\n",
    "    risk_measures = calculate_risk(stock_data)\n",
    "\n",
    "    return stock_data\n",
    "\n",
    "    # Print results\n",
    "    print(\"Stock data summary:\")\n",
    "    #print(stock_data.head())\n",
    "    #print(\"\\nRisk measures:\")\n",
    "    #print(risk_measures)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 293
    },
    "id": "3RtB3ocPveMr",
    "outputId": "99751111-ff33-4a28-a6d4-d9f9a0fa6a84"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "[*********************100%%**********************]  1 of 1 completed\n",
      "[*********************100%%**********************]  1 of 1 completed\n",
      "[*********************100%%**********************]  1 of 1 completed\n"
     ]
    },
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"stocks_data\",\n  \"rows\": 3476,\n  \"fields\": [\n    {\n      \"column\": \"Date\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"2020-01-01 00:00:00\",\n        \"max\": \"2024-08-19 00:00:00\",\n        \"num_unique_values\": 1202,\n        \"samples\": [\n          \"2024-07-31 00:00:00\",\n          \"2021-09-16 00:00:00\",\n          \"2020-04-28 00:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Open\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 682.1663433871794,\n        \"min\": 0.5,\n        \"max\": 1791.0,\n        \"num_unique_values\": 2293,\n        \"samples\": [\n          21.43000030517578,\n          18.059999465942383,\n          8.460000038146973\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"High\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 688.3752855329319,\n        \"min\": 0.550000011920929,\n        \"max\": 1794.0,\n        \"num_unique_values\": 2349,\n        \"samples\": [\n          10.770000457763672,\n          1639.0,\n          22.579999923706055\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Low\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 675.6596287294637,\n        \"min\": 0.5,\n        \"max\": 1764.6500244140625,\n        \"num_unique_values\": 2391,\n        \"samples\": [\n          1652.0,\n          1192.5999755859375,\n          18.420000076293945\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 682.0664333197068,\n        \"min\": 0.5099999904632568,\n        \"max\": 1768.6500244140625,\n        \"num_unique_values\": 2450,\n        \"samples\": [\n          15.40999984741211,\n          1452.6500244140625,\n          16.459999084472656\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Adj Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 664.6592724977754,\n        \"min\": 0.5099999904632568,\n        \"max\": 1768.6500244140625,\n        \"num_unique_values\": 2862,\n        \"samples\": [\n          7.320000171661377,\n          19.756546020507812,\n          1549.7718505859375\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Volume\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 8113961,\n        \"min\": 30000,\n        \"max\": 86705601,\n        \"num_unique_values\": 3394,\n        \"samples\": [\n          6768009,\n          8999235,\n          134700\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Ticker\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          \"HDFCBANK.NS\",\n          \"TCS\",\n          \"INFY\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Daily_Returns\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.03709707671250626,\n        \"min\": -0.39411768088291166,\n        \"max\": 0.874999906867743,\n        \"num_unique_values\": 3387,\n        \"samples\": [\n          0.013956258608038397,\n          -0.002232164206397491,\n          0.015079311864811817\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe",
       "variable_name": "stocks_data"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-25f4229c-d330-4363-865e-0e615cc7bfa7\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Ticker</th>\n",
       "      <th>Daily_Returns</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Date</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>2020-01-01</th>\n",
       "      <td>1276.099976</td>\n",
       "      <td>1280.000000</td>\n",
       "      <td>1270.599976</td>\n",
       "      <td>1278.599976</td>\n",
       "      <td>1227.428589</td>\n",
       "      <td>1836849</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-02</th>\n",
       "      <td>1279.000000</td>\n",
       "      <td>1288.000000</td>\n",
       "      <td>1279.000000</td>\n",
       "      <td>1286.750000</td>\n",
       "      <td>1235.252441</td>\n",
       "      <td>3068583</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>0.006374</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-03</th>\n",
       "      <td>1282.199951</td>\n",
       "      <td>1285.000000</td>\n",
       "      <td>1263.599976</td>\n",
       "      <td>1268.400024</td>\n",
       "      <td>1217.636841</td>\n",
       "      <td>5427775</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>-0.014261</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-06</th>\n",
       "      <td>1260.000000</td>\n",
       "      <td>1261.800049</td>\n",
       "      <td>1236.000000</td>\n",
       "      <td>1240.949951</td>\n",
       "      <td>1191.285400</td>\n",
       "      <td>5445093</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>-0.021641</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2020-01-07</th>\n",
       "      <td>1258.900024</td>\n",
       "      <td>1271.449951</td>\n",
       "      <td>1252.250000</td>\n",
       "      <td>1260.599976</td>\n",
       "      <td>1210.148926</td>\n",
       "      <td>7362247</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>0.015835</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-25f4229c-d330-4363-865e-0e615cc7bfa7')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-25f4229c-d330-4363-865e-0e615cc7bfa7 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-25f4229c-d330-4363-865e-0e615cc7bfa7');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-1e8baf18-8fad-4b0a-b930-9d0fdda84c45\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-1e8baf18-8fad-4b0a-b930-9d0fdda84c45')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-1e8baf18-8fad-4b0a-b930-9d0fdda84c45 button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "                   Open         High          Low        Close    Adj Close  \\\n",
       "Date                                                                          \n",
       "2020-01-01  1276.099976  1280.000000  1270.599976  1278.599976  1227.428589   \n",
       "2020-01-02  1279.000000  1288.000000  1279.000000  1286.750000  1235.252441   \n",
       "2020-01-03  1282.199951  1285.000000  1263.599976  1268.400024  1217.636841   \n",
       "2020-01-06  1260.000000  1261.800049  1236.000000  1240.949951  1191.285400   \n",
       "2020-01-07  1258.900024  1271.449951  1252.250000  1260.599976  1210.148926   \n",
       "\n",
       "             Volume       Ticker  Daily_Returns  \n",
       "Date                                             \n",
       "2020-01-01  1836849  HDFCBANK.NS            NaN  \n",
       "2020-01-02  3068583  HDFCBANK.NS       0.006374  \n",
       "2020-01-03  5427775  HDFCBANK.NS      -0.014261  \n",
       "2020-01-06  5445093  HDFCBANK.NS      -0.021641  \n",
       "2020-01-07  7362247  HDFCBANK.NS       0.015835  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "stocks_data = stocks(['HDFCBANK.NS','TCS','INFY'],start_date='2020-01-01', end_date='2024-08-20')\n",
    "stocks_data.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "id": "im7q_Rl7vm39"
   },
   "outputs": [],
   "source": [
    "#### Split the data into individual stocks\n",
    "\n",
    "hdfc = stocks_data[stocks_data['Ticker']=='HDFCBANK.NS'].reset_index()\n",
    "tcs = stocks_data[stocks_data['Ticker']=='TCS'].reset_index()\n",
    "infosys = stocks_data[stocks_data['Ticker']=='INFY'].reset_index()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 300
    },
    "id": "IOrACTijvu8E",
    "outputId": "f11b446c-fbfe-4ea6-c036-f85f7c4d27bd"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"hdfc\",\n  \"rows\": 8,\n  \"fields\": [\n    {\n      \"column\": \"Date\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"1970-01-01 00:00:00.000001146\",\n        \"max\": \"2024-08-19 00:00:00\",\n        \"num_unique_values\": 7,\n        \"samples\": [\n          \"1146\",\n          \"2022-04-21 17:30:28.272251392\",\n          \"2023-06-15 18:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Open\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 515.0909908399075,\n        \"min\": 201.93237266710793,\n        \"max\": 1791.0,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          1441.664441280132,\n          1593.2250366210938,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"High\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 514.916913890492,\n        \"min\": 200.7449236354243,\n        \"max\": 1794.0,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          1455.4797538504342,\n          1605.949951171875,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Low\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 510.6794495058127,\n        \"min\": 204.17833547122214,\n        \"max\": 1764.6500244140625,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          1426.9742602602973,\n          1580.2250366210938,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 511.8087567314175,\n        \"min\": 202.46052741356746,\n        \"max\": 1768.6500244140625,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          1441.3378295365844,\n          1593.9249572753906,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Adj Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 503.53306982222495,\n        \"min\": 206.01440012702577,\n        \"max\": 1768.6500244140625,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          1402.4708967225506,\n          1555.5263366699219,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Volume\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 28391838.089079104,\n        \"min\": 1146.0,\n        \"max\": 86705601.0,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          12919925.722513089,\n          16874261.0,\n          1146.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Daily_Returns\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 404.81818046058,\n        \"min\": -0.1260689398893251,\n        \"max\": 1145.0,\n        \"num_unique_values\": 8,\n        \"samples\": [\n          0.0004032741799227008,\n          0.008270263725580929,\n          1145.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-af43c00e-b42c-41ea-a9c7-60df491842c7\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Daily_Returns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>count</th>\n",
       "      <td>1146</td>\n",
       "      <td>1146.000000</td>\n",
       "      <td>1146.000000</td>\n",
       "      <td>1146.000000</td>\n",
       "      <td>1146.000000</td>\n",
       "      <td>1146.000000</td>\n",
       "      <td>1.146000e+03</td>\n",
       "      <td>1145.000000</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>mean</th>\n",
       "      <td>2022-04-21 17:30:28.272251392</td>\n",
       "      <td>1441.664441</td>\n",
       "      <td>1455.479754</td>\n",
       "      <td>1426.974260</td>\n",
       "      <td>1441.337830</td>\n",
       "      <td>1402.470897</td>\n",
       "      <td>1.291993e+07</td>\n",
       "      <td>0.000403</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>min</th>\n",
       "      <td>2020-01-01 00:00:00</td>\n",
       "      <td>770.450012</td>\n",
       "      <td>810.000000</td>\n",
       "      <td>738.750000</td>\n",
       "      <td>767.700012</td>\n",
       "      <td>736.975586</td>\n",
       "      <td>5.484040e+05</td>\n",
       "      <td>-0.126069</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>25%</th>\n",
       "      <td>2021-02-22 06:00:00</td>\n",
       "      <td>1380.000000</td>\n",
       "      <td>1395.000000</td>\n",
       "      <td>1363.062531</td>\n",
       "      <td>1377.324951</td>\n",
       "      <td>1331.357758</td>\n",
       "      <td>6.437438e+06</td>\n",
       "      <td>-0.007736</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>50%</th>\n",
       "      <td>2022-04-21 12:00:00</td>\n",
       "      <td>1491.125000</td>\n",
       "      <td>1506.274963</td>\n",
       "      <td>1479.375000</td>\n",
       "      <td>1491.525024</td>\n",
       "      <td>1447.983643</td>\n",
       "      <td>1.028824e+07</td>\n",
       "      <td>0.000621</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>75%</th>\n",
       "      <td>2023-06-15 18:00:00</td>\n",
       "      <td>1593.225037</td>\n",
       "      <td>1605.949951</td>\n",
       "      <td>1580.225037</td>\n",
       "      <td>1593.924957</td>\n",
       "      <td>1555.526337</td>\n",
       "      <td>1.687426e+07</td>\n",
       "      <td>0.008270</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>max</th>\n",
       "      <td>2024-08-19 00:00:00</td>\n",
       "      <td>1791.000000</td>\n",
       "      <td>1794.000000</td>\n",
       "      <td>1764.650024</td>\n",
       "      <td>1768.650024</td>\n",
       "      <td>1768.650024</td>\n",
       "      <td>8.670560e+07</td>\n",
       "      <td>0.115996</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>std</th>\n",
       "      <td>NaN</td>\n",
       "      <td>201.932373</td>\n",
       "      <td>200.744924</td>\n",
       "      <td>204.178335</td>\n",
       "      <td>202.460527</td>\n",
       "      <td>206.014400</td>\n",
       "      <td>9.424437e+06</td>\n",
       "      <td>0.017563</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-af43c00e-b42c-41ea-a9c7-60df491842c7')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-af43c00e-b42c-41ea-a9c7-60df491842c7 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-af43c00e-b42c-41ea-a9c7-60df491842c7');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-a9c3dbd1-b807-4d9f-95e7-b4a30849d540\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-a9c3dbd1-b807-4d9f-95e7-b4a30849d540')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-a9c3dbd1-b807-4d9f-95e7-b4a30849d540 button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "                                Date         Open         High          Low  \\\n",
       "count                           1146  1146.000000  1146.000000  1146.000000   \n",
       "mean   2022-04-21 17:30:28.272251392  1441.664441  1455.479754  1426.974260   \n",
       "min              2020-01-01 00:00:00   770.450012   810.000000   738.750000   \n",
       "25%              2021-02-22 06:00:00  1380.000000  1395.000000  1363.062531   \n",
       "50%              2022-04-21 12:00:00  1491.125000  1506.274963  1479.375000   \n",
       "75%              2023-06-15 18:00:00  1593.225037  1605.949951  1580.225037   \n",
       "max              2024-08-19 00:00:00  1791.000000  1794.000000  1764.650024   \n",
       "std                              NaN   201.932373   200.744924   204.178335   \n",
       "\n",
       "             Close    Adj Close        Volume  Daily_Returns  \n",
       "count  1146.000000  1146.000000  1.146000e+03    1145.000000  \n",
       "mean   1441.337830  1402.470897  1.291993e+07       0.000403  \n",
       "min     767.700012   736.975586  5.484040e+05      -0.126069  \n",
       "25%    1377.324951  1331.357758  6.437438e+06      -0.007736  \n",
       "50%    1491.525024  1447.983643  1.028824e+07       0.000621  \n",
       "75%    1593.924957  1555.526337  1.687426e+07       0.008270  \n",
       "max    1768.650024  1768.650024  8.670560e+07       0.115996  \n",
       "std     202.460527   206.014400  9.424437e+06       0.017563  "
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hdfc.describe()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 206
    },
    "id": "3hZ2Dy5yv2oW",
    "outputId": "57357ce8-8bbb-4c94-8233-2e5ad1fee202"
   },
   "outputs": [
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"stocks_data\",\n  \"rows\": 3,\n  \"fields\": [\n    {\n      \"column\": [\n        \"Ticker\",\n        \"\"\n      ],\n      \"properties\": {\n        \"dtype\": \"string\",\n        \"num_unique_values\": 3,\n        \"samples\": [\n          \"HDFCBANK.NS\",\n          \"INFY\",\n          \"TCS\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": [\n        \"Daily_Returns\",\n        \"mean\"\n      ],\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.000340085543426757,\n        \"min\": 0.00032568020420197495,\n        \"max\": 0.000949677082478003,\n        \"num_unique_values\": 3,\n        \"samples\": [\n          0.0004032741799227008,\n          0.000949677082478003,\n          0.00032568020420197495\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": [\n        \"Daily_Returns\",\n        \"std\"\n      ],\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.02310729467652003,\n        \"min\": 0.017563133198006006,\n        \"max\": 0.05851959022107058,\n        \"num_unique_values\": 3,\n        \"samples\": [\n          0.017563133198006006,\n          0.019500388895497735,\n          0.05851959022107058\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": [\n        \"Daily_Returns\",\n        \"var\"\n      ],\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.00177870395737283,\n        \"min\": 0.0003084636477309007,\n        \"max\": 0.0034245424396420194,\n        \"num_unique_values\": 3,\n        \"samples\": [\n          0.0003084636477309007,\n          0.00038026516707565146,\n          0.0034245424396420194\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-a3fc5119-1d54-496e-8cca-344eda2e320f\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr th {\n",
       "        text-align: left;\n",
       "    }\n",
       "\n",
       "    .dataframe thead tr:last-of-type th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th colspan=\"3\" halign=\"left\">Daily_Returns</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <th>mean</th>\n",
       "      <th>std</th>\n",
       "      <th>var</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>Ticker</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>HDFCBANK.NS</th>\n",
       "      <td>0.000403</td>\n",
       "      <td>0.017563</td>\n",
       "      <td>0.000308</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>INFY</th>\n",
       "      <td>0.000950</td>\n",
       "      <td>0.019500</td>\n",
       "      <td>0.000380</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>TCS</th>\n",
       "      <td>0.000326</td>\n",
       "      <td>0.058520</td>\n",
       "      <td>0.003425</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-a3fc5119-1d54-496e-8cca-344eda2e320f')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-a3fc5119-1d54-496e-8cca-344eda2e320f button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-a3fc5119-1d54-496e-8cca-344eda2e320f');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-2e19eff6-7a0b-4140-9640-88a1343ddae0\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-2e19eff6-7a0b-4140-9640-88a1343ddae0')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-2e19eff6-7a0b-4140-9640-88a1343ddae0 button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "            Daily_Returns                    \n",
       "                     mean       std       var\n",
       "Ticker                                       \n",
       "HDFCBANK.NS      0.000403  0.017563  0.000308\n",
       "INFY             0.000950  0.019500  0.000380\n",
       "TCS              0.000326  0.058520  0.003425"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# group be ticker and compute the mean of the returns\n",
    "\n",
    "stocks_data.groupby('Ticker').agg({'Daily_Returns':['mean','std','var']})\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "KkwEj62gwCmo"
   },
   "source": [
    "# Probability basics, distributions-Binomial, Normal distributions"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "B3V3t8jIwJan"
   },
   "source": [
    "### Binomial Distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "YDPvpLPLwLOd"
   },
   "source": [
    "* Compute the monthly returns from HDFC stock data\n",
    "* Flag as profit if  the returns is more than 5% (say) else loss\n",
    "* Compute the number of months the stock has made profilt and loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "id": "gfzSEbywwQnS"
   },
   "outputs": [],
   "source": [
    "\n",
    "def compute_monthly_returns(prices_df):\n",
    "  \"\"\"\n",
    "  This function takes a DataFrame containing adjusted daily closing prices\n",
    "  with columns 'Date' and 'Adj Close' and computes monthly returns.\n",
    "\n",
    "  Args:\n",
    "      prices_df: DataFrame containing adjusted daily closing prices.\n",
    "\n",
    "  Returns:\n",
    "      A DataFrame with monthly returns.\n",
    "  \"\"\"\n",
    "\n",
    "  # Ensure the 'Date' column is a datetime index\n",
    "  prices_df['Date'] = pd.to_datetime(prices_df['Date'])\n",
    "  prices_df.set_index('Date', inplace=True)\n",
    "  prices_df = prices_df.drop(['Daily_Returns'],axis=1)\n",
    "  # Resample data to monthly frequency, taking the end-of-month price\n",
    "#   monthly_prices = prices_df['Adj Close'].resample('M').last()\n",
    "  # monthly_prices =prices_df['Adj Close'].resample('M').agg(lambda x: (x + 1).prod() - 1)\\\n",
    "  # monthly_prices =prices_df['Adj Close'].asfreq('M').ffill()\n",
    "  month_end_prices = prices_df.resample('BM').apply(lambda x: x[-1])\n",
    "  month_end_prices['monthly_returns'] = month_end_prices['Adj Close'].pct_change()\n",
    "  # Calculate monthly returns (percentage change)\n",
    "  # monthly_returns = monthly_prices/monthly_prices.shift(1) -1\n",
    "\n",
    "  return month_end_prices\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/",
     "height": 263
    },
    "id": "t4pIPD-vwgjL",
    "outputId": "65b8a2d5-3e1e-49a2-9767-5391ecab4e15"
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "<ipython-input-8-aa90d2d3d525>:21: FutureWarning: Series.__getitem__ treating keys as positions is deprecated. In a future version, integer keys will always be treated as labels (consistent with DataFrame behavior). To access a value by position, use `ser.iloc[pos]`\n",
      "  month_end_prices = prices_df.resample('BM').apply(lambda x: x[-1])\n"
     ]
    },
    {
     "data": {
      "application/vnd.google.colaboratory.intrinsic+json": {
       "summary": "{\n  \"name\": \"hdfc_returns\",\n  \"rows\": 56,\n  \"fields\": [\n    {\n      \"column\": \"Date\",\n      \"properties\": {\n        \"dtype\": \"date\",\n        \"min\": \"2020-01-31 00:00:00\",\n        \"max\": \"2024-08-30 00:00:00\",\n        \"num_unique_values\": 56,\n        \"samples\": [\n          \"2020-01-31 00:00:00\",\n          \"2020-06-30 00:00:00\",\n          \"2022-10-31 00:00:00\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Open\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 201.4542078302481,\n        \"min\": 853.7999877929688,\n        \"max\": 1704.0,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          1231.449951171875,\n          1074.0,\n          1472.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"High\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 199.8682741560254,\n        \"min\": 873.5999755859375,\n        \"max\": 1714.9000244140625,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          1237.800048828125,\n          1078.550048828125,\n          1498.0\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Low\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 201.8483547056044,\n        \"min\": 838.0,\n        \"max\": 1696.0,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          1220.25,\n          1056.300048828125,\n          1467.25\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 200.3158531753462,\n        \"min\": 861.9000244140625,\n        \"max\": 1709.25,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          1226.300048828125,\n          1065.8499755859375,\n          1496.699951171875\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Adj Close\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 204.9858907416845,\n        \"min\": 827.4055786132812,\n        \"max\": 1686.223876953125,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          1177.2218017578125,\n          1023.1931762695312,\n          1459.79638671875\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Volume\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 9393478,\n        \"min\": 3162868,\n        \"max\": 50037886,\n        \"num_unique_values\": 56,\n        \"samples\": [\n          5589134,\n          17873065,\n          6415264\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"Ticker\",\n      \"properties\": {\n        \"dtype\": \"category\",\n        \"num_unique_values\": 1,\n        \"samples\": [\n          \"HDFCBANK.NS\"\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    },\n    {\n      \"column\": \"monthly_returns\",\n      \"properties\": {\n        \"dtype\": \"number\",\n        \"std\": 0.07472156347942437,\n        \"min\": -0.26811861172769125,\n        \"max\": 0.21739677906504018,\n        \"num_unique_values\": 55,\n        \"samples\": [\n          -0.043570391608257375\n        ],\n        \"semantic_type\": \"\",\n        \"description\": \"\"\n      }\n    }\n  ]\n}",
       "type": "dataframe",
       "variable_name": "hdfc_returns"
      },
      "text/html": [
       "\n",
       "  <div id=\"df-559311f8-13cb-46d5-bdb5-161cbe41b478\" class=\"colab-df-container\">\n",
       "    <div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>Date</th>\n",
       "      <th>Open</th>\n",
       "      <th>High</th>\n",
       "      <th>Low</th>\n",
       "      <th>Close</th>\n",
       "      <th>Adj Close</th>\n",
       "      <th>Volume</th>\n",
       "      <th>Ticker</th>\n",
       "      <th>monthly_returns</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>2020-01-31</td>\n",
       "      <td>1231.449951</td>\n",
       "      <td>1237.800049</td>\n",
       "      <td>1220.250000</td>\n",
       "      <td>1226.300049</td>\n",
       "      <td>1177.221802</td>\n",
       "      <td>5589134</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>2020-02-28</td>\n",
       "      <td>1175.500000</td>\n",
       "      <td>1185.000000</td>\n",
       "      <td>1170.099976</td>\n",
       "      <td>1177.650024</td>\n",
       "      <td>1130.518677</td>\n",
       "      <td>12156528</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>-0.039672</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>2020-03-31</td>\n",
       "      <td>853.799988</td>\n",
       "      <td>873.599976</td>\n",
       "      <td>838.000000</td>\n",
       "      <td>861.900024</td>\n",
       "      <td>827.405579</td>\n",
       "      <td>17605546</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>-0.268119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>2020-04-30</td>\n",
       "      <td>1001.400024</td>\n",
       "      <td>1019.000000</td>\n",
       "      <td>992.099976</td>\n",
       "      <td>1001.799988</td>\n",
       "      <td>961.706543</td>\n",
       "      <td>21896567</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>0.162316</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2020-05-29</td>\n",
       "      <td>944.000000</td>\n",
       "      <td>955.000000</td>\n",
       "      <td>923.450012</td>\n",
       "      <td>951.650024</td>\n",
       "      <td>913.563660</td>\n",
       "      <td>26512583</td>\n",
       "      <td>HDFCBANK.NS</td>\n",
       "      <td>-0.050060</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>\n",
       "    <div class=\"colab-df-buttons\">\n",
       "\n",
       "  <div class=\"colab-df-container\">\n",
       "    <button class=\"colab-df-convert\" onclick=\"convertToInteractive('df-559311f8-13cb-46d5-bdb5-161cbe41b478')\"\n",
       "            title=\"Convert this dataframe to an interactive table.\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "  <svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\" viewBox=\"0 -960 960 960\">\n",
       "    <path d=\"M120-120v-720h720v720H120Zm60-500h600v-160H180v160Zm220 220h160v-160H400v160Zm0 220h160v-160H400v160ZM180-400h160v-160H180v160Zm440 0h160v-160H620v160ZM180-180h160v-160H180v160Zm440 0h160v-160H620v160Z\"/>\n",
       "  </svg>\n",
       "    </button>\n",
       "\n",
       "  <style>\n",
       "    .colab-df-container {\n",
       "      display:flex;\n",
       "      gap: 12px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert {\n",
       "      background-color: #E8F0FE;\n",
       "      border: none;\n",
       "      border-radius: 50%;\n",
       "      cursor: pointer;\n",
       "      display: none;\n",
       "      fill: #1967D2;\n",
       "      height: 32px;\n",
       "      padding: 0 0 0 0;\n",
       "      width: 32px;\n",
       "    }\n",
       "\n",
       "    .colab-df-convert:hover {\n",
       "      background-color: #E2EBFA;\n",
       "      box-shadow: 0px 1px 2px rgba(60, 64, 67, 0.3), 0px 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "      fill: #174EA6;\n",
       "    }\n",
       "\n",
       "    .colab-df-buttons div {\n",
       "      margin-bottom: 4px;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert {\n",
       "      background-color: #3B4455;\n",
       "      fill: #D2E3FC;\n",
       "    }\n",
       "\n",
       "    [theme=dark] .colab-df-convert:hover {\n",
       "      background-color: #434B5C;\n",
       "      box-shadow: 0px 1px 3px 1px rgba(0, 0, 0, 0.15);\n",
       "      filter: drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.3));\n",
       "      fill: #FFFFFF;\n",
       "    }\n",
       "  </style>\n",
       "\n",
       "    <script>\n",
       "      const buttonEl =\n",
       "        document.querySelector('#df-559311f8-13cb-46d5-bdb5-161cbe41b478 button.colab-df-convert');\n",
       "      buttonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "\n",
       "      async function convertToInteractive(key) {\n",
       "        const element = document.querySelector('#df-559311f8-13cb-46d5-bdb5-161cbe41b478');\n",
       "        const dataTable =\n",
       "          await google.colab.kernel.invokeFunction('convertToInteractive',\n",
       "                                                    [key], {});\n",
       "        if (!dataTable) return;\n",
       "\n",
       "        const docLinkHtml = 'Like what you see? Visit the ' +\n",
       "          '<a target=\"_blank\" href=https://colab.research.google.com/notebooks/data_table.ipynb>data table notebook</a>'\n",
       "          + ' to learn more about interactive tables.';\n",
       "        element.innerHTML = '';\n",
       "        dataTable['output_type'] = 'display_data';\n",
       "        await google.colab.output.renderOutput(dataTable, element);\n",
       "        const docLink = document.createElement('div');\n",
       "        docLink.innerHTML = docLinkHtml;\n",
       "        element.appendChild(docLink);\n",
       "      }\n",
       "    </script>\n",
       "  </div>\n",
       "\n",
       "\n",
       "<div id=\"df-7694d39b-eb35-4510-8ad8-762c6e5c99dd\">\n",
       "  <button class=\"colab-df-quickchart\" onclick=\"quickchart('df-7694d39b-eb35-4510-8ad8-762c6e5c99dd')\"\n",
       "            title=\"Suggest charts\"\n",
       "            style=\"display:none;\">\n",
       "\n",
       "<svg xmlns=\"http://www.w3.org/2000/svg\" height=\"24px\"viewBox=\"0 0 24 24\"\n",
       "     width=\"24px\">\n",
       "    <g>\n",
       "        <path d=\"M19 3H5c-1.1 0-2 .9-2 2v14c0 1.1.9 2 2 2h14c1.1 0 2-.9 2-2V5c0-1.1-.9-2-2-2zM9 17H7v-7h2v7zm4 0h-2V7h2v10zm4 0h-2v-4h2v4z\"/>\n",
       "    </g>\n",
       "</svg>\n",
       "  </button>\n",
       "\n",
       "<style>\n",
       "  .colab-df-quickchart {\n",
       "      --bg-color: #E8F0FE;\n",
       "      --fill-color: #1967D2;\n",
       "      --hover-bg-color: #E2EBFA;\n",
       "      --hover-fill-color: #174EA6;\n",
       "      --disabled-fill-color: #AAA;\n",
       "      --disabled-bg-color: #DDD;\n",
       "  }\n",
       "\n",
       "  [theme=dark] .colab-df-quickchart {\n",
       "      --bg-color: #3B4455;\n",
       "      --fill-color: #D2E3FC;\n",
       "      --hover-bg-color: #434B5C;\n",
       "      --hover-fill-color: #FFFFFF;\n",
       "      --disabled-bg-color: #3B4455;\n",
       "      --disabled-fill-color: #666;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart {\n",
       "    background-color: var(--bg-color);\n",
       "    border: none;\n",
       "    border-radius: 50%;\n",
       "    cursor: pointer;\n",
       "    display: none;\n",
       "    fill: var(--fill-color);\n",
       "    height: 32px;\n",
       "    padding: 0;\n",
       "    width: 32px;\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart:hover {\n",
       "    background-color: var(--hover-bg-color);\n",
       "    box-shadow: 0 1px 2px rgba(60, 64, 67, 0.3), 0 1px 3px 1px rgba(60, 64, 67, 0.15);\n",
       "    fill: var(--button-hover-fill-color);\n",
       "  }\n",
       "\n",
       "  .colab-df-quickchart-complete:disabled,\n",
       "  .colab-df-quickchart-complete:disabled:hover {\n",
       "    background-color: var(--disabled-bg-color);\n",
       "    fill: var(--disabled-fill-color);\n",
       "    box-shadow: none;\n",
       "  }\n",
       "\n",
       "  .colab-df-spinner {\n",
       "    border: 2px solid var(--fill-color);\n",
       "    border-color: transparent;\n",
       "    border-bottom-color: var(--fill-color);\n",
       "    animation:\n",
       "      spin 1s steps(1) infinite;\n",
       "  }\n",
       "\n",
       "  @keyframes spin {\n",
       "    0% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "      border-left-color: var(--fill-color);\n",
       "    }\n",
       "    20% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    30% {\n",
       "      border-color: transparent;\n",
       "      border-left-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    40% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-top-color: var(--fill-color);\n",
       "    }\n",
       "    60% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "    }\n",
       "    80% {\n",
       "      border-color: transparent;\n",
       "      border-right-color: var(--fill-color);\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "    90% {\n",
       "      border-color: transparent;\n",
       "      border-bottom-color: var(--fill-color);\n",
       "    }\n",
       "  }\n",
       "</style>\n",
       "\n",
       "  <script>\n",
       "    async function quickchart(key) {\n",
       "      const quickchartButtonEl =\n",
       "        document.querySelector('#' + key + ' button');\n",
       "      quickchartButtonEl.disabled = true;  // To prevent multiple clicks.\n",
       "      quickchartButtonEl.classList.add('colab-df-spinner');\n",
       "      try {\n",
       "        const charts = await google.colab.kernel.invokeFunction(\n",
       "            'suggestCharts', [key], {});\n",
       "      } catch (error) {\n",
       "        console.error('Error during call to suggestCharts:', error);\n",
       "      }\n",
       "      quickchartButtonEl.classList.remove('colab-df-spinner');\n",
       "      quickchartButtonEl.classList.add('colab-df-quickchart-complete');\n",
       "    }\n",
       "    (() => {\n",
       "      let quickchartButtonEl =\n",
       "        document.querySelector('#df-7694d39b-eb35-4510-8ad8-762c6e5c99dd button');\n",
       "      quickchartButtonEl.style.display =\n",
       "        google.colab.kernel.accessAllowed ? 'block' : 'none';\n",
       "    })();\n",
       "  </script>\n",
       "</div>\n",
       "\n",
       "    </div>\n",
       "  </div>\n"
      ],
      "text/plain": [
       "        Date         Open         High          Low        Close    Adj Close  \\\n",
       "0 2020-01-31  1231.449951  1237.800049  1220.250000  1226.300049  1177.221802   \n",
       "1 2020-02-28  1175.500000  1185.000000  1170.099976  1177.650024  1130.518677   \n",
       "2 2020-03-31   853.799988   873.599976   838.000000   861.900024   827.405579   \n",
       "3 2020-04-30  1001.400024  1019.000000   992.099976  1001.799988   961.706543   \n",
       "4 2020-05-29   944.000000   955.000000   923.450012   951.650024   913.563660   \n",
       "\n",
       "     Volume       Ticker  monthly_returns  \n",
       "0   5589134  HDFCBANK.NS              NaN  \n",
       "1  12156528  HDFCBANK.NS        -0.039672  \n",
       "2  17605546  HDFCBANK.NS        -0.268119  \n",
       "3  21896567  HDFCBANK.NS         0.162316  \n",
       "4  26512583  HDFCBANK.NS        -0.050060  "
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "hdfc_returns = compute_monthly_returns(hdfc).reset_index()\n",
    "hdfc_returns.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "colab": {
     "base_uri": "https://localhost:8080/"
    },
    "id": "deGMJjgfwohE",
    "outputId": "4505e1b6-79d2-4dfc-a516-411363e4f9da"
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Counts: \n",
      " profit_flag\n",
      "Loss      41\n",
      "Profit    15\n",
      "Name: count, dtype: int64\n",
      "\n",
      "%\n",
      " profit_flag\n",
      "Loss      0.732143\n",
      "Profit    0.267857\n",
      "Name: proportion, dtype: float64\n"
     ]
    }
   ],
   "source": [
    "# from the stock data calculate the monthly returns\n",
    "profit_threshold = 0.05\n",
    "hdfc_returns['profit_flag'] = hdfc_returns['monthly_returns'].apply(lambda x: 'Profit' if x > profit_threshold else 'Loss')\n",
    "print('Counts: \\n', hdfc_returns['profit_flag'].value_counts())\n",
    "print('\\n%\\n',hdfc_returns['profit_flag'].value_counts(normalize=True))\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "AbnXndtewu6p"
   },
   "source": [
    "\n",
    "* what is the probability of making a profit in a given month\n",
    "* what is the probability of making profit of >5% twice a year\n",
    "* what is the probability of making profit of >5% \"atleast\" twice a year\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "Mmm1y_GCwwNY"
   },
   "outputs": [],
   "source": [
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "wACSXs6uw69i"
   },
   "source": [
    "**Activity1:**\n",
    "* What is the probability of making profit of >5% [0,1,2,... 12] times a year\n",
    "* Cumulative probability\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "RReZB_Mvw-SS"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "fM5GtY30y4vD"
   },
   "source": [
    "**Activity 2:**\n",
    "\n",
    "\n",
    "Consider TCS stock and answer the following questions\n",
    "* what is the probability of making a profit in a given month\n",
    "* what is the probability of making profit of >5% twice a year\n",
    "* what is the probability of making profit of >5% \"atleast\" twice a year\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "Q7ShMiAa6pgC"
   },
   "source": [
    "# Normal Distribution"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "CO2jlqt47F6H"
   },
   "source": [
    "#### Application of Normal distribution using PDF and CDF functions\n",
    "* what is the probability of making a profit of 6% in a given year?\n",
    "* what is the probability of making profit of 5% twice a year?\n",
    "* what is the probability of making profit of >5%  in a given month?\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "id": "7Ntv5OIz7IJk"
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "id": "gudscIWf7vRX"
   },
   "source": [
    "**Activity 3:**\n",
    "* What is the probability of hdfc stock closing price less than 1360\n",
    "* What is the probability of hdfc stock closing price between 1400 to 1500\n",
    "* What is the Probability of hdfc stock monthly returns being greater than 5% ?"
   ]
  }
 ],
 "metadata": {
  "colab": {
   "provenance": []
  },
  "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.8.19"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
