{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"%matplotlib inline\nimport pandas as pd\nimport numpy as np\nimport pathlib \nimport math\n\nimport plotly\nimport sys\n\nimport plotly.graph_objects as pgo\n\nPORTFOLIO_PKG_PATH = \"../input/portfolio/portfolio-0.1.0\"\n\nsys.path.insert(0, PORTFOLIO_PKG_PATH)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:18.033036Z","iopub.execute_input":"2022-11-03T11:03:18.033774Z","iopub.status.idle":"2022-11-03T11:03:18.042713Z","shell.execute_reply.started":"2022-11-03T11:03:18.033732Z","shell.execute_reply":"2022-11-03T11:03:18.041173Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install --upgrade -r $PORTFOLIO_PKG_PATH/requirements.txt","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:18.045510Z","iopub.execute_input":"2022-11-03T11:03:18.045947Z","iopub.status.idle":"2022-11-03T11:03:30.768178Z","shell.execute_reply.started":"2022-11-03T11:03:18.045909Z","shell.execute_reply":"2022-11-03T11:03:30.766656Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from portfolio.portfolio_stats import portfolio_expected_return, portfolio_risk\nfrom portfolio.optimal_portfolio import (\n    optimal_portfolio,\n    optimal_portfolios,\n    optimal_portfolio_frontier\n)\nfrom portfolio.prepare_data import prepare_data\nfrom portfolio.random_portfolio import random_portfolio","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:30.770022Z","iopub.execute_input":"2022-11-03T11:03:30.770403Z","iopub.status.idle":"2022-11-03T11:03:30.778285Z","shell.execute_reply.started":"2022-11-03T11:03:30.770367Z","shell.execute_reply":"2022-11-03T11:03:30.777124Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"DATAPATH = \"../input/crypto-forecasting-training-test-split\"\nDATAPATH = pathlib.Path(DATAPATH).resolve()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:30.780743Z","iopub.execute_input":"2022-11-03T11:03:30.781520Z","iopub.status.idle":"2022-11-03T11:03:30.795848Z","shell.execute_reply.started":"2022-11-03T11:03:30.781484Z","shell.execute_reply":"2022-11-03T11:03:30.794706Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"FILEPATH_raw_csv = DATAPATH / \"test.csv\"\n\nassert FILEPATH_raw_csv.exists()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:30.797616Z","iopub.execute_input":"2022-11-03T11:03:30.799036Z","iopub.status.idle":"2022-11-03T11:03:30.812996Z","shell.execute_reply.started":"2022-11-03T11:03:30.798991Z","shell.execute_reply":"2022-11-03T11:03:30.811770Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"raw_df = pd.read_csv(FILEPATH_raw_csv)\n\nraw_df.head()","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:30.814192Z","iopub.execute_input":"2022-11-03T11:03:30.815118Z","iopub.status.idle":"2022-11-03T11:03:33.590954Z","shell.execute_reply.started":"2022-11-03T11:03:30.815045Z","shell.execute_reply":"2022-11-03T11:03:33.589800Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"returns_for_assets = prepare_data(raw_df)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:33.592562Z","iopub.execute_input":"2022-11-03T11:03:33.593011Z","iopub.status.idle":"2022-11-03T11:03:36.333166Z","shell.execute_reply.started":"2022-11-03T11:03:33.592963Z","shell.execute_reply":"2022-11-03T11:03:36.331961Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"random_portfolio(returns_for_assets)","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:36.334479Z","iopub.execute_input":"2022-11-03T11:03:36.334807Z","iopub.status.idle":"2022-11-03T11:03:36.356978Z","shell.execute_reply.started":"2022-11-03T11:03:36.334772Z","shell.execute_reply":"2022-11-03T11:03:36.355899Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Calculates 500 portfolios with their respective expected return and risk relative to the performance of the assets in the dataset used","metadata":{}},{"cell_type":"code","source":"N_PORTFOLIOS = 1000\nmeans, stds = np.column_stack([random_portfolio(returns_for_assets) for _ in range(N_PORTFOLIOS)])","metadata":{"execution":{"iopub.status.busy":"2022-11-03T11:03:36.360210Z","iopub.execute_input":"2022-11-03T11:03:36.360561Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plot of the expected value of portfolios versus risk","metadata":{}},{"cell_type":"code","source":"fig = pgo.Figure()\nfig.add_trace(pgo.Scatter(\n    x=stds, \n    y=means,\n    mode=\"markers\"\n))\nfig.update_layout(\n    title=\"Mean and standard deviation of returns of randomly generated portfolios\",\n    xaxis_title=\"std\",\n    yaxis_title=\"mean\",\n)\nfig.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"optimal_portfolio_ = optimal_portfolio(returns_for_assets, 100)\noptimal_portfolio_","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Calculate the Markowitz portfolio with respect to a list of expected returns defined by the following expression\n\n$$10^{\\left(\\frac{5t}{N} -1\\right)}, \\forall t=0,...,N-1$$","metadata":{}},{"cell_type":"code","source":"N_MARKOWITZ_PORTFOLIO = 50\nmus = [10**(5.0 * t/N_MARKOWITZ_PORTFOLIO - 1.0) for t in range(N_MARKOWITZ_PORTFOLIO)]\noptimal_portfolios_ = optimal_portfolios(returns_for_assets, mus)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"markowitz_optimal_frontier_returns = []\nmarkowitz_optimal_frontier_risks = []\n\nfor opt_port in optimal_portfolios_:\n    markowitz_optimal_frontier_returns.append(portfolio_expected_return(opt_port, returns_for_assets))\n    markowitz_optimal_frontier_risks.append(portfolio_risk(opt_port, returns_for_assets))","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"markowitz_portfolio = optimal_portfolio_frontier(optimal_portfolios_, returns_for_assets)\n\nmarkowitz_portfolio_return = portfolio_expected_return(markowitz_portfolio, returns_for_assets)\nmarkowitz_portfolio_risk = portfolio_risk(markowitz_portfolio, returns_for_assets)","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig = pgo.Figure()\nfig.add_trace(pgo.Scatter(\n    x=stds, \n    y=means,\n    name=\"random_portfolios\",\n    mode=\"markers\"\n))\nfig.add_trace(pgo.Scatter(\n    x=markowitz_optimal_frontier_risks, \n    y=markowitz_optimal_frontier_returns,\n    name=\"markowitz_optimal_frontier\",\n    mode=\"lines+markers\"\n))\nfig.add_trace(pgo.Scatter(\n    x=[markowitz_portfolio_risk], \n    y=[markowitz_portfolio_return],\n    name=\"markowitz_optimal_portfolio\",\n    mode=\"markers\"\n))\n\nfig.update_layout(\n    title=\"Mean and standard deviation of returns of portfolios\",\n    xaxis_title=\"std\",\n    yaxis_title=\"mean\",\n)\nfig.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}