%23%20%2F%2F%2F%20script%0A%23%20dependencies%20%3D%20%5B%0A%23%20%20%20%22marimo%3D%3D0.19.6%22%2C%0A%23%20%20%20%22numpy%3D%3D2.3.1%22%2C%0A%23%20%20%20%22plotly%3D%3D6.2.0%22%2C%0A%23%20%20%20%22polars%3D%3D1.32.2%22%2C%0A%23%20%20%20%22monkeys%22%2C%0A%23%20%5D%0A%23%20requires-python%20%3D%20%22%3E%3D3.13%22%0A%23%20%5Btool.uv.sources%5D%0A%23%20%20%20monkeys%20%3D%20%7B%20path%20%3D%20%22..%2F..%2F..%22%2C%20editable%3Dtrue%20%7D%0A%23%20%2F%2F%2F%0A%0A%22%22%22Marimo%20notebook%20for%20simulating%20a%20random%20equity%20portfolio.%0A%0AThis%20module%20demonstrates%20the%20use%20of%20monkeys.portfolio%20to%20create%20and%20analyze%0Aa%20portfolio%20with%20random%20weights%20for%20a%20set%20of%20equities.%0A%22%22%22%0A%0Aimport%20marimo%0A%0A__generated_with%20%3D%20%220.19.6%22%0Aapp%20%3D%20marimo.App()%0A%0Awith%20app.setup%3A%0A%20%20%20%20from%20pathlib%20import%20Path%0A%0A%20%20%20%20import%20marimo%0A%20%20%20%20import%20numpy%20as%20np%0A%20%20%20%20import%20plotly.express%20as%20px%0A%20%20%20%20import%20plotly.io%20as%20pio%0A%20%20%20%20import%20polars%20as%20pl%0A%0A%20%20%20%20import%20monkeys%0A%0A%20%20%20%20pio.renderers.default%20%3D%20%22plotly_mimetype%22%0A%0A%20%20%20%20path%20%3D%20Path(__file__).parent%0A%20%20%20%20prices_file%20%3D%20path%20%2F%20%22data%22%20%2F%20%22stock-prices-new.csv%22%0A%0A%20%20%20%20prices%20%3D%20monkeys.load_prices_from_csv(prices_file)%0A%20%20%20%20returns%20%3D%20monkeys.calculate_returns(prices%2C%20method%3D%22simple%22)%0A%20%20%20%20assets%20%3D%20%5Bcol%20for%20col%20in%20prices.columns%20if%20col%20!%3D%20%22Date%22%5D%0A%0A%0A%40app.function%0Adef%20run_simulation(seed%3A%20int%20%3D%200)%3A%0A%20%20%20%20%22%22%22Run%20a%20portfolio%20simulation%20with%20random%20weights.%0A%0A%20%20%20%20Creates%20a%20portfolio%20simulation%20with%20random%20weights%20at%20each%20time%20step.%0A%0A%20%20%20%20Args%3A%0A%20%20%20%20%20%20%20%20seed%3A%20Random%20seed%20for%20reproducibility.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20tuple%3A%20DataFrame%20with%20dates%20and%20cumulative%20portfolio%20returns%2C%20and%20weight%20history.%0A%20%20%20%20%22%22%22%0A%20%20%20%20print(f%22Version%20of%20monkeys%3A%20%7Bmonkeys.__version__%7D%22)%0A%0A%20%20%20%20n_periods%20%3D%20len(returns)%0A%20%20%20%20n_assets%20%3D%20len(assets)%0A%0A%20%20%20%20%23%20Convert%20returns%20to%20numpy%20array%20for%20package%20functions%0A%20%20%20%20returns_array%20%3D%20returns.select(assets).to_numpy()%0A%0A%20%20%20%20%23%20Use%20package%20functions%20for%20simulation%0A%20%20%20%20portfolio_returns%20%3D%20monkeys.simulate_portfolio_returns(returns_array%2C%20seed%3Dseed)%0A%20%20%20%20weights_array%20%3D%20monkeys.generate_weight_history(n_assets%2C%20n_periods%2C%20seed%3Dseed)%0A%0A%20%20%20%20%23%20Convert%20weights%20to%20list%20of%20dicts%20for%20display%20compatibility%0A%20%20%20%20weight_history%20%3D%20%5Bdict(zip(assets%2C%20w%2C%20strict%3DFalse))%20for%20w%20in%20weights_array%5D%0A%0A%20%20%20%20dates%20%3D%20returns.select(%22Date%22).to_series().to_list()%0A%20%20%20%20cumulative%20%3D%20np.cumprod(1%20%2B%20portfolio_returns)%0A%0A%20%20%20%20result%20%3D%20pl.DataFrame(%7B%22Date%22%3A%20dates%2C%20%22Return%22%3A%20portfolio_returns%2C%20%22Cumulative%22%3A%20cumulative%7D)%0A%0A%20%20%20%20print(%22Simulation%20complete%22)%0A%0A%20%20%20%20return%20result%2C%20weight_history%0A%0A%0A%40app.cell%0Adef%20_()%3A%0A%20%20%20%20marimo.md(%22%23%20A%20random%20walk%20down%20an%20equity%20portfolio%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20create_portfolio()%3A%0A%20%20%20%20%22%22%22Create%20a%20portfolio%20by%20running%20the%20simulation.%0A%0A%20%20%20%20Returns%3A%0A%20%20%20%20%20%20%20%20tuple%3A%20A%20tuple%20containing%20the%20simulation%20results%20and%20weight%20history.%0A%20%20%20%20%22%22%22%0A%20%20%20%20result%2C%20weight_history%20%3D%20run_simulation()%0A%20%20%20%20return%20result%2C%20weight_history%0A%0A%0A%40app.cell%0Adef%20_(result)%3A%0A%20%20%20%20fig%20%3D%20px.line(%0A%20%20%20%20%20%20%20%20result%2C%0A%20%20%20%20%20%20%20%20x%3D%22Date%22%2C%0A%20%20%20%20%20%20%20%20y%3D%22Cumulative%22%2C%0A%20%20%20%20%20%20%20%20title%3D%22Portfolio%20Cumulative%20Return%22%2C%0A%20%20%20%20%20%20%20%20labels%3D%7B%22Cumulative%22%3A%20%22Growth%20of%20%241%22%7D%2C%0A%20%20%20%20)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(result)%3A%0A%20%20%20%20total_return%20%3D%20(result%5B%22Cumulative%22%5D%5B-1%5D%20-%201)%20*%20100%0A%20%20%20%20annualized_return%20%3D%20((result%5B%22Cumulative%22%5D%5B-1%5D)%20**%20(252%20%2F%20len(result))%20-%201)%20*%20100%0A%20%20%20%20volatility%20%3D%20result%5B%22Return%22%5D.std()%20*%20np.sqrt(252)%20*%20100%0A%20%20%20%20sharpe%20%3D%20annualized_return%20%2F%20volatility%20if%20volatility%20%3E%200%20else%200%0A%0A%20%20%20%20metrics_md%20%3D%20marimo.md(f%22%22%22%0A%20%20%20%20%23%23%20Portfolio%20Metrics%0A%0A%20%20%20%20%7C%20Metric%20%7C%20Value%20%7C%0A%20%20%20%20%7C--------%7C-------%7C%0A%20%20%20%20%7C%20Total%20Return%20%7C%20%7Btotal_return%3A.2f%7D%25%20%7C%0A%20%20%20%20%7C%20Annualized%20Return%20%7C%20%7Bannualized_return%3A.2f%7D%25%20%7C%0A%20%20%20%20%7C%20Annualized%20Volatility%20%7C%20%7Bvolatility%3A.2f%7D%25%20%7C%0A%20%20%20%20%7C%20Sharpe%20Ratio%20%7C%20%7Bsharpe%3A.2f%7D%20%7C%0A%20%20%20%20%7C%20Number%20of%20Periods%20%7C%20%7Blen(result)%7D%20%7C%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(weight_history)%3A%0A%20%20%20%20final_weights%20%3D%20weight_history%5B-1%5D%0A%20%20%20%20weights_table%20%3D%20%22%5Cn%22.join(f%22%7C%20%7Basset%7D%20%7C%20%7Bweight%3A.4f%7D%20%7C%22%20for%20asset%2C%20weight%20in%20final_weights.items())%0A%20%20%20%20weights_md%20%3D%20marimo.md(f%22%22%22%0A%20%20%20%20%23%23%20Final%20Portfolio%20Weights%0A%0A%20%20%20%20%7C%20Asset%20%7C%20Weight%20%7C%0A%20%20%20%20%7C-------%7C--------%7C%0A%20%20%20%20%7Bweights_table%7D%0A%20%20%20%20%22%22%22)%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
e86ba7476d57b83313d511d5676c7d48