{ "cells": [ { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Requirement already satisfied: pip in ./venv/lib/python3.10/site-packages (23.2.1)\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install --upgrade pip" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "yLacqQR2O_8l", "outputId": "a46a4c1e-a8a3-44c2-a5da-b6833ec35687" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n", "Note: you may need to restart the kernel to use updated packages.\n" ] } ], "source": [ "%pip install numpy -q\n", "%pip install pandas -q\n", "%pip install yfinance -q\n", "%pip install mosek -q\n", "%pip install riskfolio-lib -q" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "AXkX5PxyPP6u", "outputId": "c11a8d3b-8413-48a2-cbae-ab200a405332" }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[*********************100%***********************] 45 of 45 completed\n" ] } ], "source": [ "import numpy as np\n", "import pandas as pd\n", "import yfinance as yf\n", "import riskfolio as rp\n", "\n", "# Tickers of assets\n", "tickers = [\n", " \"ABCB4.SA\", \"ALUP11.SA\", \"B3SA3.SA\", \"BRSR6.SA\", \"BBSE3.SA\", \"BRAP4.SA\", \"BBAS3.SA\", \"AGRO3.SA\", \"CMIG3.SA\",\n", " \"CMIG4.SA\", \"CSMG3.SA\", \"CPLE6.SA\", \"CPFE3.SA\", \"CMIN3.SA\", \"CURY3.SA\", \"DIRR3.SA\", \"ENAT3.SA\", \"EGIE3.SA\",\n", " \"FESA4.SA\", \"GGBR4.SA\", \"GOAU4.SA\", \"MYPK3.SA\", \"RANI3.SA\", \"ITSA4.SA\", \"JBSS3.SA\", \"JHSF3.SA\", \"KEPL3.SA\",\n", " \"LAVV3.SA\", \"MRFG3.SA\", \"BEEF3.SA\", \"PETR3.SA\", \"PETR4.SA\", \"PSSA3.SA\", \"RAPT4.SA\", \"ROMI3.SA\", \"SANB11.SA\",\n", " \"CSNA3.SA\", \"TAEE11.SA\", \"VIVT3.SA\", \"TRPL4.SA\", \"TRIS3.SA\", \"UNIP6.SA\", \"USIM5.SA\", \"VALE3.SA\", \"VBBR3.SA\"\n", "]\n", "\n", "tickers.sort()\n", "\n", "start = \"2017-02-01\"\n", "\n", "# Downloading the data\n", "data = yf.download(tickers, start=start)\n", "data = data.loc[:, ('Adj Close', slice(None))]\n", "data.columns = tickers\n", "assets = data.pct_change().dropna()\n", "\n", "Y = assets\n", "\n", "# Creating the Portfolio Object\n", "port = rp.Portfolio(returns=Y, nea=10)\n", "\n", "# To display dataframes values in percentage format\n", "pd.options.display.float_format = '{:.4%}'.format\n", "\n", "# Choose the risk measure\n", "rm = 'MSV' # Semi Standard Deviation\n", "\n", "# Estimate inputs of the model (historical estimates)\n", "# Method to estimate expected returns based on historical data.\n", "method_mu = 'hist'\n", "# Method to estimate covariance matrix based on historical data.\n", "method_cov = 'hist'\n", "\n", "port.assets_stats(method_mu=method_mu, method_cov=method_cov, d=0.94)\n", "\n", "# Estimate the portfolio that maximizes the risk adjusted return ratio\n", "w1 = port.optimization(model='Classic', rm=rm,\n", " obj='Sharpe', rf=0.0, l=0, hist=True)\n", "\n", "# Estimate points in the efficient frontier mean - semi standard deviation\n", "ws = port.efficient_frontier(\n", " model='Classic', rm=rm, points=20, rf=0, hist=True)\n", "\n", "# Estimate the risk parity portfolio for semi standard deviation\n", "w2 = port.rp_optimization(model='Classic', rm=rm, rf=0, b=None, hist=True)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
| \n", " | weights | \n", "
|---|---|
| PETR3.SA | \n", "15.4512% | \n", "
| CMIG3.SA | \n", "13.7378% | \n", "
| PETR4.SA | \n", "12.3160% | \n", "
| CPLE6.SA | \n", "11.2214% | \n", "
| RANI3.SA | \n", "10.5928% | \n", "
| UNIP6.SA | \n", "7.0562% | \n", "
| TAEE11.SA | \n", "6.4217% | \n", "
| FESA4.SA | \n", "5.1461% | \n", "
| BBSE3.SA | \n", "4.6158% | \n", "
| KEPL3.SA | \n", "4.2727% | \n", "
| CPFE3.SA | \n", "3.4921% | \n", "
| BEEF3.SA | \n", "3.1947% | \n", "
| CURY3.SA | \n", "1.5344% | \n", "
| PSSA3.SA | \n", "0.9471% | \n", "
| BBAS3.SA | \n", "0.0000% | \n", "
| CMIG4.SA | \n", "0.0000% | \n", "
| EGIE3.SA | \n", "0.0000% | \n", "
| GOAU4.SA | \n", "0.0000% | \n", "
| TRPL4.SA | \n", "0.0000% | \n", "
| ALUP11.SA | \n", "0.0000% | \n", "
| CSMG3.SA | \n", "0.0000% | \n", "
| ABCB4.SA | \n", "0.0000% | \n", "
| GGBR4.SA | \n", "0.0000% | \n", "
| MYPK3.SA | \n", "0.0000% | \n", "
| VIVT3.SA | \n", "0.0000% | \n", "
| DIRR3.SA | \n", "0.0000% | \n", "
| AGRO3.SA | \n", "0.0000% | \n", "
| BRSR6.SA | \n", "0.0000% | \n", "
| ITSA4.SA | \n", "0.0000% | \n", "
| JBSS3.SA | \n", "0.0000% | \n", "
| LAVV3.SA | \n", "0.0000% | \n", "
| VALE3.SA | \n", "0.0000% | \n", "
| ENAT3.SA | \n", "0.0000% | \n", "
| MRFG3.SA | \n", "0.0000% | \n", "
| BRAP4.SA | \n", "0.0000% | \n", "
| RAPT4.SA | \n", "0.0000% | \n", "
| SANB11.SA | \n", "0.0000% | \n", "
| VBBR3.SA | \n", "0.0000% | \n", "
| B3SA3.SA | \n", "0.0000% | \n", "
| JHSF3.SA | \n", "0.0000% | \n", "
| TRIS3.SA | \n", "0.0000% | \n", "
| CMIN3.SA | \n", "0.0000% | \n", "
| ROMI3.SA | \n", "0.0000% | \n", "
| USIM5.SA | \n", "0.0000% | \n", "
| CSNA3.SA | \n", "0.0000% | \n", "