{ "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", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
OpenHighLowCloseAdj CloseVolumeTickerDaily_Returns
Date
2020-01-011276.0999761280.0000001270.5999761278.5999761227.4285891836849HDFCBANK.NSNaN
2020-01-021279.0000001288.0000001279.0000001286.7500001235.2524413068583HDFCBANK.NS0.006374
2020-01-031282.1999511285.0000001263.5999761268.4000241217.6368415427775HDFCBANK.NS-0.014261
2020-01-061260.0000001261.8000491236.0000001240.9499511191.2854005445093HDFCBANK.NS-0.021641
2020-01-071258.9000241271.4499511252.2500001260.5999761210.1489267362247HDFCBANK.NS0.015835
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\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", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateOpenHighLowCloseAdj CloseVolumeDaily_Returns
count11461146.0000001146.0000001146.0000001146.0000001146.0000001.146000e+031145.000000
mean2022-04-21 17:30:28.2722513921441.6644411455.4797541426.9742601441.3378301402.4708971.291993e+070.000403
min2020-01-01 00:00:00770.450012810.000000738.750000767.700012736.9755865.484040e+05-0.126069
25%2021-02-22 06:00:001380.0000001395.0000001363.0625311377.3249511331.3577586.437438e+06-0.007736
50%2022-04-21 12:00:001491.1250001506.2749631479.3750001491.5250241447.9836431.028824e+070.000621
75%2023-06-15 18:00:001593.2250371605.9499511580.2250371593.9249571555.5263371.687426e+070.008270
max2024-08-19 00:00:001791.0000001794.0000001764.6500241768.6500241768.6500248.670560e+070.115996
stdNaN201.932373200.744924204.178335202.460527206.0144009.424437e+060.017563
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\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", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Daily_Returns
meanstdvar
Ticker
HDFCBANK.NS0.0004030.0175630.000308
INFY0.0009500.0195000.000380
TCS0.0003260.0585200.003425
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\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": [ ":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", "
\n", "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
DateOpenHighLowCloseAdj CloseVolumeTickermonthly_returns
02020-01-311231.4499511237.8000491220.2500001226.3000491177.2218025589134HDFCBANK.NSNaN
12020-02-281175.5000001185.0000001170.0999761177.6500241130.51867712156528HDFCBANK.NS-0.039672
22020-03-31853.799988873.599976838.000000861.900024827.40557917605546HDFCBANK.NS-0.268119
32020-04-301001.4000241019.000000992.0999761001.799988961.70654321896567HDFCBANK.NS0.162316
42020-05-29944.000000955.000000923.450012951.650024913.56366026512583HDFCBANK.NS-0.050060
\n", "
\n", "
\n", "\n", "
\n", " \n", "\n", " \n", "\n", " \n", "
\n", "\n", "\n", "
\n", " \n", "\n", "\n", "\n", " \n", "
\n", "\n", "
\n", "
\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 }