{"id":124636,"date":"2022-02-17T12:40:59","date_gmt":"2022-02-17T17:40:59","guid":{"rendered":"https:\/\/ibkrcampus.com\/?p=124636"},"modified":"2022-11-21T09:51:52","modified_gmt":"2022-11-21T14:51:52","slug":"linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i","status":"publish","type":"post","link":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/","title":{"rendered":"Linear regression on market data &#8211; Implemented from scratch in Python and R &#8211; Part I"},"content":{"rendered":"\n<p>This is the second installment of my series on regression analysis used in finance. In the&nbsp;<a href=\"https:\/\/blog.quantinsti.com\/linear-regression\/\">first installment<\/a>, we touched upon the most important technique in financial econometrics: regression analysis, specifically linear regression and two of its most popular flavours:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>univariate linear regression, and<\/li><li>multivariate linear regression.<\/li><\/ul>\n\n\n\n<p>In this post, we apply our knowledge of regression to actual financial data. We will model the relationships from the previous post using&nbsp;<code>Python<\/code>&nbsp;and&nbsp;<code>R<\/code>. I run it in both (with downloadable code where applicable) so that readers fluent in one language or application can hopefully get more intuition on its implementation in others.<\/p>\n\n\n\n<p>Many building blocks needed to develop and implement models are available as ready-to-wear software these days. Using them as-is is now standard practice among practitioners of quantitative trading.<\/p>\n\n\n\n<p>Many assumptions underlie the linear regression model. Closely linked to them are also its shortcomings. If you plan to use linear regression for data analysis and forecasting, I&#8217;d recommend you look that up first. I intend to write on that topic next.<\/p>\n\n\n\n<p>For now, I shall shrug those worries away and get on with the implementation.<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Implementation using Python<\/li><li>Implementation using R<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"implementation-using-python\"><strong>Implementation using Python<\/strong><\/h2>\n\n\n\n<p>There are many ways to perform regression analysis in&nbsp;<code>Python<\/code>. The&nbsp;<code>statsmodels<\/code>,&nbsp;<code>sklearn<\/code>, and&nbsp;<code>scipy<\/code>&nbsp;libraries are great options to work with.<\/p>\n\n\n\n<p>For the sake of brevity, we implement simple and multiple linear regression using the first two.<\/p>\n\n\n\n<p>I point to the differences in approach as we walk through the below code.<\/p>\n\n\n\n<p>We use two ways to quantify the strength of the linear relationship between variables.<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>Calculate pairwise correlations between the variables.<\/li><li>Linear regression modeling (the preferred way).<\/li><\/ol>\n\n\n\n<p>We start with the necessary imports and get the required financial data.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>### Import the required libraries\n\nimport numpy as np\nimport pandas as pd\n\nimport yfinance as yf\nimport datetime\nimport matplotlib.pyplot as plt\n\n## To use statsmodels for linear regression\nimport statsmodels.formula.api as smf\n\n## To use sklearn for linear regression\nfrom sklearn.linear_model import LinearRegression<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/35553a4c3f2cb566574c199daa53e628#file-importing-libraries-py\" target=\"_blank\" rel=\"noreferrer noopener\">Importing libraries.py&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<p>As discussed in my previous post, we work with the historical returns of Coca-Cola (<em><em>NYSE: KO<\/em><\/em>), its competitor PepsiCo (<em><em>NASDAQ: PEP<\/em><\/em>), the US Dollar index (<em><em>ICE: DX<\/em><\/em>) and the SPDR S&amp;P 500 ETF (<em><em>NYSEARCA: SPY<\/em><\/em>).<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>####################################################\n## Fetch data from yfinance\n## 3-year daily data for Coca-Cola, SPY, Pepsi, and USD index\n\nend1 = datetime.date(2021, 7, 28)\nstart1 = end1 - pd.Timedelta(days = 365 * 3)\n\nko_df = yf.download(\"KO\", start = start1, end = end1, progress = False)\nspy_df = yf.download(\"SPY\", start = start1, end = end1, progress = False)\npep_df = yf.download(\"PEP\", start = start1, end = end1, progress = False)\nusdx_df = yf.download(\"DX-Y.NYB\", start = start1, end = end1, progress = False)\n\n####################################################\n## Calculate log returns for the period based on Adj Close prices\n\nko_df&#91;'ko'] = np.log(ko_df&#91;'Adj Close'] \/ ko_df&#91;'Adj Close'].shift(1))\nspy_df&#91;'spy'] = np.log(spy_df&#91;'Adj Close'] \/ spy_df&#91;'Adj Close'].shift(1))\npep_df&#91;'pep'] = np.log(pep_df&#91;'Adj Close'] \/ pep_df&#91;'Adj Close'].shift(1))\nusdx_df&#91;'usdx'] = np.log(usdx_df&#91;'Adj Close'] \/ usdx_df&#91;'Adj Close'].shift(1))\n\n####################################################<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/329ca3a81327dbae55b9dad609f77add#file-data-py\" target=\"_blank\" rel=\"noreferrer noopener\">Data.py&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>####################################################\n## Create a dataframe with X's (spy, pep, usdx) and Y (ko)\n\ndf = pd.concat(&#91;spy_df&#91;'spy'], ko_df&#91;'ko'], \n                pep_df&#91;'pep'], usdx_df&#91;'usdx']], axis = 1).dropna()\n\n## Save the csv file. Good practice to save data files after initial processing\ndf.to_csv(\"Jul2021_data_lin_regression.csv\")\n\n####################################################<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/00f37c1f79339cdb3bf7763b70c62f8a#file-dataframe-py\" target=\"_blank\" rel=\"noreferrer noopener\">Dataframe.py&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<p>We first create a scatter plot of the&nbsp;<em><em>SPY<\/em><\/em>&nbsp;and&nbsp;<em><em>KO<\/em><\/em>&nbsp;returns to better understand how they are related.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>####################################################\n## A scatter plot of X (spy) and Y (ko) to examine the nature of their relationship visually\n\nplt.figure(figsize = (10, 6))\nplt.rcParams.update({'font.size': 14})\nplt.xlabel(\"SPY returns\")\nplt.ylabel(\"KO returns\")\nplt.title(\"Scatter plot of daily returns (Jul 2018 to Jul 2021)\")\nplt.scatter(df&#91;'spy'], df&#91;'ko'])\nplt.show()\n\n####################################################<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/cb8d959a76eb6735f0c61ec31c92c29a#file-scatter-plot-py\" target=\"_blank\" rel=\"noreferrer noopener\">Scatter Plot.py&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" width=\"649\" height=\"399\" data-src=\"\/campus\/wp-content\/uploads\/sites\/2\/2022\/02\/scatter-plot-of-daily-returns-quantinsti.png\" alt=\"\" class=\"wp-image-124671 lazyload\" data-srcset=\"https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/02\/scatter-plot-of-daily-returns-quantinsti.png 649w, https:\/\/ibkrcampus.com\/campus\/wp-content\/uploads\/sites\/2\/2022\/02\/scatter-plot-of-daily-returns-quantinsti-300x184.png 300w\" data-sizes=\"(max-width: 649px) 100vw, 649px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 649px; aspect-ratio: 649\/399;\" \/><\/figure>\n\n\n\n<p class=\"has-text-align-center\"><em>Fig: Scatter plot of daily returns<\/em><\/p>\n\n\n\n<p>We also calculate correlations between different variables to analyze the strength of the linear relationships here.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>####################################################\n## 1. Calculate correlation between Xs and Y\n\ndf.corr()\n\n####################################################<\/code><\/pre>\n\n\n\n<p class=\"has-text-align-center\"><a href=\"https:\/\/gist.github.com\/quantra-go-algo\/1d0a550f434de30831c645a2a2213832#file-correlation-py\" target=\"_blank\" rel=\"noreferrer noopener\">Correlation.py&nbsp;<\/a>hosted with \u2764 by&nbsp;<a href=\"https:\/\/github.com\/\" target=\"_blank\" rel=\"noreferrer noopener\">GitHub<\/a><\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th>&nbsp;<\/th><th>spy<\/th><th>ko<\/th><th>pep<\/th><th>usdx<\/th><\/tr><\/thead><tbody><tr><th>spy<\/th><td>1.000000<\/td><td>0.684382<\/td><td>0.725681<\/td><td>-0.045420<\/td><\/tr><tr><th>ko<\/th><td>0.684382<\/td><td>1.000000<\/td><td>0.738264<\/td><td>-0.104387<\/td><\/tr><tr><th>pep<\/th><td>0.725681<\/td><td>0.738264<\/td><td>1.000000<\/td><td>-0.011062<\/td><\/tr><tr><th>usdx<\/th><td>-0.045420<\/td><td>-0.104387<\/td><td>-0.011062<\/td><td>1.000000<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p><em>Stay tuned for the next installment in which Vivek Krishnamoorthy will review the statsmodels.<\/em><\/p>\n\n\n\n<p><em>Visit QuantInsti for additional insight on this topic: <a href=\"https:\/\/blog.quantinsti.com\/linear-regression-market-data-python-r\/\">https:\/\/blog.quantinsti.com\/linear-regression-market-data-python-r\/<\/a>.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this post, we apply our knowledge of regression to actual financial data. <\/p>\n","protected":false},"author":646,"featured_media":99378,"comment_status":"closed","ping_status":"open","sticky":true,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[339,343,349,338,350,341,344,342],"tags":[11085,806,11082,11083,4922,865,11081,4404,595,487,7556,11084,11087,6810,11086],"contributors-categories":[13654],"class_list":{"0":"post-124636","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-data-science","8":"category-programing-languages","9":"category-python-development","10":"category-ibkr-quant-news","11":"category-quant-asia-pacific","12":"category-quant-development","13":"category-quant-regions","14":"category-r-development","15":"tag-correlation-py","16":"tag-data-science","17":"tag-data-py","18":"tag-dataframe-py","19":"tag-econometrics","20":"tag-github","21":"tag-importing-libraries-py","22":"tag-linear-regression","23":"tag-python","24":"tag-r","25":"tag-scatter-plot","26":"tag-scatter-plot-py","27":"tag-scipy","28":"tag-sklearn","29":"tag-statsmodels","30":"contributors-categories-quantinsti"},"pp_statuses_selecting_workflow":false,"pp_workflow_action":"current","pp_status_selection":"publish","acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v26.9 (Yoast SEO v27.8) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>Linear regression on market data &#8211; Implemented from scratch in Python and R &#8211; Part I<\/title>\n<meta name=\"description\" content=\"In this post, we apply our knowledge of regression to actual financial data.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/124636\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Linear regression on market data - Implemented from scratch in Python and R - Part I | IBKR Quant Blog\" \/>\n<meta property=\"og:description\" content=\"In this post, we apply our knowledge of regression to actual financial data.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/\" \/>\n<meta property=\"og:site_name\" content=\"IBKR Campus US\" \/>\n<meta property=\"article:published_time\" content=\"2022-02-17T17:40:59+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-11-21T14:51:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"900\" \/>\n\t<meta property=\"og:image:height\" content=\"550\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Vivek Krishnamoorthy\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Vivek Krishnamoorthy\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\n\t    \"@context\": \"https:\\\/\\\/schema.org\",\n\t    \"@graph\": [\n\t        {\n\t            \"@type\": \"NewsArticle\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/#article\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/\"\n\t            },\n\t            \"author\": {\n\t                \"name\": \"Vivek Krishnamoorthy\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/3219a2e26a01b50f115c36fec5150e5c\"\n\t            },\n\t            \"headline\": \"Linear regression on market data &#8211; Implemented from scratch in Python and R &#8211; Part I\",\n\t            \"datePublished\": \"2022-02-17T17:40:59+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:51:52+00:00\",\n\t            \"mainEntityOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/\"\n\t            },\n\t            \"wordCount\": 470,\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/08\\\/python-blocks.jpg\",\n\t            \"keywords\": [\n\t                \"Correlation.py\",\n\t                \"Data Science\",\n\t                \"Data.py\",\n\t                \"Dataframe.py\",\n\t                \"Econometrics\",\n\t                \"GitHub\",\n\t                \"Importing libraries.py\",\n\t                \"Linear Regression\",\n\t                \"Python\",\n\t                \"R\",\n\t                \"Scatter Plot\",\n\t                \"Scatter Plot.py\",\n\t                \"scipy\",\n\t                \"sklearn\",\n\t                \"statsmodels\"\n\t            ],\n\t            \"articleSection\": [\n\t                \"Data Science\",\n\t                \"Programming Languages\",\n\t                \"Python Development\",\n\t                \"Quant\",\n\t                \"Quant Asia Pacific\",\n\t                \"Quant Development\",\n\t                \"Quant Regions\",\n\t                \"R Development\"\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"WebPage\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/\",\n\t            \"name\": \"Linear regression on market data - Implemented from scratch in Python and R - Part I | IBKR Quant Blog\",\n\t            \"isPartOf\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\"\n\t            },\n\t            \"primaryImageOfPage\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/#primaryimage\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/#primaryimage\"\n\t            },\n\t            \"thumbnailUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/08\\\/python-blocks.jpg\",\n\t            \"datePublished\": \"2022-02-17T17:40:59+00:00\",\n\t            \"dateModified\": \"2022-11-21T14:51:52+00:00\",\n\t            \"description\": \"In this post, we apply our knowledge of regression to actual financial data.\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"ReadAction\",\n\t                    \"target\": [\n\t                        \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/\"\n\t                    ]\n\t                }\n\t            ]\n\t        },\n\t        {\n\t            \"@type\": \"ImageObject\",\n\t            \"inLanguage\": \"en-US\",\n\t            \"@id\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/ibkr-quant-news\\\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\\\/#primaryimage\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/08\\\/python-blocks.jpg\",\n\t            \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2021\\\/08\\\/python-blocks.jpg\",\n\t            \"width\": 900,\n\t            \"height\": 550,\n\t            \"caption\": \"Python\"\n\t        },\n\t        {\n\t            \"@type\": \"WebSite\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#website\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"name\": \"IBKR Campus US\",\n\t            \"description\": \"Financial Education from Interactive Brokers\",\n\t            \"publisher\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\"\n\t            },\n\t            \"potentialAction\": [\n\t                {\n\t                    \"@type\": \"SearchAction\",\n\t                    \"target\": {\n\t                        \"@type\": \"EntryPoint\",\n\t                        \"urlTemplate\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/?s={search_term_string}\"\n\t                    },\n\t                    \"query-input\": {\n\t                        \"@type\": \"PropertyValueSpecification\",\n\t                        \"valueRequired\": true,\n\t                        \"valueName\": \"search_term_string\"\n\t                    }\n\t                }\n\t            ],\n\t            \"inLanguage\": \"en-US\"\n\t        },\n\t        {\n\t            \"@type\": \"Organization\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#organization\",\n\t            \"name\": \"Interactive Brokers\",\n\t            \"alternateName\": \"IBKR\",\n\t            \"url\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/\",\n\t            \"logo\": {\n\t                \"@type\": \"ImageObject\",\n\t                \"inLanguage\": \"en-US\",\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\",\n\t                \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"contentUrl\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/wp-content\\\/uploads\\\/sites\\\/2\\\/2024\\\/05\\\/ibkr-campus-logo.jpg\",\n\t                \"width\": 669,\n\t                \"height\": 669,\n\t                \"caption\": \"Interactive Brokers\"\n\t            },\n\t            \"image\": {\n\t                \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/logo\\\/image\\\/\"\n\t            },\n\t            \"publishingPrinciples\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/about-ibkr-campus\\\/\",\n\t            \"ethicsPolicy\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/cyber-security-notice\\\/\"\n\t        },\n\t        {\n\t            \"@type\": \"Person\",\n\t            \"@id\": \"https:\\\/\\\/ibkrcampus.com\\\/campus\\\/#\\\/schema\\\/person\\\/3219a2e26a01b50f115c36fec5150e5c\",\n\t            \"name\": \"Vivek Krishnamoorthy\",\n\t            \"url\": \"https:\\\/\\\/www.interactivebrokers.com\\\/campus\\\/author\\\/vivekkrishnamoorthy\\\/\"\n\t        }\n\t    ]\n\t}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Linear regression on market data &#8211; Implemented from scratch in Python and R &#8211; Part I","description":"In this post, we apply our knowledge of regression to actual financial data.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.interactivebrokers.com\/campus\/wp-json\/wp\/v2\/posts\/124636\/","og_locale":"en_US","og_type":"article","og_title":"Linear regression on market data - Implemented from scratch in Python and R - Part I | IBKR Quant Blog","og_description":"In this post, we apply our knowledge of regression to actual financial data.","og_url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/","og_site_name":"IBKR Campus US","article_published_time":"2022-02-17T17:40:59+00:00","article_modified_time":"2022-11-21T14:51:52+00:00","og_image":[{"width":900,"height":550,"url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","type":"image\/jpeg"}],"author":"Vivek Krishnamoorthy","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Vivek Krishnamoorthy","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"NewsArticle","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/#article","isPartOf":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/"},"author":{"name":"Vivek Krishnamoorthy","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/3219a2e26a01b50f115c36fec5150e5c"},"headline":"Linear regression on market data &#8211; Implemented from scratch in Python and R &#8211; Part I","datePublished":"2022-02-17T17:40:59+00:00","dateModified":"2022-11-21T14:51:52+00:00","mainEntityOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/"},"wordCount":470,"publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","keywords":["Correlation.py","Data Science","Data.py","Dataframe.py","Econometrics","GitHub","Importing libraries.py","Linear Regression","Python","R","Scatter Plot","Scatter Plot.py","scipy","sklearn","statsmodels"],"articleSection":["Data Science","Programming Languages","Python Development","Quant","Quant Asia Pacific","Quant Development","Quant Regions","R Development"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/","url":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/","name":"Linear regression on market data - Implemented from scratch in Python and R - Part I | IBKR Quant Blog","isPartOf":{"@id":"https:\/\/ibkrcampus.com\/campus\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/#primaryimage"},"image":{"@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/#primaryimage"},"thumbnailUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","datePublished":"2022-02-17T17:40:59+00:00","dateModified":"2022-11-21T14:51:52+00:00","description":"In this post, we apply our knowledge of regression to actual financial data.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.interactivebrokers.com\/campus\/ibkr-quant-news\/linear-regression-on-market-data-implemented-from-scratch-in-python-and-r-part-i\/#primaryimage","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","width":900,"height":550,"caption":"Python"},{"@type":"WebSite","@id":"https:\/\/ibkrcampus.com\/campus\/#website","url":"https:\/\/ibkrcampus.com\/campus\/","name":"IBKR Campus US","description":"Financial Education from Interactive Brokers","publisher":{"@id":"https:\/\/ibkrcampus.com\/campus\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ibkrcampus.com\/campus\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ibkrcampus.com\/campus\/#organization","name":"Interactive Brokers","alternateName":"IBKR","url":"https:\/\/ibkrcampus.com\/campus\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/","url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","contentUrl":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2024\/05\/ibkr-campus-logo.jpg","width":669,"height":669,"caption":"Interactive Brokers"},"image":{"@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/logo\/image\/"},"publishingPrinciples":"https:\/\/www.interactivebrokers.com\/campus\/about-ibkr-campus\/","ethicsPolicy":"https:\/\/www.interactivebrokers.com\/campus\/cyber-security-notice\/"},{"@type":"Person","@id":"https:\/\/ibkrcampus.com\/campus\/#\/schema\/person\/3219a2e26a01b50f115c36fec5150e5c","name":"Vivek Krishnamoorthy","url":"https:\/\/www.interactivebrokers.com\/campus\/author\/vivekkrishnamoorthy\/"}]}},"jetpack_featured_media_url":"https:\/\/www.interactivebrokers.com\/campus\/wp-content\/uploads\/sites\/2\/2021\/08\/python-blocks.jpg","_links":{"self":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/124636","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/users\/646"}],"replies":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/comments?post=124636"}],"version-history":[{"count":0,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/posts\/124636\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media\/99378"}],"wp:attachment":[{"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/media?parent=124636"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/categories?post=124636"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/tags?post=124636"},{"taxonomy":"contributors-categories","embeddable":true,"href":"https:\/\/ibkrcampus.com\/campus\/wp-json\/wp\/v2\/contributors-categories?post=124636"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}