{"id":2598,"date":"2021-10-05T02:34:07","date_gmt":"2021-10-05T02:34:07","guid":{"rendered":"https:\/\/www.tejwin.com\/en\/insights\/xgboost-algorithm-predicts-returns-part-1\/"},"modified":"2021-10-05T02:34:07","modified_gmt":"2021-10-05T02:34:07","slug":"xgboost-algorithm-predicts-returns-part-1","status":"publish","type":"insight","link":"https:\/\/www.tejwin.com\/en\/insights\/xgboost-algorithm-predicts-returns-part-1\/","title":{"rendered":"XGBoost Algorithm Predicts Returns (Part 1)"},"content":{"rendered":"<p class=\"wp-block-paragraph\">Use algorithm to learn the investment factors and predict returns.<\/p>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1eitpp_Cg1UOHp6hnAUp6LQ.jpg\"\/><figcaption class=\"wp-element-caption\">Photo Creds:\u00a0<a href=\"https:\/\/unsplash.com\/photos\/NDfqqq_7QWM\" rel=\"noreferrer noopener\" target=\"_blank\">Unsplash<\/a><\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"9657\"><strong>Highlights<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li>Difficulty\uff1a\u2605\u2605\u2605\u2606\u2606<\/li>\n<li>Setting Virtual Environment<\/li>\n<li>XGBoost Introduction and Installation<\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"faad\"><strong>Preface<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"1208\">Recently, a lot of algorithms have emerged, and various mathematical models have been developed to solve problems. The classic model is \u201cregression\u201d. With the advancement of technology, algorithms now been developed which can improve and learn by themselves (Machine Learning). Nowaday has developed into the most popular type of neural network model (Deep Learning).<\/p>\n<p class=\"wp-block-paragraph\" id=\"4b3f\">This article introduces the tree model XGBoost and will be divided into two parts. The first part will teach how to set environment and module installation. The second part is the preprocessing of the data, training, and prediction and visualization.<\/p>\n<h2 class=\"wp-block-heading\" id=\"14d4\"><strong>XGBoost Introduction<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"88ff\">First, let\u2019s introduce the popular algorithm XGBoost. The so-called Boosting is a kind of aggregating many weak learnings into a more powerful learner, which has higher accuracy for the final prediction result.<\/p>\n<p class=\"wp-block-paragraph\" id=\"51c3\">XGBoost (Extreme Gradient Boosting) is a gradient descent algorithm, Gradient Boosted Tree (GBDT), Each step of learning is based on previous errors, and will retain the original model, and add new functions as a correction the last error, this is a collection of multiple weak learners. The application mainly solves supervised learning, which can deal with classification and regression problems as well.<\/p>\n<h2 class=\"wp-block-heading\" id=\"032d\"><strong>The Editing Environment and Modules Required<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"7570\">Mac OS and Jupyter Notebook<\/p>\n<h2 class=\"wp-block-heading\" id=\"01f4\"><strong>Virtual Environment<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"0048\">Due to XGBoost uses many modules, if the versions are inconsistent, it will cause endless errors. Therefore, we can create a new environment to install these modules. There are many ways to install them. This tutorial is a relatively simple and easy-to-understand way to minimize errors.<\/p>\n<h4 class=\"wp-block-heading\" id=\"d3e6\"><strong>Step 1.\u00a0Install Anaconda<\/strong><\/h4>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1kp4-bkLFk2LHhGvJpKQug.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"d6fa\">Anaconda can be said to be a lazy package for beginners. It solves the current situation that the inconsistency of various systems causes installation difficulties. It has organized more than 1000 packages that can be installed, which are suitable for Windows, Linux and MacOS. Operating system environment, also has a virtual environment manager, which is simple and fast for installing and executing machine learning environment.<\/p>\n<h4 class=\"wp-block-heading\" id=\"6237\"><strong>Step 2.\u00a0Click terminal<\/strong><\/h4>\n<p class=\"wp-block-paragraph\" id=\"f1f7\">Windows system is Anaconda Prompt<\/p>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/12TwBrhOD97JDsGUe2lWgqQ.png\"\/><figcaption class=\"wp-element-caption\">windows<\/figcaption><\/figure>\n<p class=\"wp-block-paragraph\" id=\"e240\">Enter the following command<\/p>\n<pre class=\"wp-block-preformatted\">conda create -n new_env_name python==3.8<\/pre>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/13A1mqW_k_92V5gjUpCtjLw.png\"\/><\/figure>\n<p class=\"wp-block-paragraph\" id=\"e269\">It will pop up and ask if you want to install it. Enter\u00a0<code>y<\/code>\u00a0and\u00a0<code>enter<\/code>\u00a0\uff01 The name of our new environment is\u00a0<code>test<\/code>. Of course you can also type any name you like.<\/p>\n<pre class=\"wp-block-preformatted\">conda env list<\/pre>\n<p class=\"wp-block-paragraph\" id=\"9880\">This command will show all of the environment we have created.<\/p>\n<p class=\"wp-block-paragraph\" id=\"b1fc\"><strong>step 3.\u00a0<\/strong>Activate environment<\/p>\n<pre class=\"wp-block-preformatted\">conda activate new_env_name<\/pre>\n<p class=\"wp-block-paragraph\" id=\"3b76\">At this time, the front bracket (base) of the terminal will turn into the name (test). It means we activate the environment successful. If the following installation fails and need to reinstall. We just remove the environment by simply entering a series of commands below.<\/p>\n<pre class=\"wp-block-preformatted\">conda env remove -n new_env_name<\/pre>\n<h2 class=\"wp-block-heading\" id=\"a538\"><strong>Install XGBoost<\/strong><\/h2>\n<h4 class=\"wp-block-heading\" id=\"c7ae\"><strong>step 1.\u00a0Activate environment<\/strong><\/h4>\n<pre class=\"wp-block-preformatted\">conda activate new_env_name<\/pre>\n<h4 class=\"wp-block-heading\" id=\"4bdf\"><strong>step 2.\u00a0Enter command<\/strong><\/h4>\n<pre class=\"wp-block-preformatted\">conda install py-xgboost<\/pre>\n<p class=\"wp-block-paragraph\" id=\"1ddf\">The same will ask if you want to install these modules, type\u00a0<code>y<\/code>\u00a0and press\u00a0<code>enter<\/code>\u00a0to start the installation, and it will be successful after running! Is it very simple!<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1yZd7GANPZhTQBNsOxcRAsg.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"54fe\"><strong>Install XGBoost visualization module graphviz<\/strong><\/h2>\n<h4 class=\"wp-block-heading\" id=\"4769\"><strong>step 1.\u00a0Install Homebrew (under our new environment)<\/strong><\/h4>\n<p class=\"wp-block-paragraph\" id=\"4079\"><a aria-label=\" (opens in a new tab)\" class=\"ek-link\" href=\"https:\/\/brew.sh\/\" rel=\"noreferrer noopener\" target=\"_blank\">Homebrew<\/a>\u00a0We can understand it as an installation method. For example, using\u00a0<code>pip<\/code>\u00a0to install python module. On macOS, Homebrew is the most widely used package management tool.<\/p>\n<pre class=\"wp-block-preformatted\">\/bin\/bash -c \"$(curl -fsSL https:\/\/raw.githubusercontent.com\/Homebrew\/install\/HEAD\/install.sh)\"<\/pre>\n<p class=\"wp-block-paragraph\" id=\"4930\">Enter the command on the terminal to install<\/p>\n<h4 class=\"wp-block-heading\" id=\"6de8\"><strong>step 2.\u00a0graphviz<\/strong><\/h4>\n<pre class=\"wp-block-preformatted\">brew install graphviz<\/pre>\n<p class=\"wp-block-paragraph\" id=\"0f32\">The above are the modules we will mainly use in this article! However, in the new environment, XGBoost does not have some of the modules we need, so we have to install them separately (pandas, matplotlib, tejapi). The command is separated by spaces.<\/p>\n<pre class=\"wp-block-preformatted\">pip install pandas matplotlib tejapi<\/pre>\n<h2 class=\"wp-block-heading\" id=\"cc41\"><strong>Install jupyter notebook<\/strong><\/h2>\n<h4 class=\"wp-block-heading\" id=\"3404\"><strong>step 1.\u00a0Open Anaconda, choose the name we just created for the environment<\/strong><\/h4>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1jgJbRM-r7zW4ZatkRgsQlA.png\"\/><\/figure>\n<h4 class=\"wp-block-heading\" id=\"3d67\"><strong>step 2.\u00a0Under jupyter notebook Click install<\/strong><\/h4>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1clr9rbb4UG5cjoSTgSeUkw.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"51a0\"><strong>Final Result<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"8f83\">Finally, checking whether the installation is successful in jupyter!<\/p>\n<figure class=\"wp-block-image aligncenter\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_3YBgENgDJbt_eCQL4qlug.png\"\/><\/figure>\n<h2 class=\"wp-block-heading\" id=\"a8eb\"><strong>Database<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"d5ac\">We use\u00a0<a href=\"https:\/\/api.tej.com.tw\/columndoc.html?subId=119\" rel=\"noreferrer noopener\" target=\"_blank\"><strong>TWN\/AFF_RAW<\/strong><\/a>\u00a0in this article. It provides trading factors for algorithms learning. Database refer to Kenneth R. French and top three financial journals (JF\u3001RFS\u3001JFE). The indicators are calculated by using Taiwan market data, and the all indicators are sorted out in a monthly frequency.<\/p>\n<pre class=\"wp-block-preformatted\">df = tejapi.get('TWN\/AFF_RAW',<br\/>                coid = '9921',<br\/>                mdate={'gte': '2015-01-01', 'lte':'2020-12-31'}<br\/>                chinese_column_name = True,<br\/>                paginate = True)<\/pre>\n<figure class=\"wp-block-image aligncenter caption-align-center\"><img decoding=\"async\" alt=\"\" src=\"https:\/\/www.tejwin.com\/en\/wp-content\/uploads\/2026\/08\/1_1y87GS2ffbPLkR-F4ztgk4w.png\"\/><figcaption class=\"wp-element-caption\">\u8cc7\u6599\u5eab\u6b04\u4f4d<\/figcaption><\/figure>\n<h2 class=\"wp-block-heading\" id=\"e70d\"><strong>Conclusion<\/strong><\/h2>\n<p class=\"wp-block-paragraph\" id=\"8298\">The part 1 of this article is about module installation. I believe that most people will encounter many installation situations when first contact the program. The arrangement of the environment is the first class for programmer. After everyone has successfully installed it, the part 2 will start to use the database. We will process the data, feed the model, and predict returns as a reference for our investment.<\/p>\n<h2 class=\"wp-block-heading\" id=\"0db9\"><strong>Extended Reading<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a class=\"ek-link\" href=\"\/en\/insight\/martingale-strategy\/\">Martingale Strategy<\/a><\/li>\n<li><a class=\"ek-link\" href=\"\/en\/insight\/efficient-frontier\/\">Efficient Frontier<\/a><\/li>\n<\/ul>\n<h2 class=\"wp-block-heading\" id=\"5358\"><strong>Related Link<\/strong><\/h2>\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/api.tej.com.tw\/index.html\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ API<\/a><\/li>\n<li><a href=\"https:\/\/eshop.tej.com.tw\/E-Shop\/Edata_intro\" rel=\"noreferrer noopener\" target=\"_blank\">TEJ E-Shop<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Use algorithm to learn the investment factors and predict returns. Highlights Preface Recently, a lot of algorithms have emerged, and various mathematical models have been developed to solve problems. The classic model is \u201cregression\u201d. With the advancement of technology, algorithms now been developed which can improve and learn by themselves (Machine Learning). Nowaday has developed [\u2026]<\/p>\n","protected":false},"featured_media":2589,"template":"","tags":[65,70],"insight_category":[16],"class_list":["post-2598","insight","type-insight","status-publish","has-post-thumbnail","hentry","tag-python","tag-tej-api","insight_category-quant-data-science"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight\/2598","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight"}],"about":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/types\/insight"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media\/2589"}],"wp:attachment":[{"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/media?parent=2598"}],"wp:term":[{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/tags?post=2598"},{"taxonomy":"insight_category","embeddable":true,"href":"https:\/\/www.tejwin.com\/en\/wp-json\/wp\/v2\/insight_category?post=2598"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}