import%20marimo%0A%0A__generated_with%20%3D%20%220.23.16%22%0Aapp%20%3D%20marimo.App(width%3D%22medium%22)%0A%0Aasync%20with%20app.setup(hide_code%3DTrue)%3A%0A%20%20%20%20%23%20Setup%20cell%20for%20marimo%20notebook%2C%20can%20be%20ignored%0A%20%20%20%20import%20marimo%20as%20mo%0A%20%20%20%20import%20sys%0A%0A%20%20%20%20%23%20Import%20packages%20if%20running%20on%20marimo%20playground%0A%20%20%20%20if%20sys.executable%20%3D%3D%20%22%2Fhome%2Fpyodide%2Fthis.program%22%3A%0A%20%20%20%20%20%20%20%20import%20micropip%0A%0A%20%20%20%20%20%20%20%20await%20micropip.install(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5B%22pint_pandas%3C%3D0.7%22%2C%20%22typing_extensions%3E%3D4.15.0%22%2C%20%22simbio%3E%3D1.1.0%22%2C%20%22matplotlib%22%2C%20%22seaborn%22%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20verbose%3DFalse%2C%0A%20%20%20%20%20%20%20%20)%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%20Stochastic%20simulations%20in%20SimBio%0A%20%20%20%20Simbio%20contains%20the%20capability%20to%20stochastically%20simulate%20systems%20using%20the%20%5BGillespie%20algorithm%5D(http%3A%2F%2Fen.wikipedia.org%2Fwiki%2FGillespie_algorithm)%3B%20this%20is%20preferable%20over%20ODE%20based%20simulations%20for%20reactions%20where%20small%20amounts%20of%20reactants%20are%20present.%20Simbio%20uses%20the%20%5Brebop%5D(https%3A%2F%2Frebop.readthedocs.io%2Fen%2Flatest%2F)%20library%20for%20this%2C%20which%20must%20be%20installed%20separately%3A%0A%20%20%20%20%60%60%60bash%0A%20%20%20%20pip%20install%20rebop%0A%20%20%20%20%60%60%60%0A%20%20%20%20To%20stochastically%20simulate%20a%20%60System%60%2C%20which%20we%20can%20define%20noramlly%3A%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%20import%20xarray%20as%20xr%0A%20%20%20%20from%20simbio.rebop%20import%20RebopSimulator%0A%20%20%20%20from%20simbio%20import%20System%2C%20Variable%2C%20initial%2C%20MassAction%2C%20RateLaw%0A%0A%20%20%20%20class%20Infection(System)%3A%0A%20%20%20%20%20%20%20%20S%3A%20Variable%20%3D%20initial(default%3D100)%0A%20%20%20%20%20%20%20%20D%3A%20Variable%20%3D%20initial(default%3D1)%0A%20%20%20%20%20%20%20%20R%3A%20Variable%20%3D%20initial(default%3D0)%0A%0A%20%20%20%20%20%20%20%20r_infect%20%3D%20MassAction(reactants%3D%5BS%2C%20D%5D%2C%20products%3D%5B2%20*%20D%5D%2C%20rate%3D0.015)%0A%20%20%20%20%20%20%20%20r_cure%20%3D%20MassAction(reactants%3D%5BD%5D%2C%20products%3D%5BR%5D%2C%20rate%3D0.1)%0A%0A%20%20%20%20return%20Infection%2C%20RateLaw%2C%20RebopSimulator%2C%20System%2C%20Variable%2C%20initial%2C%20xr%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20We%20can%20create%20a%20%60RebopSimulator%60%20for%20it%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(Infection%2C%20RebopSimulator)%3A%0A%20%20%20%20rebsim%20%3D%20RebopSimulator(Infection)%0A%20%20%20%20result%20%3D%20rebsim.solve(n_points%3D100%2C%20upto_t%3D10)%0A%20%20%20%20result.to_dataframe().plot()%0A%20%20%20%20return%20rebsim%2C%20result%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Unlike%20the%20regular%20%60Simulator.solve()%60%20which%20takes%20an%20iterable%20of%20times%20to%20%60save_at%60%2C%20%60RebopSimulator.solve()%60%20takes%20arguments%20%60upto_t%60%20to%20control%20simulation%20end%20time%20and%20%60n_points%60%20which%20decides%20how%20many%20equally%20spaced%20times%20are%20sampled.%20Since%20it%20doesn't%20count%200%2C%20%60n_points%60%20%3D%20N%20will%20a%20%60Dataset%60%20with%20a%20N%20%2B%201%20size%20time%20coordinate%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(result)%3A%0A%20%20%20%20len(result.coords%5B%22time%22%5D)%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20To%20manually%20set%20a%20seed%2C%20we%20must%20pass%20it%20to%20the%20%60rng%60%20argument%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rebsim)%3A%0A%20%20%20%20_result%20%3D%20rebsim.solve(n_points%3D1000%2C%20upto_t%3D10%2C%20rng%3D23534256).to_dataframe().plot()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Of%20course%2C%20since%20the%20simulation%20is%20stochastic%20the%20result%20will%20be%20different%20every%20time%20its%20run.%20We%20can%20run%20it%20many%20times%20to%20get%20a%20better%20idea%20of%20the%20system's%20behaviour%3A%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(rebsim%2C%20xr)%3A%0A%20%20%20%20import%20seaborn.objects%20as%20so%0A%0A%20%20%20%20df_100%20%3D%20(%0A%20%20%20%20%20%20%20%20xr.concat(%0A%20%20%20%20%20%20%20%20%20%20%20%20%5Brebsim.solve(n_points%3D0%2C%20upto_t%3D100)%20for%20_%20in%20range(10)%5D%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20dim%3D%22seed%22%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20join%3D%22outer%22%2C%0A%20%20%20%20%20%20%20%20)%0A%20%20%20%20%20%20%20%20.to_dataframe()%0A%20%20%20%20%20%20%20%20.melt(ignore_index%3DFalse)%0A%20%20%20%20%20%20%20%20.reset_index()%0A%20%20%20%20)%0A%20%20%20%20df_100%0A%20%20%20%20so.Plot(df_100%2C%20x%3D%22time%22%2C%20y%3D%22value%22%2C%20color%3D%22variable%22%2C%20group%3D%22seed%22).add(%0A%20%20%20%20%20%20%20%20so.Lines()%0A%20%20%20%20).show()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20%23%23%20RateLaws%20and%20arbitrary%20rates%0A%20%20%20%20Stochastic%20simulation%20support%20a%20reduced%20subset%20of%20%60symbolite%60%20functions%20and%20operators%20for%20rates%20both%20in%20RateLaws%20and%20MassActions.%0A%20%20%20%20%22%22%22)%0A%20%20%20%20return%0A%0A%0A%40app.cell%0Adef%20_(RateLaw%2C%20RebopSimulator%2C%20System%2C%20Variable%2C%20initial)%3A%0A%20%20%20%20from%20symbolite%20import%20real%0A%0A%20%20%20%20class%20Model(System)%3A%0A%20%20%20%20%20%20%20%20A%3A%20Variable%20%3D%20initial(default%3D100)%0A%20%20%20%20%20%20%20%20B%3A%20Variable%20%3D%20initial(default%3D1)%0A%0A%20%20%20%20%20%20%20%20r%20%3D%20RateLaw(reactants%3D%5BA%5D%2C%20products%3D%5BB%5D%2C%20rate_law%3Dreal.sqrt(A)%20%2B%201)%0A%0A%20%20%20%20rebsim_2%20%3D%20RebopSimulator(Model)%0A%20%20%20%20rebsim_2.solve(n_points%3D50%2C%20upto_t%3D1).to_dataframe().plot()%0A%20%20%20%20return%0A%0A%0A%40app.cell(hide_code%3DTrue)%0Adef%20_()%3A%0A%20%20%20%20mo.md(r%22%22%22%0A%20%20%20%20Rates%20and%20rate_laws%20can%20include%3A%0A%20%20%20%20-%20Basic%20operations%3A%20%2B%2C%20-%2C%20*%2C%20%2F%20and%20%5C*%5C*.%0A%20%20%20%20-%20Functions%20that%20can%20be%20converted%20to%20powers%3A%20%60real.exp%60%2C%20%60real.sqrt%60%20and%20%60real.hypot%60.%0A%20%20%20%20-%20Functions%20that%20can%20be%20converted%20to%20multiplication%3A%20%60real.degrees%60%20and%20%60real.radians%60%0A%20%20%20%20-%20Mathematical%20constants%3A%20%60real.e%60%2C%20%60real.pi%60%20and%20%60real.tau%60.%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
cd6c4fa46a4155595e8c1f86e068f156