%22%22%22Marimo%20notebook%20demonstrating%20the%20Hierarchical%20Risk%20Parity%20(HRP)%20portfolio%20optimization%20method.%0A%0AThis%20notebook%20implements%20the%20Hierarchical%20Risk%20Parity%20algorithm%20as%20introduced%20by%20Marcos%20Lopez%20de%20Prado.%0AIt%20compares%20different%20approaches%20to%20building%20the%20hierarchical%20tree%2C%20including%20the%20'single'%20distance%20method%0Awith%20and%20without%20bisection%2C%20and%20the%20'ward'%20method.%20The%20notebook%20visualizes%20the%20resulting%20dendrograms%0Aand%20portfolio%20weights%20for%20each%20approach.%0A%22%22%22%0A%0A%23%20%2F%2F%2F%20script%0A%23%20requires-python%20%3D%20%22%3E%3D3.11%22%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%20%20%22marimo%3D%3D0.18.4%22%2C%0A%23%20%20%20%20%20%22plotly%22%2C%0A%23%20%20%20%20%20%22polars%22%2C%0A%23%20%20%20%20%20%22pyhrp%22%2C%0A%23%20%5D%0A%23%0A%23%20%5Btool.uv.sources%5D%0A%23%20pyhrp%20%3D%20%7B%20path%20%3D%20%22..%2F..%22%2C%20editable%3Dtrue%20%7D%0A%23%0A%23%20%2F%2F%2F%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.25.2%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20from%20pyhrp.algos%20import%20risk_parity%0A%20%20%20%20from%20pyhrp.hrp%20import%20build_tree%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Hierarchical%20Risk%20Parity%20(HRP)%0A%0A%20%20%20%20We%20follow%20ideas%20by%20Marcos%20Lopez%20de%20Prado.%0A%0A%20%20%20%20-%20Compute%20the%201st%20dendrogram%20using%20the%20'single'%20distance%20method.%0A%20%20%20%20-%20Compute%20the%202nd%20dendrogram%20by%20using%20the%20order%20of%20leaves%0A%20%20%20%20%20%20of%20the%201st%20dendrogram%20(following%20an%20argument%20by%20Thomas%20Raffinot)%0A%20%20%20%20-%20Apply%20Risk%20Parity%20in%20a%20recursive%20bottom-up%20traverse%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20_prices%20%3D%20pl.read_csv(str(mo.notebook_location()%20%2F%20%22public%22%20%2F%20%22stock_prices.csv%22))%0A%20%20%20%20returns%20%3D%20_prices.drop(_prices.columns%5B0%5D).select(pl.all().pct_change()).drop_nulls().fill_null(0.0)%0A%20%20%20%20return%20(returns%2C)%0A%0A%0A%40app.cell%0Adef%20_(returns)%3A%0A%20%20%20%20from%20pyhrp.hrp%20import%20compute_corr%2C%20compute_cov%0A%0A%20%20%20%20cor%20%3D%20compute_corr(returns)%0A%20%20%20%20cov%20%3D%20compute_cov(returns)%0A%20%20%20%20return%20cor%2C%20cov%0A%0A%0A%40app.cell%0Adef%20_(cor)%3A%0A%20%20%20%20dendrogram_before%20%3D%20build_tree(cor%2C%20method%3D%22single%22)%0A%20%20%20%20dendrogram_before.plot().show()%0A%20%20%20%20return%20(dendrogram_before%2C)%0A%0A%0A%40app.cell%0Adef%20_(cov%2C%20dendrogram_before)%3A%0A%20%20%20%20root_before%20%3D%20risk_parity(dendrogram_before.root%2C%20cov)%0A%20%20%20%20root_before.portfolio.plot(names%3Ddendrogram_before.names).show()%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(cor)%3A%0A%20%20%20%20dendrogram_bisection%20%3D%20build_tree(cor%2C%20method%3D%22single%22%2C%20bisection%3DTrue)%0A%20%20%20%20dendrogram_bisection.plot().show()%0A%20%20%20%20return%20(dendrogram_bisection%2C)%0A%0A%0A%40app.cell%0Adef%20_(cov%2C%20dendrogram_bisection)%3A%0A%20%20%20%20root_bisection%20%3D%20risk_parity(dendrogram_bisection.root%2C%20cov)%0A%20%20%20%20root_bisection.portfolio.plot(names%3Ddendrogram_bisection.names).show()%0A%20%20%20%20return%20(root_bisection%2C)%0A%0A%0A%40app.cell%0Adef%20_(cor)%3A%0A%20%20%20%20dendrogram_ward%20%3D%20build_tree(cor%2C%20method%3D%22ward%22)%0A%20%20%20%20dendrogram_ward.plot().show()%0A%20%20%20%20return%20(dendrogram_ward%2C)%0A%0A%0A%40app.cell%0Adef%20_(cov%2C%20dendrogram_ward)%3A%0A%20%20%20%20root_ward%20%3D%20risk_parity(dendrogram_ward.root%2C%20cov)%0A%20%20%20%20root_ward.portfolio.plot(names%3Ddendrogram_ward.names).show()%0A%20%20%20%20return%20(root_ward%2C)%0A%0A%0A%40app.cell%0Adef%20_(root_bisection%2C%20root_ward)%3A%0A%20%20%20%20import%20plotly.graph_objects%20as%20go%0A%0A%20%20%20%20_weights_ward%20%3D%20root_ward.portfolio.weights%0A%20%20%20%20_weights_bisection%20%3D%20root_bisection.portfolio.weights%0A%20%20%20%20_assets%20%3D%20list(_weights_ward.keys())%0A%20%20%20%20_fig%20%3D%20go.Figure(%0A%20%20%20%20%20%20%20%20%5B%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Bar(name%3D%22ward%22%2C%20x%3D_assets%2C%20y%3D%5B_weights_ward%5Ba%5D%20for%20a%20in%20_assets%5D)%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20go.Bar(name%3D%22single%2Fbisection%22%2C%20x%3D_assets%2C%20y%3D%5B_weights_bisection%5Ba%5D%20for%20a%20in%20_assets%5D)%2C%0A%20%20%20%20%20%20%20%20%5D%0A%20%20%20%20)%0A%20%20%20%20_fig.update_layout(barmode%3D%22group%22%2C%20xaxis%3D%7B%22tickangle%22%3A%20-90%7D)%0A%20%20%20%20_fig.show()%0A%20%20%20%20return%0A%0A%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20app.run()%0A
91c1ad7b8d73d6758b2d4e9682d88b2f