[{"url":".","title":"index","tags":["homepage"],"text":""},{"url":"search/","title":"Search results","tags":[],"text":"window.init_search();SearchResults\nLoading..."},{"url":"assets/scripts/get_highlights/","title":"get_highlights","tags":[],"text":"if isempty get metadata \"homepage\" , \"highlights\", nothing else highlights htl \"\"\" section div class \"content\" h2 x \"name\" h2 p x \"text\" p div div class \"preview\" img src \" x \"img\" \" div section \"\"\" for x in metadata \"homepage\" \"highlights\" htl \"\"\" div class \"subjectscontainer wide\" h1 Highlights h1 div class \"contain\" highlights div div \"\"\" end"},{"url":"assets/scripts/get_subjects/","title":"get_subjects","tags":[],"text":"let sections metadata \"sidebar\" sections htl \"\"\" let input other page.input output other page.output name get output.frontmatter, \"title\", basename input.relative path desc get output.frontmatter, \"description\", nothing tags get output.frontmatter, \"tags\", String image get output.frontmatter, \"image\", nothing class \"no decoration\", \"tag replace x, \" \" \" \" \" for x in tags ..., image nothing || isempty image ? nothing htl \"\"\" a title desc class class href root url \" \" other page.url h3 name h3 img src image a \"\"\" end for other page in collections section id .pages \"\"\" for section id, section name in sections isempty sections ? nothing htl \"\"\" div class \"wide subjectscontainer\" h1 Subjects h1 div class \"subjects\" sections div div \"\"\" end"},{"url":"cheat_sheets/catalyst/","title":"Catalyst","tags":["cheat sheets"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"3\" title \"Catalyst\" date \"2025 01 26\" tags \"cheat sheets\" description \"Catalyst Cheat Sheet\" layout \"layout.jlhtml\" frontmatter.author name \"Daan Van Hauwermeiren\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Catalyst, StatsPlots, OrdinaryDiffEq using SteadyStateDiffEq using StochasticDiffEq md\"\"\" Notes before the cheat sheet In Pluto, there can only be one statement per cell because of the syntax tree that is generated to determine the order of execution . But sometimes we want to group multiple expressions for clarity. We can do that in two ways, using the same syntax, but with a different keyword both call for wrapping the code in a block. The first one is for simply wrapping multiple statements the variable names can be accessed in the rest of the notebook ```julia begin ... end ``` In the second one, the variables only live within the scope of the block. We will use this to illustrate behaviour, but we do not need the rest in any other part of the notebook, and we want to avoid having to add numerous postfixes to the variable names. ```julia let ... end ``` Note that the indentation is optional, and only used for readability. \"\"\" md\" `Catalyst` cheat sheet\" md\"\"\" important When running this notebook locally, deactivate or delete the above cell.\"\"\" md\"\"\" `Catalyst.jl` is a Julia package that provides a clean interface to building reaction networks, which can be turned into ODESystems that `DifferentialEquations.jl` can simulate. Since almost all mass transfer problems can be written using reactions or, more generally speaking, processes , `Catalyst.jl` will be our main tool for building mechanistic models. \"\"\" md\" Defining a reaction system\" md\"\"\" Use the ` reaction network` macro to define a reaction network. This macro allows you to specify reactions using a simple syntax. \"\"\" mm reaction network begin kB, kD , S E ES reversible binding kP, ES P E conversion of substrate by enzyme end species mm check the species states parameters mm check the parameters equations mm check the equations let unpack S mm extract parameter variable end osys convert ODESystem, mm convert ReactionSystem in an ODE system md\"\"\" It is also possible to add reactions using functions, such as building big reaction networks using code. For this, we refer to the documentations. \"\"\" md\" Simulation\" md\"\"\" Simulation of a reaction network builds upon DifferentialEquations.jl. Reaction networks can directly be transformed in ODE systems if you want to see the differential equations or a problem needed to solve numerically . \"\"\" u0map S 10.0, E 0.1, P 0.0, ES 0 define intial values pmap kB 0.5, kD 0.1, kP 2.2 define parameters let alternative, just as good u0map mm.S 10.0, mm.E 0.1, mm.P 0.0, mm.ES 0 pmap mm.kB 0.5, mm.kD 0.1, mm.kP 2.2 end tspan 0.0, 100. oprob ODEProblem mm, u0map, tspan, pmap sol solve oprob, Tsit5 plot sol plot all variables plot sol, idxs S, P plot only S and P begin alternative, unpack variables unpack P, S mm extract product and substrate plot sol, idxs P, S end plot sol, idxs P S P , title \"Fraction of substrate converted\" md\"\"\" You see that reaction systems have access to the variables and parameters, which are also used for plotting. \"\"\" md\" Using default options\" md\"You can already specify initial values and parameters directly in the reaction network.\" mm defaults reaction network begin species S t 10. E t 0.1 P t 0 ES t 0 parameters kB 0.5 kD 0.1 kP 2.2 kB, kD , S E ES kP, ES P E end oprob defaults ODEProblem mm defaults, , tspan no need to specify initial states and parameters plot solve oprob defaults, Tsit5 ODEProblem mm defaults, E 0.2 , tspan, kP 1.5 overwriting defaults mm ann reaction network begin species S t 10. description \"substrate\" P t 0 description \"product\" parameters kB 0.5 description \"binding rate\" kD 0.1 description \"dissociated rate rate\" kP 2.2 description \"conversion rate\" kB, kD , S E ES kP, ES P E end adding annotation to the parameters md\" Observables\" md\"\"\" Often, we are interested in the states or species in the system. However, sometimes we want to track something that is not a state but computed based on the states. This is an observable. Even though you can always compute these afterward, adding them to the system is likely useful so you have access to them while plotting. Consider the total amount of enzyme in the system. It is clear that this is conserved. \"\"\" mm obs reaction network begin observables begin Etot ~ E ES end kB, kD , S E ES kP, ES P E end observed mm obs oprob obs ODEProblem mm obs, u0map, tspan, pmap sol obs solve oprob obs, Tsit5 plot sol obs, idxs Etot, ylims 0,1 flat line, enzyme is conserved md\" Removing conserved quantities\" equations osys mm conserved convert ODESystem, mm, remove conserved true equations mm conserved enzyme E ES is conserved observed mm conserved still in observables md\" Events\" md\"\"\" Events are external perturbations of the system, either by changing the states or the parameters. There are two types of events discrete events which happen at fixed time points continuous events which happen when a variable reaches a certain value. \"\"\" md\" Discrete Events time based \" dilluting 20.0 mm.E ~ mm.E 2, mm.S ~ mm.S 2, mm.ES ~ mm.ES 2, mm.P ~ mm.P 2 at t 20, we double the volume of the reactor, hence halving the concentration named mm dil ReactionSystem equations mm , discrete events dilluting mm dillute complete mm dil oprob dil ODEProblem mm dillute, u0map, tspan, pmap sol dil solve oprob dil, Tsit5 , tstops 20 plot sol dil md\" Continuous events\" substrate feeding mm.S ~ 5 mm.S ~ mm.S 5 when the substrate reaches a concentration of 5, we add new substrate named mm f ReactionSystem equations mm , continuous events substrate feeding mm fed complete mm f oprob fed ODEProblem mm fed, u0map, tspan, pmap sol fed solve oprob fed, Tsit5 , tstops 20 plot sol fed md\" Computing steady state\" md\"It is possible to numerically compute when the system reaches a steady state.\" mm continuous reaction network begin kB, kD , S E ES reversible binding kP, ES P E conversion of substrate by enzyme 1, 0 S constant inflow 1, S, P 0 constant outflow end ssprob SteadyStateProblem mm continuous, u0map, pmap print solve ssprob, DynamicSS md\" Discrete jump equations\" md\"\"\" When modelling discrete systems, we can turn the problem into a discrete jump equation. Here, the species are described by an integer there are a discrete number of molecules. This simulation is now stochastic. \"\"\" u0map int S 500, E 5, P 0, ES 0 starting with 500 substrate molecules and 5 enzyme molecules jsys JumpInputs mm, u0map int, tspan, pmap jprob JumpProblem jsys jsol solve jprob plot jsol md\" Stochastic Differential Equations\" md\"\"\" We can also simulate a reaction system as an SDE. Be wary that no mechanism prevents concentrations from being zero \"\"\" let sys reaction network begin k1, k2 , A B end u0map A 10., B 200. pmap k1 2, k2 5 sprob SDEProblem sys, u0map, 0., 20. , pmap sol solve sprob, STrapezoid , dt 0.02 plot sol end let sys reaction network begin parameters η default noise scaling η k1, k2 , A B end pmap k1 2, k2 5, η 0.1 u0map A 10., B 200. sprob SDEProblem sys, u0map, 0., 20. , pmap sol solve sprob, STrapezoid , dt 0.02 plot sol end let sys reaction network begin parameters η default noise scaling η k1, A B, noise scaling 0.0 k2, B A, noise scaling η end pmap k1 2, k2 5, η 1 u0map A 10., B 200. sprob SDEProblem sys, u0map, 0., 20. , pmap sol solve sprob, STrapezoid , dt 0.02 plot sol end "},{"url":"cheat_sheets/cheatsheets/","title":"Overview","tags":["cheat sheets"],"text":"Overview Cheat SheetsGetting Started with Julia - live.Fastrack to Julia cheatsheet.MATLAB-Julia-Python comparative cheatsheet by QuantEcon groupPlots.jl cheatsheet"},{"url":"cheat_sheets/intro_to_julia/","title":"Intro to Julia","tags":["cheat sheets"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"2\" title \"Intro to Julia\" date \"2025 01 28\" tags \"cheat sheets\" description \"General introduction to Julia\" layout \"layout.jlhtml\" frontmatter.author name \"Daan Van Hauwermeiren\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off return quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end using Pkg Pkg.activate \".. .. pluto deployment environment\" using PlutoUI, Markdown TableOfContents using LinearAlgebra using Statistics using StatsPlots md\"\"\" important When running this notebook locally, deactivate or delete the above cell.\"\"\" md\"\"\" Notebook 1 Getting up and running First of all, welcome to the course We hope you enjoy the ride. \"\"\" md\"\"\" 0. Welcome to Pluto We will do our exercises in the Pluto notebook environment. The Pluto notebooks are pure Julia alternatives to the Jupyter notebooks you might have worked with. They are fast and reactive and come equipped with their own package manager, making it easy to distribute them. Cells are immediately executed in order of their dependencies , so not in the order that they appear. This can be confusing at first. \"\"\" one 2 change me and everything is updated two one 2 I depend on one, so I am executed second three two one I am executed last md\"To run a cell either press on the ▶ symbol or press `shift ENTER`.\" Hi there, you are not supposed to see me md\"Press on the little 👁️ left of the cell to toggle a hidden cell.\" md\"You can only have one statement per line and all your variables need to have unique names. You can split statements in two lines or wrap them in a `begin ... end` block.\" a 2 a 7 md\"Pluto might seem strange at first, though its restrictions make it very flexible and allows to easily create interactivity \" md\"period bind period Slider 0.0 0.2 5, default 1, show value true \" mycos t cos period t plot mycos, 0, 8, xlab \"t\", title \"Cos period t \" md\"\"\" 1. The basics From zero to newbie. \"\"\" md\"\"\" Let's get started with the basics. Some mathematical operations, \"\"\" 1 2 adding integers 1.0 2.0 adding floats 1 2.0 adding a float to an integer... 2 4 standard division div 2, 4 Computes 2 4 truncated to an integer 2 ÷ 4 looks nicer but does exactly the same 2^8 raising to a power 7 % 3 get the remainder of the integer division 35 \\ 7 inverse division 1 3 fractions, gives the result as a rational 1 2 1 4 2.0 3.0im complex numbers 'c' characters unicode symbol symbols, we will use this for parameters ζ any LaTeX symbol 🎉 or Unicode emoji md\"variable assignment\" x 2 md\"In the Pluto notebook environment you are currently working in, it is not possible to define the same variable in two cells. However, this is not standard Julia behaviour. You can see that redefining a variable is possible,\" begin variable1 2.0 variable1 4.0 end variable1 md\"\"\" ```julia begin statement1 statement2 end ``` Enable to wrap multiple statements, since only single line statements are allowed in this notebook environment. \"\"\" md\"Similarly, `let ... end` blocks allow you to define a separate envirionment. Everything you define in such a block is only available there.\" let myprivatevar 3.0 end myprivatevar only available in the block τ 1 37 unicode variable names are allowed md\"\"\" unicode In most Julia editing environments, unicode math symbols can be typed when starting with a '\\' and hitting ' TAB '. \"\"\" md\"\"\" Unsure what the LaTeX name for a symbol is or how to type an emoiji? Just copy paste it in the REPL with a `?` at the beginning, e.g., `?ζ` and it will tell you how to type it.\"\"\" type \\alpha and TAB md\"Operators are not needed for multiplication.\" 5x This works md\"But strings are quite essential,\" mystery \"life, the universe and everything\" md\"and string interpolation is performed with ` `.\" \"The answer to mystery is 3 2 7 \" md\"\"\" Printing can be done with `println `. These Pluto notebooks distinguish the value of the evaluation or computation from what is printed. The latter is shown in a terminal. \"\"\" println \"The answer to mystery is 3 2 7 \" md\"\"\" repetitions of strings can be done using the operators ` ` and `^`. This use of ` ` and `^` makes sense by analogy with multiplication and exponentiation. Just as `4^3` is equivalent to `4 4 4`, we expect `\"Spam\"^3` to be the same as `\"Spam\" \"Spam\" \"Spam\"`, and it is. \"\"\" breakfast \"eggs\" abetterbreakfast \"SPAM\" breakfast abetterbreakfast breakfast abetterbreakfast^3 breakfast md\"\"\" Lots of handy `String` operations are available in the standard library of Julia \"\"\" md\"Unlike `Strings`, a `Char` value represents a single character and is surrounded by single quotes.\" 'x' md\"Similarly to Matlab, when using the REPL, Julia will print the result of every statement by default. To suppress this behaviour, just end the statement with a semicolon.\" var1 10 not printed... var1 ...but still defined var2 20 md\"\"\" 2. Logical statements From zero to one. \"\"\" md\"\"\" Boolean operators Julia uses `true` and `false` for Boolean variables. \"\"\" I💖Julia true true false 1 1 2 1 1 1 2 1 1 10 1 10 2 2 or 2 ≤ 2 \\le TAB 2 2 or 2 ≥ 2 \\ge TAB Comparisons can be chained 1 2 3 2 3 2 Logical operators true && true true || false md\"Likewise, we have the Boolean logic operators `&&` AND , `||` OR and `⊻` XOR, exclusive or .\" true && true true && false true || false false || false true ⊻ false true ⊻ true md\"\"\" Chaining logic operators is frequently done in Julia as a short alternative for an `if` statement. The idea is if you use an `&&` statement, the second part is only evaluated if the first part is true The inverse is true for `||`, where the second part is only evaluated if the first part is false.\"\"\" md\"\"\" 3. Vectors and matrices Julia has powerful, flexible interfaces for vectors, matrices, and higher order tensors. A vector is defined with square brackets with the elements separated with a \",\" \"\"\" v 1, 3, 2 a vector of integers v2 1.0, 2.0, 3.0 a vector of floats v3 1.0, 2, 3 promotion occurs automatically to the most general type md\"Matrices can also be defined with spaces to separate elements in rows and semicolumns to separate rows.\" A 5 4 9 1 2 7 8 6 3 md\"You can use spaces and brackets to combine matrices and vectors \" A v 0 1 2 3 md\"Indexing is via square brackets like python and the index starts from 1 like in Matlab and R .\" v 1 first element v 0 does not exist... v end last element A 2,3 two indices for matrices A 1, first row A ,2 second column A A. 5 conditional indexing, notice the \".\" md\"Many functions exist that process collections.\" sum A size A sum A, dims 1 sum over the rows sort v size v length v count isodd, A count the number of odd elements in A sum sqrt, A sum ii √A ij md\"Many more advanced functions are available, for example linear algebra \" det A norm v eigen A mean A std v md\"Finally, there will be useful range objects, that define a linear range between begin and end values.\" 1 100 from 1 to 100 0 0.1 1 from 0 to 1 in steps of 0.1 md\"These work just like vectors.\" myrange 10 0.2 809 myrange 87 sum myrange length myrange md\"\"\" 4. Functions Julia puts the fun in functions. User defined functions can be declared as follows, \"\"\" function square x result x x return result end square 8 md\"Many of the functions we will need will be fairly simple equations. We can just define them in one line. A more condensed version of `square x `.\" s x x x s 8 md\"\"\" Functions are first class and work just like any other variable For example, you can give a function as an input in another function. In some cases, you might want to define an anonymous function , without giving them a name \"\"\" anfun x x^2 2x 8 md\"This looks like a variable but can be used as a function \" anfun 1.5 works just like any function md\"Why do we need this? Because we might want to define small functions on the fly.\" count x 4 x^2 80, 100 100 count the numbers between 100 and 100, for which their square is between 4 and 80 md\"\"\" Complete the function `clip x `, which returns `x` if 0\\le x \\le 1 , `0` if x 0 and `1` if x 1 . \"\"\" clip x missing md\"By default, a function is over the whole object. Using a `.`, you can use the function element wise.\" square A A A square. A each element squared square v square does not work for vectors square. v element wise works exp A matrix exponential exp. A element wise exponential A 1 won' t work A . 1 add 1 to each element of A md\" 5. Control flow\" md\"The `if`, `else`, `elseif` statement is instrumental to any programming language. Note that control flow is ended with an `end` statement. In constrast to Python, tabs are only for clarity but do not impact functionality.\" if 4 3 'A' elseif 3 4 'B' else 'C' end md\" 6. Looping Looping using a `for` loop can be done by iterating over a list or range. Don't forget to end with an `end` at the end. \" for i in 1 10 println \" i squared s i \" end characters \"Harry\", \"Ron\", \"Hermione\" begin for char in characters println \"Character char\" end end md\"We can use `enumerate` to generate an iterator of tuples containing the index and the values of an iterator.\" begin for i, char in enumerate characters println \" i. char\" end end pets \"Hedwig\", \"Pig\", \"Crookshanks\" md\"`zip` binds two or more iterators and yields tuples of the pairs.\" begin for char, pet in zip characters, pets println \" char has pet as a pet\" end end md\" 7. Macros Macros provide a method to include generated code in the final body of a program. It is a way of generating a new output expression, given an unevaluated input expression. When your Julia program runs, it first parses and evaluates the macro, and the processed code produced by the macro is eventually evaluated like an ordinary expression. Some nifty basic macros are ` time` and ` show`. ` time` prints the cpu time and memory allocations of an expression.\" time square 10 md\"\"\"The ` show` macro is often useful for debugging purposes. It displays both the expression to be evaluated and its result, finally returning the value of the result.\"\"\" show 1 1 md\"Macro's will be vital in the domain specific languages we use in this course. Remember, when you see an ` `, some code is changed into other code.\" md\"\"\" 8. Plotting Quite essential for scientific programming is the visualisation of the results. `Plots` is the Julia package that handles a lot of the visualisation. `StatsPlots` does the same, but with added functionality for plotting probability distributions. `rand 10 ` returns an array of 10 random floats between 0 and 1. \"\"\" plot rand 10 md\"\"\"When loading in a package for the first time Julia will have to precompile this package, hence this step can take some time.\"\"\" begin plot 1 10, rand 10 , label \"first\" plot 1 10, rand 10 , label \"second\" adding to current figure using plot scatter 1 10 , randn 10 , label \"scatter\" xlabel \"x\" ylabel \"f x \" title \"My pretty Julia plot\" end plot 0 0.1 10, x sin x x, xlabel \"x\", ylabel \"sin x x\", color red, marker square, legend none notice the use of a symbol as an argument contour 5 0.1 5, 10 0.1 10, x, y 3x^2 4y^2 x y 6 md\"You can also directly plot functions \" plot sin, 0, 2pi md\"Don't worry about making a tidy plot. For many objects solutions of differential equations , the function `plot ` is overloaded, so we only have to `plot sol ` for a pretty plot. More to follow \" md\"\"\" Exercise Stirling's approximation for factorials The factorial function, \\displaystyle n 1\\cdot 2\\cdot 3\\cdots n 2 \\cdot n 1 \\cdot n, is often used in combinatorics but also other mathematical areas. Especially for large numbers it can get quite inefficient to compute. Stirling's approximation is an approximation for factorials, \\displaystyle n \\sim \\sqrt 2\\pi n \\left \\frac n e \\right ^ n , Complete the function `stirling ` by implementing Stirling's approximation. \"\"\" stirling n missing md\"You can add your approximation to the plot below.\" scatter 1 10, factorial. 1 10 , xlab \"n\", label \"n \", yscale log10 begin Do NOT delete this cell hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text almost text Markdown.MD Markdown.Admonition \"warning\", \"Almost there \", text keep working text md\"The answer is not quite right.\" Markdown.MD Markdown.Admonition \"danger\", \"Keep working on it \", text correct text md\"Great You got the right answer Let's move on to the next section.\" Markdown.MD Markdown.Admonition \"correct\", \"Got it \", text sol stirling n √ 2π n n exp 1 ^n md\"\" Only the last evaluation is shown. end hint md\"Check out `min`and `max`.\" if ismissing clip 0.1 if clip 1 0 && clip 0.25 ≈ 0.25 && clip 3.6 ≈ 1 correct else keep working end end if ismissing stirling 5 if sol stirling 20 ≈ stirling 20 correct else keep working end end "},{"url":"cheat_sheets/turing/","title":"Turing Cheat Sheet","tags":["cheat sheets"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"4\" title \"Turing Cheat Sheet\" date \"2025 01 29\" tags \"cheat sheets\" description \"Turing Cheat Sheet\" layout \"layout.jlhtml\" frontmatter.author name \"Bram Spanoghe\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing using StatsPlots md\"\"\" important When running this notebook locally, deactivate or delete the above cell.\"\"\" md\" `Turing` cheatsheet\" md\" Distributions.jl\" distr LogNormal 2.0, 1.0 Define a LogNormal distribution with mean 2.0 and standard deviation 1.0 md\" Basic statistics\" mean distr Calculate the mean of the distribution var distr Calculate the variance std distr Calculate the standard deviation quantile distr, 0.25, 0.5, 0.75 Calculate the quartiles md\" Evaluate probability density and cumulative probability\" pdf distr, 2.0 Probability density at x 2.0 cdf distr, 5.0 Probability that a random variable is less than 5.0 md\" Sampling random values\" rand distr Draw a single random sample mysample rand distr, 1000 Generate 1000 random samples md\" Calculate statistics from samples\" mean mysample Approximate the mean using the sample std mysample Approximate the standard deviation md\" Calculate probabilities using samples\" mean x x^2 5, mysample P X^2 5 Method 1 Anonymous function and mean mean mysample.^2 . 5 P X^2 5 Method 2 Boolean operations filtered sample filter x x^2 5, mysample P X^2 5 Method 3 Filtering length filtered sample length mysample md\" Other calculations with samples\" mean sin, mysample Approximate E sin X using the sample more efficient mean sin. mysample Same md\" Turing.jl\" model function mymodel x ~ Exponential 2.0 Exponential prior for x y ~ Truncated Normal 1., x , 0.0, 10.0 Truncated Normal for y, dependent on x z ~ Poisson y Poisson distribution for z, dependent on y return z^2 y computed result optional end md\" Sampling\" xyzmodel mymodel Build the sampling model xyz rand xyzmodel Generate a single sample x, y, z xyz x , xyz y , xyz z Extract the individual variables mysamples rand xyzmodel for i in 1 1000 Generate 1000 samples xyzmodel random sample of the result z^2 y md\" Calculate probabilities using samples \" mean xyz xyz z 0, mysamples P Z 0 Method 1 Anonymous function and mean length filter xyz xyz z 0, mysamples length mysamples P Z 0 Method 2 Filtering mean rand xyzmodel z 0 for i in 1 1000 P Z 0 Method 3 Boolean Operations on samples mean xyz xyz z 0, filter xyz xyz x 1, mysamples P Z 0 | x 1 Method 1 Filtering and mean mean xyz z 0 for xyz in mysamples if xyz x 1 P Z 0 | x 1 Method 2 Boolean operations on samples md\" Inference\" xyzmodel cond xyzmodel | z 3.0, Condition the model on Z 3 logprior xyzmodel cond, x 1.3, y 0.3 loglikelihood xyzmodel cond, x 1.3, y 0.3 logjoint xyzmodel cond, x 1.3, y 0.3 log prior log likelihood chain sample xyzmodel cond, NUTS , 10 000 Obtain samples from posterior summarize chain Summarize the chain means, quantiles, etc. quantile chain Quantiles, default 2.5%, 25.0%, 50.0%, 75.0%, 97.5% generated quantities xyzmodel, chain generates the result z^2 y based on the md\" Plotting\" plot chain Create diagnostic plots of the chain traceplot, etc. chain x chain x Extract samples for ’x’ chain y chain y histogram chain x, title \"Histogram of x | z 3\" Plot posterior of ’x’ md\" Calculations on posterior samples\" mean log, chain x Approximate E log X | Z 3 mean chain x . chain y Approximate P X Y | Z 3 "},{"url":"exercises/MCMC_1-intro/","title":"4. MCMC intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"20\" title \"4. MCMC intro\" date \"2025 08 06\" tags \"exercises\" description \"MCMC intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots md\" Inference notebook 1 Intro\" md\" Problem\" md\"\"\" According to the molecular clock hypothesis https en.wikipedia.org wiki Molecular clock , the amount of mutations in a gene is proportional to how much time has passed, and identical for all species. While this is a bit of an oversimplification, the concept has become an important tool in evolutionary biology to estimate how long ago species have diverged. \"\"\" md\"\"\" Consider the below figure of a small slice of the tree of life https en.wikipedia.org wiki Tree of life biology . Every animal represents a fossilized individual living during some point in evolution. \"\"\" md\"\"\" Evolution example https raw.githubusercontent.com Kermit UGent ModSim 2a369561ce842cf079d7660a36d0d9308739dc69 examples ProbMod figures treeoflife.excalidraw.svg \"\"\" md\"\"\" We start at time 0 with a common ancestor of fish and terrestrial animals. 30 million years Ma later it diverges into ray finned fish, which will give rise to most modern fish species, and lob finned fish, which will give rise to e.g. mammals and reptiles. The ray finned fish fossil is also one of the individuals for which we have DNA for its cytochrome C gene. The number represents that it has 25 mutations in this gene compared to the gene's sequence from our starting organism, the ancient bony fish fossil. \"\"\" md\"\"\" Taking into account all fossils, we can see that the number of mutations is roughly proportional with the time that has passed. \"\"\" times 30, 138, 375, 450 observed mutations 25, 94, 302, 335 scatter times, observed mutations, xlabel \"Time My \", ylabel \"Number of mutations\", legend false, xlims 0, 500 md\"\"\" Consider now that you find a new fossil of an ancient ancestor of the seahorses . \"\"\" md\" Sharkmoment https raw.githubusercontent.com Kermit UGent ModSim 2a369561ce842cf079d7660a36d0d9308739dc69 examples ProbMod figures treeoflife2.excalidraw.svg \" md\"\"\" You don't know how old the fossil is, but you do find that the fossilized DNA contains 156 mutations in the cytochrome c gene. How old should it be estimated as? \"\"\" md\"\"\" questions What is cytochrome C 's mutation rate `α`? What is the seahorse ancestor fossil's age? \"\"\" md\" Copy paste example\" md\"This section contains the essential code for this practical. A detailed explanation is given in the next section.\" α rand Exponential 10 α times 1 plot Exponential 10 rand Poisson α times 1 plot Poisson α times 1 let model function mutations ts Exponential λ λ is the average rate of mutations α ~ Exponential 10 α ~ Exponential also works α ~ LogNormal also works α ~ Uniform 0, 100 also works num mutations zeros length ts for i in eachindex ts Poisson α ts i the probability of having α ts i mutations num mutations i ~ Poisson α ts i end return num mutations end mutation model mutations times conditioned model mutation model | num mutations observed mutations, mind the `,` after `observed mutations` mutation chain sample conditioned model, NUTS , 2000 α sp mutation chain α histogram α sp, title \"Posterior distribution of mutation rate α\", normalized probability end The Exponential Distribution is another important distribution and is typically used to model times between events or arrivals. The distribution has one parameter, λ which is assumed to be the average rate of arrivals or occurrences of an event in a given time interval. A Poisson distribution is a discrete probability distribution, meaning that it gives the probability of a discrete i.e., countable outcome. For Poisson distributions, the discrete outcome is the number of times an event occurs, represented by k. md\" Explanation\" md\"\"\" Making the model\"\"\" md\"\"\" We start again by defining a Turing model. Similar to the models of previous practical, it describes the forward process how do you generate your observations the amount of mutations based on your inputs age of fossil and parameters the mutation rate ? This may seem unintuitive, as we don't know the distribution of this gene's mutation rate `α`. However, we do have some prior knowledge about mutation rates of genes in general they don't tend to be much larger than a few bp My. We can encode this information by giving `α` the prior distribution `Exponential 10 `. \"\"\" prior alpha Exponential 10 Hence, a few mutations bp base pair per My, is here 10 mutations per My on average. plot prior alpha, title \"Prior belief of α\", legend false, xlabel \"α\", ylabel \"Probability density\" md\"\"\" note Why use `Exponential 10 ` for the prior and not `Exponential 1 `, or some other value? Choosing a prior distribution is largely subjective and a big reason why some people are not fond of Bayesian modeling. There is no \"one correct prior distribution\". However, different choices of reasonable priors often give very similar outcomes. Try running this notebook at the end with a different prior for `α`, such as `Exponential 1 ` or `Uniform 0, 100 `. When are the results significantly different? \"\"\" md\"\"\" The rest of the model is pretty straightforward if the mutation rate `α` is constant, the number of mutations after `t` million years should be about `α t`. Since the accumulation of mutations is a random process, we can't expect the number of mutations to be exactly this number. Rather, we define it to follow a probability distribution centered around this number. A Poisson distribution is chosen as a good fit for count data. \"\"\" model function mutations ts α ~ prior alpha Exponential 10 num mutations zeros length ts for i in eachindex ts num mutations i ~ Poisson α ts i end return num mutations end md\"\"\" The model is instantiated with the correct inputs and can be used to generate samples as per usual. \"\"\" mutation model mutations times mutation model random sample of num mutations chain sample mutation model, Prior , 2000 histogram chain α , normalized probability md\" Inference\" md\"\"\" The model so far has no extra information outside of our prior knowledge. We can change this by conditioning the model on observed data as follows \"\"\" conditioned model mutation model | num mutations observed mutations, md\"\"\" danger Note the `,` at the end of ` num mutations observed mutations, `. This is important, as without it Julia thinks you simply put parentheses around a variable assignment and you'll get an error See the below cell for an example. \"\"\" forgot comma mutation model | num mutations observed mutations errors because there is no `,` in the parentheses md\"\"\" We can verify that for our conditioned model, the values of `num mutations` has been set as constant \"\"\" conditioned model always returns `observed mutations` md\"\"\" What we're after is our updated belief on the distribution of `α` given the observed data. We can do this by using the `sample` function on our model. We no longer use `Prior ` as second input, and instead choose one of the following sampling algorithms `MH` Metropolis Hastings sampler `Gibbs` Gibbs sampler `PG` Particle Gibbs sampler `HMC` Hamiltonian Monte Carlo sampler `NUTS` No U Turn sampler You can find more information about them in the corresponding Julia docs. In practice, `NUTS` is often an excellent choice if all variables are continuous and `PG` is a good default choice in all other cases. `MH` and `Gibbs` also have their uses, but usually it takes more effort to make them work well. \"\"\" mutation chain sample conditioned model, NUTS , 2000 mutation chain sample conditioned model, MH , 2000 mutation chain Gives a list of anonymous functions. The plot instruction will fill in the time t automatically. t αᵢ t for αᵢ in mutation chain α 1 10 end begin scatter times, observed mutations, xlabel \"Time My \", ylabel \"Number of mutations\", label false, xlims 0, 500 , title \"Predicted trend\" for α in mutation chain α 1 10 end plot x α x, color purple, alpha 0.05, label false end plot plot t αᵢ t for αᵢ in mutation chain α 1 10 end , color purple, opacity 0.05, label false end md\"It's always a good idea to check whether your sampling process has converged. You can do this by plotting the chain. It should look like a fuzzy caterpillar.\" plot mutation chain looks appropriately fuzzy md\"\"\" note For an example of a non converged chain, try using the `MH ` sampler instead of `NUTS `. This sampling algorithm takes a lot of fiddling with its parameters or a larger number of samples for it to work well. \"\"\" md\"\"\" The chain plot also shows the resulting posterior distribution of `α`. It is the prior distribution updated with the information contained in the data . \"\"\" md\"\"\" Taking the sampled values of the mutation rate from the chain and plotting a histogram will show us the exact same distribution. The one in the chain plot was simply smoothed to look continuous. \"\"\" sp alpha mutation chain α histogram sp alpha md\"Plotting some sampled mutation rates from this distribution onto our data shows that they fit well \" begin scatter times, observed mutations, xlabel \"Time My \", ylabel \"Number of mutations\", label false, xlims 0, 500 plot x αᵢ x for αᵢ in sp alpha 1 10 end , color blue, opacity 0.1, label false end mean sp alpha sqrt var sp alpha std sp alpha md\"To answer our first question, α is ± normally distributed around 0.75 with a standard deviation of 0.025.\" md\" Seahorses extra \" md\"\"\" To answer how old the ancestral seahorse fossil is, we need to update the model a little. So far the fossil ages were considered to be known exactly and given as input to the model `ts`. Since the fossil's age is unknown, we add a parameter `fossil age`. As prior knowledge we can use the fact that it must have evolved after the ray finned fish fossil 30 Ma after weird old fish , but before modern seahorses 450 Ma after the bony fish fossil . \"\"\" model function horsetations ts α ~ Exponential 10 num mutations zeros length ts for i in eachindex ts num mutations i ~ Poisson α ts i end fossil age ~ Uniform 30, 450 horse mutations ~ Poisson α fossil age return α, num mutations, fossil age, horse mutations end md\"Then we simply repeat model instantation, conditioning and sampling \" horse model horsetations times horseditioned model horse model | num mutations observed mutations, horse mutations 156, horseditioned model horse chain sample horseditioned model, NUTS , 2000 md\"And we have our posterior distribution of `fossil age` It seems like the seahorse ancestor lived about 200 220 million years after the bony fish fossil, or about 240 million years ago.\" histogram horse chain fossil age mean horse chain fossil age std horse chain fossil age "},{"url":"exercises/MCMC_2-basics/","title":"4. MCMC basics","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"21\" title \"4. MCMC basics\" date \"2025 08 06\" tags \"exercises\" description \"MCMC basics\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots md\" Inference notebook 2 Basics\" md\" 1 Mole burrow\" md\"\"\" Consider a mole's underground tunnel network of length `X` in m . Now and then the mole makes a new molehill somewhere randomly above its tunnel, the locations of which we denote `Y`. We can formulate this as `X ~ Exponential 100 ` and `Y ~ Uniform 0, X `. questions 1. Plot the prior of `X`. Is it diffuse or informative? 1. Estimate `E Y `. 1. Estimate `E X|Y 3 ` and compare it with the prior expected value `E X `. 1. Plot the histogram of `X` given `Y 3.0`. 1. Plot the histogram of `X` again, but now given the following values for `Y` ` 3.0, 1.5, 0.9, 5.7 `. \"\"\" md\"\"\" Questions How should I understand this? If the mole digs a tunnels that is X long, then at position Y there is one hole? Or, is there a hole at every position Y for an X meters long tunnel? \"\"\" md\" 1\" plot Exponential 100 The prior only incorporates the knowledge that a mole's tunnel is probably less long than a few km this is a diffuse to weakly informative prior mijn X rand Exponential 100 mijn Y rand Uniform 0, mijn X md\" 2\" model function mole X ~ Exponential 100 Y ~ Uniform 0, X return Y end molemodel mole molemodel returns Y E Y mean molemodel for i in 1 2000 md\" 3\" cond mole molemodel | Y 3.0, md\"\"\" Questions Why doesn't this work for much higher values of Y? E.g. for Y 8 or 10? Because if you sample `X rand Exponential 100 ` and `Y rand Uniform 0, X `, you usually get much larger numbers for Y than 3. \"\"\" molechain sample cond mole, NUTS , 2000 plot molechain E Xcond3 mean molechain X E X mean Exponential 100 md\" 4\" histogram molechain X , normalized probability md\" 5\" model function mole2 X ~ Exponential 100 Ys zeros 4 now we have 4 holes Ys Ysample for i in eachindex Ys Ys i ~ Uniform 0, X end end Y obs 3.0, 1.5, 0.9, 5.7 mole cond2 mole2 | Ys Y obs, molechain2 sample mole cond2, NUTS , 2000 plot molechain2 histogram molechain2 X , normalized probability mean molechain2 X md\" 2 Potatoes\" md\"\"\" Consider a number of potatoes `N` each with an average weight `W`. You weigh them together on an old balance to get an estimate of their total weight `T`. We can formulate this as `N ~ Poisson 10 `, `W ~ Uniform 150, 250 ` and `T ~ Normal N W, 50 `. questions 1. Plot a histogram of `N` given `T 1200`. 1. Estimate `P N 6, W 175 | T 1200 `. 1. Estimate `P N 5 | T 1200, W 220 `. \"\"\" let N rand Poisson 10 W rand Uniform 150, 250 in grams T rand Normal N W, 50 in grams end md\" 1\" model function potatoes N ~ Poisson 10 W ~ Uniform 150, 250 T ~ Normal N W, 50 end potato model potatoes potato cond potato model | T 1200, potato chain sample potato cond, PG 10 , 2000 plot potato chain histogram potato chain N , normalized probability md\"\"\" The above gives of the individual probabilities of having T 1200 with 5, 6, 7 and 8 potatoes. \"\"\" Probability of having T 1200 with N 7. mean potato chain N . 7 md\" 2\" p potato1 mean potato chain N . 6 .&& potato chain W . 175 mean potato chain N . 6 .|| potato chain W . 175 md\" 3\" potato cond2 potato model | T 1200, W 220, potato cond2 potato model | T 1100, W 220, pota2 chain sample potato cond2, PG 10 , 2000 plot pota2 chain p potato2 mean pota2 chain N . 5 mean pota2 chain N . 4 mean pota2 chain N . 6 md\" 3 Lights out\" md\"\"\" You use 4 of the same LED light in your room. Let `μ` be the average lifespan of your LED lights in khr or 1000 hours and `L`ᵢ the lifespan of the `i` th LED light. Assume that `μ ~ LogNormal log 40 , 0.5 `. \"\"\" md\"\"\" questions 1. What is `E μ ` given no information about `Lᵢ` ? 1. What is a sensible distribution for `Lᵢ`? requires no code 1. What is `E μ | L 16, 20, 23, 41 `? 1. 🌟🌟🌟 EXTRA DIFFICULT BONUS QUESTION After 30 khr, two lights have died one at 16 khr and one at 20 khr. The two other lights are still working. What is the expected value of `μ` given this information? \"\"\" md\" 1\" lights prior LogNormal log 40 , 0.5 plot lights prior not asked but a visualisation can always be useful E mu mean lights prior rand lights prior md\" 2\" md\"\"\" The exponential distribution is often used to model the waiting time for an event. This makes it a natural fit for a lamp's lifespan, which is the waiting time until it breaks. We know it needs to have a mean value of μ, so `Exponential μ ` is a good choice. One could also argue for a LogNormal distribution with mean μ or a Normal distribution with mean μ restricted to only the positive values. Both would need a large variance to reflect the lack of additional information outside of the mean lifespan. \"\"\" Average waiting time before it breaks is 45 khr. The longer we wait, the higher the chance that it breaks plot Exponential 45 μ 27 plot Normal μ, sqrt μ begin plot LogNormal log μ , 1 , Exponential 45 xlim 0,400 vline mean LogNormal log μ , 1 , mean Exponential 45 end plot cdf Exponential 45 , 0 400 cdf Exponential 45 , 100 probability that it breaks before 100 khr 1 cdf Exponential 45 , 100 probability that it is still working after 100 khr md\" 3\" model function lights μ ~ lights prior lifespans zeros 4 for i in 1 length lifespans lifespans i ~ LogNormal log μ , 1 Normal μ, sqrt μ Exponential μ end end lightmodel lights | lifespans 16, 20, 23, 41 , lightschain sample lightmodel, NUTS , 2000 plot lightschain E mu cond mean lightschain μ histogram lightschain μ , normalize true begin plot LogNormal log μ , 1 , Exponential 45 xlim 0,400 vline mean LogNormal log μ , 1 , mean Exponential 45 end md\" 4 🌟🌟🌟\" md\"\"\" hint You can model the number of lights that still work as a `Binomial` distribution, the success rate of which depends on `μ`. \"\"\" 1 cdf Exponential 40 , 30 rand Binomial 2 2, 0.47 md\"\"\" After 30 khr, two lights have died one at 16 khr and one at 20 khr. The two other lights are still working. What is the expected value of `μ` given this information? \"\"\" md\"\"\" Here you need to provide two arguments to the model function How many lights still working? `n` At what time they are still working? `time observed` \"\"\" model function lights censored n, time observed μ ~ lights prior lifespans zeros 2 for i in 1 length lifespans lifespans i ~ Exponential μ end Given the observation time, what is the probability that a single light still works p stillworking 1 cdf Exponential μ , time observed cdf Exponential μ , time observed is the probability that it broke in 0, time observed Number of lights still working with the above probability n ~ Binomial n length lifespans , p stillworking return μ, lifespans, p stillworking, n end lightmodel cens lights censored 2, 30 | lifespans 16, 20 , lightmodel cens lightschain cens sample lightmodel cens, NUTS , 2000 plot lightschain cens E mu cond🌟 mean lightschain cens μ model function lights censored2 time observed μ ~ lights prior lifespans zeros 2 for i in 1 length lifespans lifespans i ~ Exponential μ end p stillworking 1 cdf Exponential μ , time observed n ~ Binomial 4, p stillworking return μ, lifespans, p stillworking, n end plot cdf Exponential 10 , 0 100 lightmodel cens2 lights censored2 30 | lifespans 16, 20 , n 2, lightmodel cens2 lightschain cens2 sample lightmodel cens2, NUTS , 2000 plot lightschain cens2 mean lightschain cens2 μ md\" 4 Fish\" md\"\"\" There are two populations of fish living in the same pond. Let `fs1` be the fraction of fish belonging to species 1, `L1` the length of a fish of species 1 and `L2` the length of a fish of species 2. Assume You have no prior information about `fs1` except that it logically needs to be in ` 0, 1 `. `L1 ~ Normal 90, 15 `. `L2 ~ Normal 60, 10 `. \"\"\" md\"\"\" questions 1. If `fs1 0.3`, what is the prior distribution of the lengths of all fish in the pond? Make a plot. 1. Estimate `fs1` if you observe fish of the following lengths ` 94.0, 88.7, 89.6, 69.8, 52.8, 84.0, 89.3, 66.4, 95.1, 81.6 `. 1. 🌟 BONUS QUESTION What is the chance fish 4 belongs to species 1? \"\"\" md\" 1\" md\"\"\" hint The distribution of fish lengths can be modelled as a `MixtureModel`. \"\"\" lengthdist MixtureModel Normal 90, 15 , Normal 60, 10 , 0.3, 0.7 histogram rand lengthdist, 10000 md\" 2\" len obs 94.0, 88.7, 89.6, 69.8, 52.8, 84.0, 89.3, 66.4, 95.1, 81.6 model function fishmixture fs1 ~ Uniform 0, 1 fraction of species 1 fish length distribution fishlendist MixtureModel Normal 90, 15 , Normal 60, 10 , fs1, 1 fs1 fishlens zeros 10 fish lengths for i in eachindex fishlens fishlens i ~ fishlendist end end fishmodel fishmixture | fishlens len obs, fishchain sample fishmodel, NUTS , 2000 plot fishchain fs1 est mean fishchain fs1 md\" 3🌟\" model function fishmixture🌟 fs1 ~ Uniform 0, 1 fraction of species 1 or probability of belonging to species 1 fishlens zeros 10 samples with fish lengths isspecies1 zeros 10 samples with 1's meaning belonging to species 1 samples with 0's meaning belonging to species 2 for i in eachindex fishlens isspecies1 i ~ Bernoulli fs1 samples belonging to species 1 or not if isspecies1 i 1.0 if belongs to species 1 fishlens i ~ Normal 90, 15 sample from distribution of species 1 else fishlens i ~ Normal 60, 10 sample from distribution of species 1 end end end fishmodel🌟 fishmixture🌟 | fishlens len obs, fishchain🌟 sample fishmodel🌟, PG 20 , 2000 plot fishchain🌟 fish species 1 large fish fish species 2 small fish 94.0, 88.7, 89.6, 69.8, 52.8, 84.0, 89.3, 66.4, 95.1, 81.6 fish 1 2 3 4 5 6 7 8 9 10 species 1 1 1 1or2 2 1 1 1or2 1 1 We expect a very high chance here because 94.0 is a large fish p fish1 is species1 mean fishchain🌟 \"isspecies1 1 \" We expect medium chance here because 69.8 is between small and large p fish4 is species1 mean fishchain🌟 \"isspecies1 4 \" We expect a very small chance here because 52.8 is a small fish p fish5 is species1 mean fishchain🌟 \"isspecies1 5 \" We expect medium chance here because 66.4 is between small and large p fish8 is species1 mean fishchain🌟 \"isspecies1 8 \" mean fishchain🌟 fs1 should be the same as before md\" 5 Circleference\" md\"\"\" Given three noisy points P 1 x 1,y 1 , P 1 x 2,y 2 and P 3 x 3,y 3 , you want to infer the corresponding circle. You can assume that the circle center can appear anywhere in the 20, 20 \\times 20, 20 square and the radius is between 0 and 50. Points are sampled randomly on the circle and have a slight amount of Gaussian noise \\sigma 0.25 works well . \"\"\" md\"\"\" questions 1. Write a small probabilistic program that can infer the center and radius of the circle. 1. What does the inferred circle look like if you condition on only one or two of the circle points? \"\"\" x1, y1 18.0, 2.1 x2, y2 7.3, 8.1 x3, y3 13.0, 23.0 begin function plotcircle p, R, xC, yC dθ 0.01 θ 0 dθ 2pi 0.1 plot p, xC . R . cos. θ , yC . R . sin. θ , label \"\", alpha 0.5, color blue return p end function plotsample R missing, xC missing, yC missing kwargs... p plot xlab \"x\", ylab \"y\", aspect ratio equal xlims 40, 40 , ylims 40, 40 , kwargs... scatter x1 , y1 , label \"P1\" scatter x2 , y2 , label \"P2\" scatter x3 , y3 , label \"P3\" ismissing R || plotcircle p, R, xC, yC dθ 0.1 return p end function plotsample p, R missing, xC missing, yC missing scatter x1 , y1 , label false scatter x2 , y2 , label false scatter x3 , y3 , label false ismissing R || plotcircle p, R, xC, yC dθ 0.1 end end scatter x1, x2, x3 , y1, y2, y3 , aspect ratio equal, xlim 40, 40 , ylim 40, 40 md\" 1\" Flat p134 in syllabus rand Flat rand 2 π Flat rand Uniform 0, 2 π model function circle σ 0.25 generate a circle center xC ~ Uniform 20, 20 yC ~ Uniform 20, 20 generate a radius R ~ Uniform 0, 50 three random points in polar coordinates θ1 ~ 2 π Flat `Uniform 0, 2 pi ` is also possible but can get the sampler stuck at 0 or 2π θ2 ~ 2 π Flat θ3 ~ 2 π Flat P1 x1 ~ Normal xC R cos θ1 , σ y1 ~ Normal yC R sin θ1 , σ P2 x2 ~ Normal xC R cos θ2 , σ y2 ~ Normal yC R sin θ2 , σ P3 x3 ~ Normal xC R cos θ3 , σ y3 ~ Normal yC R sin θ3 , σ end circlemodel circle | x1 x1, y1 y1, x2 x2, y2 y2, x3 x3, y3 y3 circlechain sample circlemodel, NUTS , 2000 plot circlechain Circle center at mean circlechain xC , mean circlechain yC mean. circlechain xC , circlechain yC Radius mean circlechain R mean circlechain θ1 180.0 π mean circlechain θ2 180.0 π mean circlechain θ3 180.0 π begin p plot xlab \"x\", ylab \"y\", aspect ratio equal, xlims 40, 40 , ylims 40, 40 , title \"Samples of P circle|P1,P2,P3 \" for i in 1 100 plotsample p, circlechain R i , circlechain xC i , circlechain yC i end p end md\" 2\" circle1 circle | x1 x1, y1 y1 This can be any circle through x1, y1 with centrer in 20, 20 x 20, 20 and radius between 0 and 50. circle2 circle1 | x2 x2, y2 y2 This can be any circle through x1, y1 and x2, y2 with centrer in 20, 20 x 20, 20 and radius between 0 and 50. chain1 sample circle1, NUTS , 100 chain2 sample circle2, NUTS , 100 begin p1 plot xlab \"x\", ylab \"y\", aspect ratio equal, xlims 40, 40 , ylims 40, 40 , title \"Samples of P circle|P1 \" for i in 1 100 plotsample p1, chain1 R i , chain1 xC i , chain1 yC i end p1 end begin p2 plot xlab \"x\", ylab \"y\", aspect ratio equal, xlims 40, 40 , ylims 40, 40 , title \"Samples of P circle|P1,P2 \" for i in 1 100 plotsample p2, chain2 R i , chain2 xC i , chain2 yC i end p2 end "},{"url":"exercises/MCMC_3-advanced/","title":"4. MCMCM advanced","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"22\" title \"4. MCMCM advanced\" date \"2025 08 06\" tags \"exercises\" description \"MCMC advanced\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI md\" Inference notebook 3 Advanced\" md\" GPS\" md\"\"\" GPS systems need to decide what road a car is following based on noisy positional data. We consider here a simplified example. At some known timepoints `ts`, we get noisy observations on the car's vertical position `ys obs` imagine it as a lattitude of sorts . There are two parallel roads lines the car can actually be on, which both have a constant vertical position. If the car is on road 1, then `y 0`. If it is on road 2, then `y 1`. The problem is visualized below. \"\"\" ts 1 10 collect ts ys obs 0.6, 0.0, 0.8, 0.7, 0.5, 0.2, 1.0, 1.2, 1.8, 1.1 begin p cardata scatter ts, ys obs, label \"Observed car positions\", xlabel \"Time\", ylabel \"Vertical position\" hline 0.0 , color orange, label \"Road 1\", linewidth 2 hline 1.0 , color blue, label \"Road 2\", linewidth 2 end md\"\"\" At some point `t switch` ∈ 0, 10 , the car switches from lane 1 to lane 2. We can describe the model as follows If `t t switch`, then `y ~ Normal 0.0, σ `, If `t t switch`, then `y ~ Normal 1.0, σ `, with `σ` some small noise parameter. \"\"\" md\"\"\" Below is a plot showing the car's trajectory for some value of `t switch`. You can adjust the slider to change this guess value. \"\"\" bind switchtime Slider 0 0.1 10, default 5.0, show value true begin plot deepcopy p cardata , 0.0, switchtime, switchtime, 10.0 , 0.0, 0.0, 1.0, 1.0 , label \"Car trajectory\", color black, linewidth 2, xticks 0, 10, switchtime , \"0\", \"10\", \"t switch\" end md\"\"\" question Infer the posterior probability of `t switch` given the data. \"\"\" model function cars ts t switch ~ Uniform 0, 10 σ ~ Exponential 1.0 ys obs zeros length ts for pointidx in 1 length ts if ts pointidx t switch ys obs pointidx ~ Normal 0.0, σ else ys obs pointidx ~ Normal 1.0, σ end end end carmodel cars ts | ys obs ys obs, carchain sample carmodel, NUTS , 2000 plot carchain histogram carchain t switch , normalized probability mean carchain t switch md\" Petridish peril inference edition \" md\"\"\" We continue with the \"petridish peril\" question from the previous practical. You've made a model to predict bacterial population levels at certain timepoints based on your knowledge of how the species in question grows. You'd now like to update the model with information about the specific strain you're using, so you inoculate a petri dish and count the number of bacteria after a short incubation period. Incorporate the following information into the model to make it more accurate The population level after 5 hours of incubating was 21000. You expect the number of bacteria you count to be Poisson distributed around the actual number. \"\"\" md\"\"\" questions 1. Now taking into account the measurement, what are the chances of your petridish being in a splittable state after 8 hours? 1. Visualise the updated growth curves. 1. 🌟 BONUS The prior for P0 being discrete doesn't allow for the use of a continuous sampler. Change the prior with a sufficiently similar continuous one to fix this. How does this affect the results? \"\"\" md\"\"\" tip Just like in the previous version of the question, `return`ing the estimated logistic function can be useful. \"\"\" logistic t, P0, r, K K 1 K P0 P0 exp r t md\" 1\" dropletdist MixtureModel Poisson 10 , Poisson 30 , 0.75, 0.25 model function petrigrowth t obs P0 ~ dropletdist r ~ LogNormal 0.0, 0.3 K ~ Normal 1e5, 1e4 logfun t logistic t, P0, r, K Pt logfun t obs Number of bacteria at the observed time P obs ~ Poisson Pt The observed number is Poisson distributed return logfun end petrimodel petrigrowth 5 | P obs 21 000, petrichain sample petrimodel, MH , 100 000 better and faster than PG 40 plot petrichain logfuns generated quantities petrimodel, petrichain sp petri logfun 8.0 for logfun in logfuns prob splittable mean sp petri . 1e4 .&& sp petri . 1e5 md\" 2\" plot logfuns 1 10 1000 , xlims 0, 12 , legend false, color skyblue, alpha 0.5 md\" 3🌟\" dropletdist🌟 MixtureModel truncated Normal 10, sqrt 10 , lower 0.0 , truncated Normal 30, sqrt 30 , lower 0.0 , 0.75, 0.25 begin plot dropletdist, label \"Original prior\" \"\" , color orange plot dropletdist🌟, label \"Continuous alternative\" \"\" , color blue With mixture models it takes some fiddling to make the labels look nice don't worry about this, it's not important for the course end model function petrigrowth🌟 t obs P0 ~ dropletdist🌟 r ~ LogNormal 0.0, 0.3 K ~ Normal 1e5, 1e4 logfun t logistic t, P0, r, K Pt logfun t obs P obs ~ Poisson Pt return logfun end let so we dont need to rename all variables petrimodel petrigrowth🌟 5 | P obs 21 000, petrichain sample petrimodel, NUTS , 2 000 logfuns generated quantities petrimodel, petrichain sp petri logfun 8.0 for logfun in logfuns prob splittable mean sp petri . 1e4 .&& sp petri . 1e5 println \"The new `prob splittable` is \", prob splittable plot petrichain end md\"Changing to all continuous distributions made the inference much higher quality in this case, as can be seen from the chain plots. This also means this result is more reliable \" "},{"url":"exercises/MCMC_4-review/","title":"4. MCMC review","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"23\" title \"4. MCMC review\" date \"2025 08 06\" tags \"exercises\" description \"MCMC review\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots function generate data n wasps 10 minbound 0, maxbound 1000 position of the nest x n, y n rand DiscreteUniform minbound, maxbound , 2 positions of the feeder stations where the wasps get marked xs, ys rand DiscreteUniform minbound, maxbound , n wasps for in 1 2 speed of the wasps v wasps rand Uniform 5, 10 , n wasps back and forth time of the wasps ts 2 sqrt x x n ^2 y y n ^2 v wasp for x, y, v wasp in zip xs, ys, v wasps return xs, ys, ts, x n, y n end let x n, y n rand DiscreteUniform 0, 1000 , 2 println x n, y n xs, ys rand DiscreteUniform 0, 1000 , 5 for in 1 2 println xs println ys v wasps rand Uniform 5, 10 , 5 println round. v wasps, digits 2 ts 2 sqrt x x n ^2 y y n ^2 v wasp for x, y, v wasp in zip xs, ys, v wasps println round. ts, digits 2 end md\" Review exercise Hornet nests\" md\"\"\" In recent years, the Asian giant hornet Vespa mandarinia has become an invasive species in a number of countries, including Belgium. Since they become aggressive when people get close to their nests, the nests often need to be removed when they appear in residential areas. Finding the nests, however, can be a difficult task the hornets can go hunting over a kilometer from their nest. \"\"\" md\"\"\" One method for finding the nest is to set up a feeder station, mark any hornets gathering food, and record how long it takes for them to fly back to their nest with it and return for more. Making an estimate of their flight speed, the return time can be used to infer the distance of that location to the nest. Repeated measurements in other locations gives enough information for a triangulation of sorts. \"\"\" md\"\"\" The Asian giant hornet https upload.wikimedia.org wikipedia commons thumb 1 19 Vespa mandarinia japonica1.jpg 1280px Vespa mandarinia japonica1.jpg The Asian giant hornet credit Picture by KENPEI on Wikipedia \"\"\" md\"\"\" Consider below the coordinates of marked hornets, as well as their return times. \"\"\" xs, ys, ts, true location generate data 20 scatter xs, ys, label \"wasp locations\", marker z ts, title \"Locations of wasps colored by return time\", xlims 0, 1000 , ylims 0, 1000 md\"\"\" question Where is the hornet nest located? You may assume the nest is somewhere within the plot's boundaries. \"\"\" plot Gamma 8, 1 model function horenaars xs, ys, ts x nest ~ Uniform 0, 1000 y nest ~ Uniform 0, 1000 v wasp ~ Uniform 5, 10 v wasp ~ Gamma 8 for i in eachindex ts dist sqrt xs i x nest ^2 ys i y nest ^2 ts i ~ Normal 2 dist v wasp, 10 end end n samples 1000 chain sample horenaars xs, ys, ts , NUTS , n samples plot chain x nest sp chain x nest y nest sp chain y nest begin scatter x nest sp, y nest sp, opacity 0.1, color blue, label \"Estimated nest locations\", xlims 0, 1000 , ylims 0, 1000 scatter xs, ys, color orange, label \"wasp locations\", marker z ts scatter true location 1 1 , true location 2 2 , color RGB 1, 1, 1 , label \"True nest location\", markershape rect end true location "},{"url":"exercises/calib_fermenter_monod/","title":"5. Calibration fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"25\" title \"5. Calibration fermenter monod\" date \"2025 08 06\" tags \"exercises\" description \"Calibration fermenter monod\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst, OrdinaryDiffEq, StatsPlots using Turing, StatsBase using LinearAlgebra, Optim md\"\"\" Exercise Fermenter Monod kinetics Calibration \"\"\" md\"\"\" In one of the previous practicals we were introduced to a fermenter in which biomass X g L grows by breaking down substrate S g L . The reactor is fed with an inlet flow rate Q in L h , which consists of a manipulable inlet concentration of substrate S in g L . This process was modelled using Monod kinetics, resulting in the model below \\begin eqnarray S X \\xrightarrow \\quad\\quad k 1 Y \\, X \\quad\\quad\\quad\\quad \\textrm with \\quad k \\cfrac \\mu max S K s \\end eqnarray \"\"\" \\begin eqnarray %S \\xrightarrow \\quad\\quad \\beta Y \\, X S \\xrightarrow \\quad\\quad r Y \\, X \\quad\\quad\\quad\\quad r \\mu \\, X \\quad \\textrm with \\quad \\mu \\mu max \\, \\cfrac S S K s \\end eqnarray md\"\"\" The reaction network object for this model could be set up as \"\"\" fermenter monod reaction network begin μmax S Ks , S X 1 Y X Alternatives X mm S, μmax, Ks , S Y X mm S, μmax, Ks X, S X 1 Y X Q V, S, X 0 Q V Sin, 0 S end convert ODESystem, fermenter monod, combinatoric ratelaws false parameters fermenter monod md\"\"\" which resulted in the following differential equations \\begin eqnarray \\cfrac dS dt & & \\cfrac Q V \\left S in S \\right \\mu max \\cfrac S S K s X\\\\ \\cfrac dX dt & & \\cfrac Q V X Y \\mu max \\cfrac S S K s X \\end eqnarray \"\"\" md\"\"\" Suppose that during an experiment measurement data have been collected of the substrate S and biomass X concentration at an interval of 5\\ h within 100\\ h \"\"\" S meas 1.0e 5, 0.0047, 0.00796, 0.01056, 0.01214, 0.01325, 0.01344, 0.01338, 0.0115, 0.00917, 0.00604, 0.00458, 0.00438, 0.00342, 0.00323, 0.00329, 0.00312, 0.00314, 0.00319, 0.00299, 0.00311 X meas 0.00052, 0.00042, 0.00074, 0.00078, 0.00122, 0.00159, 0.00242, 0.00372, 0.00534, 0.0077, 0.00935, 0.00997, 0.01114, 0.01144, 0.01264, 0.01276, 0.01183, 0.01319, 0.01256, 0.01277, 0.01377 t meas 0.0 5.0 100.0 md\"\"\" Make a scatter plot of the measured data for both S and X . Use the following options `label \\\"S meas\\\", color blue` for S , and `label \\\"X meas\\\", color red` for X . \"\"\" Uncomment and complete the instruction begin missing missing end md\"\"\" We have previously used the following parameter values \\mu max 0.40\\ h^ 1 , K s 0.015\\ g L , S in 0.022\\ g L Y 0.67 , Q 2.0\\ L h , V 40.0\\ L Furthermore, suppose that at t 0\\ h no substrate S is present in the reactor but that there is initially some biomass with a concentration of 0.0005\\ g L . Calibrate the parameter values for \\mu max and K s using the aforementioned measurement data for S and X in a timespan of 0, 100 \\,h . Take the values above as initial values for \\mu max and K s . \"\"\" md\"\"\" Create an `ODEProblem`. Use the aforementioned values as initial values for the problem. \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction params missing Uncomment and complete the instruction oprob missing Uncomment and complete the instruction md\"\"\" Declare the Turing model. Use `InverseGamma` for the standard deviations of the measurements and `LogNormal` for `μmax` and `K`. \"\"\" Uncomment and complete the instruction model function fermenter fun t meas σ S ~ missing σ X ~ missing μmax ~ missing Ks ~ missing params missing oprob missing osol missing S s ~ missing X s ~ missing end md\"\"\" Provide the time measurements to the defined function and instantly condition the model with the measurements of S and X \"\"\" fermenter cond mod missing Uncomment and complete the instruction md\"\"\" Optimize the priors \\sigma S , \\sigma X , \\mu max and K s . Do this with `MLE` method and Nelder Mead. Store the optimization results in `results mle`. If necessary, run the optimization again if you get any errors. \"\"\" results map missing Uncomment and complete the instruction md\"\"\" Visualize a summary of the optimized parameters. \"\"\" missing Uncomment and complete the instruction md\"\"\" Get the optimized values and assign them to `μmax opt` and `Ks opt`. \"\"\" μmax opt missing Uncomment and complete the instruction Ks opt missing Uncomment and complete the instruction md\"\"\" Make a plot of S and X simulated with the optimized parameter values. \"\"\" md\"\"\" Set up parameter values with optimized parameter values \"\"\" params opt missing Uncomment and complete the instruction md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt missing Uncomment and complete the instruction osol opt missing Uncomment and complete the instruction md\"\"\" Plot S and X simulated with the optimal and initial parameter values together with the measured data. We can do this to compare the found values with the initial ones and detect possible errors. \"\"\" Uncomment and complete the instruction begin missing missing missing missing missing end md\"\"\" question How do the found optimal parameter values compare to the original values? Or in other words what is the impact to be expected when we simulate the fermenter with the optimal values? \"\"\" md\"\"\" Answer \"\"\" md\"\"\" hint Think of the meaning of the estimated parameters and their impact on the variables S and X . \"\"\" "},{"url":"exercises/calib_intro/","title":"5. Calibration intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"24\" title \"5. Calibration intro\" date \"2025 08 06\" tags \"exercises\" description \"Calibration intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst, OrdinaryDiffEq, StatsPlots using Turing, StatsBase using LinearAlgebra, Optim using PlutoUI TableOfContents md\"\"\" Introduction to calibration \"\"\" md\"\"\" Goal of this practicum \"\"\" md\"\"\" In the models discussed in the previous sessions, i.e., the values of all parameters were known. In reality, the value of a parameter often needs to be calibrated, i.e., estimated from experimental data. During this parameter estimation one attempts to find the set of parameter values for which the model predictions are as close as possible to the collected experimental data. \"\"\" md\"\"\" The search of optimal parameter values usually involves a function, such as a loss function, a log likelihood function or a posterior distribution function. In this session we will be mainly using the MLE Maximum Likelihood Estimation and MAP Maximum A Posteriori estimation methods. In the MLE method, a likelihood function of a given probability density function is maximized during the search of optimal parameter values of a model in order to fit experimental data. The parameter values are considered unknown but viewed as fixed points. In the MAP method, a posterior distribution function is maximized. Instead of viewing the parameter values as fixed points, they are now treated as random variables in the model which follow a prior distribution. In other words, we have prior belief in which distribution these parameters come from Normal, Beta, etc . Once new data comes in, we update our prior belief, leading to a posterior belief. Hence, we now have a better idea from which distribution these parameters come. One caveat with the MAP method is that it only considers the most likely point without taking into account other values of parameters from posterior distribution, which leads to a huge loss of useful information in posterior distribution. A better, yet computationally exhaustive method is using the MCMC Markov chain Monte Carlo sampling methods. Here we will use the NUTS sampler in this session. \"\"\" md\"\"\" In this notebook we will calibrate the different parameters involved in the grass growth models with either MLE, MAP or MCMC. To illustrate this concept, we first go through three simple models for grass growth yield. \"\"\" md\"\"\" Grass growth models \"\"\" md\"\"\" In this notebook, three different models will be used, each modelling the yield of grass in a grassland Logistic growth model \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W Exponential growth model \\cfrac dW dt \\mu \\left W f W \\right Gompertz growth model \\cfrac dW dt \\left \\mu D \\ln W \\right W with output W the grass yield, and W f , \\mu and D parameters. The table below shows the parameter values and the initial condition that will be used as initial values for the optimization algorithm | | \\mu | W f | D | W 0 | | | | | | | | Logistic | 0.07 | 10.0 | | 2.0 | | Exponential | 0.02 | 10.0 | | 2.0 | | Gompertz | 0.09 | | 0.040 | 2.0 | Hence, for each grass growth model, we will optimize the parameter values together with the initial value. \"\"\" md\"\"\" In each of the three models we will use the following timespan \"\"\" tspan 0.0, 100.0 this will be the same for the three models md\"\"\" Logistic growth model We will illustrate the calibration with the logistic growth model \"\"\" growth log reaction network begin μ 1 W Wf , W 2W end osys log convert ODESystem, growth log md\"\"\" Check the order of the parameters \"\"\" parameters growth log md\"\"\" Next, we will need to create an `ODEProblem` in advance before we can optimize some of its parameters. We will provide the values in the aforementioned table as initial values for the problem. \"\"\" u0 log W 2.0 params log μ 0.07, Wf 10.0 oprob log ODEProblem growth log, u0 log, tspan, params log md\"\"\" The measurement data \"\"\" md\"\"\" Assume that the measured grass yields of a certain plant type over time are the following \"\"\" W meas 1.87, 2.45, 3.72, 4.32, 5.28, 7.01, 6.83, 8.62, 9.45, 10.31, 10.56, 11.72, 11.05, 11.53, 11.39, 11.7, 11.15, 11.49, 12.04, 11.95, 11.68 md\"\"\" They have been measured at the following corresponding time instances \"\"\" t meas 0 5 100 md\"\"\" If you literally want to see the time values, you can use the `collect` function to create a dense vector \"\"\" collect t meas md\"\"\" We can make a scatter plot of this data including a title, a legend label, an X axis label, X and Y axis limits in the following way \"\"\" scatter t meas, W meas, title \"Grass growth data\", label \"Yield\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 savefig \"calibration.png\" md\"\"\" Declaration of the Turing model function \"\"\" md\"\"\" In the Turing model function we will define our priors for the following magnitudes the measurement error standard deviation \\sigma W , the initial condition W 0 , and the parameters \\mu and W f . We will thereby take an `InverseGamma` prior distribution for \\sigma W and `LogNormal` prior distributions for the initial condition and the parameters. You can always plot the priors to have a look at them, e.g., with `plot LogNormal `. \"\"\" plot LogNormal Log normal with 0 log mean and unit scale plot LogNormal log 1 , 1.0 Log normal with 0 log mean and unit scale plot LogNormal log 10 , 0.50 Log normal with 1 log mean and 0.5 scale plot InverseGamma Inverse Gamma with shape 1 and scale 1 plot InverseGamma 3, 1 Inverse Gamma with shape 3 and scale 1 plot InverseGamma 5, 0.5 Inverse Gamma with shape 5 and scale 0.5 md\"\"\" Here is our Turing model function \"\"\" model function growth log fun t meas σ W ~ InverseGamma W0 ~ LogNormal μ ~ LogNormal Wf ~ LogNormal u0 log W W0 params log μ μ, Wf Wf oprob log ODEProblem growth log, u0 log, tspan, params log osol log solve oprob log, Tsit5 , saveat t meas W s ~ MvNormal osol log W , σ W^2 I return osol log optionally, to be used with MCMC end md\"\"\" note \"Some remarks\" The time points are the ones from the measurements, therefore, we set `saveat t meas`. We need to solve the ODE problem inside the Turing model function with values for W 0 , \\mu and W f sampled from the distributions. Therefore, we need to remake our ODE problem with the appropriate initial and parameter values and solve it. We will consider our solution vector for W as being multivariate normally distributed with mean vector the actual solution for W and covariance matrix a diagonal matrix with variances \\sigma W^2 . The function `MvNormal μ, Σ ` will construct a multivariate normal distribution with mean vector μ and covariance matrix Σ. Consider the following small example. The mean vector here is ` 1.7, 4.5, 3.6 ` and the variance on the diagonal are `0.5^2 I` `I` is the identity matrix . When you sample from it, you get a vector of values with mean ` 1.7, 4.5, 3.6 ` and each value with variance `0.5^2`. \"\"\" rand Uniform 0, 20 rand MvNormal 1.7, 4.5, 3.6 , 0.5^2 I md\"\"\" We now provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth log cond mod growth log fun t meas | W s W meas, md\"\"\" We are now ready to optimize the priors \\sigma W , W 0 , \\mu and W f . This is done by calling the `optimize` function, providing the previously created object `growth log inf`, the method for estimating the parameters and optionally an algorithm default Nelder Mead to implement the method. \"\"\" md\"\"\" Method Maximum Likelihood Estimation \"\"\" md\"\"\" We will use the MLE Maximum Likelihood Estimation method here and store the optimization results in `results log mle`. If you get an error the first time, try running the optimization again. \"\"\" results log mle optimize growth log cond mod, MLE , NelderMead md\"\"\" You can visualize a summary of the optimized parameters by piping them to `coeftable` \"\"\" coeftable results log mle md\"\"\" You can obtain the actual optimized values using the function `coef` on the results object in conjunction by calling the parameters by name preceded by a colon. Here we assign the optimized parameter values to some suitable variable names \"\"\" W0 opt1 log coef results log mle W0 μ opt1 log coef results log mle μ Wf opt1 log coef results log mle Wf md\"\"\" Now we can make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Setting up initial condition with optimized initial condition \"\"\" u0 opt1 log W W0 opt1 log md\"\"\" Setting up parameter values with optimized parameter values \"\"\" params opt1 log μ μ opt1 log, Wf Wf opt1 log md\"\"\" Next, we create an ODEProblem and solve it \"\"\" oprob opt1 log ODEProblem growth log, u0 opt1 log, tspan, params opt1 log osol opt1 log solve oprob opt1 log, Tsit5 , saveat 0.5 md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" begin plot osol opt1 log, label \"Logistic growth\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 , lw 2.0, title \"MLE\" scatter t meas, W meas, label \"Yield\" end md\"\"\" Method Maximum A Posterior \"\"\" md\"\"\" We will use the MAP Maximum A Posterior method here and store the optimization results in `results log map`. Try running the optimization once again if you get an error. \"\"\" results log map optimize growth log cond mod, MAP , NelderMead md\"\"\" You can visualize a summary of the optimized parameters by piping them to `coeftable` \"\"\" coeftable results log map md\"\"\" You can compare the optimized values by both methods now and find that results are quite similar \"\"\" coeftable results log mle md\"\"\" Next, you can obtain the actual optimized values using the function `coef` on the results object in conjunction by calling the parameters by name preceded by a colon. Here we assign the optimized parameter values to some suitable variable names \"\"\" W0 opt2 log coef results log map W0 μ opt2 log coef results log map μ Wf opt2 log coef results log map Wf md\"\"\" Now we can make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Setting up initial condition with optimized initial condition \"\"\" u0 opt2 log W W0 opt2 log md\"\"\" Setting up parameter values with optimized parameter values \"\"\" params opt2 log μ μ opt2 log, Wf Wf opt2 log md\"\"\" Next, we create an ODEProblem and solve it \"\"\" oprob opt2 log ODEProblem growth log, u0 opt2 log, tspan, params opt2 log osol opt2 log solve oprob opt2 log, Tsit5 , saveat 0.5 md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" begin plot osol opt2 log, label \"Logistic growth\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 , lw 2.0, title \"MAP\" scatter t meas, W meas, label \"Yield\" end md\"\"\" Method MCMC with NUTS \"\"\" md\"\"\" We will use Markov chain Monte Carlo MCMC method in combination with the No U Turn Sampler NUTS here and store the optimization results in `results log nuts`. \"\"\" results log nuts sample growth log cond mod, NUTS , 1000 md\"\"\" You can plot the sampled chain results to verify the form of the pdf for the estimated parameters \"\"\" plot results log nuts summarize results log nuts Get element at row 'W0', column 'mean' in the summary table W0 opt3 log summarize results log nuts W0, mean Alternative W0 opt3 log mean results log nuts W0 Get element at row 'μ', column 'mean' in the summary table μ opt3 log summarize results log nuts μ , mean Alternative μ opt3 log mean results log nuts μ Get element at row 'Wf', column 'mean' in the summary table Wf opt3 log summarize results log nuts Wf , mean Alternative Wf opt3 log mean results log nuts Wf md\"\"\" Now we can make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Setting up initial condition with optimized initial condition \"\"\" u0 opt3 log W W0 opt3 log md\"\"\" Setting up parameter values with optimized parameter values \"\"\" params opt3 log μ μ opt3 log, Wf Wf opt3 log md\"\"\" Next, we create an ODEProblem and solve it \"\"\" oprob opt3 log ODEProblem growth log, u0 opt3 log, tspan, params opt3 log osol opt3 log solve oprob opt3 log, Tsit5 , saveat 0.5 md\"\"\" Optionally, you can get 200 sampled solutions from the posterior parameter distributions in the following way. We will plot these together with the solution based on the mean optimized values for the parameters. \"\"\" osol log sampled generated quantities growth log cond mod, results log nuts 1 5 1000 md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" begin h plot title \"Fit posterior\" Make empty plot and return handle We first plot the 200 sampled solutions this is optional for i in eachindex osol log sampled plot h, osol log sampled i , color skyblue, alpha 0.2, label false end Now we plot the solution based on the mean optimized values plot h, osol opt3 log, label \"Logistic growth\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 , lw 1.5, color black Finally, we add the measured values scatter h, t meas, W meas, label \"Yield\" end md\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 Calibration of the exponential growth model Calibrate the initial condition and both parameters of the exponential growth model. Use the values mentioned in the Table as initials values for the optimization of the parameters. \"\"\" md\"\"\" We have seen before that a possible reaction network object for the exponential growth model can be implemented as follows \"\"\" growth exp reaction network begin μ Wf, 0 W μ, W 0 end parameters growth exp md\"\"\" Create an `ODEProblem`. Use the values in the aforementioned table as initial values for the problem. Use the same `tspan` as before. \"\"\" u0 exp missing Uncomment and complete the instruction params exp missing Uncomment and complete the instruction oprob exp missing Uncomment and complete the instruction md\"\"\" Use the same measurement data `W meas`, `t meas` as before. \"\"\" md\"\"\" Declare the Turing model function. \"\"\" Uncomment and complete the instruction model function growth exp fun t meas σ W ~ missing W0 ~ missing μ ~ missing Wf ~ missing u0 exp missing params exp missing oprob exp missing osol exp missing W s ~ missing end md\"\"\" Provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth exp cond mod missing Uncomment and complete the instruction md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and W f . Do this with both the `MLE` and `MAP` methods and the Nelder Mead algorithm. Store the optimization results in `results exp mle` and `results exp map`. \"\"\" results exp mle missing Uncomment and complete the instruction results exp map missing md\"\"\" Visualize a summary of the MLE and MAP optimized parameters. \"\"\" missing For MLE missing For MAP md\"\"\" Get the MLE optimized values and assign them to `W0 opt mle exp`, `μ opt mle exp` and `Wf opt mle exp`. \"\"\" W0 opt mle exp missing Uncomment and complete the instruction μ opt mle exp missing Uncomment and complete the instruction Wf opt mle exp missing Uncomment and complete the instruction md\"\"\" Do the same for the MAP optimized values. \"\"\" begin W0 opt map exp missing μ opt map exp missing Wf opt map exp missing end md\"\"\" Make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\" Set up initial condition with the MLE optimized initial condition \" u0 opt mle exp missing Uncomment and complete the instruction md\"\"\" Set up parameter values with the MLE optimized parameter values \"\"\" params opt exp missing Uncomment and complete the instruction md\"\"\" Do the same for the MAP optimized values. \"\"\" begin u0 opt map exp missing params opt map exp missing end md\"\"\" Create an ODEProblem and solve it. Solve it using `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt mle exp missing Uncomment and complete the instruction oprob opt map exp missing osol opt mle exp missing Uncomment and complete the instruction osol opt map exp missing md\"\"\" Plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" Uncomment and complete the instruction begin missing missing missing missing end md\"\"\" Exercise 2 Calibration of the Gompertz growth model Calibrate the initial condition and both parameters of the Gompertz growth model. Use the values mentioned in the Table as initials values for the optimization of the parameters. \"\"\" md\"\"\" We have seen before that a possible reaction network object for the Gompertz growth model can be implemented as follows \"\"\" growth gom reaction network begin μ D log W , W 2W end md\"\"\" Create an `ODEProblem`. Use the values in the aforementioned table as initial values for the problem. Use the same `tspan` as before. \"\"\" u0 gom missing Uncomment and complete the instruction params gom missing Uncomment and complete the instruction oprob gom missing Uncomment and complete the instruction md\"\"\" Use the same measurement data `W meas`, `t meas` as before. \"\"\" md\"\"\" Declare the Turing model. Take the same priors as before. \"\"\" Take for \\sigma W and W 0 the same priors and distributions as before, but take for \\mu a Uniform prior distribution in the range 0, 2 and the same for D but in the range 0, 1 . Uncomment and complete the instruction model function growth gom fun t meas σ W ~ missing W0 ~ missing μ ~ missing D ~ missing u0 gom missing params gom missing oprob gom missing osol gom missing W s ~ missing end md\"\"\" Provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth gom cond mod missing Uncomment and complete the instruction md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and D . Do this now with `MAP` method and Nelder Mead. Store the optimization results in `results gom map`. \"\"\" results gom map missing Uncomment and complete the instruction md\"\"\" Visualize a summary of the optimized parameters. \"\"\" missing Uncomment and complete the instruction md\"\"\" Get the optimized values and assign them to `W0 opt gom`, `μ opt gom` and `D opt gom`. \"\"\" W0 opt gom missing Uncomment and complete the instruction μ opt gom missing Uncomment and complete the instruction D opt gom missing Uncomment and complete the instruction md\"\"\" Make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Set up initial condition with optimized initial condition \"\"\" u0 opt gom missing Uncomment and complete the instruction md\"\"\" Set up parameter values with optimized parameter values \"\"\" params opt gom missing Uncomment and complete the instruction md\"\"\" Create an ODEProblem and solve it. Use the solver `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt gom missing Uncomment and complete the instruction osol opt gom missing Uncomment and complete the instruction md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" Uncomment and complete the instruction begin missing missing end md\"\"\" question Which grass growth model fits best these data? How can you prove this numerically? \"\"\" md\" Answer missing\" "},{"url":"exercises/calib_irrigation/","title":"5. Calibration irrigation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"26\" title \"5. Calibration irrigation\" date \"2025 08 06\" tags \"exercises\" description \"Calibration irrigation\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst, OrdinaryDiffEq, StatsPlots using Turing using StatsBase using LinearAlgebra using Optim md\" Exercise Irrigation experiment Calibration \" md\"\"\" In one of the previous practica we were introduced to an irrigation experiment carried out on a soil column consisting of two layers of soil, each with specific soil characteristics. However, here the volume of water per unit of time, r , irrigated evenly over the soil column, will be kept constant at 5\\ mm\\,h^ 1 in these new experiments. The water falls on the upper layer and percolates to the lower layer. The relative moisture content in both layers i.e., relative to their residual moisture contents is denoted by S 1 and S 2 . A model description of the relative moisture content in both soil layers is given by \\begin align \\frac dS 1 dt & r\\left 1 \\cfrac S 1,res S max \\right \\cfrac r S max S 1 \\cfrac k S max S 1 \\\\ \\frac dS 2 dt & \\cfrac k S max S 1 v \\,S 2^2 \\end align where v 10^ 3 \\ h^ 1 \\,mm^ 1 and S 1,res 10 \\ mm . Previously, we also assumed k 3\\ mm\\,h^ 1 and S max 150\\ mm . \"\"\" md\"\"\" The reaction network object for this model could be set up as \"\"\" irrigation mod reaction network begin k Smax, S1 S2 v, 2S2 0 r 1 S1res Smax , 0 S1 r Smax, S1 0 end md\"\"\" In order to have better estimates the parameters k and S max , two experiments were conducted, each with a different initial condition 1. Starting from zero relative moisture content in both soil layers. 2. Starting from a relative moisture content of 140\\ mm in the top layer, and 135\\ mm in the bottom layer. The measurement data consist of measurements of the relative moisture contents S 1 and S 2 measured at intervals of 10\\ h within a timespan of 150\\ h . \"\"\" md\"\"\" The measurement data for the 1st experiment are \"\"\" S1 meas1 0.2, 35.94, 52.49, 66.86, 60.66, 67.81, 73.22, 71.31, 72.94, 64.08, 70.11, 68.53, 70.54, 63.63, 67.39, 62.84 S2 meas1 0.63, 6.2, 17.67, 22.96, 35.41, 44.08, 43.5, 53.34, 47.57, 47.77, 43.96, 52.22, 46.67, 46.74, 46.46, 39.92 md\"\"\" The measurement data for the 2nd experiment are \"\"\" S1 meas2 137.96, 106.15, 90.15, 84.64, 76.15, 75.73, 73.32, 68.48, 70.06, 69.36, 70.91, 72.13, 76.25, 74.34, 74.93, 71.58 S2 meas2 124.08, 80.14, 60.15, 50.12, 49.66, 47.78, 46.56, 48.41, 42.7, 43.72, 49.03, 51.91, 48.24, 46.14, 51.22, 43.78 md\"\"\" For both experiments \"\"\" t meas 0 10 150 md\"\"\" We can make a scatter plot of the measured data for both S 1 and S 2 for the 1st and 2nd experiments in the following way \"\"\" begin scatter t meas, S1 meas1, label \"S1 meas\", color blue, title \"Experiment 1\" scatter t meas, S2 meas1, label \"S2 meas\", color red, ylims 0, 150 end begin scatter t meas, S1 meas2, label \"S1 meas\", color blue, title \"Experiment 2\" scatter t meas, S2 meas2, label \"S2 meas\", color red, ylims 0, 150 end md\"\"\" Calibrate the parameter values for k and S max using the aforementioned measurement data for S 1 and S 2 in a timespan of 0, 150 \\,h . Take the values from above as initial values for k and S max . \"\"\" md\"\"\" Create an `ODEProblem`. Use the aforementioned values as initial values for the problem. \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction params missing Uncomment and complete the instruction oprob missing md\"\"\" Declare the Turing model. Make sure you take both experiments into account for optimizing k and S max . Use `InverseGamma` for the standard deviations of the measurements, `LogNormal` for k and `Uniform` between 100 and 200 for Smax . \"\"\" Uncomment and complete the instruction model function irrigation fun t meas σ S1 ~ missing σ S2 ~ missing k ~ missing Smax ~ missing params missing u01 missing oprob1 missing osol1 missing S1 s1 ~ missing S2 s1 ~ missing u02 missing oprob2 missing osol2 missing S1 s2 ~ missing S2 s2 ~ missing end md\"\"\" Provide the time measurements to the defined function and instantly condition the model with the measurements of S 1 and S 2 from both experiments \"\"\" irrigation cond mod missing Uncomment and complete the instruction md\"\"\" Optimize the priors \\sigma S1 , \\sigma S2 , k and S max . Do this with `MLE` method and Nelder Mead. Store the optimization results in `results mle`. \"\"\" results mle missing Uncomment and complete the instruction md\"\"\" Visualize a summary of the optimized parameters. \"\"\" missing Uncomment and complete the instruction md\"\"\" Get the optimized values and assign them to `k opt` and `Smax opt`. \"\"\" k opt missing Uncomment and complete the instruction Smax opt missing Uncomment and complete the instruction md\"\"\" Make plots of S 1 and S 2 for both experiments simulated with the optimized parameter values. \"\"\" md\"\"\" Set up parameter values with optimized parameter values \"\"\" params opt missing Uncomment and complete the instruction md\"\"\" Plot the simulation results S 1 and S 2 for the 1st experiment together with the corresponding measured data. Therefore initialize a vector `u01` with initial conditions for the 1st experiment. \"\"\" u01 missing Uncomment and complete the instruction oprob1 opt missing Uncomment and complete the instruction osol1 opt missing Uncomment and complete the instruction Uncomment and complete the instruction begin missing missing missing end md\"\"\" Plot the simulation results S 1 and S 2 for the 1st experiment together with the corresponding measured data. Therefore initialize a vector `u02` with initial conditions for the 2nd experiment. \"\"\" u02 missing Uncomment and complete the instruction oprob2 opt missing Uncomment and complete the instruction osol2 opt missing Uncomment and complete the instruction Uncomment and complete the instruction begin missing missing missing end md\"\"\" question Do your simulations fit well the measurements? \"\"\" md\" Answer missing\" "},{"url":"exercises/exercises/","title":"Introduction","tags":["exercises"],"text":"main a img {\n    width: 5rem;\n    margin: 1rem;\n}\nExercises descriptionHere are renders of the exercises, see all pages on the left.To download all exercises: see Ufora.We have annotated the exercises with either a number or an extra prefix.XYZWill be covered in exercise lession 1.EXTRA. XYZAdditional exercises that will not be covered in the guided exercises.Notes on the dependenciesIf you insist on downloading the exercises from this website, note that because we are rendering the notebooks here, we make use of our a specific environment. You will need to update this on your system. Look out for the cell with:using Pkg\nPkg.activate(\"../../pluto-deployment-environment\")\nChange this to the your current folder so that the Project and Manifest files are generated there:using Pkg\nPkg.activate(\".\")"},{"url":"exercises/model_selection_intro/","title":"7. Model selection intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"35\" title \"7. Model selection intro\" date \"2025 08 06\" tags \"exercises\" description \"Model selection intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using PlutoUI TableOfContents using OrdinaryDiffEq, StatsPlots using Catalyst using Turing, StatsBase using LinearAlgebra, Optim md\"\"\" Introduction to model selection \"\"\" md\"\"\" Goal of this practicum \"\"\" md\"\"\" In previous practicals, we have developed models to study phenomena and predict future behavior. We have also estimated the parameters associated with these models and we have also analyzed the sensitivity of the model predictions to changes in these parameters. We found that the mathematical structure of different models determines the sensitivity to errors in the parameters and errors in the model itself and we determined how these errors propagate through the model, allowing us to quantify the uncertainty in the predictions. \"\"\" md\"\"\" In this practical, we investigate how to make an objective choice between different candidate models by weighing the complexity of the models against the fit to the experimental data and the quality of the prediction. We will use two information criteria often used in practice to balance model quality and complexity the Akaike information criterion AIC and the Bayesian information criterion BIC . \"\"\" md\"\"\" In the Akaike information criterion, the fit or quality of the model likelihood L is compared against the number of model parameters k , thus giving a measure of the balance between complexity and quality of the fit AIC 2k 2\\,\\log L \"\"\" md\"\"\" The Bayesian information criterion gives similar information, but penalizes complexity more heavily BIC k\\,\\log n 2\\,\\log L where n is the number of data points considered. \"\"\" md\"\"\" In this notebook we will compare the different grass growth models and judge the quality of their fit to the calibration data set in order to select the simplest or least complex model that best represents the system. \"\"\" md\"\"\" Grass growth models \"\"\" md\"\"\" In this notebook, three different models will be used, each modelling the yield of grass in a grassland Logistic growth model \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W Exponential growth model \\cfrac dW dt \\mu \\left W f W \\right Gompertz growth model \\cfrac dW dt \\left \\mu D \\ln W \\right W with output W the grass yield, and W f , \\mu and D parameters. The table below shows some typical values for the parameters | | \\mu | W f | D | | | | | | | Logistic | 0.07 | 10.0 | | | Exponential | 0.02 | 10.0 | | | Gompertz | 0.09 | | 0.04 | We will use an initial condition of W 0 2.0 for each and a simulation time of 100 days. \"\"\" md\"\"\" In each of the three models we will use the following timespan \"\"\" tspan 0.0, 100.0 this will be the same for the three models md\"\"\" The calibration data \"\"\" md\"\"\" Assume that the measured grass yields of a certain plant type over time are the following \"\"\" W meas 1.87, 2.45, 3.72, 4.32, 5.28, 7.01, 6.83, 8.62, 9.45, 10.31, 10.56, 11.72, 11.05, 11.53, 11.39, 11.7, 11.15, 11.49, 12.04, 11.95, 11.68 md\"\"\" They have been measured at the following corresponding time instances \"\"\" t meas 0 5 100 md\"\"\" We can make a scatter plot of this data including a title, a legend label, an X axis label, X and Y axis limits in the following way \"\"\" scatter t meas, W meas, title \"Grass growth data\", label \"Yield\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 md\"\"\" Logistic growth \"\"\" md\"\"\" \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W \\ W 0 2.0, \\mu 0.07 and W f 10.0\\ We will start by modelling our system and simulating using the aforementioned parameters values, initial condition and timespan in a way that we are familiar with. \"\"\" md\"\"\" Implementation of the system \"\"\" growth log reaction network begin species W t 2.0 default initial condition parameters μ 0.07 Wf 10.0 default parameter values μ 1 W Wf , W 2W end md\"\"\" Convert the reaction model to check that we work with the correct differential equation \"\"\" osys log convert ODESystem, growth log md\"\"\" Setting initial conditions, timespan and parameter values \"\"\" u0 log W 2.0 md\"\"\" For the sake of clarity, we will use the variables `μ log` and `Wf log` to store the parameter values. \"\"\" μ log 0.07 Wf log 10.0 params log μ μ log, Wf Wf log md\"\"\" Creating and solving the ODEProblem and plotting results \"\"\" oprob log ODEProblem growth log, u0 log, tspan, params log Also possible here if initial conditions and parameter values are defined in the catalyst model oprob log ODEProblem growth mod log, , tspan, osol log solve oprob log, Tsit5 , saveat 0.5 begin plot osol log, label \"model\", lw 2, ylabel \"W\" scatter t meas, W meas, title \"Logistic growth model\", label \"data\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 end md\"\"\" We can see that the model does not predict well the data set for the considered parameter values. Thus we will use the data to both calibrate the model parameters and assess the quality of the fit. \"\"\" md\"\"\" Parameter estimation \"\"\" md\"\"\" We declare our Turing model function \"\"\" model function growth log fun t meas σ W ~ InverseGamma W0 ~ LogNormal μ ~ LogNormal Wf ~ LogNormal u0 log W W0 params log μ μ, Wf Wf oprob log ODEProblem growth log, u0 log, tspan, params log osol log solve oprob log, Tsit5 , saveat t meas W s ~ MvNormal osol log W , σ W^2 I return osol log optionally, to be used with MCMC end md\"\"\" We now provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth log cond mod growth log fun t meas | W s W meas, md\"\"\" We are now ready to optimize the priors \\sigma W , W 0 , \\mu and W f . This is done by calling the `optimize` function, providing the previously created object `growth log inf`, the method for estimating the parameters and optionally an algorithm default Nelder Mead to implement the method. \"\"\" md\"\"\" We will use the MLE Maximum Likelihood Estimation method here and store the optimization results in `results log mle`. If you get an error the first time, try running the optimization again. \"\"\" results log mle optimize growth log cond mod, MLE , NelderMead md\"\"\" You can visualize a summary of the optimized parameters by piping them to `coeftable` \"\"\" coeftable results log mle md\"\"\" You can obtain the actual optimized values using the function `coef` on the results object in conjunction by calling the parameters by name preceded by a colon. Here we assign the optimized parameter values to some suitable variable names \"\"\" W0 opt log coef results log mle W0 μ opt log coef results log mle μ Wf opt log coef results log mle Wf md\"\"\" Now we can make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Setting up initial condition with optimized initial condition \"\"\" u0 opt log W W0 opt log md\"\"\" Setting up parameter values with optimized parameter values \"\"\" params opt log μ μ opt log, Wf Wf opt log md\"\"\" Next, we create an ODEProblem and solve it \"\"\" oprob opt log ODEProblem growth log, u0 opt log, tspan, params opt log osol opt log solve oprob opt log, Tsit5 , saveat 0.5 md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" begin plot osol opt log, label \"model\", xlabel \"t\", ylabel \"W\", xlims 0, 100 , ylims 0, 14 , lw 2.0, title \"Calibrated logistic growth model\" scatter t meas, W meas, label \"data\" end md\"\"\" We can extract from the calibration results the log likelihood or quality of the fit \"\"\" L log results log mle.lp md\"\"\" Model selection criteria \"\"\" md\"\"\" Akaike information criterion \"\"\" md\"\"\" To calculate the AIC, we can implement a function that uses the information from the calibration \"\"\" function AIC results, measurements L results.lp k length results.values n length measurements return 2k 2L L log likelihood end md\"\"\" This function uses the results from the calibration, from where we can extract as well the number of calibrated parameters, which includes the estimated prediction error \"\"\" k log length results log mle.values alternative length coef results log mle md\"\"\" The AIC will use this to balance the complexity with the quality of the fit. For the logistic model \"\"\" AIC log AIC results log mle, W meas md\"\"\" Bayesian information criterion \"\"\" md\"\"\" We can also calculate the BIC in a similar way to the AIC \"\"\" function BIC results, measurements L results.lp k length results.values n length measurements return k log n 2L L log likelihood end md\"\"\" The BIC will additionally use the length of the data set for the complexity penalty term \"\"\" n length W meas BIC log BIC results log mle, W meas md\"\"\" question What conclusions can we extract from a comparison of the AIC or BIC for different models? \"\"\" md\"\"\" Conclusions Lower values are better. For the same value of L , a simpler model would be preferred. BIC seems to penalize more complex models than AIC for the same values of L and k . \"\"\" md\"\"\" The posterior model probability \"\"\" md\"\"\" We can use the AIC to compute the posterior probabilities of the different candidate models P M i|D \\propto \\exp AIC M i 2 \"\"\" md\"\"\" The following function will use the supplied AIC of several models to compute the normalized posterior probability that the model is the \"true model\", explaining the considered data set \"\"\" function posterior AICs AICs vector of AIC values AICmin minimum AICs posterior zeros length AICs for i in eachindex 1 length AICs posterior i exp AICmin AICs i 2 end return round. posterior sum posterior digits 3 normalized sum end posterior AIC log md\"\"\" note This function will be used to compare the different candidate models more than one . \"\"\" md\"\"\" Least squares model fitting \"\"\" md\"\"\" The Akaike information criterion can be reformulated in terms of least squares if we assume that the model residuals are normally and independently distributed with zero mean, giving rise to AIC 2k n \\log \\bigg \\frac SSR n \\bigg where SSR is the squared sum of the model's residuals . For small data sets, a correction is done AIC c 2k n \\log \\bigg \\frac SSR n \\bigg \\frac 2k k 1 n k 1 When the number of observations is large enough, the corrected AIC c and AIC are identical. The Bayesian information criterion can also be expressed in terms of the residuals BIC k\\log n n \\log \\bigg \\frac SSR n \\bigg Both criteria are implemented below and can be used to compare the fitness of different models. \"\"\" function AIC LS SSR, n, k if n 40 return 2k n log SSR n else return 2k n log SSR n 2k k 1 n k 1 end end function BIC LS SSR, n, k return k log n n log SSR n end md\"\"\" We can thus obtain the squared sum of residuals from the calibrated model prediction and the data \"\"\" function SSR y pred, y data return sum y pred y data .^2 squared sum of residuals end md\"\"\" We can now calculate the SSR and alternative AIC and BIC forms for the logistic model \"\"\" begin W log solve oprob opt log, Tsit5 , saveat t meas W model prediction SSR log SSR W log, W meas AIC LS log AIC LS SSR log, n, k log BIC LS log BIC LS SSR log, n, k log end AIC log, BIC log AIC LS log, BIC LS log md\"\"\" note See the exercises below to apply the different criteria for model selection to the other models. \"\"\" md\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 Compare the logistic and exponential models \"\"\" md\"\"\" Calibrate the initial condition and both parameters of the exponential growth model. Use the values mentioned in the Table as initials values for the optimization of the parameters. Then compare the fit to that of the logistic model by plotting both predictions in the same figure. \"\"\" md\"\"\" \\cfrac dW dt \\mu \\left W f W \\right \\ W 0 2.0, \\mu 0.02 and W f 10.0 \"\"\" growth exp reaction network begin μ Wf, 0 W μ, W 0 end md\"\"\" Use the same measurement data `W meas`, `t meas` as before. \"\"\" md\"\"\" Declare the Turing model function. \"\"\" model function growth exp fun t meas σ W ~ InverseGamma W0 ~ LogNormal μ ~ LogNormal Wf ~ LogNormal u0 exp W W0 params exp μ μ, Wf Wf oprob exp ODEProblem growth exp, u0 exp, tspan, params exp osol exp solve oprob exp, Tsit5 , saveat t meas W s ~ MvNormal osol exp W , σ W^2 I end md\"\"\" Provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth exp cond mod growth exp fun t meas | W s W meas, md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and W f . Do this with both the `MLE` and `MAP` methods and the Nelder Mead algorithm. Store the optimization results in `results exp mle` and `results exp map`. \"\"\" results exp mle optimize growth exp cond mod, MLE , NelderMead md\"\"\" Visualize a summary of the optimized parameters. \"\"\" coeftable results exp mle md\"\"\" Get the optimized values and assign them to `W0 opt exp`, `μ opt exp` and `Wf opt exp`. \"\"\" W0 opt exp coef results exp mle W0 μ opt exp coef results exp mle μ Wf opt exp coef results exp mle Wf md\"\"\" Make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\" Set up initial condition with optimized initial condition \" u0 opt exp W W0 opt exp md\"\"\" Set up parameter values with optimized parameter values \"\"\" params opt exp μ μ opt exp, Wf Wf opt exp md\"\"\" Create an ODEProblem and solve it. Solve it using `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt exp ODEProblem growth exp, u0 opt exp, tspan, params opt exp osol opt exp solve oprob opt exp, Tsit5 , saveat 0.5 md\"\"\" Plot now W simulated with the optimized initial value and parameter values of both logistic and exponential models together with the measured data that was used to find the optimized values. \"\"\" Uncomment and complete the instruction begin plot missing missing missing title \"Comparison logistic vs. exponential growth\" end md\"\"\" question By looking at the figure, how can you decide which candidate model is better? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Compare now the fit of both models by applying both the AIC and BIC criteria. \"\"\" md\"\"\" Extract the log probability and number of parameters from the calibration results of the exponential \"\"\" L exp results exp mle.lp k exp length coef results exp mle md\"\"\" Calculate the AIC and BIC for the exponential model \"\"\" AIC exp AIC results exp mle, W meas BIC exp BIC results exp mle, W meas L log, L exp AIC log, AIC exp BIC log, BIC exp md\"\"\" question Draw your conclusions. \"\"\" md\" missing \" md\"\"\" Exercise 2 Comparison of the three models \"\"\" md\"\"\" Perform the calibration of the Gompertz model and compare its fitness to the other two candidates. \"\"\" md\"\"\" \\cfrac dW dt \\left \\mu D \\ln W \\right W \\ W 0 2.0, \\mu 0.09 and D 0.04. \"\"\" growth gom reaction network begin μ D log W , W 2W end md\"\"\" Declare the Turing model. Take the same priors as before. \"\"\" Take for \\sigma W and W 0 the same priors and distributions as before, but take for \\mu a Uniform prior distribution in the range 0, 2 and the same for D but in the range 0, 1 . model function growth gom fun t meas σ W ~ InverseGamma W0 ~ LogNormal μ ~ LogNormal D ~ LogNormal u0 gom W W0 params gom μ μ, D D oprob gom ODEProblem growth gom, u0 gom, tspan, params gom osol gom solve oprob gom, Tsit5 , saveat t meas W s ~ MvNormal osol gom W , σ W^2 I end md\"\"\" Provide the time measurements to the defined function this results in the Turing model and instantly condition the Turing model with the measurements of W \"\"\" growth gom cond mod growth gom fun t meas | W s W meas, md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and D . Do this now with `MAP` method and Nelder Mead. Store the optimization results in `results gom map`. \"\"\" results gom mle optimize growth gom cond mod, MLE , NelderMead md\"\"\" Visualize a summary of the optimized parameters. \"\"\" coeftable results gom mle md\"\"\" Get the optimized values and assign them to `W0 opt gom`, `μ opt gom` and `D opt gom`. \"\"\" W0 opt gom coef results gom mle W0 μ opt gom coef results gom mle μ D opt gom coef results gom mle D md\"\"\" Make a plot of W simulated with the optimized initial condition and parameter values. \"\"\" md\"\"\" Set up initial condition with optimized initial condition \"\"\" u0 opt gom W W0 opt gom md\"\"\" Set up parameter values with optimized parameter values \"\"\" params opt gom μ μ opt gom, D D opt gom md\"\"\" Create an ODEProblem and solve it. Use the solver `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt gom ODEProblem growth gom, u0 opt gom, tspan, params opt gom osol opt gom solve oprob opt gom, Tsit5 , saveat 0.5 md\"\"\" Finally, we plot W simulated with the optimized initial value and parameter values together with the measured data that was used to find the optimized values. \"\"\" Uncomment and complete the instruction begin plot missing missing missing title \"Comparison logistic vs. exponential growth\" end L gom missing k gom missing AIC gom missing BIC gom missing AIC log, AIC exp, AIC gom BIC log, BIC exp, BIC gom md\"\"\" question Draw your conclusions. \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" You can use the following graph with all the information calculated so far for your conclusions. \"\"\" plot bar 1 3, AIC log, AIC exp, AIC gom , title \"AIC\", ylims 0, 50 , bar 1 3, BIC log, BIC exp, BIC gom , title \"BIC\", ylims 0, 50 , bar 1 3, L log, L exp, L gom , title \"Log probability\", ylims 20, 0 , bar 1 3, k log, k exp, k gom , title \"no. parameters\", ylims 0, 8 , xticks 1 3, \"Logistic\", \"Exponential\", \"Gompertz\" , legend none md\"\"\" Exercise 3 Calculation of the posterior probabilities \"\"\" md\"\"\" Use the above implemented function `posterior` to calculate the posterior model probabilities. \"\"\" posteriors missing posteriors md\"\"\" We can summarize all calculated criteria so far in the following table | Model | k | Log L | AIC | BIC | P M i\\|D | | | | | | | | | Logistic | k log | round L log digits 3 | round AIC log digits 3 | round BIC log digits 3 | posteriors 1 | | Exponential | k exp | round L exp digits 3 | round AIC exp digits 3 | round BIC exp digits 3 | posteriors 2 | | Gompertz | k gom | round L gom digits 3 | round AIC gom digits 3 | round BIC gom digits 3 | posteriors 3 | \"\"\" md\"\"\" question Draw your conclusions. Does the posterior probability give the same raking as the other criteria? \"\"\" md\" missing \" md\"\"\" Exercise 4 Comparison with least squares \"\"\" md\"\"\" Repeat below the comparison to least squares for the exponential and Gompertz models. \"\"\" begin Uncomment and complete the instruction W exp missing SSR exp missing AIC LS exp missing BIC LS exp missing end begin Uncomment and complete the instruction W gom missing SSR gom missing AIC LS gom missing BIC LS gom missing end AIC LS log, AIC LS exp, AIC LS gom BIC LS log, BIC LS exp, BIC LS gom posterior AIC LS log, AIC LS exp, AIC LS gom plot bar 1 3, AIC LS log, AIC LS exp, AIC LS gom , title \"AIC\", ylims 40, 0 , bar 1 3, BIC LS log, BIC LS exp, BIC LS gom , title \"BIC\", ylims 40, 0 , bar 1 3, SSR log, SSR exp, SSR gom , title \"SSR\", ylims 0, 8 , bar 1 3, k log, k exp, k gom , title \"no. parameters\", ylims 0, 8 , xticks 1 3, \"Logistic\", \"Exponential\", \"Gompertz\" , legend none, suptitle \" Least squares \" md\"\"\" question Draw your conclusions. Do the SSR and alternative AIC and BIC provide the same model ranking? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Additional exercises \"\"\" md\"\"\" 1. MAP estimation \"\"\" md\"\"\" We can repeat the calibration and take into account the priors to obtain the MAP estimation. \"\"\" results log map optimize growth log cond mod, MAP , NelderMead coeftable results log map md\"\"\" question How will this affect the different criteria for the model selection? How is log L compared to MLE? \"\"\" md\"\"\" 2 Watanabe Akaike information criterion WAIC \"\"\" md\"\"\" The AIC and BIC are easy to compute but do not take into account the uncertainty in the predictions for the assessment of the model. The more complex Widely Applicable Information Criterion WAIC or Watanabe Akaike information criterion takes samples from the posterior distribution and provides a measure of uncertainty for each observation, which can be used for model assessment. \"\"\" md\"\"\" We can generate new samples from the posterior distribution with MCMC. \"\"\" N 200 results log nuts sample growth log cond mod, NUTS , N plot results log nuts md\"\"\" The log pointwise predictive density lppd is the sum of the log likelihood of all observations \"\"\" lppd log sum results log nuts.value , lp md\"\"\" The second part of WAIC is the variance of the log likelihood of each observation, also called the effective number of parameters, p WAIC , considered here as a penalty term, similarly to AIC and BIC \"\"\" pWAIC log sum results log nuts.value , lp .^2 N lppd log N ^2 sum results log nuts.value , lp . lppd log N .^2 N md\"\"\" Finally, WAIC is defined as WAIC 2 \\text lppd p WAIC \"\"\" WAIC log 2 lppd log pWAIC log md\"\"\" We repeat the calculation for the exponential and Gompertz models below. \"\"\" results exp nuts sample growth exp cond mod, NUTS , N lppd exp sum results exp nuts.value , lp pWAIC exp sum results exp nuts.value , lp .^2 N lppd exp N ^2 WAIC exp 2 lppd exp pWAIC exp results gom nuts sample growth gom cond mod, NUTS , N lppd gom sum results gom nuts.value , lp pWAIC gom sum results gom nuts.value , lp .^2 N lppd gom N ^2 WAIC gom 2 lppd gom pWAIC gom WAIC log, WAIC exp, WAIC gom posterior WAIC log, WAIC exp, WAIC gom md\"\"\" question Why is the WAIC criterion significantly better for model selection despite its complexity? \"\"\" md\"\"\" References 1. https en.wikipedia.org wiki Watanabe%E2%80%93Akaike information criterion https en.wikipedia.org wiki Watanabe%E2%80%93Akaike information criterion 2. https civil.colorado.edu ~balajir CVEN6833 bayes resources RM StatRethink Bayes.pdf https civil.colorado.edu ~balajir CVEN6833 bayes resources RM StatRethink Bayes.pdf \"\"\" "},{"url":"exercises/ode_model_anaerobic_fermentation/","title":"EXTRA. ODE anaerobic fermentation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"10\" title \"EXTRA. ODE anaerobic fermentation\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of anaerobic fermentation\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using PlutoUI TableOfContents md\"\"\" Exercise Anaerobic fermentation \"\"\" md\"\"\" Part 1 \"\"\" md\"\"\" An operator at a beverage factory would like to model the anaerobic fermentation that occurs in one of his reactors. After a literature review, he finds that sucrose S is converted to ethanol E via glucose G under the action of an enzyme called invertase I from yeast using the following reaction stoichiometry all components have the unit mol\\ L^ 1 C 12 H 22 O 11 I \\xrightarrow r 1 2C 6 H 12 O 6 I C 6 H 12 O 6 \\xrightarrow r 2 2C 2 H 5 OH 2CO 2 The operator knows that both reactions are carried out in isothermal conditions in a reactor with volume V L . The operator knows from literature that the reaction rate r 1 is first order in both sucrose and invertase and with specific reaction rate k 1 . The reaction rate r 2 is second order with respect to glucose with specific reaction rate k 2 . Additionally, the reaction is inhibited by ethanol itself according to \\cfrac K E K where K represents the ethanol concentration at which r 2 achieves half of its maximum reaction rate. The initial concentrations and the parameter values are summarised in the following tables | S 0 | I 0 | G 0 | E 0 | CO 2,0 | | | | | | | | 0.04 | 0.02 | 0.00 | 0.01 | 0.00 | | k 1 | k 2 | K | | | | | | 0.40 | 0.65 | 0.50 | \"\"\" md\" Implementation of the system \" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of S , I , G , E and CO 2 during 1440\\ min 24\\ h . Name it `anaerobic fermentation1`. Tips For the reaction with reaction rate r 2 , in order to have a second order reaction with respect to glucose, you need to double the stoichiometric coefficients, i.e., you reaction should be 2G \\rightarrow 4E 4CO 2 . For the inhibition factor \\cfrac K E K you can use the function `mmr ..., ..., ... ` \"\"\" Uncomment and complete the instruction anaerobic fermentation1 reaction network begin species missing missing missing end md\"\"\" Check out the species. \"\"\" missing Uncomment and complete the instruction md\"\"\" Convert the system to a symbolic differential equation model and inspect your differential equations. \"\"\" osys1 missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u01` with the initial conditions \"\"\" u01 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan1 missing Uncomment and complete the instruction md\"\"\" Initialize a vector `params1` with the parameter values \"\"\" params1 missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob1` \"\"\" oprob1 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol1` \"\"\" osol1 missing Uncomment and complete the instruction md\"\"\" Plot the results. Use a line width of 2 `linewidth ...` . \"\"\" missing Uncomment and complete the instruction md\"\"\" Interprete the results. Try to come up with an answer to the following questions 1. Why is the concentration of invertase I constant zero, and the concentration of sucrose S becoming zero? \"\"\" md\" Answer missing\" md\"\"\" 2. Try to explain the peak in the glucose G concentration. \"\"\" md\" Answer missing\" md\"\"\" 3. Why is the difference in ethanol E and CO 2 concentration constant? \"\"\" md\" Answer missing\" md\"\"\" Part 2 \"\"\" md\"\"\" Additionally, sucrose and glucose are added at a flow rate Q in , L\\ min^ 1 and respective concentrations S in and G in . The same flow rate is removed from the reactor but the invertase I stays in the reactor. Now the volume V of the reactor will matter. Furthermore, the invertase enzyme degrades at a rate d 0.003\\ min^ 1 . The additional parameter values are summarised in the following table | Q in | V | S in | G in | d | | | | | | | | 1.00 | 100 | 0.12 | 0.05 | 0.003 | \"\"\" md\"\"\" Make a copy of the content of the previous reaction network object and complement it with the new information. Name it `anaerobic fermentation2`. \"\"\" Uncomment and complete the instruction anaerobic fermentation2 reaction network begin species missing parameters missing missing ... missing end md\"\"\" Convert the system to a symbolic differential equation model and inspect your differential equations. \"\"\" osys2 missing Uncomment and complete the instruction md\"\"\" Make an exact copy of `u01` and rename it to `u02` with the initial conditions \"\"\" u02 missing Uncomment and complete the instruction md\"\"\" Make an exact copy of `tspan1` and rename it to `tspan2` \"\"\" tspan2 missing Uncomment and complete the instruction md\"\"\" Make a copy of `params1`, rename it to `params2` and supplement it with the new parameter values \"\"\" param2 missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol2` \"\"\" osol2 missing Uncomment and complete the instruction md\"\"\" Plot the results. Use a line width of 2 `linewidth ...` . If you only want to see the curves for, e.g., E , S and G , you can use the option `idxs E, S, G ` in the `plot` command. \"\"\" missing Uncomment and complete the instruction md\"\"\" Interprete the results. \"\"\" md\" Answer missing\" md\"\"\" Check out the last concentrations at the end time for each of the species. Tips You can see the last values of all species with `osol2.u end ` If later you need all last values separately, you can access the last value of S with `osol2 S end ` and then you can put everything on one line separating the values with comma's. \"\"\" missing Uncomment and complete the instruction osol2 S end , ..., ..., ..., ... Uncomment and complete the instruction md\"\"\" Create a vector named `u guess` in the same way as `u02`, but now with the end values of the species. \"\"\" u guess2 missing Uncomment and complete the instruction md\"\"\" Calculate the steady state values of the species \"\"\" Sw2, Iw2, Gw2, Ew2, CO2w2 missing Uncomment and complete the instruction md\"\"\" Check ou the steady states \"\"\" missing md\"\"\" Part 3 \"\"\" md\"\"\" We now want to keep a relatively high production of ethanol. Therefore, if the invertase decreases to 0.008 , then the invertase is instantaneously renewed to the initial concentration of 0.02 . Apply the change in the invertase concentration using a continuous event. \"\"\" md\"\"\" Create the correct condition. \"\"\" condition3 missing Uncomment and complete the instruction md\"\"\" Include the condition into the reaction network model . \"\"\" Uncomment and complete the instruction named anaerobic fermentation3 c missing md\"\"\" Complete the reaction network model . \"\"\" Uncomment and complete the instruction anaerobic fermentation3 c com missing md\"\"\" Create a new ODE problem. \"\"\" oprob3 missing Uncomment and complete the instruction md\"\"\" Solve the new ODE problem. Make a `deepcopy`, use `Tsit5 ` and `saveat 0.5`. \"\"\" osol3 missing md\"\"\" Plot the results. \"\"\" missing md\"\"\" Interprete the results. \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_birth_death/","title":"1. ODE birth rate","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"4\" title \"1. ODE birth rate\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of birth rate\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Exercise Simple birth death model for mice In a simple birth death model for mice, the birth rate of mice represents the rate at which new individuals are added to the population through reproduction. This rate is influenced by factors such as the number of reproductive females, their fertility, and the frequency of reproduction cycles. Conversely, the death rate reflects the rate at which individuals are removed from the population due to mortality factors such as predation, disease, and environmental stressors. Together, these rates interact dynamically to shape the population dynamics of mice in their natural habitat. Denote the number of mice by X , the average birth rate by b mice day , and the average death rate by d day^ 1 . Hence, assume for this overly simplified model, that the birth of mice is a zeroth order process and that the death of mice is a first order process. \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of X with time. Name it `birth death`. \"\"\" Uncomment and complete the instruction birth death reaction network begin missing end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Part 1 Simulate the evolution of the number of mice per day during 10 years starting off with 2 mice. Assume that per year 25 pups are born. Suppose the death rate to be 0.0015\\ day^ 1 . \"\"\" md\"\"\" First, calculate the birth rate in mice day . \"\"\" missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `param` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following questions 1. What is the approximate steady state value for X ? \"\"\" md\" Answer missing\" md\"\"\" Part 2 Suppose that at t 3\\ years the death rate of the mice population increases by 50\\,\\% due to a new predator species in the area. Use the same initial condition, timespan and parameter values. Simulate the evolution of the number of mice. \"\"\" md\"\"\" Create the condition . Store it in `condition2` \"\"\" condition2 missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `birth death2`. \"\"\" named birth death2 missing Uncomment and complete the instruction md\"\"\" Complete the new reaction system . Name it `birth death2 com`. \"\"\" birth death2 com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol2` \"\"\" osol2 missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in the death rate? \"\"\" md\" Answer missing\" md\"\"\" 2. If the death rate increases at a different timepoint, would you reach the same steady state value for X ? Explain. \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_catalyst_intro/","title":"1. ODE catalyst intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"2\" title \"1. ODE catalyst intro\" date \"2025 02 07\" tags \"exercises\" description \"ODE catalyst intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off return quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Introduction to Catalyst ODE \"\"\" md\"\"\" Catalyst.jl is a symbolic modelling package for construction, analysis and high performance simulation of chemical reaction networks. Catalyst defines symbolic ReactionSystems, which can be created programmatically or easily specified using Catalyst's D omain S pecific L anguage DSL . \"\"\" md\"\"\" This notebook describes the syntax for building chemical reaction network models using Catalyst's DSL. We will illustrate this by implementing and solving an infection model by means of ODE O rdinary D ifferential E quations . \"\"\" md\"\"\" The infection model \"\"\" md\"\"\" It is important to model the outbreak of infectious diseases in order to devise appropriate measures to avoid global epidemics. In this exercise, we consider an isolated group of people in which a viral disease is spreading. We use an infection model similar to the SIR model but slightly extended for this purpose. We are interested in the evolution of the number of susceptible S , infected I , deceased D and resistant R persons.\\ We make the following assumptions 1. Transmission of the disease from an infected person to a susceptible person takes place through direct contact. The chance of any two inhabitants of the group coming into contact with each other is \\beta , and the probability of infection after contact between an infected and a susceptible person is \\alpha . 2. Note that the above assumption implicitly states that the probability of two neighbours coming into contact with each other is as high as the probability of two people living at two extremes of the territory coming into contact with each other. 3. A person leaves the infection period at a rate r hence, a person is contagious for an average of 1 r days. Without appropriate medication, a fraction m of infected people die and a fraction 1 m of infected people acquire immunity after healing. 4. We assume that there is no migration in or out of the population. \"\"\" md\" Below we summarize the relevant variables species \" md\"\"\" | Variable | Unit | Meaning | | | | | | ``S`` | persons | number of susceptible persons | | ``I`` | persons | number of infected persons | | ``D`` | persons | number of deceased persons | | ``R`` | persons | number of resistant persons | \"\"\" md\" Below we summarize the parameters \" md\"\"\" | Variable | Unit | Meaning | | | | | | ``\\alpha`` | ``\\frac persons contact `` | chances of getting infected after contact | | ``\\beta`` | ``\\frac contact persons^2\\,day `` | contact rate | | ``r`` | ``\\frac 1 day `` | rate of leaving infection period | | ``m`` | ``\\frac person person `` | fraction of persons deceasing | | ``1 m`` | ``\\frac person person `` | fraction of persons becoming resistant | \"\"\" md\"\"\" Hence, the infection rate is ``\\alpha \\beta``. This means that a susceptible person meets an infected person ``S I``, this will result in ``2I`` at a rate ``\\alpha \\beta``. Futhermore, an infected person ``I`` will either become a deceased person ``D`` at a rate ``m r`` or become a resistant person ``R`` at rate `` 1 m r`` \"\"\" md\"\"\" Our infection model has three reaction events 1. Infection, where a susceptible persons meets an infected persons and also becomes infected. 2. Deceasing, where an infected person die. 3. Recovery, where an infected person recovers and becomes resitant. \"\"\" md\"\"\" Each reaction is also associated with a specific rate 1. ``\\alpha \\beta``, the infection rate. 2. ``m r``, the death rate. 3. `` 1 m r``, the recovery rate. \"\"\" md\"\"\" Hence, the following infection reactions are S I \\xrightarrow \\alpha \\beta 2I I \\xrightarrow mr D I \\xrightarrow 1 m r R \"\"\" md\"\"\" We are going to implement this system of reactions using Catalyst. \"\"\" md\"\"\" We first load the Catalyst package, which is required for the code in this introduction to run \"\"\" md\"\"\" Implementation of the system The following code creates a so called reaction network object , that we have named `infection model`, that implements the aforementioned reactions . \"\"\" infection model reaction network begin α β, S I 2I r m, I D r 1 m , I R end md\" Each line between `begin` and `end` corresponds to a reaction . Each reaction consists of a reaction rate the expression on the left hand side of `,` , a set of substrates the expression in between `,` and ` ` , a set of products the expression on the right hand side of ` ` . The substrates and the products may contain one or more reactants, separated by ` `. \" md\"\"\" hint \"Hint\" The Greek letters can be visualized by typing a backslash followed by the name of the Greek letter and then the TAB key. For example `\\alpha` followed by the TAB key results in in a list where you can choose `α`. \"\"\" md\" The reaction model is stored in the variable `infection model` the variable name can be chosen freely . It is a symbolic representation of the chemical network. \" md\"\"\" You can get a list of the different reaction species with the command `species` \"\"\" species infection model md\"\"\" To get a list of the reaction parameters , you can use the command `parameters` \"\"\" parameters infection model md\"\"\" important \"Important\" You can also get the different species and parameters using ` unpack` followed by comma separated species and or parameter names followed by the equal sign and the name of the reaction network model. For example \"\"\" unpack S, I, D, R infection model md\"\"\" The reaction model can be converted to a symbolic differential equation model via \"\"\" osys convert ODESystem, infection model md\"\"\" Note that the model equations are essencially \\cfrac dS t dt \\alpha \\beta S t I t \\cfrac dI t dt \\alpha \\beta S t I t r I t \\cfrac dD t dt m r I t \\cfrac dR t dt 1 m r I t \"\"\" md\"\"\" You can get a list of the differential equations with the command `equations` \"\"\" equations osys md\"\"\" To get a list of the state variables, you can use the command `unknowns` \"\"\" unknowns osys md\"\"\" To get a list of the parameters, you can use the command `parameters` \"\"\" parameters osys md\"\"\" Simulating the system as an ODE problem We first need to load the Differential and Plot package, which is required for simulating the system and plotting the results. \"\"\" md\"\"\" Now we wish to simulate our model. To do this, we need to provide some the following information Initial conditions for the state variables S , I , D and R . The parameter values for \\alpha , \\beta , r and m . The timespan, which is the timeframe over which we wish to run the simulation. Assume in this example that there are 10\\,000\\,000 people in the country, and that initially 1\\,000 person are infected. Hence, I 0 1\\,000 , S 0 10\\,000\\,000 I 0 9\\,999\\,000 , D 0 0 and R 0 0 .\\ Furthermore, we take the following values for the parameters \\alpha 0.08\\ person contact , \\beta 10^ 6 \\ contact person^2\\,day , r 0.2\\ day^ 1 i.e. a person is contagious for an average of 5\\ days and m 0.4 . The following table summarizes the above values |Initial conditions |Parameters | | | | | S 0 9\\,999\\,000 | \\alpha 0.08 | | I 0 1\\,000 | \\beta 10^ 6 | | D 0 0 | r 0.2 | | R 0 0 | m 0.4 | Finally, we want to run our simulation from day 0 till day 90 . \"\"\" md\"\"\" Setting initial conditions \"\"\" md\"\"\" The initial conditions are given as a Vector . This is a type which collects several different values. To declare a vector, the values are specific within brackets, ` `, and separated by `,`. Since we have four species, the vector holds four elements. E.g., we set the value of I using the ` I 1` syntax. Here, we first denote the name of the species with a colon ` ` pre appended , next follows a ` ` and then the value of `I`.\\ The vector holding the initial conditions for S , I , D and R can be created in the following way \"\"\" u0 S 9 999 000.0, I 1 000.0, D 0.0, R 0.0 md\" Note that the order of the vector elements doesn't matter, because the initial values of each of the species is indicated using its variable name. \" md\"\"\" Setting the timespan \"\"\" md\"\"\" The timespan sets the time point at which we start the simulation typically `0.0` is used and the final time point of the simulation. These are combined into a two valued Tuple. Tuples are similar to vectors, but are enclosed by ` ` and not ` `. Again, we will let both time points be decimal valued. \"\"\" tspan 0.0, 90.0 md\"\"\" Setting parameter values \"\"\" md\"\"\" Similarly, the parameters values are also given as a vector. We have four parameters, hence, the parameter vector will also contain four elements. We use a similar notation for setting the parameter values as the initial condition first the colon, then the parameter name, then an arrow, then the value . \"\"\" params α 0.08, β 1.0e 6, r 0.2, m 0.4 md\"\"\" Creating an ODEProblem \"\"\" md\" Next, before we can simulate our model, we bundle all the required information together in a so called ODEProblem . Note that the order in which the input the model name, the initial condition, the timespan, and the parameter values is provided to the ODEProblem matters Here, we save our ODEProblem in the `oprob` variable. \" oprob ODEProblem infection model, u0, tspan, params md\"\"\" Solving the ODEProblem \"\"\" md\"\"\" We can now simulate our model. We do this by providing the ODEProblem to the `solve` function. There are some examples https docs.sciml.ai DiffEqDocs stable getting started online on how to solve ODE problems with the DifferentialEquations.jl package. We save the output to the `sol` variable. Optionally, one can provide a solver method https docs.sciml.ai DiffEqDocs stable solvers ode solve Full List of Methods e.g., `Tsit5` , and the time stepsize with `saveat` . \"\"\" https docs.sciml.ai DiffEqDocs stable solvers ode solve Full List of Methods osol solve oprob osol solve oprob, Tsit5 , saveat 0.5 md\"\"\" Note that at the different time points the variables values in the solution are decimal numbers and not integer numbers , despite the fact that we are applying the model to individuals. This is inherent to using an ODE approach. Later on, we will see how we can discretise the problem, and hence, work on the level of individual infections reactions .\\ Futhermore, note that executing the `solve` command at different occasions with an ODE problem will never modify the solution because ODE problems are deterministic . This will become different when simulating the individual infection reaction events by means of a stochastic random algorithm. \"\"\" md\"\"\" Plotting the results \"\"\" md\"\"\" Finally, we can plot the solution through the plot function. \"\"\" plot osol md\"\"\" If you want to plot less species, like for example just S and I , you can specify this with the option `idxs S, I ` notice the brackets in the plot function. \"\"\" plot osol, idxs S, I brackets md\"\"\" If you want a fase plot of for example just I versus S , you can specify this with the option `idxs S, I ` notice the parentheses in the plot function. You can indicate the S and I axes with the additional options `xlab \"S\"` and `ylab \"I\"`. \"\"\" plot osol, idxs S, I , xlab \"S\", ylab \"I\" parentheses md\"\"\" If you want to see the final values of S , I , D and R , type \"\"\" osol.u end md\"\"\" If you want the vector of, e.g., S values separately, type \"\"\" osol S md\"\"\" If you want the last value in the S vector, type \"\"\" osol S end md\"\"\" If you want the time vector separately, type \"\"\" osol.t md\" More advanced examples \" md\"\"\" In Example 1 we will show you one way of how you could analyze the simulation results for a limited range of parameter values. In Examples 2 and 3 we will apply some new concepts, namely discrete and continuous events. The latter will basically affect, e.g., one or more parameter values or state variables during the solving process based on one or more conditions also called events . These conditions can be either time or state variable related A time related condition is a vector of one or more timepoint s for which the value of one or more parameter s or state variable s need to be altered. We refer to them as discrete events . A state variable related condition is usually a condition for a certain value of a state variable. We will refer to them as continuous events . \"\"\" md\"\"\" Important remark \\ You may have noticed that while using the Pluto notebooks, when you change the value of some variable e.g., a parameter or an initial condition that your results plots will subsequently and automatically be altered based on the currect variable values in memory. In some cases this can be advantageous, in others not. For the latter reason, in this notebook, we will use slightly different variable names for some variables in order not to alter other results. \"\"\" md\"\"\" Example 1 Influence of r Influence of the duration of infection 1 r for average infection periods of between 10 , days and 1 day contagious r between 0.1 and 1.0 , step 0.1 , default value 0.1 . \"\"\" md\"\"\" We will create a slider for the r values between 0.1 and 1.0 , stepsize 0.1 , default value 0.1 . \"\"\" bind r Slider 0.1 0.1 1, default 0.1, show value true md\"\"\" We will create een new parameter value vector, ODE problem and solution object by putting `1` at the end of the corresponding variable names. In that way, the previous simulation results will be unaffected The model, the initial conditions and the timespan are identical as before. In there we also use the variable `r` coupled to the slider. \"\"\" params1 α 0.08, β 1.0e 6, r r, m 0.4 put semi colon at end of instruction to avoid seeing its output. oprob1 ODEProblem infection model, u0, tspan, params1 osol1 solve oprob1, Tsit5 , saveat 0.5 plot osol1, ylim 0, 1e7 md\"\"\" Now, change the value of r in the `param1` vector and analyze the effect in the plot. \"\"\" md\"\"\" Try to interpret the results yourself. Ask yourself the following questions 1. What are the trends in the results obtained? \"\"\" md\" Answer missing\" md\"\"\"2. How can this be explained from the model structure?\"\"\" md\" Answer missing\" md\"\"\" Example 2 Discrete Event Suppose that regulations are such that on day 14, people need to reduce their contacts by 50%. Hence, this means that the parameter value \\beta needs to be divided by a factor of 2 at timepoint 14. In order to realize that we need to now the order of the parameters in the model because we will need to address the value of \\beta by means of an index. \"\"\" md\"\"\" We need to state that the parameter \\beta needs to be reduce by 50\\% at time t 14\\,days . We put this in a condition named `condition2`. \"\"\" condition2 14.0 infection model.β ~ infection model.β 2 md\"\"\" The discrete time event needs to be included in our model. \"\"\" named infection model2 ReactionSystem equations infection model , discrete events condition2 md\"\"\" After that, we need to complete our reaction network model . \"\"\" infection model2 com complete infection model2 md\"\"\" Then we need to create a new ODE problem. \"\"\" oprob2 ODEProblem infection model2 com, u0, tspan, params md\"\"\" Finally, the ODE problem can be solved. Notice that you need to make a deepcopy of the ODE problem, because otherwise changes to the parameter \\beta will remain after the first call to `solve`. \"\"\" osol2 solve deepcopy oprob2 , Tsit5 , saveat 0.5 md\"\"\" Now we can plot the results. \"\"\" plot osol2 md\"\"\" If you want to see the final values of S , I , D and R , type \"\"\" osol2.u end md\"\"\" Try to interpret the results yourself. Ask yourself the following questions 1. What are the trends in the results obtained? \"\"\" md\" Answer missing\" md\"\"\" 2. How much less casualties are there compared to not altering the contact rate? \"\"\" md\" Answer missing\" md\"\"\" Example 3 Continuous Event Suppose that when the number of infected individuals reaches 1\\,000\\,000 , then 999\\,000 of them are promptly put into isolation or removed from the population . Hence, a 1000 individuals remain infected at some point. \"\"\" md\"\"\" Normally in a continuous event the value of one or more species can be changed when a certain condition is met. In our specific case we want the change in the species happening only once So, if you want that the continuous event ''when I reaches 10^6 then 999000 is subtrated from I '' happens only once, then we need to include a ficticious new species in our reaction network model . We will call this ficticious species `pwc` a short for p roceed w ith c ondition and we set it default to `true`. \"\"\" infection model3 reaction network begin species pwc t true α β, S I 2I r m, I D r 1 m , I R end species infection model3 md\"\"\" We create the condition in the following way. When `pwc` is true then I will be changed and also `pwc` will become `false`, so that the condition happens only once. We assume hereby that I will never reach 0 \"\"\" condition3 infection model3.I ~ 1e6 infection model3.pwc infection model3.I ~ infection model3.I 0.999e6, infection model3.pwc ~ false md\"\"\" The continuous event needs to be included in our model. \"\"\" named infection model3 c ReactionSystem equations infection model3 , continuous events condition3 md\"\"\" After that, we need to complete our reaction network model . \"\"\" infection model3 c com complete infection model3 c md\"\"\" Then we need to create a new ODE problem. \"\"\" oprob3 ODEProblem infection model3 c com, u0, tspan, params md\"\"\" Finally, the ODE problem can be solved. Notice that you need to make a deepcopy of the ODE problem again. \"\"\" osol3 solve deepcopy oprob3 , Tsit5 , saveat 0.1 md\"\"\" Now we can plot the results. \"\"\" plot osol3 md\"\"\" If you want to see the final values of S , I , D , R and `pwc`, type \"\"\" osol3.u end md\"\"\" Try to interpret the results yourself. Ask yourself the following questions 1. What are the trends in the results obtained? \"\"\" md\" Answer missing\" md\"\"\" 2. How much less casualties are there compared to not putting 999\\,000 individuals into isolation? Hint you also need to take into account the casualties in the 999\\,000 individuals that had been put into isolation. \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_fermenter_firstorder/","title":"EXTRA. ODE fermentor first order","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"6\" title \"EXTRA. ODE fermentor first order\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of a first order fermentor\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Exercise Fermenter First order kinetics \"\"\" md\"\"\" In a fermenter reactor biomass X grows on substrate S . The reactor is fed with a inlet flow rate Q in L h , which consist of a manipulable input concentration of substrate S in g L . Inside the reactor, biomass, with a concentration of X g L , is produced through first order kinetics first order in S \\begin eqnarray % S \\xrightarrow \\quad\\quad \\beta Y \\, X S \\xrightarrow \\quad\\quad \\text r Y \\, X \\quad\\quad\\quad\\quad r \\beta \\, S \\end eqnarray with \\beta h^ 1 the reaction rate constant, and Y gX gS the yield coefficient which is defined here by the amount of produced biomass by consumption of one unit of substrate. Futhermore, the reactor is drained with an outlet flow Q L h , which consist of the current concentrations of substrate S g L and biomass X g L inside the reactor. The volume V L of the reactor content is kept constant by setting Q in Q . \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of substrate S and biomass X with time. Name it `fermenter firstorder`. \"\"\" Uncomment and complete the instruction fermenter firstorder reaction network begin missing Y X is created from one S at a rate β missing S is created at a rate Q V Sin missing S and X are degraded at a rate Q V S end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" The parameter values are \\beta 0.98 , Y 0.80 , Q 2.0 , V 40.0 and S in 2.2\\ g L . With the latter values, the fermenter reactor is in steady state operation with concentrations for substrate S 0.1068\\ g L and biomass X 1.6746\\ g L . Suppose that at timepoint t 20\\ h , the concentration of substrate in the inlet flow cf. S in is suddently increased to 3.4\\ g L . Simulate the evolution of S and X during 120 hours. \"\"\" md\"\"\" Initialize a vector `u₀` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `params` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Create the condition that contains the timepoint for the sudden change in S in . Store it in `condition` \"\"\" condition missing md\"\"\" Make a new reaction system where the discrete event is included. Name it `fermenter firstorder2`. \"\"\" named fermenter firstorder c missing md\"\"\" Complete the new reaction system . Name it `fermenter firstorder c com`. \"\"\" fermenter firstorder c com missing md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol` \"\"\" osol missing md\"\"\" Plot the results \"\"\" missing md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in S in ? \"\"\" md\" Answer missing\" md\"\"\" 2. Can you argue, by means of reasoning or by determining and analyzing the operating point, why the increase of X is larger than the increase of S ? \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_fermenter_monod/","title":"EXTRA. ODE fermentor monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"7\" title \"EXTRA. ODE fermentor monod\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of a fermentor monod\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots md\" Exercise Fermenter Monod kinetics \" md\"\"\" In a fermenter reactor biomass grows on substrate. The reactor is fed with a inlet flow rate Q in L h , which consist of a manipulable input concentration of substrate S in g L . Inside the reactor, biomass, with a concentration of X g L , is produced through Monod kinetics \\begin eqnarray %S \\xrightarrow \\quad\\quad \\beta Y \\, X S \\xrightarrow \\quad\\quad r Y \\, X \\quad\\quad\\quad\\quad r \\mu \\, X \\end eqnarray where \\mu \\mu max \\, \\cfrac S S K s is called the specific growth rate h^ 1 . Therein, \\mu max is the maximum speficic growth rate, and K s g L is the so called half velocity constant i.e. the value of S when \\mu \\mu max 0.5 . Futhermore, Y gX gS is the yield coefficient which is defined here by the amount of produced biomass by consumption of one unit of substrate. The reactor is drained with an outlet flow Q L h , which consist of the current concentrations of substrate S g L and biomass X g L inside the reactor. The volume V L of the reactor content is kept constant by setting Q in Q . \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of substrate S and biomass X with time. Name it `fermenter monod`. Tip The specific growth rate \\mu \\mu max \\, \\cfrac S S K s can be implemented with `mm S, μmax, Ks `. The function `mm` stands for the Michaelis Menten kinetics, whcih is equivalent to Monod kinetics. \"\"\" fermenter monod reaction network begin ... Y X is created from one S at a rate mm S, μmax, Ks X ... S is created at a rate Q V Sin ... S and X are degraded at a rate Q V S end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. Keep in mind that `mm S, μmax, Ks ` stands for \\mu max \\, \\cfrac S S K s . \"\"\" osys missing md\"\"\" The parameter values are \\mu max 0.40 , K s 0.015 , Y 0.67 , Q 2.0 , V 40.0 and S in 0.02\\ g L . Suppose that at t 0\\ h no substrate S is present in the reactor but that there is initially some biomass with a concetration of 0.0005\\ g L . Simulate the evolution of S and X during 200 hours. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `param` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Part 1 Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol1` \"\"\" osol1 missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing Uncomment and complete the instruction md\"\"\" Inspect the final values in both the S and X vector.\\ Tip use something like ` osol1 ... ... , osol1 ... ... ` \"\"\" osol1 ... ... , osol1 ... ... Uncomment and complete the instruction md\"\"\" We will now show you how to determine the steady state values for S and X under the current conditions cf. current initial values and current parameter values . \"\"\" md\"\"\" First, we initialize a vector `u guess1` with the final values for S and X \"\"\" u guess1 missing md\"\"\" Then we make a so called SteadyStateProblem based on the ODEProblem but now with `u guess1` as initial conditions Finally we use `solve` to solve the steady state problem. The outputs are the steady state values for S and X which we have denoted as `Seq1` and `Xeq1`. \"\"\" Seq1, Xeq1 missing md\"\"\" Next, we can just inspect these values \"\"\" missing md\"\"\" Interpret the results. Ask yourself the following questions 1. Explain why S first increases and then decreases while X only increases during the first 50 hours. \"\"\" md\" Answer missing\" md\"\"\" 2. What are the steady state values of S and X . \"\"\" md\" Answer missing\" md\"\"\" Part 2 Suppose that the substrate inlet concentration S in suddenly increases to 0.022\\ g L at t 100\\ h . Simulate the evolution of S and X . \"\"\" md\"\"\" Create the condition that contains the timepoint for the sudden change in S in . Store it in `condition2` \"\"\" condition2 missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `fermenter monod2`. \"\"\" named fermenter monod2 missing Uncomment and complete the instruction md\"\"\" Complete the new reaction system . Name it `fermenter monod2 com`. \"\"\" fermenter monod2 com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol2` \"\"\" osol2 missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing md\"\"\" Calculate the state state values for S and X . \"\"\" md\"\"\" Inspect the final values in both the S and X vector.\\ Tip use something like ` osol2 ... ... , osol2 ... ... ` \"\"\" osol2 ... ... , osol2 ... ... Uncomment and complete the instruction md\"\"\" Initialize a vector `u guess2` with the final values for S and X \"\"\" u guess2 missing Uncomment and complete the instruction md\"\"\" Initialize a vector `param mod` with the parameter values. Notice that all parameter values will be the same, except the one of S in . \"\"\" params mod missing Uncomment and complete the instruction md\"\"\" Make and solve the steady state problem. Call the output values `Seq2` and `Xeq2`. \"\"\" Seq2, Xeq2 missing Uncomment and complete the instruction md\"\"\" Inspect those values. \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in S in ? \"\"\" md\" Answer missing\" md\"\"\" 2. Find the steady state values of S and X . Is the steady state value of S influenced by the increase of S in ? Show how you can deduce that from the differential equations. \"\"\" md\" Answer missing\" md\"\"\" 3. Can you explain why X increased permanently? \"\"\" md\" Answer missing\" md\"\"\" Part 3 Suppose that the inlet outlet flow Q is suddenly doubled at t 100\\ h . Simulate the evolution of S and X . \"\"\" md\"\"\" Create the condition that contains the timepoint for the sudden change in Q . Store it in `condition3` \"\"\" condition3 missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `fermenter monod3`. \"\"\" named fermenter monod3 missing Uncomment and complete the instruction md\"\"\" Complete the new reaction system . Name it `fermenter monod3 com`. \"\"\" fermenter monod3 com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob3` \"\"\" oprob3 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol3` \"\"\" osol3 missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of doubling of Q ? \"\"\" md\" Answer missing\" md\"\"\" 2. Can you argue, by means of reasoning, why S increases and X decreases? \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_infection/","title":"1. ODE infection","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"3\" title \"1. ODE infection\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of infection\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots md\" Exercises infection model \" md\"\"\" We will work here with the same infection model as in the Introdution to Catalyst revisit the concerned notebook if necessay . We shortly summarize some important aspects of the model and give a condensed version of the solution method and the examples. \"\"\" md\"\"\" The state variables | Variable | Unit | Meaning | | | | | | ``S`` | persons | number of susceptible persons | | ``I`` | persons | number of infected persons | | ``D`` | persons | number of deceased persons | | ``R`` | persons | number of recovered persons | \"\"\" md\"\"\" The parameters | Variable | Unit | Meaning | | | | | | ``\\alpha`` | ``\\frac persons contact `` | chances of getting infected after contact | | ``\\beta`` | ``\\frac contact persons^2\\,day `` | contact rate | | ``r`` | ``\\frac 1 day `` | rate of leaving infection period | | ``m`` | ``\\frac person person `` | fraction of persons deceasing | | ``1 m`` | ``\\frac person person `` | fraction of persons becoming resistant | \"\"\" md\"\"\" The infection model has three reaction events Infection, where a susceptible persons meets an infected persons and also becomes infected. The infection rate is \\alpha \\beta . Deceasing, where an infected person die. The death rate is m r . Recovery, where an infected person recovers. The recovery rate 1 m r . \"\"\" md\"\"\" The infection reactions are S I \\xrightarrow \\alpha \\beta 2I I \\xrightarrow mr D I \\xrightarrow 1 m r R \"\"\" md\"\"\" Load the Catalyst package \"\"\" md\"\"\" Examples \"\"\" md\" Implementation of the system \" infection model reaction network begin α β, S I 2I r m, I D r 1 m , I R end md\"\"\" The species \"\"\" species infection model md\"\"\" Alternatively \"\"\" unpack S, I, D, R infection model md\"\"\" The parameters \"\"\" parameters infection model md\" Convert the reaction model if you want to see the symbolic differential equation model \" osys convert ODESystem, infection model md\"\"\" Getting a list of the differential equations, the state variables and the parameters \"\"\" equations osys unknowns osys parameters osys md\"\"\" Simulating the system as an ODE problem Load the packages Differential and Plot \"\"\" md\"\"\" Setting initial conditions, timespan and parameter values \"\"\" u0 S 9 999 000.0, I 1 000.0, D 0.0, R 0.0 tspan 0.0, 90.0 params α 0.08, β 1.0e 6, r 0.2, m 0.4 md\"\"\" Creating and solving the ODEProblem and plotting results \"\"\" oprob ODEProblem infection model, u0, tspan, params osol solve oprob, Tsit5 , saveat 0.5 plot osol plot osol, idxs S, I only S and I plot osol, idxs S, I , xlab \"S\", ylab \"I\" fase plot I vs S osol.u end md\"\"\" Example 1 Influence of r Influence of the duration of infection 1 r for average infection periods of between 10 , days and 1 day contagious r between 0.1 and 1.0 , step 0.1 , default value 0.1 . \"\"\" bind r Slider 0.1 0.1 1, default 0.1, show value true params1 α 0.08, β 1.0e 6, r r, m 0.4 oprob1 ODEProblem infection model, u0, tspan, params1 osol1 solve oprob1, Tsit5 , saveat 0.5 plot osol1, ylim 0, 1e7 md\"\"\" Change the value of r in the `params1` vector to visualize the effect in the plot. \"\"\" md\"\"\" Example 2 Discrete event Suppose that regulations are such that on day 14, people need to reduce their contacts by 50%. \"\"\" condition2 14.0 infection model.β ~ infection model.β 2 named infection model2 ReactionSystem equations infection model , discrete events condition2 infection model2 com complete infection model2 oprob2 ODEProblem infection model2 com, u0, tspan, params osol2 solve deepcopy oprob2 , Tsit5 , saveat 0.5 plot osol2 osol2.u end md\"\"\" Example 3 Continuous event Suppose that when the number of infected individuals reaches 1\\,000\\,000 , then 999\\,000 of them are promptly put into isolation or removed from the population . \"\"\" infection model3 reaction network begin species pwc t true α β, S I 2I r m, I D r 1 m , I R end condition3 infection model3.I ~ 1e6 infection model3.pwc infection model3.I ~ infection model3.I 0.999e6, infection model3.pwc ~ false named infection model3 c ReactionSystem equations infection model3 , continuous events condition3 infection model3 c com complete infection model3 c oprob3 ODEProblem infection model3 c com, u0, tspan, params osol3 solve deepcopy oprob3 , Tsit5 , saveat 0.5 plot osol3 osol3.u end md\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 Influence of \\alpha Evaluate the effect of a decreasing risk of infection after contact with an infected person, i.e. r 0.2 , \\beta 0.1 and \\alpha between 8\\% and 20\\% . Use the same initial values and timespan as before. \"\"\" md\"\"\" Make a slider for \\alpha in the range of 0.08 and 0.20 with a step of 0.02 . Take a default value of 0.08 . \"\"\" missing Uncomment and complete the instruction md\" Initialize vector `params ex1` with parameter values \" params ex1 missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob ex1` \"\"\" oprob ex1 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem and store the solution in `osol ex1` \"\"\" osol ex1 missing Uncomment and complete the instruction md\"\"\" Plot the solutions \"\"\" missing Uncomment and complete the instruction md\"\"\" Change the value of \\alpha in the `params ex1` vector to visualize the effect in the plot. \"\"\" md\"\"\" Try to interpret the results yourself. Ask yourself the following questions 1. What are the trends in the obtained results? \"\"\" md\" Answer missing\" md\"\"\" 2. How can this be explained from the model structure? \"\"\" md\" Answer missing\" md\"\"\" Exercise 2 Administration of medicinal products Scientists have developed a medicine that heals sick people and makes them immune to the disease. After administering medication, the infection duration is reduced to two days. All treated patients heal and acquire immunity to the virus. The model will have to be extended with two additional parameters. Parameter b the fraction of infected persons undergoing treatment. Parameter h the rate at which the infected persons treated are no longer contagious day^ 1 . Administering the drug to a fraction of the infected individuals affects two reactions I \\rightarrow D and I \\rightarrow R , with the following assumptions The fraction of infected persons treated b has a reduced infection duration. The fraction of infected individuals not receiving treatment 1 − b still has the same duration of infection. The mortality rate m only affects the group of sick people who were not given any medication. All treated individuals recover. A fraction of the untreated individuals also heals. Check the effect on the epidemic when 0\\% , 25\\% , 50\\% , 75\\% and 100\\% of infected individuals are treated with h 0.5 . Use the same initial conditions and timespan as before. \"\"\" md\"\"\" Set up the new reaction network model and name it `infection med` \"\"\" infection med reaction network begin Uncomment and complete the instruction α β, S I 2I ..., I D ..., ... , I R end md\"\"\" Convert to an ODE system. Check the differential equations and make sure you understand each term. \"\"\" osys ex2 missing Uncomment and complete the instruction md\"\"\" Set up parameter values \"\"\" params ex2 missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob ex2` \"\"\" oprob ex2 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem and store the solution in `osol ex2` \"\"\" osol ex2 missing Uncomment and complete the instruction md\"\"\" Plot the solutions \"\"\" missing Uncomment and complete the instruction md\"\"\" Change the value of b in the `params ex2` vector to visualize the effect in the plot. Interpret the obtained plots. \"\"\" md\"\"\" Try to answer the following questions 1. Why does the peak in the number of infected individuals shift to the right when the value of b increases? \"\"\" md\" Answer missing\" md\"\"\" 2. Why does the number of recovered individuals first rise when the value of b increases and then fall when the value of b continues to increase? \"\"\" md\" Answer missing\" md\"\"\" Check the number of fatalities \"\"\" missing md\"\"\" Exercise 3 Adding vaccination to the model Scientists have developed a vaccine that makes healthy people immediately immune to the disease. Vaccination affects several differential equations Susceptible individuals are vaccinated at a rate of v with unit day^ 1 . These persons can therefore no longer be infected. The vaccinated persons become resistant. We are going to use a vaccination rate v so that the number of fatalities is about 10 times smaller after a period of 90 days compared to those in absence of vaccination cf. Exercise 2 . The vaccination programme is launched 2 days after the outbreak of the disease. Assume that individuals are still being treated b 0.2 and h 0.5 . Extend the model obtained in the previous exercise for the launch of a vaccination campaign after the outbreak of the disease. Find out via trial and error what the minimum vaccination rate need to be so that the number of fatalities is 10 times smaller after a period of 90 days compared to those in absence of vaccination cf. Exercise 2 . Consider an initial step size in v of 0.01 and then fine tune with a step size of 0.001 . Use the same initial values and timespan as before. \"\"\" md\"\"\" Set up the new reaction network model and name it `infection med vac` \"\"\" Uncomment and complete the instruction infection med vac reaction network begin α β, S I 2I ..., I D ..., ... , I R ..., ... ... end md\"\"\" Convert to an ODE system. Check the differential equations and make sure you understand each term. \"\"\" osys ex3 missing Uncomment and complete the instruction md\"\"\" Make a slider and bind it to the variable `v`. Use a range 0.0, 0.1 , step size 0.001 and default value of 0.0 . \"\"\" missing Uncomment and complete the instruction md\"\"\" Set up parameter values \"\"\" params ex3 missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob ex3` \"\"\" oprob ex3 missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem for step wise increasing values of v and store the solution in `osol ex3 vac`. Consider an initial step size in v of 0.01 and then fine tune with a step size of 0.001 . \"\"\" osol ex3 no vac missing Uncomment and complete the instruction md\"\"\" Compare the latter with the number of fatalities when no vaccination is was available cf. Exercise 2 by setting up a condition a boolean expression return either `true` or `false` here below where the final number of fatalities with vaccination divided by 10 is compared with use larger than or smaller than the number of fatalities without vaccination \"\"\" missing Uncomment and complete the instruction md\"\"\" Once you have found the required value of v launch the vaccination programme 2 days after the outbreak. Set up the 2 day time condition and store it in `condition ex3` \"\"\" condition ex3 missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `infection med vac c`. \"\"\" named infection med vac c missing Uncomment and complete the instruction md\"\"\" Complete the new reaction system . Name it `infection med vac c com`. \"\"\" infection med vac c com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob ex3 c` \"\"\" oprob ex3 c missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol ex3`. \"\"\" osol ex3 missing Uncomment and complete the instruction md\"\"\" Plot the solutions \"\"\" missing Uncomment and complete the instruction "},{"url":"exercises/ode_model_irrigation/","title":"1. ODE irrigation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"5\" title \"1. ODE irrigation\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of irrigation\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Exercise Irrigation experiment \"\"\" md\"\"\" An irrigation experiment is carried out on a soil column consisting of two layers of soil, each with specific soil characteristics. An adjustable volume of water per unit of time, r , is irrigated evenly over the soil column, starting with 5\\ mm\\,h^ 1 mm indicates a volume of water 1\\ mm 10^ 3 \\,m^3 . After 60\\ h the added flow rate is increased to 10\\ mm\\,h^ 1 . The water falls on the upper layer and percolates to the lower layer. The relative moisture content in both layers i.e., relative to their residual moisture contents is denoted by S 1 and S 2 . Initially a moisture content of 30\\ mm is present in the upper layer cf. S 1 and of 25\\ mm in the lower layer cf. S 2 . The residual moisture content in the upper layer is S 1,res 10 \\ mm . A model description of the relative moisture content in both soil layers is given by \\begin align \\frac dS 1 dt & r\\left 1 \\cfrac S 1,res S max \\right \\cfrac r S max S 1 \\cfrac k S max S 1 \\\\ \\frac dS 2 dt & \\cfrac k S max S 1 v \\,S 2^2 \\end align Here S max 150\\ mm is the saturated water quantity for the top soil layer, k is the percolation ratio 3\\ mm\\,h^ 1 and v is the flow factor into the groundwater 10^ 3 \\ h^ 1 \\,mm^ 1 . Three measurements are made over the duration 150\\ h of the experiment The excess running water runoff r \\cfrac S 1 S 1,res S max , The underground outflow into groundwater v\\,S 2^2 , The amount of percolation to deeper soil layers \\cfrac k S max S 1 . The latter three are called observables . \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of the three afore mentioned measurements during 150\\ h . Name it `irrigation mod`. Tips You can use any kind of expression for the reaction rates. The term v \\,S 2^2 is created by the reaction `v, 2S₂ 0` \"\"\" Uncomment and complete the instruction irrigation mod reaction network begin missing ... end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `params` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Unpack the variables and parameters so that we can use them in an intuitive way to calculate to observables. For R we will take the value of 5 10 2 7.5 . \"\"\" unpack ..., ..., ..., ..., ..., ... ... Uncomment and complete the instruction md\"\"\" Create the condition that contains the timepoint for the sudden change in R . Store it in `condition` \"\"\" condition missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `irrigation mod c`. \"\"\" named irrigation mod c missing md\"\"\" Complete the new reaction system . Name it `irrigation mod c com`. \"\"\" irrigation mod c com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Calculate the observables. Take r 5.0 10.0 2 7.5 . We summarize the observables here. The excess running water runoff R \\cfrac S 1 S 1,res S max , The underground outflow into groundwater v\\,S 2^2 , The amount of percolation to deeper soil layers \\cfrac k S max S 1 . \"\"\" Uncomment and complete the instruction begin runoff ... outflow ... percolation ... end md\"\"\" Plot the runoff, outflow and percolation. \"\"\" plot osol idxs ..., ..., ... , labels \"...\" \"...\" \"...\" md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in r ? \"\"\" md\" Answer missing\" md\"\"\" 2. Argue why the outflow and the percolation tend to the same value. \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_soil_cont_plant_uptake/","title":"EXTRA. ODE soil","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"8\" title \"EXTRA. ODE soil\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of soil\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Exercise Soil Contamination with Plant Uptake \"\"\" md\"\"\" The following system of differential equations models the decay of a pollutant in soil and its uptake by plants. The variable C t in mg kg is the concentration of the pollutant in the soil and P t in mg kg is the concentration of the pollutant in the plants at time t . \\begin align \\cfrac dC dt & r k 1 C t k 2 C t P t \\\\ \\cfrac dP dt & k 2 C t P t k 3 P t \\end align The interpratation of the parameters is the following r represents the rate at which the pollutant enters the soil from external sources. k 1 is the natural degradation rate of the pollutant in the soil. k 2 is the uptake coefficient, representing the rate at which plants absorb pollutant from the soil. k 3 is the natural degradation rate of the pollutant in the plant. The natural degradation of the pollutant in the soil or plant could be accounted for by processes like radiation decay, microbial degradation, volatilization, or leaching. \"\"\" md\"\"\" Model the aforementioned system of differential equations using a reaction network object . Name it `soil cont plant uptake`. \"\"\" Uncomment and complete the instruction soil cont plant uptake reaction network begin missing end md\"\"\" Convert the system to a symbolic differential equation model and verify that you get the same system of differential equations as given in the problem. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Suppose that we simulate the evoluation of the pollutant in the soil and plant during 400 days. The inital pollutant concentrations in the soil and plant both have the value of 0.001\\ mg kg . In the simulation, the soil is being contaminated at a rate 0.06\\ mg kg \\cdot day . The degradation rates and uptake coefficient have the following values k 1 4.1 \\times 10^ 3 , k 2 1.9 \\times 10^ 2 , k 3 2.2 \\times 10^ 2 . There units are consistent with the units of the aforementioned values. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `param` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Plot the solutions \"\"\" missing Uncomment and complete the instruction md\"\"\" 1. Interprate the simulation results cf. peak in C and increase of P in terms of the used parameter values. \"\"\" md\" Answer missing\" md\"\"\" 2. How would you modify the basic model to make it a more realistic biological model cf. hill, monod, ... . \"\"\" md\" Answer missing\" md\"\"\" 3. What are the units of the parameters k₁, k₂ and k₃? \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_water_evap_infil/","title":"EXTRA. ODE water evaporation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"9\" title \"EXTRA. ODE water evaporation\" date \"2025 02 07\" tags \"exercises\" description \"ODE model of water evaporation\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI using Catalyst using OrdinaryDiffEq, StatsPlots hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" Exercise Water evaporation and infiltration Consider a water reservoir, such as a lake, where the water in the reservoir is in contact with the air as well as with the groundwater. We will denote the water level in the reservoir as W and the groundwater level as G . The water in the reservoir evaporates at a rate k 1 i.e. the evaporation coefficient and there can be infiltration into or from the groundwater at a rate k 2 i.e., infiltration coefficient depending on the difference in the water level in the reservoir and groundwater cf. W G There is a natural constant inflow of water into the reservoir at a rate I . At time t 0\\ s a pumping device is switched on such that the reservoir is rapidly being emptied at an outflow rate O until the level of the water reservoir drops to zero. From then on, the pump is switched off. \"\"\" md\"\"\" question Set up a system of differential equations modelling the above problem. \"\"\" hint md\"\"\" The system of differential equations that models the water level in a reservoir W and the groundwater level G considering evaporation, infiltration, inlet flow and outlet flow can be written down as \\begin align \\frac dW dt & I O k 1 \\cdot W k 2 \\cdot W G \\\\ \\frac dG dt & k 2 \\cdot W G \\end align \"\"\" md\"\"\" Model the aforementioned system of differential equations using a reaction network object . Name it `water evap infil`. \"\"\" Uncomment and complete the instruction water evap infil reaction network begin missing end md\"\"\" Convert the system to a symbolic differential equation model and verify that you get the same system of differential equations as given in the problem. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Both water levels are initially 6.75\\ m . The inflow rate is constant and is 2.7\\ m min . The evaporation and infiltration coefficient are 0.4\\ min^ 1 and 1.0\\ min^ 1 respectively. The outflow rate due to the pump is 20\\ m min and the pump stops working when W equals zero. We wish to simulate the evolution of W and G during 20\\ min . \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `param` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Set up a the condition , name it `condition`. \"\"\" condition missing Uncomment and complete the instruction md\"\"\" Make a new reaction system where the discrete event is included. Name it `water evap infil c`. \"\"\" named water evap infil c missing Uncomment and complete the instruction md\"\"\" Complete the new reaction system . Name it `water evap infil c com`. \"\"\" water evap infil c com missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.1`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Plot the results \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following questions 1. Can you clearly see the drop in W ? To what value does W drops? \"\"\" md\" Answer missing\" md\"\"\" 2. Why does G also drop when W drops? Explain. \"\"\" md\" Answer missing\" md\"\"\" 3. To what values are W and G tending to go? Was the system with the initial values for W and G and no outflow in equilibrium? Explain. \"\"\" md\" Answer missing\" "},{"url":"exercises/optim_wastewater_treatment/","title":"5. Optimisation wastewater treatment","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"27\" title \"5. Optimisation wastewater treatment\" date \"2025 08 06\" tags \"exercises\" description \"Optimisation wasterwater treatment\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst, OrdinaryDiffEq, StatsPlots using Turing, StatsBase, Optim using PlutoUI TableOfContents md\"\"\" Exercise Wastewater treatment Optimisation \"\"\" md\"\"\" Consider a wastewater treatment plant where wastewater circulates in cylindrical tanks so that microorganisms can break down the organic material present. At the top of such a tank with volume V\\ m^3 , wastewater enters at a flow rate q\\ m^3 h . The concentration of organic material in the inflow is known and equal to C in \\ kg m^3 . At the bottom of the tank, wastewater and microorganisms leave the tank at the same flow rate q\\ m^3 h so that the volume of wastewater in the tank remains constant. The concentration of organic material in the tank is denoted as C\\ kg m^3 and the concentration of microorganisms is denoted as X\\ kg m^3 . The microorganisms in the tank break down the organic material at a rate proportional to r\\cfrac K s K s C \\ m^3\\,h^ 1 \\,kg^ 1 with yield coefficient Y . The factor K s\\ kg m^3 is the concentration of C where the rate is half its maximum rate and r\\ m^3\\,h^ 1 \\,kg^ 1 is the maximum growth rate coefficient. Furthermore, the microorganisms degrade with rate coefficient k d . In the middle of the tank there is a mixing system that ensures that the wastewater and microorganisms are well mixed. This means that the concentration in the outflow is equal to the concentration in the tank C out C and X out X . The system of differential equations describing the change in the concentrations C t and X t is given by \\cfrac dC dt \\cfrac q V \\left C in C\\right r\\cfrac K s K s C \\,C\\,X \\cfrac dX dt \\cfrac q V X k d\\,X Y\\,r\\cfrac K s K s C \\,C\\,X The initial concentrations and the parameter values are summarised in the following tables | C 0 | X 0 | | | | | 4.0 | 0.01 | | q | V | r | C in | K s | k d | Y | | | | | | | | | | 5.0 | 50 | 0.4 | 3.0 | 5.2 | 0.10 | 1.2 | The amount of organic waste that is being broken down by the microorganisms depends on the flow rate q . First Part 1 , we will simulate the system with the parameters given above. Secondly Part 2 , we will optimize the value of the flow rate q so that the concentration of organic waste in the tank is at most 0.28\\ kg\\,m^ 3 . Take a simulation time of 72\\ hours in both cases. \"\"\" md\"\"\" Part 1 Simulation In this part, we will simulate the system with the parameters given above. \"\"\" md\"\"\" Implementation of the system \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem. Name it `wastewater treatment`. tip You can use the repressive Michaelis Menten function `mmr C, r, Ks ` for r\\cfrac K s K s C . \"\"\" Uncomment and complete the instruction wastewater treatment reaction network begin parameters missing species missing missing missing missing missing end md\"\"\" Convert the system to a symbolic differential equation model and verify your system of differential equations. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Setting up initial conditions, timespan and parameter values \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `params` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Creating an ODE problem, solve the problem and plot results \"\"\" md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.1`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Plot the results. Use `ylim 0, 3 ` and `lw 2` or `linewidth 2` as options. \"\"\" Uncomment and complete the instruction begin missing hline 0.28 , ls dash, lw 2, lc green, lab \"C 0.28\" end md\"\"\" Check out the end value of the organic waste. \"\"\" missing Uncomment and complete the instruction md\"\"\" Part 2 Optimization In this part, we will optimize the value of the flow rate q so that the concentration of organic waste in the tank is at most 0.28\\ kg\\,m^ 3 . \"\"\" md\"\"\" First, declare the Turing model function. Sample the flow rate q prior from an uniform distribution in the range 0, 5 \\ kg\\,m^ 3 . Suppose therein that the desired final value of the organic waste i.e. 0.28\\ kg\\,m^ 3 is normally distributed with mean the end value obtained from the solution and standard deviation 10^ 3 \\ kg\\,m^ 3 . \"\"\" Uncomment and complete the instruction model function wastewater treatment inference q ~ missing u0 missing tspan missing params missing oprob missing osol missing C d ~ missing end md\"\"\" Define the desired value for the organic waste with the variable name `C val`. \"\"\" missing Uncomment and complete the instruction md\"\"\" Now condition the model with the desired value \"\"\" wastewater treatment cond mod missing md\"\"\" Optimize the prior for q . Do this with `MLE` method and Nelder Mead. Store the optimization results in `results mle`. \"\"\" results mle missing Uncomment and complete the instruction md\"\"\" Check out the coefficient table. \"\"\" missing md\"\"\" Get the optimized value for q and assign it to `q opt`. \"\"\" q opt missing Uncomment and complete the instruction md\"\"\" Set up parameter values with the optimized parameter value. \"\"\" params opt missing Uncomment and complete the instruction md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveat 0.1`. \"\"\" oprob opt missing Uncomment and complete the instruction osol opt missing Uncomment and complete the instruction md\"\"\" Plot C and X simulated with both the initial and the optimized parameter values. Use `ylim 0, 3 ` and `lw 2` or `linewidth 2` as options. The dashed line indicates C 0.28\\ kg\\,m^ 3 . \"\"\" Uncomment and complete the instruction begin missing plot osol, ls dash, lw 1, lab none hline 0.28 , ls dash, lw 2, lc green, lab \"C 0.28\" end md\"\"\" question Does the value of C respect now the limit in the concentration? Draw your conclusion. \"\"\" md\"\"\" Conclusion missing \"\"\" "},{"url":"exercises/probabilistic_selection/","title":"7. Probability selection","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"36\" title \"7. Probability selection\" date \"2025 08 06\" tags \"exercises\" description \"Probability selection\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using Optim, StatsBase using PlutoUI md\" Model selection\" TableOfContents md\" Who's that distribution?\" md\"\"\" You decide to turn your life around and invest all your money into clams , or more specifically, pearl farming . Before setting up your full scale farm, you decide to test the pearl producing capabilities of different species of mollusk. You cultivate 10 different species, wait a year, and collect and measure the resulting pearls. You want to compare the species by fitting a distribution to the pearl sizes. This way you can compare average size, expected deviation and the probability to get a really big pearl. However, you don't know what distribution the pearl sizes follow. Since they're positive real numbers, 2 good candidates are the `Exponential` and `LogNormal` distributions. \"\"\" md\"\"\" question For every molluks species, does the data follow an Exponential or a LogNormal distribution? \"\"\" md\"\"\" Picture of a black pearl in its shell https upload.wikimedia.org wikipedia commons thumb 2 24 Black pearl and his shell.jpg 1280px Black pearl and his shell.jpg Source Brocken Inaglory Wikipedia \"\"\" md\" Data\" ╠═╡ begin function generate point firstdistr rand 0.5 if firstdistr medist Exponential rand Uniform 0.1, 10 else medist LogNormal rand Uniform 0.1, log 10 , rand Uniform 0.1, 1.0 end n samples rand Poisson 15 samples rand medist, n samples .| x round x, digits 2 return samples end distr data generate point for in 1 10 end ╠═╡ distr data 5.23, 2.79, 5.81, 4.36, 7.46, 4.46, 0.83, 6.45, 6.2, 6.53, 6.24, 8.72, 3.15 , 1.12, 1.04, 0.09, 0.06, 0.67, 0.33, 0.41, 0.87, 1.23, 4.28, 7.46, 1.21, 0.19, 0.3, 0.59, 1.74, 0.66, 5.97, 0.3, 1.43, 1.11 , 0.79, 3.37 , 6.84, 11.28, 9.32, 6.27, 6.73, 10.28, 13.69, 8.32, 6.95 , 0.48, 8.69, 3.92 , 1.53, 1.83, 1.86, 0.87, 1.53, 2.51, 2.14, 1.82, 0.28, 3.57, 0.42, 1.67, 2.39, 4.18 , 6.0, 2.37, 14.05, 4.01, 8.51, 5.29, 5.24, 18.01, 2.65, 8.91, 6.37, 2.54 , 0.58, 2.41, 12.87, 14.67, 3.97, 13.8, 2.54, 4.7, 17.6, 18.3, 11.16, 0.81, 18.86, 2.3 , 1.07, 0.6, 2.24, 0.02, 13.28, 4.88, 0.22, 18.54, 2.81, 2.97, 9.29, 2.98, 23.94, 0.39, 29.25, 1.05, 5.52, 0.39, 4.81, 3.73, 0.49 , 8.39, 10.45, 1.93, 12.18, 3.26, 5.12, 8.3, 4.09, 20.41, 0.61, 18.31 md\"You can choose the mollusk species here and see the data for its pearl sizes.\" md\"Mollusk species\" bind distr index Slider 1 10, show value true pearlsizes distr data distr index histogram pearlsizes, bins 0 ceil maximum pearlsizes md\" Model definition\" md\"\"\" We need to define a model for the two candidate distributions. The likelihood was already given above. For the priors, you can assume the following Exponential model μ ~ `Uniform 0, 10 ` LogNormal model μ ~ `Uniform 0, log 10 ` σ ~ `Uniform 0, 1 ` \"\"\" md\"\"\" note The `LogNormal` distribution is a bit weird `LogNormal μ, σ ` gives the distribution of the exponential of a normally distributed value with mean μ and standard deviation σ ```math \\begin gather X \\sim \\text Normal μ, σ \\, , \\\\ \\Rightarrow \\text exp X \\sim \\text LogNormal μ, σ \\, . \\end gather ``` This means that μ is not actually the mean of a `LogNormal μ, σ `, but something closer to log μ it's complicated . Hence the `log 10 ` in the prior above. \"\"\" model function expon num pearls μ exp ~ Uniform 0, 10 pearls zeros num pearls for i in 1 num pearls pearls i ~ Exponential μ exp end end model function lognorm num pearls μ lognorm ~ Uniform 0, log 10 σ lognorm ~ Uniform 0, 1.0 pearls zeros num pearls for i in 1 num pearls pearls i ~ LogNormal μ lognorm, σ lognorm end end md\"Instantiate the models and condition them on the available data.\" expmodel expon length pearlsizes | pearls pearlsizes, lognormmodel lognorm length pearlsizes | pearls pearlsizes, md\" Maximum likelihood\" md\"\"\" Determine the maximum likelihood estimation MLE of the parameter values given the data, using the `NelderMead ` algorithm. Plot the fitted parameters on the data for a visual comparison. \"\"\" exp res optimize expmodel, MLE , NelderMead exp mean coef exp res μ exp lognorm res optimize lognormmodel, MLE , NelderMead lognorm mean, lognorm spread coef lognorm res μ lognorm, σ lognorm begin histogram pearlsizes, normalize pdf plot Exponential exp mean , linewidth 3 end begin histogram pearlsizes, normalize pdf plot LogNormal lognorm mean, lognorm spread , linewidth 3 end md\" Bayes factor\" md\"\"\" Compare both models using the Bayes factor K . Start off by calculating the model evidence P D \\mid M of the data D for each model M , approximating the integral with a Riemann sum https en.wikipedia.org wiki Riemann sum \"\"\" md\"\"\" ```math P D \\mid M \\int \\theta\\in\\Theta P D \\mid M, \\theta \\, P \\theta \\, d \\theta \\approx \\sum i P D \\mid M, \\theta i \\, P \\theta i \\, \\Delta \\theta i ``` \"\"\" md\"\"\" The figure below illustrates the different probabilities involved. The red curve is the product of the two curves above, and the area underneath it is the model evidence we want to calculate. \"\"\" prior exp m exp logprior expmodel, μ exp m, likelihood exp m exp loglikelihood expmodel, μ exp m, posterior exp m exp logjoint expmodel, μ exp m, prior likelihood not yet normalized with evidence let xs 0.1 0.1 15 ys posterior exp x for x in xs p likelihood plot x likelihood exp x , xlims 0, 15 , label \"Likelihood P D | M, μ \", color blue, width 2 p prior plot prior exp, label \"Prior P μ \", color cyan, width 2, xlims 0, 15 p post plot xs, ys, label \"Unnormalized posterior P D| M \", color red, width 2, line dash, xlims 0, 15 , xlabel \"μ exp\", ribbon ys, zeros length xs , yticks round. 0 maximum ys 10 maximum ys , sigdigits 1 plot p likelihood, p prior, p post, ylabel \"density\", plottitle \"Evidence\", layout 3, 1 end Δm 0.1 begin evidence exp 0.0 for m in 0.1 Δm 10 likelihood per point pdf Exponential m , pearlsize for pearlsize in pearlsizes likelihood prod likelihood per point prior pdf Uniform 0, 10 , m evidence exp likelihood prior Δm end println evidence exp end Δs 0.01 begin evidence lognorm 0.0 for m in 0.1 Δm log 10 for s in 0.1 Δs 1.0 likelihood per point pdf LogNormal m, s , pearlsize for pearlsize in pearlsizes likelihood prod likelihood per point prior pdf Uniform 0, log 10 , m pdf Uniform 0, 1.0 , s evidence lognorm likelihood prior Δm Δs end end println evidence lognorm end md\"\"\" Now calculate the Bayes factor as follows ```math K \\frac P M 2 \\mid D P M 1 \\mid D \\frac P D \\mid M 2 \\, P M 2 P D \\mid M 1 \\, P M 1 ``` \"\"\" P M exp 0.5 P M lognorm 1 P M exp bayes factor evidence lognorm P M lognorm evidence exp P M exp md\"\"\" Another comparison we can make between the models is calculating whether the first model is the correct one ```math \\begin align P M 1 \\mid D & \\frac P D \\mid M 1 \\, P M 1 P D \\, , \\\\& \\frac P D \\mid M 1 \\, P M 1 P D \\mid M 1 \\, P M 1 P D \\mid M 2 \\, P M 2 \\, . \\end align ``` \"\"\" P M exp cond D evidence exp P M exp evidence exp P M exp evidence lognorm P M lognorm md\"\"\" extra A faster way to calculate the model evidences is using Turing's `logjoint` function and array comprehensions https docs.julialang.org en v1 manual arrays man comprehensions . \"\"\" evidence1 sum exp logjoint expmodel, μ exp m, Δm for m in 0.1 Δm 10 evidence2 sum exp logjoint lognormmodel, μ lognorm m, σ lognorm s, Δm Δs for m in 0.1 Δm 10 for s in 0.1 Δs 1 md\" AIC\" md\"\"\" Using the likelihoods calculated above, calculate the Akaike Information Criterion AIC for both models \"\"\" md\"\"\" ```math \\text AIC 2 k 2 \\, \\text log L ``` \"\"\" md\"\"\" tip To get your model's best possible AIC value, you need the highest possible loglikelihood. By definition, this corresponds with your MLE . If `opt res` is the variable returned by the `optimize` function, you can get the correspondig maximal loglikelihood using `opt res.lp`. \"\"\" AIC num params, loglikelihood 2 num params 2 loglikelihood AIC exp AIC 1, exp res.lp AIC lognorm AIC 2, lognorm res.lp md\" BIC\" md\"\"\" Do the same for the dissapointingly non Bayesian Bayesian Information Criterion BIC \"\"\" md\"\"\" ```math \\text BIC k \\, \\text log n 2 \\, \\text log L ``` \"\"\" BIC num observations, num params, loglikelihood num params log num observations 2 loglikelihood BIC exp BIC length pearlsizes , 1, exp res.lp BIC lognorm BIC length pearlsizes , 2, lognorm res.lp md\" Overlapping cells\" md\"\"\" When counting cells, overlapping cells are a common cause of errors. Here we will tackle a simplified version of the problem where we try to distinguish whether a point cloud originates from one or two circles. \"\"\" md\"\"\" Overlapping cell picture https media.springernature.com full springer static image art%3A10.1007%2Fs11334 022 00478 y MediaObjects 11334 2022 478 Fig1 HTML.png?as webp Source Efficient detection and partitioning of overlapped red blood cells using image processing approach Dhar 2022 \"\"\" md\" Data\" cell data 0.68 1.34 0.53 0.5 1.85 0.68 0.57 1.55 0.16 0.04 1.06 1.34 1.41 1.67 1.56 0.51 0.36 1.73 0.4 1.5 0.97 1.62 3.71 1.98 0.9 1.55 1.82 4.56 2.46 2.18 1.23 1.06 , 0.3 0.29 0.99 2.58 0.38 2.16 1.51 0.36 0.9 1.27 0.3 0.77 0.6 0.94 0.73 0.63 1.67 0.39 2.15 0.29 0.91 2.4 0.18 2.23 2.05 1.49 0.16 0.49 , 3.23 1.51 2.78 1.1 2.52 0.76 1.34 3.79 0.39 0.76 0.08 0.63 0.11 2.2 1.48 2.94 0.82 0.87 0.38 2.21 , 1.56 0.53 1.02 0.53 2.08 1.22 0.12 1.04 0.95 0.74 0.18 0.04 1.19 0.76 0.58 0.69 0.88 1.1 0.93 1.72 , 0.11 0.57 2.06 1.59 1.45 1.11 2.2 1.24 0.89 0.67 0.17 1.21 0.89 1.01 0.01 1.9 1.26 1.48 0.6 0.74 1.6 0.45 0.56 0.53 0.45 2.05 2.68 1.75 0.35 0.67 0.44 0.4 0.79 2.12 2.59 1.31 1.66 0.54 0.2 3.03 0.16 0.56 , 0.21 0.36 0.89 0.83 0.36 1.75 2.84 0.46 1.1 3.34 1.61 0.08 0.38 2.23 0.27 1.6 2.72 1.87 1.48 0.1 0.83 0.26 0.46 0.57 , 3.15 0.1 0.77 1.62 0.5 0.28 0.66 0.01 1.93 0.15 0.94 0.42 1.79 0.27 0.01 1.7 0.96 2.35 1.61 0.05 0.28 0.06 1.26 1.64 0.48 0.42 1.47 1.05 0.03 0.65 0.74 0.26 0.89 1.43 0.83 1.55 0.48 1.72 , 2.16 1.24 3.64 1.18 1.11 2.4 1.19 1.14 1.26 1.11 0.95 2.14 1.88 1.5 2.43 0.64 1.84 0.05 0.83 1.5 4.44 1.13 0.33 0.98 0.34 3.2 0.41 0.77 0.1 1.33 0.76 0.73 2.07 0.64 1.96 0.7 1.34 0.84 2.28 0.95 0.28 0.24 , 0.38 2.4 2.14 0.65 0.23 1.37 0.7 0.74 0.17 2.53 1.42 0.03 1.25 2.24 0.0 1.12 2.23 0.93 0.86 0.89 1.61 0.93 1.51 1.58 , 1.53 0.05 0.39 1.14 0.04 0.36 0.78 3.02 0.28 2.49 0.3 0.55 1.58 0.24 2.5 1.84 0.67 1.69 1.57 0.57 1.96 1.94 3.22 1.47 0.57 0.45 0.23 0.93 md\"You can choose the cell picture and visualize the data here.\" md\"Picture idx\" bind picture idx Slider 1 length cell data , show value true xs, ys eachrow cell data picture idx scatter xs, ys, xlims 5, 5 , ylims 5, 5 md\" Model definition\" md\"\"\" The model for one cell is defined as follows The points originate from one pointcloud with a centre `xm`, `ym` . `xm` and `ym` both follow a standard Normal distribution. All x values follow a Normal distribution around `xm` with σ 1 . All y values follow a Normal distribution around `ym` with σ 1 . \"\"\" model function singlecell n xm ~ Normal 0, 1 ym ~ Normal 0, 1 xs ~ filldist Normal xm, 1.0 , n ys ~ filldist Normal ym, 1.0 , n end md\"\"\" The model for two cells is very similar The points originate from one of two pointclouds, one with centre `xm1`, `ym1` , the other with centre `xm2`, `ym2` . `xm1`, `ym1`, `xm2` and `ym2` all follow standard Normal distributions. All x values follow either a Normal distribution σ 1 around `xm1` or `xm2`, with equal chance for either. The same idea goes for the y values. \"\"\" md\"\"\" hint To model the likelihood, consider the humble `MixtureModel`. \"\"\" model function doublecell n xm1 ~ Normal 0, 1 xm2 ~ Normal 0, 1 ym1 ~ Normal 0, 1 ym2 ~ Normal 0, 1 xsdist MixtureModel Normal xm1, 1.0 , Normal xm2, 1.0 ysdist MixtureModel Normal ym1, 1.0 , Normal ym2, 1.0 xs ~ filldist xsdist, n ys ~ filldist ysdist, n end my dist filldist Normal 0, 1 , 3 rand my dist md\"Instantiate and condition the models.\" n length xs singlemodel singlecell n | xs xs, ys ys, doublemodel doublecell n | xs xs, ys ys, md\" Maximum likelihood\" function plotsinglecell xm, ym bounds 5 mydist MvNormal xm, ym , 1.0 0.0 0.0 1.0 xs bounds 0.1 bounds ys bounds 0.1 bounds f x,y pdf mydist, x, y contourf xs, ys, f, xlims bounds, bounds , ylims bounds, bounds , color viridis, aspect ratio equal, legend false, title \"Single cell model\" end function plotdoublecell xm1, xm2, ym1, ym2 bounds 5 mydist MixtureModel MvNormal xm1, ym1 , 1.0 0.0 0.0 1.0 , MvNormal xm2, ym2 , 1.0 0.0 0.0 1.0 , xs bounds 0.1 bounds ys bounds 0.1 bounds f x,y pdf mydist, x, y contourf xs, ys, f, xlims bounds, bounds , ylims bounds, bounds , color viridis, aspect ratio equal, legend false, title \"Two cells model\" end md\"\"\" Determine the maximum likelihood estimation MLE of the parameter values given the data, using the `NelderMead ` algorithm. \"\"\" singleres optimize singlemodel, MLE , NelderMead single xm, single ym coef singleres xm, ym doubleres optimize doublemodel, MLE , NelderMead double xm1, double xm2, double ym1, double ym2 coef doubleres xm1, xm2, ym1, ym2 md\"Visualise the results\" begin plotsinglecell single xm, single ym scatter xs, ys end begin plotdoublecell double xm1, double xm2, double ym1, double ym2 scatter xs, ys end md\" AIC\" md\"Using the MLE results from the previous section, determine the AIC of both models. You can use the implementation from previous exercise.\" AIC single AIC 2, singleres.lp AIC double AIC 4, doubleres.lp "},{"url":"exercises/probmod_1-intro/","title":"3. ProbMod intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"16\" title \"3. ProbMod intro\" date \"2025 03 07\" tags \"exercises\" description \"Introduction to the sampling practicals\" layout \"layout.jlhtml\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI TableOfContents md\" Sampling notebook 1 Intro\" md\"\"\" This notebook will guide you through the basics of sampling in Julia. \"\"\" md\"\"\" To start off, load the required packages. \"\"\" md\" Problem\" md\"\"\" Let's go back to the circle throw example from the theory. The idea is simple a circle with radius 1 has an area of π. If you throw darts at the unit square 1, 1 x 1, 1 with uniform probability, the probability of a dart landing inside that circle is the area of the circle over the area of the square. ```math \\begin align A circle & π \\\\ A square & 1 1 ^2 4 \\\\ P inside & \\frac A circle A square \\frac π 4 \\end align ``` \"\"\" md\"\"\" This means if we can estimate this probability, we can estimate π easily ```math \\begin align P inside & \\frac π 4 \\\\ π & 4 \\, P inside \\end align ``` \"\"\" md\"\"\" However simple the problem, this probability is not simple to calculate by hand ```math P inside \\int 0 ^ 1 \\, \\int 0^ \\sqrt 1 y^2 dx \\, dy ``` \"\"\" md\"\"\" This, as one may guess, only gets worse for more complex problems. Which is why we use sampling instead \"\"\" md\"\"\" note Are you a real mathhead? Try computing the integral by hand Hint there should be an inverse tangent function somewhere down the line. \"\"\" md\" Explanation\" md\" Defining the model\" md\"\"\" Turing models are defined as julia functions preceded by the ` model` macro. Inside of them, you can define random variables with the \"`var ~ Distribution params `\" syntax, aside from doing the usual programming stuff. \"\"\" md\"\"\" Our circle problem can be defined as follows Sample the `x` and `y` coordinates of the dart uniformly between 1 and 1. Calculate the distance to the centre of the circle 0, 0 . The dart is within the circle if the distance is smaller than the radius 1. \"\"\" md\"This translates to the following in Turing\" model function distances x ~ Uniform 1, 1 y ~ Uniform 1, 1 dist sqrt x^2 y^2 return dist end md\"Calling this function will return a Turing model \" dist model distances md\" Sampling the model output\" md\"There's a number of things we can do with the model. The most simple is calling it, which will give the return value of the function after sampling a value for all random variables, here `x` and `y`.\" dist model md\"We can use this to generate a large number of samples and make estimations about the probability\" n samples 1 000 sp dists dist model for sample idx in 1 n samples histogram sp dists, title \"Distances of points to origin\", bins 20, legend nothing md\"Currently, we have samples of the distance to the origin. We can easily transform these to samples of being inside the circle or not, and subsequently estimate the desired probability.\" sp inside sp dists . 1 the circle has a radius of 1 md\"\"\" note We may as well have checked whether the distance was smaller than 1 inside of the Turing function and returned that instead. Calling the model would have then immediately given us `sp inside`. While that approach is also perfectly fine, it would have made visualising the distribution of distances more difficult. \"\"\" begin histogram sp dists sp inside , title \"Distances of points to origin\", label \"Inside circle\", bins 15 histogram sp dists . sp inside , label \"Not inside circle\", bins 5 end prob inside length sp dists sp inside length sp dists mean sp inside shorter alternative 4 prob inside md\"\"\" note How many samples do you need to get that beautiful `3.14` consistently? How about the yet even more charming `3.1415`? \"\"\" md\" Sampling everything with `sample`\" md\"\"\" An alternate way to generate a number of samples from our model is to use the `sample` function. Rather than getting samples of the function's output, this returns sampled values of all the random variables . \"\"\" md\"\"\" The inputs for the function `sample` are the Turing model. the sampler. the desired amount of samples. The second argument, the choice of sampler, is mostly important when doing inference , as we'll see in practical 4. When we want to simply sample from the model's priors without any inference, we use the `Prior ` sampler. \"\"\" dist chain sample dist model, Prior , n samples md\"\"\" note The `lp` column in the above output of `sample` is the log probability of that sample. This example uses 2 continuous distributions with an interval of size 2, so the value is always the same \\text ln P X x P Y y \\text ln 1 2 1 2 \\text ln 1 4 1.39 \"\"\" md\"\"\" The samples are visualised below \"\"\" scatter dist chain x , dist chain y , aspect ratio equal, label \"Dart locations\" md\"The sample values of a random variable can be acquired by indexing the resulting chain with the variable's name as a `Symbol` or `String` \" dist chain x or dist chain \"x\" md\"This can be useful for making plots, for example.\" md\" The `generated quantities` function\" md\"What if we want the function's return value too? We could calculate it based on our random variables by hand as `sqrt. dist chain x .^2 dist chain y .^2 `, or use the `generated quantities` function.\" sp dists alt generated quantities dist model, dist chain sp inside alt sp dists alt . 1 scatter dist chain x , dist chain y , aspect ratio equal, groups vec sp inside alt , label \"Outside of circle\" \"Inside of circle\" Note The `sample` method returns matrices. For plotting, vectors are often preferred, which is why we convert `sp inside alt` to a vector here. md\" For loops for many variables\" md\"A common problem when defining the problem as a Turing model is many random variables being involved, often with the same distribution. Turing allows variables to be defined in a for loop for this reason. Consider the circle example again but using a loop this time, which can easily be generalized to n dimensions \" model function distances loop coords zeros 2 initiatilize a vector of length 2 filled with zeros for i in 1 length coords coords i ~ Uniform 1, 1 assign every element of the vector a random variable end dist sqrt sum coords.^2 end distloop model distances loop sp loop distloop model for i in 1 n samples 4 mean sp loop . 1 md\"If you want to retrieve one of the random variables using `sample`, you can simply index the output as follows \" loop chain sample distloop model, Prior , n samples loop chain \"coords 1 \" md\" Working with Distributions\" md\"\"\" Under the hood, Turing makes use of Julia's `Distributions` package. Knowing some basic functionality of this package can be useful. \"\"\" md\"\"\" note Turing automatically loads Distributions into the workspace, so \"`using Distributions`\" is not necessary when Turing has been loaded. \"\"\" md\"Considering the humble example of `X ~ Exponential 10 `, let's do some plotting, sampling and calculating.\" plot Exponential 10 spX rand Exponential 10 , n samples histogram spX pdf Exponential 10 , 0 cdf Exponential 10 , 20 md\"Just for fun, we can work out the circle example again without Turing.\" begin sp dists noturing zeros n samples for i in 1 n samples x rand Uniform 0, 1 y rand Uniform 0, 1 dist x^2 y^2 sp dists noturing i dist end sp inside noturing sp dists noturing . 1 4 mean sp inside noturing end md\" The essentials\" md\"\"\" The most essential code for the first practical is reiterated here without long explanations to provide an easy reference for making the practical exercises. Side note the code is wrapped in a `let` block so Pluto won't complain about the same variable names being used again. \"\"\" ╠═╡ let n samples 1000 model function distances x ~ Uniform 1, 1 y ~ Uniform 1, 1 dist sqrt x^2 y^2 return dist end dist model distances instantiate model sp dists dist model for sample idx in 1 n samples make sample steekproef or `sp` sp inside sp dists . 1 transform into sample of whether point is inside the circle prob inside mean sp inside amount of points in circle amount of points println \"Pi is estimated as 4 prob inside \" alternative way of generating the same sample dist chain sample dist model, Prior , n samples sp dists2 generated quantities dist model, dist chain sp x dist chain x this method allows recovery of stochastic variables sp y dist chain y scatter sp x, sp y, group vec sp dists2 . 1 , legend false, aspect ratio equal which is nice for plotting end ╠═╡ "},{"url":"exercises/probmod_2-basics/","title":"3. ProbMod basics","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"17\" title \"3. ProbMod basics\" date \"2025 03 07\" tags \"exercises\" description \"Basic sampling exercises\" layout \"layout.jlhtml\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI TableOfContents md\" Sampling notebook 2 Basics\" md\" 1 Double Poisson\" md\"\"\" Let `X ∼ Poisson 10 ` and `Y ~ Poisson X `. 1. Plot the exact distribution of `X` and use sampling n 10 000 to generate a histogram of `Y`. 2. Estimate the following probabilities `P 3 Y ≤ 10 `. `P Y^2 100 `. 3. Consider `var X|Y 15 ` and `var Y|X 15 `. Estimate them numerically. One of the two has a simple analytical answer which one is it, and what is its exact value? \"\"\" md\" 1 Plots\" model function doublepoisson X ~ missing Y ~ missing return Y end dpmodel doublepoisson spY missing histogram spY md\" 2 Probabilities\" md\"\"\" tip When comparing a vector of values to a single number, don't forget to use `.` to execute operations element wise in Julia ✅ `spY . 1` compares every element of `spY` to `1` ❌ `spY 1` compares an entire vector with a single number → errors \"\"\" probXY1 missing probXY2 missing md\" 3 Variances\" md\"\"\" hint To create a sample of X that is conditional on some value s of Y , you can start from a sample of X and select only those elements for which the corresponding sample of Y has the conditioned value s . In other words, you'll need to index `spX` based on `spY` and vice versa for \\text var Y ∣ X . \"\"\" spX missing varXcondY missing varYcondX missing missing analytical answer of missing md\" 2 Combinations\" md\"\"\" Let `U ~ Uniform 0, 4 `, `V ∼ Normal U, 1 ` and `W ~ TriangularDist 0, 4, U `. 1. Use sampling n 10 000 to make a histogram of `|V − W|`. 2. Estimate `P V W ` and `P V W 10 ` 3. Are `V` and `W` independent? \"\"\" md\" 1 Histogram\" model function combinations U ~ missing V ~ missing W ~ missing end spVW missing md\" 2 Probabilities\" probVW1 missing probVW2 missing md\" 3 Independence\" md\"\"\" hint One way to prove dependence is showing that E V \\neq E V \\mid W \\leq w for at least one value w . If the expected value of V can change based on some information about W , they can't be independent \"\"\" md\"\"\" 3 Dice \"\"\" md\"You're playing a fun game of Caverns and Chimeras, and are facing off against the mighty Carl the Chimera. The fight is not going great and your next spell needs to deal 50 or more damage to slay the scary monster before it kills you. Spells deal damage equal to the sum of the dice they let you roll. You can choose between your 2 mightiest spells Watercube lets you throw 4 dice with 20 sides each. Dirtprism lets you throw 20 dice with 4 sides each. \" md\"\"\" questions 1. What is the probability that Watercube does the job? Also plot a histogram of its damage. 1. Do the same for Dirtprism. 1. What is the probability that watercube deals more damage than dirtprism? \"\"\" md\" 1 Watercube\" md\"\"\" hint Consider the humble `DiscreteUniform` distribution. Not sure how it works? Open the 🔍 Live Docs at the bottom right of the screen for more information \"\"\" model function watercube roll1 ~ missing roll2 ~ missing roll3 ~ missing roll4 ~ missing dicesum roll1 roll2 roll3 roll4 return dicesum end watermodel watercube p watercube kills missing missing histogram md\" 2 Dirtprism\" model function dirtprism check the \"For loops for many variables\" section from the intro notebook dicesum missing consider the `sum` function return dicesum end dirtmodel dirtprism p dirtprism kills missing missing histogram md\" 3 Comparison\" p watercube is better missing md\" 4 Super eggs\" md\"\"\" When a chicken lays an egg, there's a small chance it contains two egg yolks. This chance, as well as the number of eggs a chicken lays per year, go down as the chicken gets older. \"\"\" md\"\"\" You can make the following assumptions The age A of a random chicken in years is discrete and Uniformly distributed between 0 and 12. The number of eggs N an A year old chicken lays in a year is Poisson distributed with mean 300 20 \\, A . The probability P of an A year old chicken's egg having a double yolk is distributed as a `Beta 1, 800 100 A `. \"\"\" md\"\"\" questions 1. If someone hands you a random chicken, what is the probability it will lay 2 or more double eggs in a year? 1. Compare the distributions of double eggs for 1 year old and 3 year old chickens. \"\"\" md\" 1 Probability\" md\"\"\" hint In this exercise, the output variable the number of double yolked eggs is also a random variable In other words, it also follows some distribution. When considering what distribution, consider that each of the N eggs represents a \"trial\" with a P chance of success for a double yolk. \"\"\" model function eggs return missing end p multiple double eggs missing md\" 2 Histograms\" missing histogram 1 missing histogram 2 md\" 5 Birthdays\" md\"\"\" Sometimes, people are born on the same day of the year. \"\"\" md\"\"\" question What is the probability that, in a class of 150 students, 3 or more share a birthday? \"\"\" md\"\"\" tip You can solve this among other possibilities using either a for loop and the `count occurences` function given below, or the `Multinomial` distribution. \"\"\" count occurences vec count element , vec for element in unique vec count occurences 5, 107, 364, 5, 5, 364 three 5's, one 107 and two 364's model function birthdays missing end "},{"url":"exercises/probmod_3-advanced/","title":"3. ProbMod advanced","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"18\" title \"3. ProbMod advanced\" date \"2025 03 07\" tags \"exercises\" description \"Advanced sampling exercises\" layout \"layout.jlhtml\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI TableOfContents md\" Sampling notebook 3 Advanced\" md\" 1 Petridish peril\" md\"\"\" Living the microbiology master thesis life, your mornings consist of inoculating petridishes with bacteria. Somewhere along the day, you need to split them. You want to do this after there's a decent amount of bacteria in the dish 10\\ 000 but before they have overgrown the entire dish and start dying 100\\ 000 . This condition we call splittable . You'd like to estimate how long after inoculation you should return to your bacteria so that they're most likely to be in a splittable state. \"\"\" md\"\"\" Bacteria follow logistic growth , and you can use the following assumptions The initial population size P 0 has a 75% chance of originating from a small droplet and a 25% chance for a big droplet For small droplets, `P0` follows a `Poisson 10 ` For big droplets, `P0` follows a `Poisson 30 ` The growth rate r follows a `LogNormal 0.0, 0.3 ` The growth capacity K of the inoculated medium follows a `Normal 1e5, 1e4 ` \"\"\" md\"\"\" questions 1. Plot the prior distribution of P0. 2. What is the probability your bacteria are in a splittable state 8 hours after inoculation? 3. Plot 100 of the sampled logistic growth curves from 0 to 12 hours. \"\"\" md\" 1 Droplet Prior\" md\"\"\" tip A simple way of representing the distribution of P0 is through a mixture model. Mixture models are a way of modeling something that has a chance to be from different, simple distributions. If you wanted to model a variable that has a 0.8 chance of being from a `Normal 0, 1 ` and a 0.2 chance of being from an `Exponential 10 `, you would model it as follows in Turing `MixtureModel Normal 0, 1 , Exponential 10 , 0.8, 0.2 ` For the interested reader, mixture models are explained in more detail in theory section `4.5.2`. \"\"\" dropletdist missing md\" 2 Probability\" md\"\"\" tip You can `return` the logistic function estimated within the model and retrieve it using `generated quantities` to make plotting easier later on. Remember anonymous functions can be defined using `myfun x ...` \"\"\" logistic t, P0, r, K K 1 K P0 P0 exp r t model function petrigrowth P0 ~ dropletdist r ~ missing K ~ missing logfun missing return logfun end petri model missing chain petri missing logfuns missing sp petri missing prob splittable missing md\" 3 Plot\" md\"\"\" tip Plotting a function `myfun` is as simple as entering `plot myfun `. The same syntax applies if `myfun` is a vector of functions. However, don't forget it was asked to plot only 100 growth curves. \"\"\" missing plot md\" 2 Attraction\" md\"\"\" Following a course on electromagnetism will teach one that computing the net force between 2 arbitrary shapes can be a terrifying task. Tragedy has it then, that this is a very general problem with application from making fusion reactors to space travel. We can ease the pain by turning it into a sampling problem. We'll start in a humble manner and simulate the gravitational force between 2 cubes . Both cubes are size 1. The first cube is in 0, 1 x 0, 1 x 0, 1 , and the second cube in 1.1, 2.1 x 0, 1 x 0, 1 , as shown in the figure below. \"\"\" begin xe 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 ye 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 1, 0, 1, 1, 0, 1, 1 ze 0, 0, 1, 1, 0, 0, 1, 1, 0, 0, 0, 0, 0, 1, 1, 1, 1 xe2 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0 . 1.1 plot xlims 0.5, 2.5 , ylims 1, 2 , zlims 0, 3 plot xe, ye, ze color blue, linewidth 0.5, label \"cube 1\" plot xe2, ye, ze color orange, lw 0.5, label \"cube 2\" end md\"\"\" The gravitational force can be estimated by randomly sampling a point from both cubes and using the formula for gravitational force between those points, ignoring all constants ```math F \\frac 1 r^2 ``` \"\"\" md\"\"\" questions 1. What is the estimated net force between the two cubes? Is this the same as if you had treated the cubes as point masses? 1. How many samples do you need to estimate this force reliably? Define a reliable estimator as one having a standard deviation of 0.1. Visualise the distribution of the estimator. \"\"\" md\" 1 Net Force\" model function cubeforce x1 ~ missing y1 ~ missing z1 ~ missing x2 ~ missing y2 ~ missing z2 ~ missing F missing return F end cubemodel cubeforce force sp missing force average missing pointmass force missing doesn't require Turing, only maths md\" 2 Variance of Estimator\" bind required samples Slider 10 10 200, show value true estimator mean cubemodel for in 1 required samples a single estimation of the force given `required samples` samples estimator sp missing a sample of estimations given `required samples` samples estimator sp σ missing standard deviation of the force estimator missing histogram "},{"url":"exercises/probmod_4-review/","title":"3. ProbMod review","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"19\" title \"3. ProbMod review\" date \"2025 03 07\" tags \"exercises\" description \"Review sampling exercise\" layout \"layout.jlhtml\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots md\" Review exercise Buffon's needles\" md\"\"\" A wise man once said \"there is no greater joy than estimating π\" https en.wikipedia.org wiki Approximations of %CF%80 . Next to throwing darts at the unit square, another method to accomplish this is using Buffon's needle problem https en.wikipedia.org wiki Buffon%27s needle problem . The experiment is as follows consider a floor with parallel lines all a distance of 1 away from eachother. Now drop a needle of length 1 and width ~0 on the floor with a random position and angle . What is the probability P cross that the needle will cross one of the lines? \"\"\" md\"The following image illustrates the problem imagine l t 1 for two needles, where `a` crosses a line and `b` does not.\" md\"\"\" Buffon's needles https upload.wikimedia.org wikipedia commons thumb 5 58 Buffon needle.svg 1920px Buffon needle.svg.png \"\"\" md\"\"\" Using sampling magic, it's not difficult to make an estimate of this probability, \\hat P cross . Solving the problem analytically shows that the exact value is ```math P cross \\frac 2 \\pi ``` Therefore, our estimator for π is ```math \\hat π \\frac 2 \\hat P cross ``` \"\"\" md\"\"\" question Estimate π using the Buffon's needle approximation. \"\"\" md\"\"\" hint Assuming the lines are vertical, you only need to consider the x coordinates of both ends of the needle. \"\"\" "},{"url":"exercises/sde_model_aging/","title":"2. SDE aging","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"12\" title \"2. SDE aging\" date \"2025 02 07\" tags \"exercises\" description \"SDE aging\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StochasticDiffEq, StatsPlots using Distributions md\"\"\" Exercise Aging and saturated repair \"\"\" md\"\"\" Aging is ultimately correlated with damaged cells. These damaged cells are called senescent cells . Senescent cells are cells that eventually stop multiplying but don't die off when they should. They instead remain and secrete factors that cause chronic inflammation and reduce regeneration, leading to disease and decline . Let X denote the number of senescent cells or the damage in a human body. Research shows that they are produced at a rate proportional to age . Fortunately, in living organisms, these senescent cells are removed by so called natural killer cells . However, like many biological processen, this biological process of removing senescent cells is saturated . Hence, the model that we could adopt in order to predict the number of senescent cells or damaged cells X in a human body, has two features 1. production of damage that rises linearly with age , and 2. the saturating removal of damage . A possible model is the following differential equation \\cfrac dX dt \\mu t \\beta \\cfrac X X \\kappa Lets denote X in trillions tn . The term \\mu t stands for the procution of senescent cells, and the term \\beta \\cfrac X X \\kappa for the removal of senescent cells. The time t is in years y . The coefficient \\eta tn y^2 is a proportionality factor for the production, \\beta tn y is the removal rate coefficient and \\cfrac X X \\kappa is the corresponding saturation factor, with \\kappa tn the amount of X at which they inhibit half of their own removal rate. If this model was all there was, then all individuals would age at the same rate and die at the same age. The model does not explain why genetically identical organisms could differ in the number of senenscent cells. Therefore, we will introduce noise in the model by treating it as a Stochastic Differential Equation SDE model, where noise will be added to both, production and removal processes. \"\"\" md\"\"\" Implementation of the system \"\"\" md\"\"\" Implement the above ODE into a reaction network object , and name it `senescent cells rn`. Take a default initial value X t 0 0.0 for the species X , and default values of \\mu 0.00558 , \\beta 0.4464 , \\kappa 1.116 for the parameters in the model. In addition to the parameters, take \\eta 0.1 as the default noise scaling factor, and, furthermore, set the noise scaling to 0.5 for the process exhibiting the saturating removal of damage. \"\"\" Uncomment and complete the instruction senescent cells rn reaction network begin species missing parameters missing default noise scaling missing missing missing end md\"\"\" Convert this reaction model into a symbolic differential equation model and verify that you get the correct differential equation as mentioned above. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Setting initial condition, time span and parameters. \"\"\" md\"\"\" Initialize a vector `u0` with the default initial condition, set the timespan for the simulation we will simulate from 0\\ y to 120\\ y , and initialize a vector `param` with the default parameter values. In that way, later, you can change the initial condition and the parameter values if you want to try other values. \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction parms missing Uncomment and complete the instruction md\"\"\" Simulating the system as an SDE problem \"\"\" md\"\"\" Create the SDE problem. \"\"\" sprob missing Uncomment and complete the instruction md\"\"\" Solve the SDE problem using `EM `as solver and time step `dt 0.1`. \"\"\" ssol missing Uncomment and complete the instruction md\"\"\" Plot the solutions. Use the option `ylim 0, 6 ` in order to limit the range of X . \"\"\" missing Uncomment and complete the instruction md\"\"\" Execute the cell, where the SDE problem is being solved, a few times and watch the stochastic changes in the solutions. \"\"\" md\"\"\" Simulating the system as an EnsembleProblem. \"\"\" md\"\"\" In order to see to have an idea of the extend of the stochastic effect on the solutions, we can make a so called EnsembleProblem . This allows us to plot many possible solutions in one plot. \"\"\" md\"\"\" Create an `EnsembleProblem` based on `sprob`. \"\"\" esprob missing Uncomment and complete the instruction md\"\"\" Solve the ensemble problem. Use `EM ` as solver, take a time step `dt 0.1`, use the options `save everystep true`, and `trajectories 100`. \"\"\" essol missing Uncomment and complete the instruction md\"\"\" Plot the solutions. Use the option `ylim 0, 6 ` in order to limit the range of X . \"\"\" missing Uncomment and complete the instruction md\"\"\" Distribution of ages at 5 trillion senescent cells \"\"\" md\"\"\" Set up a histogram that shows the distribution of ages once the 5 trillion senescent cells are present in the body. \"\"\" md\"\"\" hints The number of senescent cells of the `i` th trajoctory can be accessed with `essol.u i X `. The index of the first element in the `i` th trajectory that is greater than 5 can be found with `findfirst 5 , essol.u i X `. An index is a valid index when it if not `nothing`. The time at index position `j` can be accessed with `essol.u i .t j ` Appending an element, e.g., `x` to an array `times` can be done as follow `append times, x ` \"\"\" Uncomment and complete the instruction begin times make empty vector for missing for loop from 1 to 100, default step is 1 find index of first element that is greater than 5 missing if missing if index is a valid index missing append time to vector times end end end md\"\"\" Make a histogram with the array `times`. Use `bins range 0, 120, length 121 `. \"\"\" missing Uncomment and complete the instruction md\"\"\" Check the mean. \"\"\" missing Uncomment and complete the instruction md\"\"\" Check the standard deviation. \"\"\" missing Uncomment and complete the instruction md\"\"\" Check the minimum value. \"\"\" missing Uncomment and complete the instruction md\"\"\" Check the maximum value. \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret the results. Ask yourself the following question 1. Suppose that 5 trillion senescent cells is about the maximum a human body can bear. What is the approximate corresponding range of ages? \"\"\" md\" Answer missing\" md\"\"\" 2. What is the effect of halving the damage rate \\mu ? \"\"\" md\" Answer missing\" md\"\"\" 3. What is the effect of doubling the damage removal rate \\beta ? \"\"\" md\" Answer missing\" md\"\"\" 4. What is the effect of halving the noise? \"\"\" md\" Answer missing\" "},{"url":"exercises/sde_model_catalyst_intro/","title":"2. SDE catalyst intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"11\" title \"2. SDE catalyst intro\" date \"2025 02 07\" tags \"exercises\" description \"SDE catalyst intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StochasticDiffEq, StatsPlots md\"\"\" Solving SDE problems with Catalyst \"\"\" md\"\"\" Stochastic Differential Equations SDEs are mathematical equations used to model systems influenced by random noise. They extend Ordinary Differential Equations ODEs by incorporating terms that represent stochastic processes , typically in the form of a Wiener process or Brownian motion. SDEs are widely used in various fields, such as physics, biology, finance, and engineering, to describe the evolution of systems under uncertainty or with inherent randomness. \"\"\" md\"\"\" We will illustrate the concepts of SDE problems using the infection model that was used to introduce the Catalyst package and how to solve it as an ODE problem.\\ In a previous notebook elaborated on the infection model in detail, hence, we will here limit ourselves to summarizing the variables, parameters and reactions . \"\"\" md\"\"\" Below we summarize the variables species \"\"\" md\"\"\" | Variable | Unit | Meaning | | | | | | ``S`` | persons | number of susceptible persons | | ``I`` | persons | number of infected persons | | ``D`` | persons | number of deceased persons | | ``R`` | persons | number of resistant persons | \"\"\" md\"\"\" Below we summarize the parameters \"\"\" md\"\"\" | Variable | Unit | Meaning | | | | | | ``\\alpha`` | ``\\frac persons contact `` | chances of getting infected after contact | | ``\\beta`` | ``\\frac contact persons^2\\,day `` | contact rate | | ``r`` | ``\\frac 1 day `` | rate of leaving infection period | | ``m`` | ``\\frac person person `` | fraction of persons deceasing | | ``1 m`` | ``\\frac person person `` | fraction of persons becoming resistant | \"\"\" md\"\"\" Hence, the infection rate is ``\\alpha \\beta``. This means that a susceptible person meets an infected person ``S I``, this will result in ``2I`` at a rate ``\\alpha \\beta``. Futhermore, an infected person ``I`` will either become a deceased person ``D`` at a rate ``m r`` or become a resistant person ``R`` at rate `` 1 m r`` \"\"\" md\"\"\" Our infection model has three reaction events Infection, where a susceptible persons meets an infected persons and also becomes infected. Deceasing, where an infected person die. Recovery, where an infected person recovers and becomes resistant. \"\"\" md\"\"\" Each reaction is also associated with a specific rate ``\\alpha \\beta``, the infection rate. ``m r``, the death rate. `` 1 m r``, the recovery rate. \"\"\" md\"\"\" Hence, the following infection reactions are S I \\xrightarrow \\alpha \\beta 2I I \\xrightarrow mr D I \\xrightarrow 1 m r R \"\"\" md\"\"\" We are going to implement this system of reactions using Catalyst. \"\"\" md\"\"\" We first load the Catalyst package, which is required for the code in this introduction to run \"\"\" md\"\"\" Implementation of the system The following code creates a so called reaction network object , that we have named `infection sde model`, that implements the aforementioned reactions .\\ \"\"\" infection sde model reaction network begin parameters η 40 default noise scaling η α β, S I 2I, noise scaling 60.0 r m, I D r 1 m , I R end md\"\"\" Note that we have now introducted a new parameter cf. ` parameters η` and ` default noise scaling η` . This parameter represent a default noise scaling parameter applying to all reactions default value is 1 . You can overwrite this default value for specific reactions by specifying ` noise scaling ... ` on the same line.\\ They are in principle not necessary to solve the problem as a SDE problem but they can in many cases be very useful see later .\\ When solving SDE problems, some random noise will be introduced upon the reaction rates of all reactions. \"\"\" md\"\"\" Similarity as before, you can get a list of the reaction species with the function `species`, and a list of the parameters with the function `parameters`. \"\"\" parameters infection sde model md\"\"\" Note that the parameter \\eta also is present in the list. \"\"\" md\"\"\" This reaction model can of course also be converted to a symbolic differential equation model \"\"\" osys convert ODESystem, infection sde model md\"\"\" Simulating the system as an SDE problem We first need to load the Differential and Plot package, which is required for simulating the system and plotting the results. \"\"\" md\"\"\" Assume, as before, that there are 10\\,000\\,000 people in the country, and that initially 1\\,000 person are infected. Hence, I 0 1\\,000 , S 0 10\\,000\\,000 I 0 9\\,999\\,000 , D 0 0 and R 0 0 .\\ Furthermore, we take the following values for the parameters \\alpha 0.08\\ person contact , \\beta 10^ 6 \\ contact person^2\\,day , r 0.2\\ day^ 1 and m 0.4 .\\ Finally, we want to run our simulation from day 0 till day 90 . \"\"\" md\"\"\" Setting initial conditions \"\"\" u0 S 9 999 000.0, I 1 000.0, D 0.0, R 0.0 md\"\"\" Setting the timespan \"\"\" tspan 0.0, 90.0 md\"\"\" Setting parameter values In the parameter list, you could also mention another default value for the default noise scaling parameter. \"\"\" params α 0.08, β 1.0e 6, r 0.2, m 0.4, η 50 md\"\"\" Creating a SDEProblem Create the SDE problem. \"\"\" sprob SDEProblem infection sde model, u0, tspan, params md\"\"\" Solving the SDEProblem There are many solving methods available for solving SDE problems. You can find a list of methods here https docs.sciml.ai DiffEqDocs stable solvers sde solve Full List of Methods . We will simply use the first one in this list, namely `EM `, with the time step option `dt 0.1` that will introduce some randomness at every time step. \"\"\" ssol solve sprob, EM , dt 0.1 md\"\"\" Finally, we can plot the solution through the plot function. \"\"\" plot ssol md\"\"\" You might notice that is you run the above instruction `ssol solve sprob, EM , dt 0.1 ` subsequent times, you will each time get different solutions plots due to the randomness introduced by treating the problem as SDE problem. \"\"\" md\"\"\" Simulating the system as an EnsembleProblem. In order to see to have an idea of the extend of the stochastic effect on the solutions, we can create a so called EnsembleProblem . This allows us to plot many possible solutions in one plot. In order to create an EnsembleProblem , you need to create an SDEProblem first. Since we already have our SDEProblem called `sprob`, we can readily create an EnsembleProblem from this. All you need to do is call the function `EnsembleProblem` with `sprob` as argument. \"\"\" md\"\"\" Creating a EnsembleProblem Create the ensemble problem. \"\"\" esprob EnsembleProblem sprob md\"\"\" Solving the EnsembleProblem Solving the ensemble problem can be done with our, yet familiar, function `solve` as we did when solving the SDE problem, but now we need to provide a few more arguments. The first additional argument and value that we will provide is `save everystep true`, this will ensure that every simulation will be saved. The second argument indicates how many trajectories simulations you want to make. If you want an ensemble of 100 simulations, you can put `trajectories 100`. Hence, the function call would look like this `essol try solve esprob, EM , dt 0.1, save everystep true, trajectories 100 ` You can try this by uncommenting the instruction below and run the cell. \"\"\" essol try solve esprob, EM , dt 0.1, save everystep true, trajectories 100 md\"\"\" You will have noticed the detection of instabilities and the abortions. This is because of the stochastic effects that can cause calculations to become unstable. In order to cope with that, we will make sure that at every step the states S , I , R and D always remain within their boundaries. Here this is in the interval 0, 10000000 . To realize this we can create a so called `DisceteCallback` function using the functions below, namely, `condition` and `affect `. Both put in a `DisceteCallback` function they basically will make sure that at each integration step, the states cf. `integrator.u i ` will not go below 0 or above 10000000 . \"\"\" function condition u, t, integrator true end function affect integrator for i 1 4 if integrator.u i 10000000 integrator.u i 10000000 end if integrator.u i 0 integrator.u i 0 end end end md\"\"\" Combining them in a `DiscreteCallback` function \"\"\" cb DiscreteCallback condition, affect , save positions false,true md\"\"\" The option `save positions false,true ` serves to save only the states after the `affect ` function was called, and not the states before. \"\"\" md\"\"\" Now we can solve the ensemble problem while including the callback function. \"\"\" essol solve esprob, EM , dt 0.1, callback cb, save everystep true, trajectories 100 plot essol "},{"url":"exercises/sde_model_fermenter_secondorder/","title":"2. SDE fermentor 2nd order","tags":["exercises"],"text":" A Pluto.jl notebook v0.19.46 frontmatter order \"13\" title \"2. SDE fermentor 2nd order\" date \"2025 02 07\" tags \"exercises\" description \"SDE fermentor 2nd order\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StochasticDiffEq, StatsPlots md\"\"\" Exercise Fermenter 2nd order kinetics SDE \"\"\" md\"\"\" In a fermenter reactor biomass grows on substrate. The reactor is fed with a inlet flow rate Q in L h , which consist of a manipulable input concentration of substrate S in g L . Inside the reactor, biomass, with a concentration of X g L , is produced through second order kinetics \\begin eqnarray %S \\xrightarrow \\quad\\quad \\beta Y \\, X S \\xrightarrow \\quad\\quad r Y \\, X \\quad\\quad\\quad\\quad r k \\, S\\,X \\end eqnarray with k L\\,gS^ 1 h^ 1 the reaction rate constant, and Y gX gS the yield coefficient which is defined here by the amount of produced biomass by consumption of one unit of substrate. Futhermore, the reactor is drained with an outlet flow Q L h , which consist of the current concentrations of substrate S g L and biomass X g L inside the reactor. The volume V L of the reactor content is kept constant by setting Q in Q . \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of substrate S and biomass X with time as a Stochastic Differential Equation SDE problem with noise scaling. Name it `fermenter sde secondorder`. \"\"\" md\"\"\" Assign the following noise scaling values `η 0.10` for the main reaction default value for `η` noise scaling of `0.05` for the reaction describing the inlet S in noise scaling of `0.0` for the remaining reactions \"\"\" Uncomment and complete the instruction fermenter sde secondorder reaction network begin parameters missing missing missing missing end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction params missing Uncomment and complete the instruction sprob missing Uncomment and complete the instruction md\"\"\" Solve the SDE problem. Use `EM ` with `dt 0.1`. Store the solution in `ssol` \"\"\" ssol missing Uncomment and complete the instruction md\"\"\" Plot the results with the option `ylim 0.0, 2.0 ` \"\"\" missing md\"\"\" Create an `EnsembleProblem` in order to visualize a multiple solutions. Store it in `esprob`. \"\"\" esprob missing Uncomment and complete the instruction md\"\"\" Solve the `EnsembleProblem` using the same solver and time step as before, for 100 trajectories. Store the solution in `essol`. \"\"\" essol missing Uncomment and complete the instruction md\"\"\" Plot the results. Use as option again `ylim 0.0,2.0 ` and also `linealpha 0.5` to modify the line boldness. \"\"\" missing "},{"url":"exercises/sens_bitrophic_model/","title":"6. Sensitivity bitrophic model","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"30\" title \"6. Sensitivity bitrophic model\" date \"2025 08 06\" tags \"exercises\" description \"Sensitivity bitrophic model\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using ForwardDiff md\"\"\" Exercise Bitrophic model Sensitivity analysis \"\"\" md\"\"\" The dynamic relationship between a field crop and a voracious insect population within an ecosystem can be represented by a bitrophic model. Such model typically consists of two variables the abundance of the field crop, often representing a primary producer such as a plant species, and the population size of the voracious insect, which acts as a consumer feeding on the crop. The differential equations below describe how changes in the crop population affect the growth and behavior of the insect population, and vice versa, under the influence of an insecticide. \\begin eqnarray \\frac dC dt & \\theta C \\left 1 \\frac C k \\right fCA \\\\ \\frac dA dt & \\phi f CA 1 p \\, \\mu A \\end eqnarray Understanding this bitrophic interaction is crucial for predicting the impact of insect predation on crop yields and devising effective strategies for pest management in agriculture and ecological conservation efforts. In these equations, C and A are both expressed in kg ha , \\theta 0.2\\ d^ 1 , k 4000\\ kg ha , f 0.001\\ ha kg\\,d , the efficiency ratio \\phi 0.2 , and the mortality ratio \\mu 0.1\\ d^ 1 . The crop can be treated with an insecticide which increases the insect's death coefficient by a factor of p 3 . The factor p depends on the applied insecticide concentration and can therefore be controlled externally. At the beginning of a season, 100\\ kg of the crop and 0.5\\ kg of insects per ha are present. \"\"\" md\"\"\" Set up a reaction network model by analysing the terms in the above differential equations and simulate the evolution of C and A for 200 days. Next, perform a sensitivity analysis of C and A wrt. the parameters \\theta , \\phi and p . \"\"\" md\"\"\" Set up a reaction network model and name it `bitrophic model`.\\ Hints C is growing i.e., C \\rightarrow 2C at a rate \\theta \\left 1 \\frac C k \\right . The insects A eat crops C i.e., C A at a rate f resulting in an increase of a factor of \\phi more insects i.e., 1 \\phi A . The insects A are dying i.e., A \\rightarrow 0 at a rate 1 p \\, \\mu . \"\"\" bitrophic model reaction network begin missing Uncomment and complete the instruction missing Uncomment and complete the instruction missing Uncomment and complete the instruction end md\"\"\" Check out the species and the parameters. \"\"\" missing Uncomment and complete the instruction missing Uncomment and complete the instruction md\"\"\" Convert the system to a symbolic differential equations model, name it `osys` and verify, by analyzing the differential equations, that your model is correctly implemented. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0` with the initial conditions, define the timespan in `tspan` and initialize a vector `param` with the parameter values \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction md\"\"\" For clarity, we will use the variables `θ`, `ϕ` and `p` to store the parameter values that are used for the calculation of the sensitivity functions. \"\"\" θ 0.2 ϕ 0.2 p 3 params missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob`. Next, solve the ODE problem using `Tsit5 ` and `saveat 0.5`, and store the solution in `osol`. Finally plot the results. \"\"\" oprob missing Uncomment and complete the instruction osol missing Uncomment and complete the instruction missing Uncomment and complete the instruction md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question 1. What happens to C and A during the first 30 days? \"\"\" md\" Answer missing\" md\"\"\" question 2. Why does C starts to decline around day 40? \"\"\" md\" Answer missing\" md\"\"\" question 3. What happens to C and A from day 50 on, do they finally reach steady state values? \"\"\" md\" Answer missing\" md\"\"\" Write a solution function with as argument a vector of the parameters that you want the sensitivity on , and that returns the outputs. \"\"\" Uncomment and complete the instruction function bitrophic model sim params θ, ϕ, p missing u0 missing tspan missing params missing oprob missing osol missing return missing end md\"\"\" Make two functions based on the solution function that each returns a single output, hence, one function that returns the output C , and another function that returns the output A . \"\"\" bitrophic model sim C params missing Uncomment and complete the instruction bitrophic model sim A params missing Uncomment and complete the instruction md\"\"\" Make the time vector. \"\"\" t vals missing Uncomment and complete the instruction md\"\"\" Compute the two outputs C and A for the given parameter values. \"\"\" C sim missing Uncomment and complete the instruction A sim missing Uncomment and complete the instruction md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouputs C and A . Hence, you need to call `ForwardDiff.jacobian` twice. \"\"\" sens C missing Uncomment and complete the instruction sens A missing Uncomment and complete the instruction md\"\"\" Extract the absolute sensitivities of the outputs on the different parameters. \"\"\" Uncomment and complete the instruction begin sens C on θ missing sens C on ϕ missing sens C on p missing end Uncomment and complete the instruction begin sens A on θ missing sens A on ϕ missing sens A on p missing end md\"\"\" Compute the normalized sensitivities. \"\"\" Uncomment and complete the instruction begin sens C on θ rel missing sens C on ϕ rel missing sens C on p rel missing end Uncomment and complete the instruction begin sens A on θ rel missing sens A on ϕ rel missing sens A on p rel missing end md\"\"\" Plot the sensitivity functions of C and A on \\theta . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" ` and an x label `xlabel \"...\"` . \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question 1. In steady state, does \\theta have any influence on C ? Explain why this could be. \"\"\" md\" Answer missing\" md\"\"\" question 2. In steady state, why does \\theta have a positive effect on A ? Explain why this could be. \"\"\" md\" Answer missing\" md\"\"\" Plot the sensitivity functions of C and A on \\phi . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" ` and an x label `xlabel \"...\"` . \"\"\" missing Uncomment and complete the instruction md\"\"\" Plot the sensitivity functions of C and A on p . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" ` and an x label `xlabel \"...\"` . \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question 1. In steady state, does \\phi have a positive or negative effect on C ? Explain why this could be. \"\"\" md\" Answer missing\" md\"\"\" question 2. In steady state, does p have a positive or negative effect on C ? Explain why this could be. \"\"\" md\" Answer missing\" "},{"url":"exercises/sens_fermenter_monod/","title":"6. Sensitivity fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"29\" title \"6. Sensitivity fermenter monod\" date \"2025 08 06\" tags \"exercises\" description \"Sensitivity fermenter monod\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using ForwardDiff md\"\"\" Exercise Fermenter Monod kinetics Sensitivity analysis \"\"\" md\"\"\" In one of the previous practicals we were introduced to a fermenter in which biomass X g L grows by breaking down substrate S g L . The reactor is fed with an inlet flow rate Q in L h , which consists of a manipulable input concentration of substrate S in g L . This process was modelled using Monod kinetics, resulting in the model below \\begin eqnarray S X \\xrightarrow \\quad\\quad k 1 Y \\, X \\quad\\quad\\quad\\quad \\textrm with \\quad k \\cfrac \\mu max S K s \\end eqnarray \"\"\" md\"\"\" The reaction network object model for this problem could be defined as \"\"\" fermenter monod reaction network begin μmax S Ks , S X 1 Y X Alternatives mm S, μmax, Ks X, S Y X mm S, μmax, Ks X, S X 1 Y X Q V, S, X 0 Q V Sin, 0 S end md\"\"\" which resulted in the following differential equations \"\"\" md\"\"\" \\begin eqnarray \\cfrac dS dt & & \\cfrac Q V \\left S in S \\right \\mu max \\cfrac S S K s X\\\\ \\cfrac dX dt & & \\cfrac Q V X Y \\mu max \\cfrac S S K s X \\end eqnarray \"\"\" md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. In case you want to use the `mm` function, keep in mind that `mm S, μmax, Ks ` stands for \\mu max \\, \\cfrac S S K s . \"\"\" osys missing Uncomment and complete the instruction md\"\"\" The parameter values are \\mu max 0.40 , K s 0.015 , Y 0.67 , Q 2.0 , V 40.0 and S in 0.022\\ g L . Suppose that at t 0 no substrate S is present in the reactor but that there is initially some biomass with a concetration of 0.0005\\ g L .\\ Compute the following in a timespan of 0, 100 \\,h The sensitivities of S and X wrt. \\mu max , K s and S in . Plot the following A figure with the sensitivity functions of S and X wrt. S in . A figure with the sensitivity functions of S wrt. \\mu max , K s and S in . A figure with the sensitivity functions of X wrt. \\mu max , K s and S in . Interpret your results. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions, set the timespan and initialize a vector `param` with the parameter values \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction md\"\"\" For the sake of clarity, we will use the variables `μmax`, `Ks` and `Sin` to store the parameter values that are used for the calculation of the sensitivity functions. \"\"\" μmax missing Uncomment and complete the instruction Ks missing Uncomment and complete the instruction Sin missing Uncomment and complete the instruction params missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob`. Next, solve the ODE problem using `Tsit5 ` and `saveat 0.5`, and store the solution in `osol`. Finally plot the results. \"\"\" oprob missing Uncomment and complete the instruction osol missing Uncomment and complete the instruction missing Uncomment and complete the instruction md\"\"\" Write a solution function with as argument a vector of the parameters with the values for which we want to calculate the sensitivity , and that returns the outputs. \"\"\" Uncomment and complete the instruction function fermenter monod sim params μmax, Ks, Sin missing u0 missing tspan missing params missing oprob missing osol missing return missing end md\"\"\" Make two functions based on the solution function, where each returns a single output hence, one function that returns the output S , and another function that returns the output X . \"\"\" fermenter monod sim S params missing Uncomment and complete the instruction fermenter monod sim X params missing Uncomment and complete the instruction md\"\"\" Make the time vector. \"\"\" t vals missing Uncomment and complete the instruction md\"\"\" Compute the two outputs S and X for the given parameter values. \"\"\" S sim missing Uncomment and complete the instruction X sim missing Uncomment and complete the instruction md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouputs S and X . Hence, you need to call `ForwardDiff.jacobian` twice. \"\"\" sens S missing Uncomment and complete the instruction sens X missing Uncomment and complete the instruction md\"\"\" Extract the absolute sensitivities of the outputs on the different parameters. \"\"\" Uncomment and complete the instruction begin sens S on μmax missing sens S on Ks missing sens S on Sin missing end Uncomment and complete the instruction begin sens X on μmax missing sens X on Ks missing sens X on Sin missing end md\"\"\" Compute the normalized sensitivities. \"\"\" Uncomment and complete the instruction begin sens S on μmax rel missing sens S on Ks rel missing sens S on Sin rel missing end Uncomment and complete the instruction begin sens X on μmax rel missing sens X on Ks rel missing sens X on Sin rel missing end md\"\"\" Plot the sensitivity functions of S and X wrt. S in . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" ` and an x label `xlabel \"...\"` , and set the line width to 2 `linewidth ...` . \"\"\" missing Uncomment and complete the instruction maximum sens X on Sin rel md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question Which output variable, S or X , is most sensitive to S in in steady state? \"\"\" md\" Answer missing\" md\"\"\" question Why is the sensitivity function of S wrt. S in at first positive but then becomes zero? \"\"\" md\" Answer missing\" md\"\"\" Plot the sensitivity functions of S wrt. \\mu max , K s and S in . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" \"...\" ` and an x label `xlabel \"...\"` , and set the line width to 2 `linewidth ...` . \"\"\" missing Uncomment and complete the instruction md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question Which parameter, \\mu max , K s or S in , affects the output S the most in steady state? Why is this? \"\"\" md\" Answer missing\" md\"\"\" question Why is the sensitivity function of S wrt. \\mu max negative? \"\"\" md\" Answer missing\" md\"\"\" question Why is the sensitivity function of S wrt. K s positive? Does this correspond to the meaning of the half saturation constant K s or substrate concentration that allows to achieve half the maximum growth rate? In other words do higher values of K s support growth at low substrate concentrations? \"\"\" md\" Answer missing\" md\"\"\" Plot the sensitivity functions of X on \\mu max , K s and S in . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" \"...\" ` and an x label `xlabel \"...\"` , and set the line width to 2 `linewidth ...` . \"\"\" missing md\"\"\" Interpret your results. Try to answer the following question s \"\"\" md\"\"\" question Which parameter, \\mu max , K s or S in , affects the output X the most in steady state? \"\"\" md\" Answer missing\" md\"\"\" question Why is the sensitivity function of X wrt. \\mu max positive? How does this compare to substrate S ? \"\"\" md\" Answer missing\" md\"\"\" question Why is the sensitivity function of X wrt. K s negative? Compare it to that of substrate S . \"\"\" md\" Answer missing\" "},{"url":"exercises/sens_insuline/","title":"6. Sensitivity insuline","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"31\" title \"6. Sensitivity insuline\" date \"2025 08 06\" tags \"exercises\" description \"Sensitivity insuline\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI, OrdinaryDiffEq, ForwardDiff, Catalyst md\"\"\" Exercise The minimal glucose model and dynamic compensation sensitivity The Minimal Model of Glucose Regulation is a mathematical model used to describe how the body regulates glucose sugar levels in the blood. It was developed by Richard Bergman and Claudio Cobelli in the late 1970s and has become a cornerstone in diabetes research. We will use this exercise to study insulin sensitivity. The basic model considers only the concentration of glucose G t in mmol L and the concentration of insulin I t in mmol L Glucose is added to a system with a zeroth order rate of m later m t if we model a non fixed input . Glucose is removed from the blood with a rate of sGI , where s is the insulin sensitivity. Insulin decays according to first order kinetics with a rate parameter \\gamma \\beta cells produce insulin as a response to higher glucose concentrations. This is according to a saturated process, so it is well approximated using a Hill function n 2 . The rate of insulin production is given by qBf G , with B the amount of \\beta cells, q the maximal rate of insulin production unit of cells and f G the Hill function. \"\"\" md\"\"\" The Hill function is defined as hill X, v, K, n \\cfrac v\\,X^n X^n K^n In order to have an idea of how it looks like, lets define it as `f insuline G ` for v 1 , K 5 and n 2 \"\"\" f insulin G hill G, 1, 5, 2 md\"\"\" The following plot gives a fairly realistic response of insulin production as a function of glucose concentration in the blood. \"\"\" plot f insulin, 0, 30, xlab \"G mmol L \", ylab \"f G \", title \"Insulin production rate\" md\"Below is a reaction network implementing this model. All parameters are set to 1.0 for didactic purposes.\" glucose insuline circuit reaction network begin parameters q 1.0 s 1.0 γ 1.0 m 1.0 B 1.0 Ks 1.0 m, 0 G s I, G 0 B hill G, q, Ks, 2 , 0 I γ, I 0 end md\"\"\" Convert the system to a symbolic differential equation model and inspect the differential equations. You do not need to make a new variable, just call `convert` with the right arguments. \"\"\" missing Uncomment and complete the instruction md\"\"\" Simulate the system over a time interval of 0.0 to 10.0 hours with m 1.0 for various initial glucose concentrations e.g., between 0.1 and 5.0 by means of the variable `G0` bound to the slider just here below. Use an initial insuline concentration of 0.0 . \"\"\" bind G0 Slider 0.1 0.1 5.0, default 1.0, show value true G0 Putting a semi colon after an instruction will hide its return value. oprob1 missing Uncomment and complete the instruction sol1 missing Uncomment and complete the instruction md\"\"\" Plot the results. Use thereby `ylim 0.0, 5.5 `. \"\"\" missing Uncomment and complete the instruction md\"\"\" question \"Question\" What are the steady state concentrations for the two species? Does this depend on initial glucose levels given enough time ? \"\"\" md\" Answer missing\" md\"\"\" Check out the final glucose and insuline concentrations at the end time . \"\"\" missing Uncomment and complete the instruction md\"\"\" Create a vector named `u1 guess` with the previous final values. \"\"\" u1 guess missing Uncomment and complete the instruction md\"\"\" Calculate the steady state values of glucose and insuline. \"\"\" eq missing Uncomment and complete the instruction Geq missing Ieq missing md\"\"\" Check ou the steady state values for glucose and insulin. \"\"\" missing, missing Uncomment and complete the instruction md\"\"\" Now simulate the system but rather than with m being a constant glucose input, we give in a pulse of glucose i.e., drinking a soda with a peak at t 5 h. Note that our parameter now depends on the time \"\"\" glucose pulse t .5 exp t 5 ^2 plot glucose pulse, 0, 10, label \"G mmol L \", xlabel \"t\" md\"\"\" For purpose of solving the ODE problem we will need to define t as a default time variable with the command below. \"\"\" t default t md\"\"\" Redo the ODE problem but now with ` m glucose pulse t ` as parameter. Use an initial value of 0.0 for both glucose and insuline concentrations. \"\"\" oprob2 missing Uncomment and complete the instruction md\"\"\" Solve the new ODE problem using `Tsit5 ` and `saveat 0.01`. \"\"\" sol2 missing Uncomment and complete the instruction md\"\"\" Plot the results. \"\"\" missing Uncomment and complete the instruction md\"\"\" Up to now, we set s , the insulin sensitivity to 1 . This parameter represents how sensitive the body is to insuline in taking up glucose. Aging and obesity increase glucose resistance 1 s , resulting in diabetes Explore the effect of this parameter on your plots below. \"\"\" md\"\"\" We make a slider so that s can get values between 0.1 and 5.0 in step of 0.1. \"\"\" bind s Slider 0.1 0.1 5.0, default 1, show value true s md\"\"\" Below we have made a function that computes shows the steady state glucose concentration after 100 hours. Initial values for G and I are 0.0 and m was set to 0.5 . \"\"\" function glucose steady state s oprob ODEProblem glucose insuline circuit, G 0.0, I 0.0 , 0., 100. , m 0.5, s s sol solve oprob, Tsit5 , saveat 0.01 return sol G end final steady state glucose concentration is returned end md\"\"\" Calling the function results in the final steady state glucose concentration for a specific valu of s set by the slider above. \"\"\" glucose steady state s md\"\"\" Below is a plot of the steady state value of G as a function of s . \"\"\" plot glucose steady state, 0.1, 5, xlabel \"s\", ylabel \"Gss\", label \"Gss\", ylim 0, 5 md\"\"\" Use automatic differentiation with `ForwardDiff.derivative ..., ... ` to compute the absolute and relative sensitivity index. Is this system sensitive to the insuline sensitivity s ? \"\"\" md\"\"\" Calculate the absolute sensitivity. \"\"\" Uncomment and complete the instruction sens G s ForwardDiff.derivative ..., ... md\"\"\" Display the absolute sensitivity for the current s value cf. slider . \"\"\" missing Uncomment and complete the instruction md\"\"\" Calculate the normalized total relative sensitivity. \"\"\" sens G rel s missing Uncomment and complete the instruction md\"\"\" Display the normalized total relative sensitivity for the current s value cf. slider . \"\"\" missing Uncomment and complete the instruction md\"\"\" We see that the final glucose concentration is highly dependent on s This seems to be a flaw in the model, as we can imagine that the physiological parameters can greatly differ from person to person for example, a person can have a large pancreas . The final glucose concentration should not depend on the insuline sensitivity s . A mechanism that stabilizes this is called dynamic compensation . Simply put, we have assumed here that the amount of beta cells B is fixed. However, in practice, these cells are capable of dividing, growing, and thus producing more insulin. Their growth rate depends on the concentration of glucose, creating an additional feedback loop that stabilizes the physiological circuit. ```julia μ G , B 2B dynamic compensation ``` \"\"\" md\"\"\" Growth rate function depending on the glucose concentration. \"\"\" μ G 0.3atan 0.5 G 1 md\"\"\" The growth rate of the \\beta cells follows a sigmoid shape, being negative when G is smaller than a threshold and positive if G exceeds this threshold. This curve is plotted below. \"\"\" plot μ, xlim 0, 30 , xlab \"G\", label \"μ G \", title \"Glucose dependent growth rate\" md\"\"\" Add dynamic compensation to the model and show that this greatly reduces the sentitivty w.r.t. s . \"\"\" md\"\"\" Robust version of a reaction network object with dynamic compensation. ```julia glucose insuline circuit robust reaction network begin parameters q 1 s 1 γ 1 m 1 Ks 1.0 species B t 1 m, 0 G s I, G 0 B hill G, q, Ks, 2 , 0 I γ, I 0 μ G , B 2B end ``` \"\"\" md\"\"\" Create the aforementioned robust version of a reaction network object . \"\"\" Uncomment and complete the instruction glucose insuline circuit robust reaction network begin parameters missing species missing missing ... end md\"\"\" Convert the system to a symbolic differential equation model and inspect the differential equations. You do not need to make a new variable, just call `convert` with the right arguments. \"\"\" missing Uncomment and complete the instruction md\"\"\" Simulate the system over a time interval of 0 to 10 hours with default parameter values, and initial glucose and insuline concentrations of 5.0 and 0.0 , repectively. Use `Tsit5 ` and `saveat 0.01` to solve. \"\"\" oprob robust missing Uncomment and complete the instruction sol robust missing Uncomment and complete the instruction md\"\"\" Plot the results. Use thereby `ylim 0.0, 5.5 `. \"\"\" missing Uncomment and complete the instruction md\"\"\" Implement a function that computes shows the steady state glucose concentration after 100 hours. Set initial values for G and I to 0.0 and set m to 0.5 . Tip copy the body of the former function `glucose steady state` and adapt. \"\"\" Uncomment and complete the instruction function glucose steady state robust s oprob missing sol missing return missing end md\"\"\" Create a new slider object with a range between 0.1 and 5.0 and step size 0.1 , and bind it to the new variable `s robust`. Tip copy the previous slider and adapt. \"\"\" missing Uncomment and complete the instruction md\"\"\" Call the function `glucose steady state robust` with `s robust` as argument and observe the new steady state glucose concentration for different insulin sensitivity values. \"\"\" missing Uncomment and complete the instruction md\"\"\" Plot of the new steady state value of G as a function of s in the range 0.1, 5.0 . You might need to use `ylim 0.98, 1.02 `. \"\"\" missing Uncomment and complete the instruction md\"\"\" Use automatic differentiation with `ForwardDiff.derivative ..., ... ` to compute the normalized total relative sensitivity index. Is this new system sensitive to the insuline sensitivity s ? Answer missing \"\"\" sens G rel robust s ... Uncomment and complete the instruction md\"\"\" Display the new normalized total relative sensitivity for the current s value cf. slider . \"\"\" sens G rel robust s robust "},{"url":"exercises/sens_intro/","title":"6. Sensitivity intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"28\" title \"6. Sensitivity intro\" date \"2025 08 06\" tags \"exercises\" description \"Sensitivity intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots using ForwardDiff md\"\"\" Introduction to sensitivity analysis \"\"\" md\"\"\" Goal of this practicum \"\"\" md\"\"\" Sensitivity functions indicate how sensitive the model output is to a change in parameter values. When a model output is very sensitive to a certain parameter, a small change in the value of this parameter will have a large influence on the value of the model output. Sensitivity functions thus provide important information about the model and are implicitly used to estimate parameters and explicitly in the context of optimal experimental design. \"\"\" md\"\"\" The sensitivity function that measures how sensitive output y i is to changes in parameter \\theta j is given by the partial derivative S ij \\cfrac \\partial \\hat y i \\theta \\partial \\theta j \\tag 1 \"\"\" md\"\"\" Expression 1 is sometimes referred to as the absolute sensitivity . Try to understand why the above expression 1 does indeed give us the information we were promised in the first paragraph. question How will we be able to see from the value determined by the above expression 1 whether or not output \\hat y i is sensitive to a change in \\theta j ? What is then the meaning of a negative sensitivity? \"\"\" md\"Answer missing\" md\"\"\" Sometimes expression 1 can be evaluated analytically. Usually, however, we will have to approximate the partial derivative numerically. Expression 1 can be made more specific \\cfrac \\partial \\hat y i \\theta \\partial \\theta j \\approx \\cfrac \\hat y i \\theta j \\Delta\\theta j \\hat y i \\theta j \\Delta\\theta j \"\"\" md\"\"\" Thus, to calculate the sensitivity function numerically, the model is evaluated for the parameter values \\theta j and \\theta j \\Delta\\theta j and the difference between these evaluations is taken. \"\"\" md\"\"\" Since quantity 1 is dependent on the units, a normalized variant is often used s ij \\cfrac \\partial \\hat y i \\theta \\partial \\theta j \\cdot \\cfrac \\theta j \\hat y i \\tag 2 The interpretation of 2 is how much the output changes per cent if the parameter is increased by one per cent. It assumes positive nonzero model outputs and parameters, which is often the case for biochemical models. Using the normalized variant allows you to compare all possible sensitivity functions with each other. \"\"\" md\"\"\" We now calculate and interpret sensitivity functions for some given models. To illustrate these concepts, we first consider three simple models describing the growth of grass. \"\"\" md\"\"\" Grass growth models \"\"\" md\"\"\" In this notebook, three different models will be used, each modelling the yield of grass in a grassland Logistic growth model \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W Exponential growth model \\cfrac dW dt \\mu \\left W f W \\right Gompertz growth model \\cfrac dW dt \\left \\mu D \\ln W \\right W with output W the grass yield, and W f , \\mu and D parameters. The table below shows some typical values for the parameters | | \\mu | W f | D | | | | | | | Logistic | 0.07 | 10.0 | | | Exponential | 0.02 | 10.0 | | | Gompertz | 0.09 | | 0.04 | We will use an initial condition of W 0 2.0 for each and a simulation time of 100 days. \"\"\" md\"\"\" We will illustrate how to compute the local normalized sensitivity functions for the logistic model. The same will be left as exercises below for the exponential and Gompertz models. Important We will use consequently ` log`, ` exp` and ` gom` appended to relevant variables names in order to indicate their model origin and to prevent cell disabling that occurs when using the same variables names in these Notebooks. \"\"\" md\"\"\" Modelling logistic growth \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W \\ W 0 2.0, \\mu 0.07 and W f 10.0\\ We will start by modelling our system and simulating using the aforementioned parameters values, initial condition and timespan in a way that we are familiar with. \"\"\" md\"\"\" Implementation of the system \"\"\" growth log reaction network begin species W t 2.0 default initial condition parameters μ 0.07 Wf 10.0 default parameter values μ 1 W Wf , W 2W end md\"\"\" Convert the reaction model to check that we work with the correct differential equation \"\"\" osys log convert ODESystem, growth log md\"\"\" Setting initial conditions, timespan and parameter values \"\"\" u0 log W 2.0 tspan 0.0, 100.0 this will be the same for the three models md\"\"\" For the sake of clarity, we will use the variables `μ log` and `Wf log` to store the parameter values. \"\"\" μ log 0.07 Wf log 10.0 params log μ μ log, Wf Wf log md\"\"\" Creating and solving the ODEProblem and plotting results \"\"\" oprob log ODEProblem growth log, u0 log, tspan, params log Also possible here if initial conditions and parameter values are defined in the catalyst model oprob log ODEProblem growth mod log, , tspan, osol log solve oprob log, Tsit5 , saveat 0.5 plot osol log md\"\"\" Local Sensitivity Analysis LSA \"\"\" md\"\"\" In order to compute the local sensitivity functions, we will need to load the `ForwardDiff` package \"\"\" md\"\"\" We need to write a solution function with as argument a vector of the parameters those values for which we want to calculate the sensitivity , and that returns the solution time vector and outputs . \"\"\" function growth sim log params μ, Wf params u0 log W 2.0 tspan 0.0, 100.0 oprob log ODEProblem growth log, u0 log, tspan, μ μ, Wf Wf osol log solve oprob log, Tsit5 , saveat 0.5 return osol log end md\"\"\" Next, we will need to make a function that returns a single output based on the solution function. \"\"\" growth sim W log params growth sim log params W md\"\"\" Now make a time vector that is the same as the time vector from the solution. \"\"\" t vals log 0 0.5 100.0 Alternatives t vals log tspan 1 0.5 tspan 2 t log growth sim log μ log, Wf log t md\"\"\" Compute the single output with the given parameter values. This will give us exactly the same output that we simulated before in a familiar way. \"\"\" W log growth sim W log μ log, Wf log md\"\"\" Use now the function `ForwardDiff.jacobian` to compute the sensitivities. This function takes two arguments the solution function and a vector with the parameter values. \"\"\" sens W log ForwardDiff.jacobian growth sim W log, μ log, Wf log md\"\"\" To get the absolute sensitivities of W wrt. \\mu , and of W on W f , you need to use indexing with `sens W log` `sens W log ,1 ` gives the absolute sensitivity of W wrt. \\mu . `sens W log ,2 ` gives the absolute sensitivity of W wrt. W f . \"\"\" sens W on μ log sens W log ,1 sensitivity of W on μ sens W on Wf log sens W log ,2 sensitivity of W on Wf md\"\"\" We now calculate the normalized sensitivities. For that we need to multiply by the parameter value and divide by the ouput. Beware that all element wise operations need a dot in front of the operator, e.g. as in `. ` and `. `. \"\"\" sens W on μ rel log sens W on μ log . μ log . W log sens W on Wf rel log sens W on Wf log . Wf log . W log md\"\"\" We are now ready to plot the two normalized sensitivity functions. We provide the time vector first argument and a vector of the two sensitivity functions second argument . Additionally, you can provide a title, legend labels and a x and or y label. \"\"\" plot t vals log, sens W on μ rel log, sens W on Wf rel log , title \"Normalized sensitivities\", label \"W on μ\" \"W on Wf\" , xlabel \"Time day \" md\"\"\" Notice that in the `label` option there is no comma separating the labels. \"\"\" md\"\"\" Conclusions From the sensitivity plot of W wrt. \\mu it can be seen that W is most sensitive to \\mu in the time region 5, 30 . The latter corresponds to the region where the yield rate is largest i.e., when the growth is largest . This makes sense because when looking at the differential equation, \\mu is approximately the growth rate for relatively small W values. From the sensitivity plot of W wrt. W f it can be seen that W is most sensitive to W f in the region where time values are large cf. operating point . The latter corresponds to the region where the yield rate stagnates i.e., when the yield reaches a steady value . This makes sense because when looking at the differential equation, W f is the steady state value. \"\"\" md\"\"\" question What name do we often give to the steady state value of W or parameter W f ? Do these local sensitivity results match your expectations in terms of the different impacts of the parameters? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 Sensitivity analysis of the exponential growth model \"\"\" md\"\"\" \\cfrac dW dt \\mu \\left W f W \\right \\ W 0 2.0, \\mu 0.02 and W f 10.0 \"\"\" md\"\"\" Create a reaction network object for the exponential growth model. Name it `growth exp`. \"\"\" Uncomment and complete the instruction growth exp reaction network begin species missing parameters missing missing end md\"\"\" Convert the system to a symbolic differential equation model name it `osys exp` and verify, by analyzing the differential equation, that your model has been correctly implemented. \"\"\" osys exp missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0 exp` with the initial condition \"\"\" u0 exp missing Uncomment and complete the instruction md\"\"\" We will use the same timespan as before, so no need to redefine it. \"\"\" md\"\"\" For the sake of clarity, we will use the variables `μ exp` and `Wf exp` to store the parameter values. \"\"\" μ exp missing Wf exp missing md\"\"\" Initialize a vector `params exp` with the parameter values \"\"\" params exp missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob exp` \"\"\" oprob exp missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol exp` \"\"\" osol exp missing Uncomment and complete the instruction md\"\"\" Plot the result \"\"\" missing Uncomment and complete the instruction md\"\"\" Write a solution function with as argument a vector of the parameters the values for which we want the sensitivity , and that returns the outputs. \"\"\" Uncomment and complete the instruction function growth sim exp params μ, Wf missing u0 exp missing tspan missing oprob exp missing osol exp missing return missing end md\"\"\" Make a function based on the solution function that returns a single output. \"\"\" growth sim W exp params missing Uncomment and complete the instruction md\"\"\" Make the time vector. \"\"\" t vals missing Uncomment and complete the instruction md\"\"\" Compute the output for the given parameter values. \"\"\" W exp missing Uncomment and complete the instruction md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouputt. \"\"\" sens W exp missing Uncomment and complete the instruction md\"\"\" Extract the absolute sensitivities of the outputs on the different parameters. \"\"\" sens W on μ exp missing Uncomment and complete the instruction sens W on Wf exp missing Uncomment and complete the instruction md\"\"\" Compute the normalized sensitivities. \"\"\" sens W on μ rel exp missing Uncomment and complete the instruction sens W on Wf rel exp missing Uncomment and complete the instruction md\"\"\" Plot both normalized sensitivity functions with appropriate title and labels \"\"\" missing Uncomment and complete the instruction md\" Draw your conclusions missing missing \" md\"\"\" Exercise 2 Sensitivity analysis of the Gompertz growth model \"\"\" md\"\"\" \\cfrac dW dt \\left \\mu D \\ln W \\right W \\ W 0 2.0, \\mu 0.09 and D 0.04. \"\"\" md\"\"\" Create a reaction network object for the Gompertz growth model. Name it `growth gom`. \"\"\" Uncomment and complete the instruction growth gom reaction network begin species missing parameters missing missing end md\"\"\" Convert the system to a symbolic differential equation model name it `osys gom` and verify, by analyzing the differential equation, that your model has been correctly implemented. \"\"\" osys gom missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0 gom` with the initial condition \"\"\" u0 gom missing Uncomment and complete the instruction md\"\"\" We will use the same timespan as before, so no need to redefine it. \"\"\" md\"\"\" For the sake of clarity, we will use the variables `μ gom` and `D gom` to store the parameter values. \"\"\" μ gom missing D gom missing md\"\"\" Initialize a vector `params gom` with the parameter values \"\"\" params gom missing Uncomment and complete the instruction md\"\"\" Create the ODE problem and store it in `oprob gom` \"\"\" oprob gom missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol gom` \"\"\" osol gom missing Uncomment and complete the instruction md\"\"\" Plot the result \"\"\" missing Uncomment and complete the instruction md\"\"\" Write a solution function with as argument a vector of the parameters the values for which we want the sensitivity , and that returns the outputs. \"\"\" Uncomment and complete the instruction function growth sim gom params μ, D missing u0 gom missing tspan missing oprob gom missing osol gom missing return missing end md\"\"\" Make a function based on the solution function that returns a single output. \"\"\" growth sim W gom params missing Uncomment and complete the instruction md\"\"\" Make the time vector. \"\"\" t vals gom missing Uncomment and complete the instruction md\"\"\" Compute the output for the given parameter values. \"\"\" W gom missing Uncomment and complete the instruction md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouput s . \"\"\" sens W gom missing Uncomment and complete the instruction md\"\"\" Extract the absolute sensitivities of the outputs on the different parameters. \"\"\" sens W on μ gom missing Uncomment and complete the instruction sens W on D gom missing Uncomment and complete the instruction md\"\"\" Compute the normalized sensitivities. \"\"\" sens W on μ rel gom missing Uncomment and complete the instruction sens W on D rel gom missing Uncomment and complete the instruction md\" Plot both sensitivity functions with appropriate title and labels \" missing Uncomment and complete the instruction md\" Draw your conclusions missing missing \" "},{"url":"exercises/ssa_model_catalyst_intro/","title":"2. SSA catalyst intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"14\" title \"2. SSA catalyst intro\" date \"2025 02 07\" tags \"exercises\" description \"SSA catalyst intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, JumpProcesses, StatsPlots md\"\"\" Introduction to Catalyst SSA \"\"\" md\"\"\" Catalyst.jl is a symbolic modeling package for analysis and high performance simulation of chemical reaction networks. Catalyst defines symbolic ReactionSystems, which can be created programmatically or easily specified using Catalyst's domain specific language DSL . \"\"\" md\"\"\" This notebook describes the syntax for building chemical reaction network models using Catalyst's D omain S pecific L anguage DSL . We will illustrate this by implementing and solving an infection model by means of an SSA S tochastic S imulation A lgorithm . \"\"\" md\"\"\" The infection model revisited \"\"\" md\"\"\" For the sake of clarity we restate the description of the previous infection model. It is important to model the outbreak of infectious diseases in order to devise appropriate measures to avoid global epidemics. In this exercise we consider an isolated group of people in which a viral disease is spreading. An infection model similar to the SIR model but slightly extended will be used for this purpose. We are interested in the evolution of the number of susceptible S , infected I , deceased D and resistant R persons.\\ We make the following assumptions 1. Transmission of the disease from an infected person to a susceptible person takes place through direct contact. The chance of any two inhabitants of the group coming into contact with each other is \\beta , and the probability of infection after contact between an infected and a susceptible person is \\alpha . 2. Note that the above assumption implicitly states that the probability of two neighbours coming into contact with each other is as high as the probability of two people living at two extremes of the territory coming into contact with each other. 3. A pereson leaves the infection period at a rate r hence, a person is contagious for an average of 1 r days. Without appropriate medication, a fraction m of infected people die and a fraction 1 m of infected people acquire immunity after healing. 4. We assume that no one crosses the territory borders. \"\"\" md\"\"\" | Variable | Unit | Meaning | | | | | | ``S`` | persons | number of susceptible persons | | ``I`` | persons | number of infected persons | | ``D`` | persons | number of deceased persons | | ``R`` | persons | number of resistant persons | \"\"\" md\"\"\" | Variable | Unit | Meaning | | | | | | ``\\alpha`` | ``\\frac persons contact `` | chances of getting infected after contact | | ``\\beta`` | ``\\frac contact persons^2\\,day `` | contact rate | | ``r`` | ``\\frac 1 day `` | rate of leaving infection period | | ``m`` | ``\\frac person person `` | fraction of persons deceasing | | ``1 m`` | ``\\frac person person `` | fraction of persons becoming resistant | \"\"\" md\"\"\" Hence, the infection rate is ``\\alpha \\beta``. This means that a susceptible person meets an infected person ``S I``, this will result in ``2I`` at a rate ``\\alpha \\beta``. Futhermore, an infected person ``I`` will either become a deceased person ``D`` at a rate ``m r`` or become a resistant person ``R`` at rate `` 1 m r`` \"\"\" md\"\"\" Our infection model has three reaction events Infection, where a susceptible persons meets an infected persons and also becomes infected. Deceasing, where an infected person die. Recovery, where an infected person recovers. \"\"\" md\"\"\" Each reaction is also associated with a specific rate ``\\alpha \\beta``, the infection rate. ``m r``, the death rate. `` 1 m r``, the recovery rate. \"\"\" md\"\"\" Hence, the following infection reactions are S I \\xrightarrow \\alpha \\beta 2I I \\xrightarrow mr D I \\xrightarrow 1 m r R \"\"\" md\"\"\" We are going to implement this system of reactions using Catalyst. \"\"\" md\"\"\" We first load the Catalyst package, which is required for the code in this introduction to run \"\"\" md\"\"\" Implementation of the system First we create a reaction network object , that we have named `infection model`, that implements the aforementioned reactions . \"\"\" infection model reaction network begin α β, S I 2I r m, I D r 1 m , I R end md\"\"\" You can get a list of the different reaction species with the command `species` \"\"\" species infection model md\"\"\" The reaction model can be converted to a symbolic differential equation model via \"\"\" osys convert ODESystem, infection model md\"\"\" You can get a list of the differential equations with the command `equations` \"\"\" equations osys md\"\"\" To get a list of the state variables, you can use the command `unknowns` \"\"\" unknowns osys md\"\"\" To get a list of the parameters, you can use the command `parameters` \"\"\" parameters osys md\"\"\" Simulating the system as a Discrete Jump problem \"\"\" md\"\"\" We first need to load the OrdinaryDiffEq and StatsPlots packages, which are required for simulating the system and plotting the results. Additionally, the JumpProcesses package is needed to define and solve Jump problems. \"\"\" md\"\"\" Instead of simulating our model with the species defined as decimal numbers, we will simulate the individual reaction events through the so called Gillespie algorithm . This algorithm is a so called Stochastic Simulation Algorithm SSA .\\ The Gillespie algorithm is a computational method used to simulate discrete and stochastic random processes. The algorithm models the changes in a system over time by considering individual events and their probabilities, this allows to understand how random fluctuations affect the system's behavior. \"\"\" md\"\"\" To illustrate the simulation based on the Gillespie algorithm, we will use the same infection model as before, but considering much less individuals. Hence, we will use different initial conditions, parameter values and timespan as with the ODE problem. \"\"\" md\"\"\" Assume in this example that there are 50 people on the territory, and that initially 1 person is infected. Hence, I 0 1 , S 0 50 I 0 49 , D 0 0 and R 0 0 .\\ Furthermore, we take the following values for the parameters \\alpha 0.15\\ person contact , \\beta 0.1\\ contact person^2\\,day , r 0.2\\ day^ 1 i.e. a person is contagious for an average of 5\\ days and m 0.6 .\\ Finally, we want to run our simulation from day 0 till day 60 . \"\"\" md\"\"\" Setting initial conditions The vector holding the initial conditions for S , I , D and R is \"\"\" u0 S 49, I 1, D 0, R 0 md\"\"\" Setting parameter values The vector holding the parameter values for \\alpha , \\beta , r and m is \"\"\" params α 0.15, β 0.1, r 0.2, m 0.6 md\"\"\" Setting the timespan \"\"\" tspan 0.0, 60.0 md\"\"\" Creating an DiscreteProblem \"\"\" md\"\"\" Unlike the previous approach with ODEProblem denoting a deterministic ordinary differential equation , we wish to simulate our model as a jump process where each reaction event denotes a single jump in the state of the system . We do this by first creating a DiscreteProblem , and then using this as an input to a JumpProblem . \"\"\" md\"\"\" We create a DiscreteProblem by calling the `DiscreteProblem` function. Applying this function ensures that the problem is approached at a level of individual infections reactions . Hence, the variable values will be integers. Note that the order in which the input the model name, the initial condition, the timespan, and the parameter values is provided to `DiscreteProblem` matters Here, we save our DiscreteProblem in the `dprob` variable. \"\"\" dprob DiscreteProblem infection model, u0, tspan, params md\"\"\" Next, we create a so called JumpProblem by calling the `JumpProblem` function. Applying this function ensures that the infections reactions will happen stochastically. Note again that the order in which the input the model name, the DiscreteProblem variable, the simulation method is provided to `JumpProblem` matters The simulation method is denoted by the option `Direct `, which we recommend for now. \"\"\" jprob JumpProblem infection model, dprob, Direct md\"\"\" Solving the DiscreteProblem \"\"\" md\"\"\" Finally, we can simulate our model using the solve function, and plot the solution using the `plot` function. Here, the `solve` function also has a second argument `SSAStepper `, which we recommend for now. This is a time stepping algorithm that calls the `Direct` solver method to advance a simulation. \"\"\" dsol solve jprob, SSAStepper md\"\"\" Note that at the different time points the variables values in the solution are integer numbers and reflect the number of persons in either state S , I , D and R .\\ Futhermore, note that executing the `solve` command at different occasions will result in other solutions because of the stochastic character of the applied method. \"\"\" md\"\"\" Finally, we can plot the solution through the plot function. \"\"\" plot dsol md\"\"\" Below is a piece of code that solves the problem a 1000 times and stores the time values at which the number of infected persons becomes zero. \"\"\" begin times make empty vector while length times 1000 while statement dsol2 solve jprob, SSAStepper solve the problem j findfirst dsol2 I . 0 find index of first 0 if j nothing if index is a valid index append times, dsol2.t j append time to vector times end end end md\"\"\" The vector `times` is now filled with time values at which the number of infected persons becomes zero. \"\"\" times md\"\"\" With this vector we make a histogram so that you can have an idea of the distribution when the infected persons becomes zero. \"\"\" histogram times, bins range 0, 60, length 61 histogram times, bins range 0, 60, length 61 , normalize pdf "},{"url":"exercises/ssa_model_foxes_rabbits/","title":"2. SSA foxes rabbits","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"15\" title \"2. SSA foxes rabbits\" date \"2025 02 07\" tags \"exercises\" description \"SSA foxes rabbits\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils begin add this cell if you want the notebook to use the environment from where the Pluto server is launched using Pkg Pkg.activate \".. .. pluto deployment environment\" end using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, JumpProcesses, StatsPlots using PlutoUI TableOfContents md\"\"\" Exercise foxes and rabbits An ODE and a discrete jump problem \"\"\" md\"\"\" Rabbits live on some secluded territory. Their maximum growth rate coefficient is r year^ 1 , and their population capacity is R m \\ rabbits . The rabbits die of old age or sickness with a dying rate coefficient d year^ 1 . At t 0 , foxes intrude the territory and stay there. The foxes exclusively feed themselves with the rabbits. They hunt the rabbits at a rate proportional to the number of foxes proportionality factor is h year^ 1 \\ foxes^ 1 . The population of foxes grows at a rate proportional to the number of rabbits proportionality factor is g year^ 1 \\ rabbits^ 1 . The foxes die of old age or sickness with a dying rate coefficient \\delta year^ 1 . The initial number of rabbits on the territory is 89, the initial number of foxes intruding the territory is 2. \"\"\" md\"\"\" Exercises 1. Make simulations of the evolution of rabbits and foxes as a ODE problem in the time interval 0, 10 \\ years . 2. Make simulations of the evolution of rabbits and foxes as a discrete jump problem in the time interval 0, 10 \\ years . Assume the following parameter values r 18.4\\ year^ 1 , R m 120\\ \\ rabbits , d 2.0\\ year^ 1 , h 1.4\\ year^ 1 \\ foxes ^ 1 , g 0.05\\ year^ 1 \\ rabbits ^ 1 and \\delta 1.0\\ year^ 1 . \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem. Name it `foxes rabbits rn`. Hints Use the variable names `R` and `F` for the rabbits and foxes respectively. Use the variable names `r`, `Rm`, `d`, `h`, `g` and `δ` for the parameters. \"\"\" Uncomment and complete the instruction foxes rabbits rn reaction network begin species missing parameters missing missing natural population growth of the rabbits missing deaths by age or sickness of the rabbits missing hunting of rabbits by the foxes missing gaining of foxes by hinting rabbits missing deaths by age or sickness of the foxes end md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model makes sense. \"\"\" osys missing Uncomment and complete the instruction md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing Uncomment and complete the instruction md\"\"\" Set the timespan for the simulation \"\"\" tspan missing Uncomment and complete the instruction md\"\"\" Initialize a vector `params` with the parameter values \"\"\" params missing Uncomment and complete the instruction md\"\"\" Exercise 1 Solve the problem as an ODE problem. \"\"\" md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.05`. Store the solution in `osol` \"\"\" osol missing Uncomment and complete the instruction md\"\"\" Plot the solution. \"\"\" missing Uncomment and complete the instruction md\"\"\" Exercise 2 Solve the problem as a Discrete jump problem. \"\"\" md\"\"\" Create a DiscreteProblem and store it in `dprob` \"\"\" dprob missing Uncomment and complete the instruction md\"\"\" Create a JumpProblem and store it in `jdprob`. Use the simulation method `Direct `. \"\"\" jdprob missing Uncomment and complete the instruction md\"\"\" Solve the problem and store it in `jdsol`. Use the `SSAStepper ` stepping algorithm. \"\"\" jdsol missing Uncomment and complete the instruction md\"\"\" Plot the solution. \"\"\" missing Uncomment and complete the instruction md\"\"\" Solve the problem several times by running the cell which solves the problem and see what happens in the plot. \"\"\" md\"\"\" Think of your class of probability theory, why does the SSA model alsways lead to extinction? \"\"\" md\" Answer missing\" md\"\"\" Write a piece of code that solves the problem a 1000 times and stores the time values at which the rabbits die out. \"\"\" Uncomment and complete the instruction begin times make empty vector missing ... end md\"\"\" Make a histogram so that you can have an idea of the distribution when the rabbits die out. Use `bins range 0, 10, length 121 `. \"\"\" missing Uncomment and complete the instruction "},{"url":"exercises/uncert_bitrophic_model/","title":"6. Uncertainty bithropic model","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"34\" title \"6. Uncertainty bithropic model\" date \"2025 08 06\" tags \"exercises\" description \"Uncertainty bithropic model\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using Measurements md\"\"\" Exercise Bitrophic model Uncertainty analysis \"\"\" md\"\"\" In one of the previous practicals we were introduced to a bitrophic model in which the dynamic relationship between a field crop C and a voracious insect population A within an ecosystem was modelled. \\begin eqnarray \\frac dC dt & \\theta C \\left 1 \\frac C k \\right fCA \\\\ \\frac dA dt & \\phi f CA 1 p \\, \\mu A \\end eqnarray \"\"\" md\"\"\" The reaction network object for this model could be set up as \"\"\" bitrophic model reaction network begin θ 1 C k , C 2C f, C A 1 ϕ A 1 p μ, A 0 end md\"\"\" Assume the uncertainties in the following parameter values \\theta 0.20 \\pm 0.02\\ d^ 1 \\phi 0.20 \\pm 0.02 p 3.0 \\pm 0.2 and that the uncertainty in the other parameters values k 4000\\ kg ha , f 0.001\\ ha kg\\,d and \\mu 0.1\\ d^ 1 are negligible. Suppose that at the beginning of a season, 100\\ kg of the crop and 0.5\\ kg of insects per ha are present. Perform an uncertainty analysis by plotting the uncertainty bands on the simulation results of C and A in a timespan of 0, 200 \\,days . Interpret your results. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions, and set the timespan \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction md\"\"\" We initialize a vector `params uncert` with the parameter values and their corresponding uncertainty \"\"\" params uncert missing Uncomment and complete the instruction md\"\"\" We create the corresponding ODE problem and store it in `oprob uncert` \"\"\" oprob uncert missing Uncomment and complete the instruction md\"\"\" We solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert` \"\"\" osol uncert missing Uncomment and complete the instruction md\"\"\" Plot the results simulation of the output variables C and A together with their uncertainty band \"\"\" missing Uncomment and complete the instruction md\"\"\" Try to relate the local sensitivity analysis to the uncertainty analysis. Hence, study the effect of the individual parameter uncertainties on the output variables C and A and compare with your local sensitivity results of the corresponding parameter. In order to do that, analyse the effect on the uncertainty bands for C and A by taking one uncertainty on a parameter at a time. In other words, analyse the subsequent cases separately Assume uncertainty only in \\theta Assume uncertainty only in \\phi Assume uncertainty only in p \"\"\" md\"\"\" question Draw your conclusions. \"\"\" md\" Answer missing\" "},{"url":"exercises/uncert_fermenter_monod/","title":"6. Uncertainty fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"33\" title \"6. Uncertainty fermenter monod\" date \"2025 08 06\" tags \"exercises\" description \"Uncertainty fermenter monod\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using Measurements md\"\"\" Exercise Fermenter Monod kinetics Uncertainty analysis \"\"\" md\"\"\" In one of the previous practicals we were introduced to a fermenter in which biomass X g L grows by breaking down substrate S g L . The reactor is fed with a inlet flow rate Q in L h , which consist of a manipulable input concentration of substrate S in g L . This process was modelled using Monod kinetics, resulting in the model below \\begin eqnarray S X \\xrightarrow \\quad\\quad k 1 Y \\, X \\quad\\quad\\quad\\quad \\textrm with \\quad k \\cfrac \\mu max S K s \\end eqnarray \"\"\" md\"\"\" The reaction network object for this model could be set up as \"\"\" fermenter monod reaction network begin μmax S Ks , S X 1 Y X Q V, S, X 0 Q V Sin, 0 S end md\"\"\" which resulted in the following differential equations \"\"\" md\"\"\" \\begin eqnarray \\cfrac dS dt & & \\cfrac Q V \\left S in S \\right \\mu max \\cfrac S S K s X\\\\ \\cfrac dX dt & & \\cfrac Q V X Y \\mu max \\cfrac S S K s X \\end eqnarray \"\"\" osys missing md\"\"\" Assume the uncertainties in the following parameter values \\mu max 0.40 \\pm 0.06\\ h^ 1 K s 0.015 \\pm 0.003 \\ g L S in 0.022 \\pm 0.004\\ g L and that the uncertainty in the other parameters values Y 0.67 , Q 2.0\\ L h , V 40.0\\ L are negligible. Suppose that at t 0 no substrate S is present in the reactor but that there is initially some biomass with a concetration of 0.0005\\ g L . Perform an uncertainty analysis by plotting the uncertainty bands on the simulation results of S and X in a timespan of 0, 100 \\,h . Interpret your results. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions, and set the timespan \"\"\" u0 missing Uncomment and complete the instruction tspan missing Uncomment and complete the instruction md\"\"\" We initialize a vector `params uncert` with the parameter values and their corresponding uncertainty \"\"\" params uncert missing Uncomment and complete the instruction md\"\"\" We create the corresponding ODE problem and store it in `oprob uncert` \"\"\" oprob uncert missing Uncomment and complete the instruction md\"\"\" We solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert` \"\"\" osol uncert missing Uncomment and complete the instruction md\"\"\" Plot the results simulation of the output variables S and X together with their uncertainty band \"\"\" missing md\"\"\" Try to relate the local sensitivity analysis to the uncertainty analysis. Hence, study the effect of the individual parameter uncertainties on the output variables S and X and compare with your local sensitivity results of the corresponding parameter. In order to do that, analyse the effect on the uncertainty bands for S and X by taking one uncertainty on a parameter at a time. In other words, analyse the subsequent cases separately Assume uncertainty only in \\mu max Assume uncertainty only in K s Assume uncertainty only in S in \"\"\" md\"\"\" question Draw your conclusions. \"\"\" md\" Answer missing\" "},{"url":"exercises/uncert_intro/","title":"6. Uncertainty intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"32\" title \"6. Uncertainty intro\" date \"2025 08 06\" tags \"exercises\" description \"Uncertainty intro\" layout \"layout.jlhtml\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using Catalyst using OrdinaryDiffEq, StatsPlots using PlutoUI TableOfContents using Measurements md\" Introduction to uncertainty analysis \" md\" Goal of this practicum \" md\" Parameter uncertainty plays a crucial role in shaping the behavior of output variables. Model equations describe how systems evolve over time, often incorporating parameters representing various aspects of the system's characteristics. However, these parameters are rarely known with absolute certainty and they often come with inherent uncertainty due to measurement errors, variability in real world conditions, or incomplete knowledge about the system. This uncertainty can propagate through the model equations, leading to uncertainties in the predicted outcomes. Consequently, understanding the influence of parameter uncertainty becomes essential for assessing the reliability and robustness of the model predictions, as well as for making informed decisions based on these predictions. Techniques such as sensitivity analysis and uncertainty quantification are employed to explore and quantify the impact of parameter uncertainty on the output variables, providing insights into the system's behavior and guiding the refinement of models for improved accuracy and reliability. \" md\" Uncertainty in model parameters manifests as variability in the predicted outcomes, resulting in error bars around the output variables. These error bars represent the range of potential values that the output variables could take due to the uncertainty in the parameters. As the uncertainty in parameters increases, the width of these error bars typically expands, reflecting the increased variability and unpredictability in the model's predictions. \" md\" We will now compute the variability in the output variables reflected as error bars assuming some uncertainty in the model parameters. To illustrate this concept, we first revisit the two simple models modelling the growth of grass. \" md\" Grass growth models \" md\" In this notebook, two different models will be used, each modelling the yield of grass in a grassland Logistic growth model \\cfrac dW dt \\mu \\left 1 \\cfrac W W f \\right W Exponential growth model \\cfrac dW dt \\mu \\left W f W \\right with output W the grass yield, and W f and \\mu parameters. The table below show some typical values for the parameters together with their uncertainties | | \\mu | W f | | | | | | Logistic | 0.07 \\pm 0.02 | 10.0 \\pm 0.15 | | Exponential | 0.02 \\pm 0.01 | 10.0 \\pm 0.15 | We will use an initial condition of W 0 2.0 for each and a simulation time of 100 days. \" md\"\"\" We will illustrate how to compute the error bars, due to model parameter uncertainty, in conjunction with the output variable simulation for the logistic model. The same will be left as exercises below for the exponential model. Important We will use consequently ` log` and ` exp` appended to relevant variables names in order to indicate their model origin and to prevent cell disabling that occurs when using the same variables names in these Notebooks. \"\"\" md\"\"\" Uncertainty analysis of the logistic growth model We will start by modelling our system and simulating using the aforementioned parameters values and uncertainties, initial condition and timespan. \"\"\" md\"\"\" Implementation of the system \"\"\" growth mod log reaction network begin μ 1 W Wf , W 2W end md\"\"\" Convert the reaction model to check that we work with the correct differential equation \"\"\" osys log convert ODESystem, growth mod log md\"\"\" In order to use uncertainties in the parameter values, we will need to load the package `Measurements` \"\"\" md\"\"\" Setting initial conditions \"\"\" u0 log W 2.0 md\"\"\" Set the timespan for the simulation \"\"\" tspan 0.0, 100.0 md\"\"\" In order to see the effect of the individual parameter uncertainties on the output variable, we will analyse this only for didactical reasons assuming different study cases 1. \\mu has a nominal value of 0.07 and a standard deviation of 0.02 while W f is exactly known and equal to 10.0 . 2. W f has a nominal value of 10.0 and a standard deviation of 1.5 while \\mu is exactly known and equal to 0.07 . 3. \\mu has a nominal value of 0.07 and a standard deviation of 0.02 and similarily W f has a nominal value of 10.0 and a standard deviation of 1.5 . The last study case is the true case because in reality all uncertainties will contribute at once. \"\"\" md\"\"\" tip \"Tip\" If `p` is a parameter and `σₚ` is its standard deviation, then we will define the uncertainty in the parameter `p` as `p ± σₚ` in the vector with parameters and uncertainties. The symbol `±` can be visualized by typing a backslash followed by the letters `pm` p lus m inus and then the TAB key. \"\"\" md\"\"\" Study case 1 \"\"\" md\"\"\" We initialize a vector `params1 uncert log` with the parameter values and uncertainty standard deviation only in the first parameter \"\"\" params1 uncert log μ 0.07±0.02, Wf 10.0 md\"\"\" We create the corresponding ODE problem and store it in `oprob1 uncert log` \"\"\" oprob1 uncert log ODEProblem growth mod log, u0 log, tspan, params1 uncert log md\"\"\" We solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol1 uncert log` \"\"\" osol1 uncert log solve oprob1 uncert log, Tsit5 , saveat 2.0 md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" plot osol1 uncert log md\"\"\" When thinking back of the sensitivity of W to the parameter \\mu , we saw that the corresponding sensitivity function had a maximum around t 33\\ s . Looking at the above plot with error bars, we can see that the largest error bars largest uncertainty in the output variable occurs at the time points where the sensitivity is strongest. \"\"\" md\"\"\" Study case 2 \"\"\" md\"\"\" We initialize a vector `params2 uncert log` with the parameter values and uncertainty standard deviation only in the second parameter \"\"\" params2 uncert log μ 0.07, Wf 10.0±1.5 md\"\"\" We create the corresponding ODE problem and store it in `oprob2 uncert log` \"\"\" oprob2 uncert log ODEProblem growth mod log, u0 log, tspan, params2 uncert log md\"\"\" We solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol2 uncert log` \"\"\" osol2 uncert log solve oprob2 uncert log, Tsit5 , saveat 2.0 md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" plot osol2 uncert log md\"\"\" When thinking back of the sensitivity of W to the parameter W f , we saw that the corresponding sensitivity function was strongest in the tail of the curve around the steady state value. Looking at the above plot with error bars, we can see that the largest error bars largest uncertainty in the output variable occurs at the tail of the curve where the sensitivity is strongest. \"\"\" md\"\"\" Study case 3 \"\"\" md\"\"\" We initialize a vector `params uncert log` with the parameter values and uncertainty standard deviation in all parameters \"\"\" params uncert log μ 0.07±0.02, Wf 10.0±1.5 md\"\"\" We create the corresponding ODE problem and store it in `oprob uncert log` \"\"\" oprob uncert log ODEProblem growth mod log, u0 log, tspan, params uncert log md\"\"\" We solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert log` \"\"\" osol uncert log solve oprob uncert log, Tsit5 , saveat 2.0 md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" plot osol uncert log md\"\"\" Now we see the combined effect of uncertainty in both parameters, \\mu and W f . \"\"\" md\"\"\" Exercise Uncertainty analysis of the exponential growth model Perform an uncertainty analysis of the exponential growth model. Use the parameter uncertainties mentioned in the Table in the Grass growth models sections. \"\"\" md\"\"\" A possible reaction network object for the exponential growth model can be implemented as follows \"\"\" growth exp reaction network begin μ Wf, μ , 0 W end md\"\"\" The vector `u0 exp` with the initial condition is \"\"\" u0 exp missing md\"\"\" Initialize a vector `params uncert exp` with the parameter values and their uncertainties standard deviation in all parameters .\\ Remark you can use the same variable and leave a single uncertainty if you want to see the effect of the uncertainty in only one parameter later on. \"\"\" params uncert exp missing Uncomment and complete the instruction md\"\"\" Create the corresponding ODE problem and store it in `oprob uncert exp` \"\"\" oprob uncert exp missing Uncomment and complete the instruction md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert exp` \"\"\" osol uncert exp missing Uncomment and complete the instruction md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" missing Uncomment and complete the instruction md\"\"\" Draw your conclusions \"\"\" md\" missing\" "},{"url":"homework/hw1/","title":"sample homework","tags":["module2","track_julia","track_material","homeworks","pluto","PlutoTeachingTools"],"text":" A Pluto.jl notebook v0.20.6 frontmatter homework number \"1\" order \"2.5\" title \"sample homework\" tags \"module2\", \"track julia\", \"track material\", \"homeworks\", \"pluto\", \"PlutoTeachingTools\" layout \"layout.jlhtml\" description \"sample howework\" using Markdown using InteractiveUtils using PlutoTeachingTools, PlutoUI md\"\"\" Sample Homework This notebook showcases some of the features of `PlutoTeachingTools.jl` https github.com JuliaPluto PlutoTeachingTools.jl and how to use these to write homework assignment in Pluto. \"\"\" tip md\"\"\"For a deeper tour of `PlutoTeachingTools.jl`, check their documentation https juliapluto.github.io PlutoTeachingTools.jl example.html \"\"\" md\"\"\" Useful functionalities `PlutoTeachingTools.jl` has some functions like `correct`, `still missing`, here a few demoes \"\"\" correct still missing keep working keep working md\"you can also give custom text to the boxes\" hint md\"this is a hint, hover the box to unblur the text\" md\"\"\" Exercise 1 a simple exercise Replace missing with the value `1`. \"\"\" x missing if ismissing x still missing elseif x 1 && x isa Int correct elseif x 1 && x isa Int b1 almost md\"\"\"Your variable has the right value, but it's not quite the right answer. Read carefully the instructions\"\"\" b2 hint md\"\"\"What type should the value of x be?\"\"\" md\"\"\" b1 b2 \"\"\" else keep working md\"\"\"That is not the right answer Keep trying \"\"\" end md\"\"\" here is a short demo of how it looks like when the student tries to solve the exercise \"\"\" Resource \"https user images.githubusercontent.com 49938764 249749643 8cc12de3 2b50 4182 b95d 686c2c18332c.mov\", width 500, autoplay \"\", loop \"\" md\"\"\" Exercise 2 Write a function called `myfun` that takes as input an integer and returns its square. Define a variable called `y` and assign `myfun 3 ` to it. \"\"\" let if isdefined myfun func not defined myfun else test values 1, 2, 3, 4, 5 msg1 correct for t in test values if myfun t t^2 msg1 keep working md\"Test failed for input t, expected t^2 , but got myfun t \" break end end msg1 end end if isdefined y var not defined y elseif y 9 correct else keep working md\"Evaluated expression y y is incorrect.\" end md\"\"\" and here is a quick demo of the exercise in action \"\"\" Resource \"https user images.githubusercontent.com 49938764 249748007 d0b2d773 6b21 49d4 89db ad737af510fe.mov\", width 500, autoplay \"\", loop \"\" "},{"url":"mod1_setup_website/basic_info/","title":"Fill course basic information","tags":["module1","track_setup","teaching","metadata"],"text":"Add basic informationIf you look at the homepage of the template website, you will see it has a bunch of placeholder text, such as “name of your course”, “a short catchy phrase” etc.To customize this, you will need to customize the metadata of the website. That is, add basic info for your class.To do so, you will need to fill the info in the files under the folder src/_data. Let us analyze these one by one.course_info.jlThis file contains a julia Dict with the basic info of the class. For each key (course_name, course_subtitle, etc.) replace the corresponding placeholder with an appropriate text for your class.When filling the institution_logo data with the name of your university logo file, do not forget to actually put the file under src/assets.Authors are listed as a vector of pairs, where the first element is the author name and the second is their homepage address. If you dont have a homepage address for the author, put an empty string \"\".homepage.jlThis file contains metadata for the info displayed in the homepage, particularlytitle: the title displayed on top of the homepagedisclaimer: the disclaimer displayed below the title. If you don’t want a disclaimer, you can remove this entry.highlights: in this entry you can specify the highlights of your class, which will be displayed on the homepage. This entry should be a vector of highlights. Each entry in the vector should be a dict with the following fields\nname: the title of the highlighttext: short description of the highlightimg: link to an image summarizing the highlightsidebar.jlIn this file you can specify the sidebar of the website. All lecture materials will be grouped in modules in the sidebar, which are defined in this file.The modules in the file are specified as a vector of pairs, in the formmodule_id => module_title\nfor example\"module1\" => \"Week 1: Introduction to the class\"\nTo link a file to a module, you will need to add the module identifier in the page tags. For more info about this, see Add frontmattertracks.jlIn this file you will specify tracks. Tracks can be used to group lectures across modules, e.g. if they have a commmon theme. When a track is selected on the sidebar, only the pages\nbelonging to that track will be highlighted.Similar to modules, tracks are stored in a vector of pairs in the formtrack_id => track_title\nfor example\"julia\" => \"💻 Julia programming\"\nTo link a file to a track, you will need to add the track id, prefixed with track_, to the tags of the page. For example, to add a lesson to the julia track defined above, you would add the tag track_julia to the tags of that lesson file.LicenseChoosing an appropriate license is important to make your material properly reusable.For text, popular licenses are Creative Commons, for example CC BY-SA 4.0For code, an OSI open source license is recommended. For example MIT or Apache 2.0 license.To add the license, open the file LICENSE.md and replace the text<insert license for your material judge>\nwith your license(s)."},{"url":"mod1_setup_website/getting_started/","title":"Getting started","tags":["module1","track_setup","teaching","repository structure"],"text":"Fork the templateGo to the template repository and click Use this template on the top-right corner. This will fork the repository under your github profile.Folder structureLet us have a look at what this repository looks like. The most important folder, where you will be mainly working is src. Here you will place all your lecture materials. So let us take a closer look at this.Opening the src folder, you will see the following_data folder: here you will place metadata about your website (university name, class semester, define tracks, etc.), more on this in the next lesson._include: This folder contains the layout templates that are used to generate the final pages on your website. Unless you want to tweak the layout, you will not need to modify this.assets: in this folder you can place all attachements, such as your university logo and other pictures. The folder also contains the CSS and scripts used to render the website.That was for the “infrastructure part” of the website, the rest is content! To add new pages to your website, simply them under the src folder. You can group them in subfolders, as done in this template, but that is not a strict requirement.When downloading this template, you will get the following material:installation.md: this page contains instructions on how to install Julia and Pluto. If you find it useful, you may keep it as is, or edit to match your wanted installation instructions.cheatsheets.md: contains a list of julia related resources. Again, you can keep it or remove it.logistics.md: empty markdown page, where you can describe the logistics of your classindex.jlmd: this is used to render the homepage. Do not remove or modify this!search.md: this is used to render the search tab on the sidebar, do not modify or remove this file.The remaining foldersmod1_setup_websitemod2_add_materialmod3_publish_websitehomeworkare placeholder samples, used to showcase what a deployed website looks like. As a bonus, these placeholder files actually document how to use this template.  You can read it and see what the final result looks like on the template webpage.When starting adding your course material, you will most likely want to remove these."},{"url":"mod1_setup_website/working_locally/","title":"Working locally","tags":["module1","track_setup","track_julia","PlutoSliderServer","pluto"],"text":"Working locallyOpen this repository in VS Code, and install the recommended extensions.To start running the development server, open the VS Code command palette (press Cmd+Shift+P), and search for Tasks: Run Task, then PlutoPages: run development server. The first run can take some time, as it builds up the notebook outputs cache. Leave it running.This will start two things in parallel: the PlutoPages.jl notebook (which generates the website), and a static file server (with Deno_jll). It will open two tabs in your browser: one is the generation dashboard (PlutoPages), the other is the current site preview (Deno_jll).Whenever you edit a file, PlutoPages will automatically regenerate! Refresh your browser tab. If it does not pick up the change, go to the generation dashboard and click the “Read input files again” button.Note!: This workflow is recommended for writing static content, styles, and for site maintenance. But for writing Pluto notebooks, it’s best to prepare the notebook first, and then run the site (because it re-runs the entire notebook on any change)."},{"url":"mod2_add_material/add_markdown/","title":"Add markdown files","tags":["module2","track_material","markdown","frontmatter"],"text":"Add markdown filesIf your lecture does not need to run code or use interactivity. You can write it as a markdown file.As an extra twist, you can evaluate julia code inside a $ symbol. For example,$(1 + 1)\nwill become2Add Front-matterFor each file, markdown or pluto, you will need to add a front-matter, which specifies the page metadata. For markdown files, the front-matter is specified at the top of the file between three dashes ---. For example, the front-matter of this file is---\ntitle: \"Add markdown file\"\norder: 1\nchapter: 2\nsection: 1\nlayout: \"md.jlmd\"\ntags: [\"module2\", \"track_material\", \"markdown\", \"frontmatter\"]\n---\nYou will need to specify the following attributestitle: title of the pageorder: the position of the page in the module on the sidebar. Hint!: You can also use fractional numbers, e.g. 1.5. This can be handy for homeworks, so you can include the homework between the first and second lesson without messing up lessons counting.layout: set to \"md.jlmd\", unless you are using some custom layoutchapter and section (optional): used to number the page. If for example chapter=1 and section=2, the page will be displayed as 1.2 on the sidebar and page header.image (optional): link to summarizing image to display in the subjects section on the homepage. If left empty, the page wont be included in the subjects section. If no page has an image field in the front-matter, the subjects section is not displayed.description (optional): short description of the notebookyoutube_id (optional): youtube id of the video associated with the page. If included, the page header will embed the youtube video.homework_number: needed only for homeworks, the number of the homeworktags: list of keywords for the page. It should at least include the module name, as defined in _data/sidebar.jl to include the page in the sidebar. You can also associate pages to a given track by adding the track id, prefixed with track_ to the tags. For example, if you want to include the page in the julia track, add track_julia in the tags list.Markdown 101If you are not familiar with markdown, you can see for example here. Here is a quick and dirty cheatsheetUse # for headers, for example# Header\n## Subheader\n### Sub-sub-header\nYou can create links with the syntax[text](adddress)\nFor example the link to the mardown tutorial above was typed as[here](https://www.markdowntutorial.com/)\nYou can insert pictures with the syntax![optional alternative text](link-to-picture)\nfor example![](https://raw.githubusercontent.com/JuliaLang/julia-logo-graphics/master/images/julia-logo-color.png)\nwill give"},{"url":"mod2_add_material/add_pluto/","title":"Add Pluto notebooks","tags":["module2","track_julia","track_material","Pluto","PlutoUI"],"text":" A Pluto.jl notebook v0.19.25 frontmatter chapter 2 section 2 order 2 image \"https raw.githubusercontent.com fonsp Pluto.jl 580ab811f13d565cc81ebfa70ed36c84b125f55d demo plutodemo.gif\" title \"Add Pluto notebooks\" tags \"module2\", \"track julia\", \"track material\", \"Pluto\", \"PlutoUI\" layout \"layout.jlhtml\" using Markdown using InteractiveUtils using PlutoTeachingTools, PlutoUI TableOfContents md\"\"\" Add Pluto notebooks Pluto.jl https plutojl.org is a revolutionary text editor for reactive and interactive programming. To start creating a Pluto notebook, open a terminal and launch Julia, then do ```julia using Pluto Pluto.run ``` This will launch a Pluto session, where you can write your notebook. To add the front matter, you can use Plut FrontmatterGUI, as the following short video clip shows. danger md\"For pluto notebooks, you will need to set layout to layout.jlhtml\" \"\"\" html\"\"\" video controls \"controls\" width \"800\" height \"600\" name \"Video Name\" source src \"https user images.githubusercontent.com 6933510 207080363 b912d591 f6f6 4522 a6fe 701e5ab04f0b.mov\" video \"\"\" md\"\"\" Pluto 101 Pluto is a notebook for Julia It is reactive , lightweight and has powerful interactivity tools . This will allow you to make your lesson material more engaging for students. Here are a few highlights of Pluto. tip md\" To learn more, check out Pluto featured notebooks https featured.plutojl.org , the JuliaCon video at the beginning of this notebook, or the presentations at PlutoCon 2021 https www.youtube.com playlist?list PLP8iPy9hna6T5sNOTeGdiqygHe 09geEW .\" Writing code in Pluto In Pluto code is written in cells, to add some code, simply create a new cell and type in. Each cell should contain 1 julia expression function definition, if statement, variable assignment, etc. . ```julia if rand 0.5 \"hi\" else \"there\" end ``` or ```julia a 1 ``` or ```julia function f return rand ^ 2 end ``` However , multiple expressions in the same cell are not allowed, for example ```julia a 1 b 2 a b ``` cannot be written in the same cell. You have two alternatives 1. Split it into multiple cells recommended to make reactivity better . 2. Wrap your staments inside a `begin ... end` or `let ... end` block. The difference is that the latter introduces a local scope, hence variables defined inside `let` are not visibles from outside. Reactivity Pluto is reactive This means that if you define a variable `a` in a cell, when you edit the variable value, all cells depending on that variable are automatically re evaluated. A few notes 1. As mentioned above, better to have a single variable assignment per cell, this will make the dependency graph slimmer and reactivity smoother. 2. Code modifying a given variable should be in the same cell, i.e. you cannot have two cells modifying the same variable. Here is a summarizing demo \"\"\" Resource \"https raw.githubusercontent.com fonsp Pluto.jl 580ab811f13d565cc81ebfa70ed36c84b125f55d demo plutodemo.gif\", width 350 md\"\"\" Built in environment Pluto is designed with reproducibility in mind To use packages registered in the Julia general registry, just type `using MyPackage` in some cells, as done at the beginning of this notebook. Pluto will automatically download the package The `Project.toml` and `Manifest.toml` what Julia uses to record all libraries, their versions and dependencies are stored inside the notebook, making it fully batteries included Resource \"https user images.githubusercontent.com 6933510 134823403 fbb79d7f dd3e 4712 b5d5 b48ad0770f13.gif\", width 400 \"\"\" md\"\"\" Interactivity Pluto has great support to make your notebooks interactive It allows you to associate variables with sliders and buttons that you can use to interactively change the result of the code. https user images.githubusercontent.com 6933510 136196607 16207911 53be 4abb b90e d46c946e6aaf.gif The easiest way to harness the power of Pluto interactivity is to use PlutoUI.jl https github.com juliapluto PlutoUI.jl , which is showcased in the next lecture https juliapluto.github.io mod2 add material plutoui showcase . \"\"\" "},{"url":"mod2_add_material/plutoui_showcase/","title":"PlutoUI showcase","tags":["module2","track_julia","track_material","Pluto","PlutoUI","interactivity"],"text":" A Pluto.jl notebook v0.19.25 frontmatter chapter \"2\" image \"https user images.githubusercontent.com 6933510 174067690 50c8128d 748b 4f50 8a76 2ce18166642b.png\" order \"3\" section \"3\" title \"PlutoUI showcase\" tags \"module2\", \"track julia\", \"track material\", \"Pluto\", \"PlutoUI\", \"interactivity\" layout \"layout.jlhtml\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end end using PlutoUI md\"\"\" PlutoUI.jl Pluto notebooks can use ` bind` to add interactivity to your notebook. It's a simple concept it uses the same reactivity that you have when editing code, except now you use sliders and buttons, instead of editing code. This notebook showcases some features of `PlutoUI.jl` , which allows you to easily add interactivity to your notebooks. This notebook is from Pluto featured notebooks https featured.plutojl.org , make sure to also check the others to learn more cool Pluto tricks \"\"\" md\"\"\" To use it in other notebooks Simply import the `PlutoUI` package, and Pluto's built in package manager takes care of the rest \"\"\" TableOfContents This is all you need to get a nice table of content md\"\"\" Basics \"\"\" md\" Slider\" bind x Slider 5 15 x md\"The first argument is a `Vector` or range. You can set the default value using a keyword argument \" bind y Slider 20 0.1 30, default 25 y md\"\"\" Scrubbable `Scrubbable` makes a number interactive, you can click and drag its value left or right. Try it in the text below \"\"\" md\"\"\" If Alice has bind a Scrubbable 20 apples, and she gives bind b Scrubbable 3 apples to Bob... \"\"\" md\"\"\" ...then Alice has a b apples left. \"\"\" md\"\"\" Use the Live Docs to learn more about `Scrubbable` \"\"\" md\" NumberField A `NumberField` can be used just like a `Slider`, it just looks different \" bind x different NumberField 0 100, default 20 md\" CheckBox\" bind z CheckBox z md\"Default value \" bind having fun CheckBox default true having fun having fun ? md\"🎈🎈\" md\"☕\" md\" TextField\" bind s TextField s md\"With a default value \" bind sentence TextField default \"te dansen omdat men leeft\" sentence md\"You can also create a multi line text box \" bind poem TextField 30, 3 , \"Je opent en sluit je armen,\\nMaar houdt niets vast.\\nHet is net zwemmen\" poem by Sanne de Kroon split poem, \"\\n\" md\" Select\" bind vegetable Select \"potato\", \"carrot\" vegetable bind favourite function Select sin, cos, tan, sqrt favourite function 2 md\"Instead of an array of values, you can also give an array of pairs , where the first item is the bound value, and the second item is displayed. \" bind fruit Select \"apple\" \"🍎\", \"melon\" \"🍉\" fruit md\"\"\" MultiSelect This widget allows the user to select multiple element by holding `Ctrl` `Cmd` while clicking a more items. \"\"\" bind vegetable basket MultiSelect \"potato\", \"carrot\", \"boerenkool\" vegetable basket md\"Just like `Select`, you can also give an array of pairs.\" md\"\"\" MultiCheckBox This widget allows the user to select multiple elements using checkboxes. \"\"\" bind fruit basket MultiCheckBox \"apple\", \"blueberry\", \"mango\" fruit basket md\"\"\" You can use `MultiSelect` and `MultiCheckBox` with any vector of objects, not just strings \"\"\" bind my functions MultiCheckBox sin, cos, tan f π for f in my functions md\"Just like `Select`, you can also give an array of pairs. See the Live Docs for `MultiCheckBox` for all the customization options \" md\" Button\" bind clicked Button \"Hello world\" clicked md\"\"\" Button as reactive trigger In the example above, any cell that references `clicked` will re evaluate when you click the button. This means that you can a button as a reactive trigger , by referencing its value in another cell. \"\"\" bind go Button \"Recompute\" let go md\"I am rand 1 15 years old \" end md\" FilePicker\" bind important document FilePicker important document md\"The file picker is useful if you want to show off your notebook on a dataset or image uploaded by the reader . It will work anywhere you don't access files using their path. The caveat is that large files might take a long time to get processed everything needs to pass through the browser. If you are using large datasets, a better option is to use `Select` to let the reader pick a filename. You can then read the file using `Base.read filename, type `\" md\" Extras\" md\" Clock\" bind t Clock t md\"You can set the interval `5.0` seconds , and disable the UI `true` \" bind t slow Clock 5.0, true t slow md\"You can use a `Clock` to drive an animation Or use it to repeat the same command at an interval just like with `Button`, you can reference a bound reactive variable without actually using it \" md\" DownloadButton\" md\"\"\" The download button is not an input element that you can ` bind` to, it's an output that you can use to get processed data from your notebook easily. The second argument is the output filename . \"\"\" DownloadButton poem, \"poem.txt\" DownloadButton 0x01, 0x02, 0x03 , \"secret data.bin\" md\"\"\" High level inputs \"\"\" md\"\"\" Confirm Normally, when you move a `Slider` ref or type in a `TextField` ref , all intermediate values are sent back to ` bind`. By wrapping an input element in `confirm`, you get a button to manually control when the value is sent , intermediate updates are hidden from Pluto. \"\"\" bind distance confirm Slider 1 100 distance md\"\"\" `confirm` can be wrapper around any input element to create a new one, including inputs from other packages, or inputs that you have made yourself \"\"\" md\"\"\" Combine This next high level component is a bit tricky, but very powerful Using `combine`, you can create a single input out of multiple existing ones In the example below, we create a new input, `wind speed input` . Notice that the list of wind directions is dynamic if you add a new direction, a 5th slider will appear \"\"\" import PlutoUI combine function wind speed input directions Vector return combine do Child inputs md\"\"\" name Child name, Slider 1 100 \"\"\" for name in directions md\"\"\" Wind speeds inputs \"\"\" end end bind speeds wind speed input \"North\", \"East\", \"South\", \"West\" speeds speeds.North md\"\"\" Use the Live Docs to learn more about `combine` and to see additional examples. 🙋 `combine` is very useful in combination with HypertextLiteral.jl https github.com MechanicalRabbit HypertextLiteral.jl , which you can learn using our JavaScript sample notebook. \"\"\" md\"\"\" Loading resources Notebooks use data from different places. For example, you use `Base.read` https docs.julialang.org en v1 base io network ~ text read filename%3A%3AAbstractString%2C%20String to access local data files inside your Julia code, and `Downloads.jl` https github.com JuliaLang Downloads.jl for remote data interwebs . `PlutoUI` helps you communicate with the person reading the notebook To get remote media URL inside your Markdown text , use `PlutoUI.Resource`. To get local media file inside your Markdown text , use `PlutoUI.LocalResource`. With media , we mean images , video and audio. We strongly recommend that you use remote media inside Pluto notebooks If your notebook uses local images, then those images will not show when someone else opens your notebook, unless they have the same images on their computer, at the exact same location. More on this later. \"\"\" md\"\"\" Resource If you just want to show images inside Markdown , you can use the built in syntax without `PlutoUI` ``` md\"Here is a dog https fonsp.com img doggoSmall.jpg \" ``` `PlutoUI.Resource` has some extra features specify image dimensions and spacing support for videos support for audio\"\"\" dog url \"https upload.wikimedia.org wikipedia commons thumb 1 15 Welsh Springer Spaniel.jpg 640px Welsh Springer Spaniel.jpg\" Resource dog url, width x x different t rex url \"https upload.wikimedia.org wikipedia commons transcoded 6 62 Meow.ogg Meow.ogg.mp3\" flower url \"https upload.wikimedia.org wikipedia commons 4 41 Sunflower Flower Opening Time Lapse.ogv\" md\"\"\"Hello I am a dog Resource dog url \"\"\" md\"\"\"And I sound like this Resource t rex url \"\"\" md\"\"\"This is my flower friend Resource flower url, width 200 \"\"\" md\" Attributes You can pass additional HTML attributes to `Resource`, these will be added to the element. For example \" md\"\"\" Resource dog url, width 20 Resource dog url, width 50 Resource dog url, width 100 Resource dog url, width 100, style \"filter grayscale 100% border 3px solid black \" \"\"\" Resource flower url, width 200, autoplay \"\", loop \"\" md\" YouTube, Vimeo, etc. If you use `Resource` for video, the URL has to point to a video file like `.mp4` or `.mov` . Popular video sites don't give you that link, instead, you can use their embed codes . You can find these inside the video player, by right clicking or using the menu buttons. You then use that inside an HTML block ``` html\\\"\\\"\\\" ~ paste embed code here ~ \\\"\\\"\\\" ``` You might need to change the `width` to `100%` to make it fit.\" html\"\"\" div style \"padding 56.25% 0 0 0 position relative \" iframe src \"https player.vimeo.com video 438210156\" style \"position absolute top 0 left 0 width 100% height 100% \" frameborder \"0\" allow \"autoplay fullscreen\" allowfullscreen iframe div script src \"https player.vimeo.com api player.js\" script \"\"\" md\" LocalResource not recommended The examples above use `Resource` to make media from a URL available inside Markdown. To use local files , simply replace `Resource` with `LocalResource` , and use a file path instead of a URL.\" html\" span style 'font family cursive color purple ' I really hope that this works span \" md\"\"\"Hello I am a dog LocalResource \"C \\\\Users\\\\fons\\\\Pictures\\\\hannes.jpg\" \"\"\" md\"\"\" html\" span style 'font family cursive color purple ' OOPS span \" , it didn't html\" br \" Here are two tips for getting local images to work correctly 1. Go to imgur.com https imgur.com and drag&drop the image to the page. Right click on the image, and select \"Copy image location\". You can now use the image like so ```PlutoUI.Resource \"https i.imgur.com SAzsMMA.jpg\" ``` 2. If your notebook is part of a git repository, place the image in the repository and use a relative path ```PlutoUI.LocalResource \".. images cat.jpg\" ``` \"\"\" md\" Why does it have to be so difficult? Pluto only stores code in the notebook file, not images. This minimal file format is very valuable, but it means that images need to be addressed , not stored. Addressing local files is fragile if someone else opens the notebook, or if you move the notebook to a different folder, that image file needs to be available at exactly the same path. This is difficult to do correctly, and if it works for you, it is hard to tell if it will work for someone else. Putting images online might be a hassle, but once it works, it will work everywhere The stateless nature of URLs means that the images will work regardless of how the notebook file is accessed, while keeping a minimal file format.\" md\" PlutoUI without Pluto Huh? Did you know that you can run Pluto notebooks without Pluto ? If your notebook is called `wow.jl`, then ```sh julia wow.jl ``` will run the notebook just fine. When you use ` bind`, your notebook can still run without Pluto Sort of. Normally, all bound variables are assigned the value `missing` when you run it elsewhere. However, the `PlutoUI` types have all been configured to assign a more sensible default value. For example, if your notebook contains ```julia bind x Slider 10 20 ``` and you run it without Pluto, then this statement simply assigns `x 10`. \" md\"`Pluto` and `PlutoUI` work independently of each other In fact, you could write a package with fun input elements, or add ` bind`able values to existing packages.\" md\" Appendix\" space html\" br br br \" space space space space space "},{"url":"mod3_publish_website/deploy_static/","title":"Deploy your website as static","tags":["module3","track_setup","deploy","netlify","github actions","github pages"],"text":"Deploying with github pagesDeploying your website as static page with github pages is a breeze.Whenever you push to main, the website will be deployed to a branch called gh-pages. All you need to do is go to your repository and from Settings > Pages choose to deploy from gh-pages branch, as the following picture shows.After that, the website will be available athttps://yourusername.github.io/your-repository-name\nNote that this is a static webpage, so sliders will not work. Students will still be able to play with interactivity by downloading the notebook or running it on binder.If you want interactivity to work on the webpage, you can eitherPrecompute the notebooks outputs (experimental)orRun your own server"},{"url":"mod3_publish_website/precompute_output/","title":"Precompute the pluto notebooks","tags":["module3","track_setup","track_julia","deploy","precompute","Pluto","PlutoSliderServer"],"text":"COMING SOON"},{"url":"mod3_publish_website/setup_server/","title":"Setup a server for your website","tags":["module3","track_setup","deploy","server","dynamic","droplet"],"text":"COMING SOON"},{"url":"project/project/","title":"Introduction","tags":["project"],"text":"main a img {\n    width: 5rem;\n    margin: 1rem;\n}\nProject assignmentModelling and Simulation of Biological SystemsAssignmentThis project aims to apply the principles of the course Modelling and Simulation to a small, self-contained example related to bioscience engineering. To this end, you can draw inspiration from one of our abstracts. You must work on your project in the Pluto notebook environment in the provided template (Dutch or English).Your project must be of a maximum length of eight to ten pages when printed. Your project contains the following:[ ] an abstract with context, why it is relevant, a summary of what you have done and a conclusion[ ] a model based on differential equations or a probabilistic model (a stochastic program)[ ] a clear outline of the variables and parameters[ ] it has some aspect of either process optimization (so you use optimization to improve a parameter, input or decision), calibration (you tweak a parameter based on sampling or optimization) or gains some deeper insight in the system (e.g., how inputs influence the output).[ ] you use some form of uncertainty assessment, sensitivity analysis or some other stochastic componentThese aspects can be extended or limited, as you choose. For example, you use your model for some process optimization via an optimizer but you can also tune a parameter by hand.You must submit the project as a PDF, HTML, and Julia (.jl) file through UFORA by 9 May.Each project should also end with a small attribution who did what parts according to the CRediT (Contribution Roles Taxonomy) classification. For example:MS: Conceptualization, Methodology, Writing – Review & Editing; BP: Software, visualization, Formal Analysis; DG: Writing – Original Draft Preparation.Rubric for gradingCategory0-1 Points (Unsatisfactory)2 Points (Developing)3 Points (Satisfactory)4 Points (Good)5 Points (Excellent)Clarity and Organization of NotebookNotebook is very difficult to follow. Code and explanations are disorganized or missing.Notebook has some organization, but it’s challenging to understand the logic and purpose.Notebook follows a generally clear structure with basic explanations of code and results.Notebook is well-organized, with clear sections and explanations that guide the reader’s understanding.Notebook is exceptionally well-structured, with detailed comments and explanations that make it effortless to follow the project’s logic.Quality of Mathematical ModelModel is irrelevant to the chosen biological phenomenon or has major conceptual flaws.Model shows some relevance to the problem but has significant simplifications or inaccuracies.Model accurately captures the essential aspects of the biological phenomenon.Model demonstrates good understanding of the system and includes relevant details and assumptions.Model is sophisticated and incorporates nuanced or insightful elements that reflect a deep understanding of the biological system.Quality of AnalysisAnalysis is absent or uses incorrect techniques. Results are not presented or interpreted.Analysis is attempted but flawed (errors, inappropriate methods). Results are presented with limited interpretation.Analysis uses appropriate techniques and produces mostly correct results with basic interpretation.Analysis employs suitable techniques leading to correct and meaningful results. Interpretation offers some insights.Analysis utilizes a range of techniques providing a comprehensive understanding of the model behavior. Interpretation offers significant insights and implications.Tips and adviceKeep it as simple as possible. A project exploring a small model with two or three variables, outlining a well-known concept from your courses can lead to excellent projects or marks.You can use generative AI tools to help you at any point of the project, from brainstorming the initial idea to writing and proofreading text to helping with the code. You still have to explain everything and make sure the text looks “natural”.Make sure you explain your reasoning well. Let others read it if it is clear how to do it.Make uninteresting or hard-to-understand pieces of code invisible if they don’t help the reader.You can use DifferentialEquations.jl or ModelingToolkit.jl if you want to build models that are beyond the scope of Catalyst.jl. For example, if you want to model a thermal process. However, this you personal choice, projects that use extra software will not be marked higher. It is perfectly possible to work out an exellent project using only the examples from the practical notes and theory course.We expect figures to be tidy (titles, labels and axis). See the example projects for how to do this.Use clear variable names and use comments (#) to annotate parts that are not clear.The programming can and should be minimal.Teaching staff will be available for feedback after the lectures and labs or at designated time slots.Keep it as simple as possible!"},{"url":"project/project_antibiotics/","title":"example antibiotics","tags":["project"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"3\" title \"example antibiotics\" date \"2025 02 07\" tags \"project\" description \"Project example antibiotics\" layout \"layout.jlhtml\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils This Pluto notebook uses bind for interactivity. When running this notebook outside of Pluto, the following 'mock version' of bind gives bound variables a default value instead of an error . macro bind def, element format off return quote local iv try Base.loaded modules Base.PkgId Base.UUID \"6e696c72 6542 2067 7265 42206c756150\" , \"AbstractPlutoDingetjes\" .Bonds.initial value catch b missing end local el esc element global esc def Core.applicable Base.get, el ? Base.get el iv el el end format on end begin using Pkg Pkg.activate \".. .. pluto deployment environment\" make this cell invisible when you are finished title \"Modelling the effect of antibiotics on microbial resistence\" names \"Michiel\" academic year \"2023 2024\" email main person \"mail domain.be\" using PlutoUI interactivity using StatsPlots plotting TableOfContents end using Catalyst, OrdinaryDiffEq using SciMLSensitivity using ForwardDiff md\"\"\" title join names, \", \", \" and \" \"\"\" md\"\"\" Abstract The discovery of antibiotics is one of the greatest medical advancements of the 20th century. In this project, we use a simple ordinary differential equation ODE system to model the effect of antibiotic dosing on a system containing susceptible and resistant bacteria. Bacteria grow with a simple completion model susceptible bacteria grow faster due to the fitness cost associated with being resistant . Antibiotics can be added to the system. Higher concentrations kill bacteria more effectively, though antibiotics are quickly removed from the system. When choosing doses and dosing time, can we maximally reduce bacterial infection while limiting our total use? \"\"\" md\"\"\" Model In this system, we model three variables the number of susceptible S t and resistant R t bacteria and the concentration of antibiotics C t . The following processes take place the growth rate of the bacteria depends on their total density according to a logistic growth \\mu r 1 \\frac S R K . susceptible bacteria have a fitness cost of a , limiting their growth rate \\mu 1 a both S and R are removed according to a first order process with rate \\theta the antibiotic kills off bacteria according to a first order kinetics, with the rate determined by a Hill function. Resistant bacteria have twice as high K s . antibiotics leave the system degradation and removal with a rate of g . \"\"\" antibiotics reaction network begin species S t 500.0 R t 50.0 C t 0.0 r 1 S R K , S 2S growth susceptible bacteria r 1 S R K 1 a , R 2R growth resistant bacteria with fitness cost θ, S, R ∅ natural removal of the bacteria hill C, v, Ks, 4 , S ∅ killing S bacteria via antibiotics hill C, v, 2Ks, 4 , R ∅ killing R bacteria via antibiotics g, C ∅ removal of antibiotics end md\"These reactions form the following system of ordinary differential equations \" convert ODESystem, antibiotics parameters antibiotics md\" Simulation and analysis\" md\" One dose of antibiotics\" md\"We set some sensible parameter values \" pars r 2.7, K 1e3, θ 0.2, a 0.2, g log 2 , v 5.3, Ks 4. md\"We can simulate the system. Let us assume an initial concentration of antibiotics C 0 at t 0 and see what the effect is.\" tspan 0.0, 50.0 bind C0 Slider 0 100, show value true, default 20 oprob ODEProblem antibiotics, C C0 , tspan, pars plot solve oprob, RK4 md\"\"\" We note that When no antibiotic is present, the susceptible bacteria quickly take over. At low concentrations 2 20 , the resistant bacteria are killed off, while the resistant bacteria take temporarily over. High antibiotic concentrations 40 show nearly complete eradication of both types of bacteria. We note that after a while when all the antibiotics have left the system , the bacteria quickly return to total capacity. What if we give a second dose of antibiotic at a different time? \"\"\" md\" Second dose of antibiotics\" bind D Slider 1.0 100.0, show value true bind tdose Slider 1.0 35.0, show value true ps cb tdose antibiotics.C ~ antibiotics.C D named antibiotics2 ReactionSystem equations antibiotics , discrete events ps cb obprob2 ODEProblem complete antibiotics2 , C C0 , tspan, pars no AB in the system begin plot solve obprob2 vline tdose , label \"dosing time\", ls dash, title \"Second dosing with concentration of D at day tdose\" end md\"A strong second dose after about eight days can keep the population in check for a while. However, if we give a lower dose, the resistant bacteria will strongly dominate \" md\" Sensitivity analysis\" md\"We can explore the system further by performing a local sensitivity analysis. Let us explore two parameters the effectivity of the antibiotics given by parameter K s , the concentration where it at half its maximal effectivty and a , the fitness cost of the resistant bacteria. We only consider a single initial dose of antibiotics.\" begin tsteps 0 0.1 30 S Ks, a solve remake oprob, p Ks Ks, a a , RK4 , saveat tsteps S R Ks, a solve remake oprob, p Ks Ks, a a , RK4 , saveat tsteps R sens S Ks, a ForwardDiff.jacobian S, Ks, a sensitivity for susceptible bacteria sens R Ks, a ForwardDiff.jacobian R, Ks, a sensitivity for resistant bacteria end bind Ks Slider 1.0 1.0 20, show value true, default 4 bind a Slider 0 0.1 0.9, show value true, default 0.1 md\"Below is a simulation with the given parameters \" plot tsteps, S Ks, a R Ks, a , label \"S\" \"R\" , xlab \"t\", title \"Bacterial load with Ks Ks and a a\" md\"Next, we perform a sensitivity analysis for Ks and a, respectively.\" plot tsteps, sens S Ks, a ,1 sens R Ks, a ,1 , label \"S\" \"R\" , xlab \"t\", title \"Sensitivity for Ks\" md\"We see here that K s greatly positively impacts both types of bacteria shortly after the the antibiotics has been removed. The greater K s , the higher the antibiotics concentration needs to be to substantionally effect the bacterial density. The effect is the greatest for resistant bacteria. At longer time intervals, a small increase in K s give a positive effect on the susceptible bacteria and a negative effect on the resistant ones. Due to competition, this is a zero sum game and if the susceptible bacteria are less harmed, they can easier take back a large share of the system.\" plot tsteps, sens S Ks, a ,2 sens R Ks, a ,2 , label \"S\" \"R\" , xlab \"t\", title \"Sensitivity for a\" md\"Above, we see the effect of the fitness cost a , the decrease in growth rate the resistant bacteria show. Though the fitness cost only directly impacts the resistant bacteria, both are impacted due to competition. Just after the initial boom when the antibiotics have dissipated, there is a large negative local minimum for the resistant bacteria. After that, we see a strong negative effect for the resistant bacteria and, conversely, a positive effect for the susceptible ones.\" md\"\"\" Conclusion This toy model illustrates the effect of antibiotic treatment on a mixed population of susceptible and resistant bacteria. It highlights the importance of a sufficiently strong initial dose to prevent resistant bacteria from dominating the population. The model also demonstrates that the timing of a second dose can significantly influence the outcome, with a well timed second dose potentially keeping the bacterial population in check. However, this model is a simplification of real world scenarios. It assumes a simple first order removal of antibiotics, whereas actual pharmacokinetics involve absorption, distribution, metabolism, and excretion. Incorporating these factors would enhance the model's accuracy. Additionally, the model could be improved by including a more realistic immune response, capable of eliminating bacteria at low concentrations. Future work could also explore the effects of multiple doses or continuous antibiotic infusions, as well as the impact of stochastic fluctuations in bacterial growth and antibiotic effects. \"\"\" md\" Appendix\" "},{"url":"project/project_estrogen/","title":"example estrogen","tags":["project"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"5\" title \"example estrogen\" date \"2025 02 07\" tags \"project\" description \"Project example estrogen\" layout \"layout.jlhtml\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils begin using Pkg Pkg.activate \".. .. pluto deployment environment\" make this cell invisible when you are finished title \"Estrogen Estimation\" names \"Vo Orbeeld\", \"Pro Ject\" x 4 academic year \"202 x 202 x 1 \" email main person \"mail domain.be\" using PlutoUI interactivity using Random set seed using Turing sampling using StatsPlots plots TableOfContents end md\"\"\" title join names, \", \", \" and \" \"\"\" md\"\"\" Note This project is an adaption of the following blog post https www.oxinabox.net 2022 11 11 Estimating Estrogen.html by Dr. Frames Catherine White. Students are sadly not permitted to copy existing blog posts for their own projects. \"\"\" ╠═╡ begin using Pkg Pkg.activate \"..\" end ╠═╡ md\"\"\" Abstract \"\"\" md\"\"\" As a trans femme on HRT, I would like to know the concentrations of estradiol in my blood at all hours of day. This is useful as the peak, the trough and average all have effects. However, I only get blood tests a finite number of times per day – usually once. I am not a medical doctor, but I am the kind of doctor who can apply scientific modelling to the task of estimating curves based on limited observations. I am honestly surprised no one has done this. The intersection of trans folk and scientific computing is non trivial. After all, the hardest problem in computer science is gender dysphoria. \"\"\" md\"\"\" Model \"\"\" md\"\"\" In this blog post https web.archive.org web 20230128040153 http transascity.org sublingual versus oral estrogen on Sublingual versus Oral Estrogen they approximated the estradiol function with a linear to the peak then an exponential decay. c t \\begin cases \\frac c \\max t t \\max & \\text if t \\le t \\max\\\\ c \\max 2^ t t \\max t 1 2 & \\text if t \\max t\\,. \\end cases \"\"\" estrogen conc t, c max 100, t max 3, halflife 3 ifelse t t max, c max t max t, c max 2^ t t max halflife ╠═╡ function estrogen conc t, c max 100, t max 3, halflife 3 if t t max c c max t max t else c c max 2^ t t max halflife end return c end ╠═╡ plot estrogen conc, xlims 0, 24 , title \"Estrogen concentration model\", xlabel \"t h \", ylabel \"Estrogen concentration c pg ml \", legend nothing md\"\"\" The curve defines the current blood concentration of estradiol c at time t hours after application of the gel. It is described by 3 parameters `c max` the peak concentration. `t max` the time it takes to reach peak concentration. `halflife` the time it takes for the concentration to half after reaching peak. \"\"\" md\"\"\" It’s broadly biologically plausible. We expect a fast initial absorption, that should end at some point in few few hours. Since it is fast and short, it doesn’t really matter what we model it with, so linear is fine. Then we expect a tail off as it is consumed. It makes sense for the rate of absorption to be related to the quantity remaining – which suggests some exponential. We see this kind of thing very frequently in biological systems. This all might be nonsense, I am no systems biologist. \"\"\" md\"\"\" Calibration \"\"\" md\"\"\" Järvinen et al. 1997 give 3 curves for single dose. I am going to plot the data from Järvinen et al against curves using my formula, best fit by my own inspection. I am downshifting all the data from Järvinen et al by 25 pg mL, as that data was from post menopausal cis women, who produce about 25 pg mL of estradiol on their own before you take into account HRT. We only want to model the HRT component. \"\"\" md\" Visual inspection\" t obs 0, 1, 2, 3, 4, 6, 8, 10, 12, 16, 24 c obs 0, 25, 100, 132, 90, 82, 60, 55, 32, 15, 4 , 25, 35, 70, 75, 55, 45, 35, 32, 22, 15, 4 , 5, 14, 17, 20, 12, 10, 5, 2, 5, 5, 4 color palette RGB 91 255, 206 255, 250 255 RGB 245 255, 169 255, 184 255 RGB 1, 1, 1 custom colors for the occassion p data scatter t obs, c obs, color color palette, bg lightgray, label \"A200 obs\" \"A400 obs\" \"Amax obs\" , xlabel \"t h \", ylabel \"Estrogen concentration pg ml \" estimated funcs t estrogen conc t, 132, 3, 3.5 , t estrogen conc t, 100, 2.5, 3.5 , t estrogen conc t, 20, 3.5, 2.7 plot p data, estimated funcs, color color palette, label \"A200 pred\" \"A400 pred\" \"Amax pred\" md\"\"\" By looking at these plots, it seems a pretty decent model. Of course with enough degrees of freedom, you can fit an elephant. However, we have 10 points and only 3 degrees of freedom, of which we only varied 2 of them across the 3 datasets. So it seems like we are good. \"\"\" md\"\"\" Now I just fit those curves by eye. We can find the the most likely parameters via least squares regression. But really we are not after a single curve at all. We are interested in distributions over possible curves, given the observations. These tell use the possible realities that would explain what we are seeing. \"\"\" md\" Bayesian inference\" md\"\"\" To begin with lets think about our priors. These are our beliefs about the values the parameters might take before we look at the data. `c max` is somewhere between 0 and 500 pg mL ie. 0 1835 pmol L . If your E2 is above that something is very wrong. For now let’s not assume anything more and just go with a Uniform distribution. Though perhaps we could do something smarter hand select something that tailed off nicely towards the ends. `t max` is somewhere between 1 and 4 hours, we know this because the instruction say don’t let anyone touch you for the first hour so its definitely still absorbing then , and common wisdom is to not wash the area for at least 4 hours – so it must be done but then. If we use a Triangular distribution it has some push towards the center. `halflife`, we know this has to be positive, since otherwise it would not decay. Being log normal makes sense since it appears in an exponential. We would like it to have mode of 3.5 since that is what by eye we saw fit the curves all nicely probably bad Bayesian cheating here and because that means it is mostly all decayed by 24 hours – it can’t all that much higher usually since otherwise wouldn’t need daily doses, nor that much lower since in that case would need multiple doses per day. To set the mode to 3.5 we use `LogNormal log 3.5 1, 1 ` \"\"\" plot Uniform 0, 500 , TriangularDist 1, 4 , LogNormal log 3.5 1, 1 , legend false, linewidth 2, title \"c max\" \"t max\" \"halflife\" , layout 1, 3 md\"\"\" The other component we will want is an error term. We want to express our observations of the concentration as being noisy samples from a normal distribution centered on the actual curve we are estimating. So we need an error term which will allow some wiggle room about that curve, without throwing off the inference for the real parameters. We will define a variable called `err` which is the standard deviation of this error term. Our prior on this error term should be positive with a peak at 0 and rapidly tailing off. Gamma 1, 1 meets our requirement. \"\"\" plot Gamma 1,1 , title \"err\", legend false model function single dose t obs, c obs c max ~ Uniform 0, 500 t max ~ TriangularDist 1, 4 halflife ~ LogNormal log 3.5 1, 1 err ~ Gamma 1, 1 for i in eachindex t obs c pred estrogen conc t obs i , c max, t max, halflife c obs i ~ Normal c pred, err end end chain sample single dose t obs, c obs 1 , NUTS , 2000 plot chain md\"So let’s look at the distribution over curves as represented by samples .\" md\"\"\" We see this nice kinda clear and fairly small range of values for the parameters `c max`, `t max`, `halflife`. The error term, `err`, is quite large \"\"\" md\" Using less datapoints\" md\"\"\" Now that we have shown we can do inference to find distributions over parameters that fit the data let’s get on to a more realistic task. No one gets blood tests every few hours outside of an experimental data gathering exercise. The most frequent blood tests I have heard of is every 2 weeks, and most are more like every 3 6 months. So what we are really interested in is inferring what could be happening with blood levels from a single observation. \"\"\" chain 1 sample single dose 8 , 60 , NUTS , 2000 md\"\"\" So that’s actually really informative. There are a range of possible explanations. From a very small t max and a large c max meaning it peaked early and has tailed off a lot, to the more likely ones which look more like the kind of curves we were seeing based on the experimental data with more frequent measurements. \"\"\" md\"\"\" We can add more observation points and cut down the number of realities we might be in. This realistically is actually a practical thing to do. You can see in the following plots that if we add a reading of 60 at 8 hours after application we break the possible universes into two possible sets of explanations. One set where the 3 hour reading is while it is still rising, and one set where it is falling. \"\"\" chain 2 sample single dose 3, 8 , 100, 60 , NUTS , 2000 chain 3 sample single dose 1, 3, 8 , 50, 100, 60 , NUTS , 2000 md\"\"\" Conclusion \"\"\" md\"\"\" This is just a first look at this topic. I imagine I might return to it again in the future. Here are some extra things we might like to look at Determining optimal times to test 3 readings will not always capture the curve, different times may be more informative than others, especially when we consider the error level vs the signal level. Average levels the distribution of average level is likely fairly collapsed – multiple different sets of parameter values can lead to same average level. Multi day Since estrogen doesn’t hit zero at 24 hours can model across days. Can also include a term for variation in when it was applied in the day since people are not that consistent. Multiday is crucial for making the model realistic Dose changes extending beyond multiday, people change there does, and we know higher dose leads to higher levels so we can insert that prior knowledge. Probabilistic programming is a cool technique for working on pharmacodynamics. It lets us handle the fact that we have many unknowns about people’s individual biology, while still narrowing down a possible set of worlds they might live in. \"\"\" md\" Appendix\" function plot estrogen estimations chain, t obs, c obs c max, t max, halflife chain param for param in c max, t max, halflife est funcs t estrogen conc t, c max i , t max i , halflife i for i in eachindex c max plot est funcs, color color palette 2 , xlims 0, 25 , ylims 0, 200 , label nothing, opacity 0.01 scatter t obs, c obs, color color palette 1 , label nothing end plot estrogen estimations chain, t obs, c obs 1 plot estrogen estimations chain 1, 8 , 60 plot estrogen estimations chain 2, 3, 8 , 100, 60 plot estrogen estimations chain 3, 1, 3, 8 , 50, 100, 60 md\"\"\" References \"\"\" md\"\"\" Järvinen, A., Granander, M., Nykänen, S., Laine, T., Geurts, P., & Viitanen, A. 1997 . Steady‐state pharmacokinetics of oestradiol gel in post‐menopausal women effects of application area and washing. BJOG An International Journal of Obstetrics & Gynaecology, 104, 14 18. \"\"\" "},{"url":"project/project_ladybugs/","title":"example ladybugs","tags":["project"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"6\" title \"example ladybugs\" date \"2025 02 07\" tags \"project\" description \"Project example ladybugs\" layout \"layout.jlhtml\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils begin using Pkg Pkg.activate \".. .. pluto deployment environment\" make this cell invisible when you are finished title \"APHID ANNIHILATION 🤘🔥🔥\" names \"Vo Orbeeld\", \"Pro Ject\" x 4 academic year \"202 x 202 x 1 \" email main person \"mail domain.be\" using PlutoUI interactivity using Random set seed using Catalyst, JumpProcesses, OrdinaryDiffEq modeling using Optim using StatsPlots plots TableOfContents end md\"\"\" title join names, \", \", \" and \" \"\"\" md\"\"\" Abstract Belgium's native ladybugs, or lady beetles, have had it rough the last few decades. In 1995, the Asian lady beetle Harmonia axyridis was introduced in Belgium as a biological pest control agent, and quickly established itself as the new kingpin R.L. Koch, 2003 . While useful for controlling agricultural pests such as aphids, the beetle has been found to have a negative effect on biodiversity by outcompeting native ladybugs, essentially bullying the poor things Brown et al., 2008 . The goal of this project is to create a simple model for the dynamics of a ladybug population living off aphids and subsequently to use it to investigate what feeding strategy is optimal for the ladybugs. We then intend to help the native ladybugs by passing this information on to them. Since we are dealing with relatively small population sizes that can go to 0, we have chosen to model this system as a discrete stochastic jump process. We optimized the ladybug's aphid predation rate for an ideal deterministic case and then investigated whether how this optimal feeding rate performed in a stochastic environment. \"\"\" md\"\"\" Model \"\"\" md\"\"\" First we introduce our notations for the different components of the model. Variables 🦗 The aphid population size 🐞 The ladybug population size Parameters 🚩🦗 The environment's carrying capacity for aphids. 🍼🦗 The aphid's birth rate. 👌🦗 The aphid's target population level as designated by the ladybugs. 🍴🐞 The ladybug's aphid predation rate. This is the rate at which native ladybugs eat aphids. 💀🐞 The ladybug's mortality rate. We'll now introduce the model step by step in the following section. Let's start by defining the time span over which we'll simulate our model six months, equating about one growing season \"\"\" tspan 0.0, 6 30.0 md\"\"\" Aphids \"\"\" md\"\"\" We start with the aphids, which will serve as the ladybugs' food source. We will use a simple Gompertz model for their growth, as aphids are known for their explosive exponential population growth, yet their maximum population size is limited by how many aphids their host plant can feed. In reality aphids are of course able to switch host plants, but for simplicity's sake we will consider an aphid population with one single host plant which does not deteriorate . \"\"\" aphid rn reaction network begin parameters 🚩🦗 10 000 🍼🦗 0.1 species 🦗 t 1000 🍼🦗 🚩🦗 🦗 🚩🦗, 🦗 2🦗 end sol aph JumpInputs aphid rn, , tspan, | JumpProblem | solve plot sol aph, label \"Aphids\", palette okabe ito, title \"Evolution of an unbothered aphid population\", size 700, 400 , margin 3Plots.mm md\"\"\" Get eaten by ladybugs \"\"\" md\"\"\" Next we add the ladybugs. They eat aphids to make more ladybugs, and then die of old age after living a fulfilling ladybug life. We assume two ladybugs need to eat 30 aphids to produce a child. Additionally, the rate at which aphids get eaten scales linearly with the amount of ladybugs, but not the amount of aphids. This is because we assume the number of ladybugs will be the limiting factor 20 ladybugs will eat twice as many aphids as 10 ladybugs, but it does not matter whether 1000 or 2000 aphids are present we thus also assume the ladybugs have no difficulty finding the aphids . Finally, we also assume ladybugs will always leave a certain number of aphids alive lest their food source goes extinct. To model ladybug mortality, we also assume a first order process. The more ladybugs are present, the more ladybugs will die on any given day by reaching old age, or perhaps getting eaten by a bird. \"\"\" ladybug rn reaction network begin parameters 🚩🦗 10 000 🍼🦗 0.1 👌🦗 300 🍴🐞 0.3 💀🐞 0.1 species 🦗 t 1000 🐞 t 10 🍼🦗 🚩🦗 🦗 🚩🦗, 🦗 2🦗 🍴🐞 🐞 🦗 👌🦗 🦗, 2🐞 30🦗 3🐞 💀🐞, 🐞 0 end md\"\"\" Let's double check everything is in order \"\"\" convert ODESystem, ladybug rn sol bugged JumpInputs ladybug rn, , tspan, | JumpProblem | solve plot sol bugged, label \"Aphids\" \"Ladybugs\" , palette okabe ito, yaxis log, ylims 1, 1e4 , title \"Evolution of a very much bothered aphid population & ladybugs \", size 800, 500 , margin 3Plots.mm md\"\"\" And that's it for the structure of our ladybug model \"\"\" md\" Simulation and analysis\" md\"\"\" Optimisation of ladybug behaviour \"\"\" md\"\"\" Given our simple ladybug model, we'd like to find the optimal ladybug behaviour so that they have the largest possible population size at the end of the growing season. To this end we'll optimize a loss function that returns the negative ladybug population size at the end of the growing season maximize population size minimize the inverse in function of the aphid predation rate 🍴🐞 and the desired minimal aphid population level 👌🦗 the other 3 parameters are outside of the ladybugs' control \"\"\" md\"\"\" For the callibration process, we'll assume a perfectly deterministic system by modeling it as an ODE problem rather than a jump problem. This is because trying to optimize a strongly stochastic process is a pain and we'd rather not deal with that. \"\"\" ladyprob ODEProblem ladybug rn, , tspan, function ladybug loss params sol remake ladyprob, p 🍴🐞 params 1 , 👌🦗 params 2 | x solve x, reltol 1e 9, abstol 1e 9 set tolerance of the solver very low or the optimizer finds the right parameters to break it and make the aphid population go below 0 for even more ladybugs return sol 🐞 end end md\"\"\" We use constrained optimisation since both parameters must be positive. In addition, we set a minimum value of 100 for the aphids' target aphid population level. This is an estimation for how many aphids are needed to start a new population the next growing season. \"\"\" res optimize ladybug loss, 0, 100 , 1, Inf , 0.3, 300 , Fminbox NelderMead params opt 🍴🐞 res.minimizer 1 , 👌🦗 res.minimizer 2 remake ladyprob, p params opt | x solve x, reltol 1e 9, abstol 1e 9 | x plot x, yaxis log, ylims 1, 1e4 , label \"Aphids\" \"Ladybug\" , palette okabe ito, title \"Evolution of an optimally bothered aphid population & ladybugs \", size 800, 500 , margin 3Plots.mm md\"\"\" We can see that a lower predation rate allows for the aphid population to flourish while the ladybug population steadily rises, until the ladybug population reaches a critical size near the end of the growing season. At this point there are so many ladybugs the aphid population starts collapsing ending with a strong crash just at the end of the growing season. Patience seems to be the key \"\"\" md\"\"\" Uncertainty assessment of ladybug behaviour \"\"\" md\"\"\" We've found a set of optimal parameters for ladybug behaviour, but that was for a deterministic case. We now want to get an idea of whether these parameters will truly result in high population sizes when considering a more realistic stochastic scenario. To achieve this, we perform a simple grid search over different values of the aphid predation rate 🍴🐞 in the neighbourhood of the optimal value. We perform multiple runs per point to take into account the random nature of the outcome. We did not take the target aphid level 👌🦗 into account as quickly playing around with different values showed very little change in the output not shown for brevity . \"\"\" function ladybug performance 🍴🐞, jump prob sol remake jump prob, p 🍴🐞 🍴🐞, 👌🦗 res.minimizer 2 | solve return sol 🐞 end we're interested in the ladybug population level at the end of the growing season end begin Random.seed 1337 for reproducability num repeats 100 jump prob DiscreteProblem ladybug rn, , tspan, | x JumpProblem ladybug rn, x plot title \"Final ladybug population sizes in function of aphid predation rate\", xlabel \"Aphid predation rate\", ylabel \"Ladybug population size\", size 800, 500 , margin 3Plots.mm grid points 0.05 0.01 0.2 for grididx, 🍴🐞 in enumerate grid points performances ladybug performance 🍴🐞, jump prob for in 1 num repeats do a few simulations with the same value for 🍴🐞 boxplot grididx , performances, label false, outliers false, color orange end xticks 1 length grid points , string. grid points boxplots always have a width of 1, so we need to fidget with the x axis a little end md\"\"\" From this figure we can see that ladybugs with a predation rate that is too low 0.1 always go extinct by the end of the growing season, presumably because they die faster than they replenish the population. Around the optimal value we see a big peak in the expected and maximum final population sizes, steadily decreasing as the predation rate grows with what seems another local optimum near 0.16 . This confirms our previous finding that patient ladybugs will, on average, perform well with respect to their population size at the end of the growing season, though not quite as well as in a deterministic scenario. \"\"\" md\"\"\" Conclusion In our project we made a simple model for the dynamics of a ladybug predating on aphids. We then optimized the predation strategy of the ladybugs in a deterministic scenario and found that a patient predation strategy can be very rewarding. We then checked whether this was also true in a stochastic scenario, and found that it was indeed the case. Some following steps for this model would be to Validate whether it can approximate real aphid ladybug dynamics by trying to calibrate it on real data and change the model if required Introduce invasive ladybugs to the model and investigate their impact on the native ladybugs w.r.t. some of the model parameters \"\"\" md\" Appendix\" md\"\"\" References \"\"\" md\"\"\" Koch, R. L. 2003 . The multicolored Asian lady beetle, Harmonia axyridis a review of its biology, uses in biological control, and non target impacts. Journal of insect Science, 3 1 , 32. Brown, P. M. J., Adriaens, T., Bathon, H., Cuppen, J., Goldarazena, A., Hägg, T., ... & Roy, D. B. 2008 . Harmonia axyridis in Europe spread and distribution of a non native coccinellid. From biological control to invasion the ladybird Harmonia axyridis as a model species, 5 21. \"\"\" "},{"url":"project/project_template/","title":"template (EN)","tags":["project"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"2\" title \"template EN \" date \"2025 02 07\" tags \"project\" description \"Project Template\" layout \"layout.jlhtml\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils begin using Pkg Pkg.activate \".. .. pluto deployment environment\" make this cell invisible when you are finished title \"Our super cool project\" names \"Alice\", \"Bob\", \"Carol\" academic year \"202x 202 x 1 \" email main person \"mail domain.be\" using PlutoUI interactivity using StatsPlots plotting TableOfContents end md\"\"\" title join names, \", \", \" and \" \"\"\" md\"\"\" Abstract About 250 words about your project 1 2 sentence basic introduction to your topic, accessible to every bioengineering student 1 2 sentences bit more specialized introduction 1 2 sentences general goal of the project 2 3 sentences short overview of how you built the model and what analysis you did \"\"\" md\"\"\" Model general outline of the model variables parameters For example, the metabolic rate y as a function of the mass m of an organism follows a power law. \"\"\" metabolic rate y m a 0.75, C0 1 C0 m^a note that you likely will use a Catalyst model md\" Simulation and analysis\" md\"\"\" Explore your model \"\"\" plot y, 0.01, 1000, label \"metabolic rate\", xlab \"mass kg \" md\"\"\" Conclusion A short conclusion of your analysis with a relection on how you would improve this model. \"\"\" md\"\"\" Attribution one sentence about who did what mainly according to the CRediT https en.wikipedia.org wiki Contributor Roles Taxonomy Contribution Roles Taxonomy classification. \"\"\" md\" Appendix\" "},{"url":"project/project_template_nl/","title":"template (NL)","tags":["project"],"text":" A Pluto.jl notebook v0.20.6 frontmatter order \"2\" title \"template NL \" date \"2025 02 20\" tags \"project\" description \"Project Template\" layout \"layout.jlhtml\" frontmatter.author name \"Michiel Stock\" using Markdown using InteractiveUtils begin using Pkg Pkg.activate \".. .. pluto deployment environment\" maak deze cel onzichtbaar als je klaar bent title \"Ons supercoole project\" names \"Alice\", \"Bob\", \"Carol\" academic year \"202x 202 x 1 \" email main person \"mail domain.be\" using PlutoUI Interactiviteit using StatsPlots Plotten TableOfContents end md\"\"\" title join names, \", \", \" and \" \"\"\" md\"\"\" Abstract Ongeveer 250 woorden voor jullie project 1 2 zinnen basisintroductie tot jullie onderwerp, toegankelijk voor elke bio ingenieurstudent 1 2 zinnen iets meer gespecialiseerde introductie 1 2 zinnen algemeen doel van het project 2 3 zinnen kort overzicht van hoe jullie het model hebben gebouwd en welke analyse jullie hebben uitgevoerd \"\"\" md\"\"\" Model Algemene schets van het model variabelen parameters Bijvoorbeeld, de stofwisselingssnelheid y als functie van de massa m van een organisme volgt bijvoorbeeld een machtswet. \"\"\" stofwisselingssnelheid y m a 0.75, C0 1 C0 m^a jullie zullen waarschijnlijk een Catalyst model gebruiken md\" Simulatie en analyse\" md\"\"\" Verken jullie model \"\"\" plot y, 0.01, 1000, label \"metabolic rate\", xlab \"mass kg \" md\"\"\" Besluit Een korte conclusie van uw analyse met een reflectie over hoe u dit model zou verbeteren. \"\"\" md\"\"\" Toeschrijving één zin over wie wat deed, voornamelijk volgens de CRediT https en.wikipedia.org wiki Contributor Roles Taxonomy Contribution Roles Taxonomy classificatie. \"\"\" md\" Bijlage\" "},{"url":"welcome/errata/","title":"Errata","tags":["welcome"],"text":"main a img {\n    width: 5rem;\n    margin: 1rem;\n}\nNew errata?Have you found a spelling mistake, an error? Do you think there might be a typo somewhere? Let us know on the dedicated forum on Ufora!Errata overviewPlaceholder: errata will be listed here"},{"url":"welcome/installation/","title":"Software installation","tags":["welcome"],"text":"First-time setup: Install Julia & PlutoText and pictures version:Step 1: Install Julia 1.11.2Go to https://julialang.org/downloads and download the current stable release, Julia 1.11.2, using the correct version for your operating system (Linux x86, Mac, Windows, etc).Step 2: Run JuliaAfter installing, make sure that you can run Julia. On some systems, this means searching for the “Julia 1.11.2” program installed on your computer; in others, it means running the command julia in a terminal. Make sure that you can execute 1 + 1:Make sure that you are able to launch Julia and calculate 1+1 before proceeding!Step 3: Install PlutoNext we will install the Pluto, the notebook environment that we will be using during the course. Pluto is a Julia programming environment designed for interactivity and quick experiments.Open the Julia REPL. This is the command-line interface to Julia, similar to the previous screenshot.Here you type Julia commands, and when you press ENTER, it runs, and you see the result.To install Pluto, we want to run a package manager command. To switch from Julia mode to Pkg mode, type ] (closing square bracket) at the julia> prompt:\njulia> ]\n\n(@v1.11.2) pkg>\nThe line turns blue and the prompt changes to pkg>, telling you that you are now in package manager mode. This mode allows you to do operations on packages (also called libraries).To install Pluto, run the following (case sensitive) command to add (install) the package to your system by downloading it from the internet.\nYou should only need to do this once for each installation of Julia:\n(@v1.11.2) pkg> add Pluto\nThis might take a couple of minutes, so you can go get yourself a cup of tea!You can now close the terminal.Step 4: Use a modern browser: Mozilla Firefox or Google ChromeWe need a modern browser to view Pluto notebooks with. Firefox and Chrome work best.Second time: Running Pluto & opening a notebookRepeat the following steps whenever you want to work on a project or homework assignment.Step 1: Start PlutoStart the Julia REPL, like you did during the setup. In the REPL, type:julia> using Pluto\n\njulia> Pluto.run()\nThe terminal tells us to go to http://localhost:1234/ (or a similar URL). Let’s open Firefox or Chrome and type that into the address bar.If you’re curious about what a Pluto notebook looks like, have a look at the Featured Notebooks. These notebooks are useful for learning some basics of Julia programming.If you want to hear the story behind Pluto, have a look a the JuliaCon presentation.If nothing happens in the browser the first time, close Julia and try again. And please let us know!Step 2a: Opening a notebook from the webThis is the main menu - here you can create new notebooks, or open existing ones. Our homework assignments will always be based on a template notebook, available in this GitHub repository. To start from a template notebook on the web, you can paste the URL into the blue box and press ENTER.For example, homework 0 is available here. Go to this page, and on the top right, click on the button that says “Edit or run this notebook”. From these instructions, copy the notebook link, and paste it into the box. Press ENTER, and select OK in the confirmation box.The first thing we will want to do is to save the notebook somewhere on our own computer; see below.Step 2b: Opening an existing notebook fileWhen you launch Pluto for the second time, your recent notebooks will appear in the main menu. You can click on them to continue where you left off.If you want to run a local notebook file that you have not opened before, then you need to enter its full path into the blue box in the main menu. More on finding full paths in step 3.Step 3: Saving a notebookWe first need a folder to save our homework in. Open your file explorer and create one.Next, we need to know the absolute path of that folder. Here’s how you do that in Windows, MacOS and Ubuntu.For example, you might have:C:\\Users\\fons\\Documents\\18S191_assignments\\ on Windows/Users/fons/Documents/18S191_assignments/ on MacOS/home/fons/Documents/18S191_assignments/ on UbuntuNow that we know the absolute path, go back to your Pluto notebook, and at the top of the page, click on “Save notebook…”.This is where you type the new path+filename for your notebook:Click Choose.Step 4: Sharing a notebookAfter working on your notebook (your code is autosaved when you run it), you will find your notebook file in the folder we created in step 3. This the file that you can share with others, or submit as your homework assignment to Canvas.\nconst run = f => f();\nrun(async () => {\nconst versions = await (await fetch(`https://julialang-s3.julialang.org/bin/versions.json`)).json()\nconst sortby = v => v.split(\"-\")[0].split(\".\").map(parseFloat).reduce((a,b) => a*10000 + b)\nconst version_names = Object.keys(versions).sort((a,b) => sortby(a) - sortby(b)).reverse()\nconst stable = version_names.find(v => versions[v].stable)\nconsole.log({stable})\nconst pkg_stable = /\\d+\\.\\d+/.exec(stable)[0]\ndocument.querySelectorAll(\"auto-julia-version\").forEach(el => {\n    console.log(el)\n    el.innerText = el.getAttribute(\"short\") == null ? stable : pkg_stable\n})\n});"},{"url":"welcome/logistics/","title":"Class logistics","tags":["welcome"],"text":"main a img {\n    width: 5rem;\n    margin: 1rem;\n}\nIn generalLook at Ufora for the latest announcements and practicalities.Course logisticsClasses will be held in A1.1 Tuesday at 1PMExercises in PC-lab E.-1.1 Monday mornings and Tuesday afternoon.Course notesThe MODSIM course notes in pdf format can be found as a download on Ufora or in the release notes on this GitHub repository.Course slidesThe MODSIM slides in pdf format can be found as a download on Ufora or belowIntroductionModelling with ODEs"}]