[{"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":"5. MCMC intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"27\" title \"5. MCMC intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"MCMC intro\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI TableOfContents md\" Inference notebook 1 Intro\" md\" Problem\" md\"\"\" According to the molecular clock hypothesis https en.wikipedia.org wiki Molecular clock , the average amount of mutations in a gene \\bar N is proportional to how much time t has passed, and identical for all species \\bar N \\alpha \\, t \\, . 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 i.e. 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 , ylims 0, 400 md\"This leads us to our first question \" md\"\"\" question What are realistic values for cytochrome C 's mutation rate `α`? \"\"\" md\"\"\" Figuring out the answer to the first question allows for some exciting follow up questions Consider for example 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\"Our second question is then \" md\"\"\" question What values are likely for the seahorse ancestor fossil's age? \"\"\" 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 N based on your inputs time that has passed t and parameters the mutation rate α ? As discussed in the introduction, we assume a linear relationship between the average amount of mutations \\bar N and t \\bar N \\alpha \\, t \"\"\" md\"\"\" Since the accumulation of mutations is a random process, we can't expect the number of mutations N to be exactly the predicted average \\bar N . We can however expect it to be close to the predicted average. We can express this by saying the amount of mutations follows a distribution centered around the expected average N \\sim \\text Poisson \\bar N \\, . \"\"\" md\"\"\" note Can you explain why a Poisson distribution is a natural fit here? \"\"\" md\"\"\" One problem our model uses the gene's mutation rate `α`, but we don't know what it is. However, we do have some prior knowledge about mutation rates of essential 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 2 `. \"\"\" prior alpha Exponential 2 plot prior alpha, title \"Prior belief of α\", legend false, xlabel \"α\", ylabel \"Probability density\" md\"\"\" We can sample some values from this prior distribution and use them to plot the a priori expected trends between time t and average number of mutations \\bar N \"\"\" begin plot xlabel \"Time My \", ylabel \"Number of mutations\", xlims 0, 500 , ylims 0, 400 , title \"A priori relationship between t and mean N\" scatter times, observed mutations, label \"Cyt C\", color deeppink for α in rand prior alpha, 200 plot x α x, color dodgerblue, alpha 0.3, label false end plot end md\"\"\" We can see that some of the mutation rates from our prior distribution result in a relationship that matches the data well. However, our prior belief is much too broad only a few of the lines are realistic for the data. Finding which of these lines from our prior belief are realistic for the data is essentially what inference does. \"\"\" md\"\"\" note Why use `Exponential 2 ` for the prior? 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 10 ` or `Uniform 0, 100 `. When are the results significantly different? \"\"\" md\"\"\" In summary , we now have a model that predicts our output N from the input t \\bar N α \\, t \\, . N \\sim \\text Poisson \\bar N \\, . a prior distribution for the parameter α . \"\"\" md\"We then translate it into a Turing model \" model function mutations ts α ~ prior alpha prior distribution of parameter N zeros length ts output variable in this model we have multiple values, so we need to preallocate a vector for i in 1 length ts N average α ts i N i ~ Poisson N average end return N end md\"\"\" As you may notice, we have defined the mutation times t as an input to the Turing model. This is not strictly necessary you can also just hardcode the given values of t , variable `times`, in the Turing model, but this way you can easily define the model for different values of the input variables. You simply instantiate the model with the correct values of t as follows \"\"\" mutation model mutations times md\"And can then generate random samples of the output as we are used to \" chain sample mutation model, Prior , 2000 α sp chain α random sample of α histogram α sp, title \"Sample of prior of α\" generated quantities mutation model, chain random samples of N 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 | N observed mutations, md\"Note the syntax we tell Turing that the value of the random variable `N` defined in our Turing model should be the values given by the variable `observed mutations`.\" md\"\"\" danger Note the `,` at the end of ` N observed mutations, `. This is important, as without it Julia thinks you simply put parentheses around a variable assignment and you'll get an error Uncomment the below cell for an example. \"\"\" forgot comma mutation model | N observed mutations md\"\"\" We can verify that for our conditioned model, the value of `N` has been set as constant \"\"\" conditioned chain sample conditioned model, Prior , 5 generated quantities conditioned model, conditioned chain 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 see the `🔍Live Docs` in the bottom right corner . In practice, `NUTS` is often an excellent choice if all prior distributions are continuous and `PG` with 10 20 particles 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 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 as above. The one in the chain plot was simply smoothed to look continuous. \"\"\" alpha samples mutation chain α histogram alpha samples note the difference with the prior distribution md\"Plotting some sampled mutation rates from this distribution onto our data shows that they fit well \" begin plot xlabel \"Time My \", ylabel \"Number of mutations\", xlims 0, 500 , ylims 0, 400 , title \"A posteriori relationship between t and mean N\" for α in alpha samples 1 10 end plot x α x, color dodgerblue, alpha 0.1, label false end scatter times, observed mutations, label \"Cyt C\", color deeppink plot end md\"And finally we can answer our first question \" mean alpha samples sqrt var alpha samples md\"\"\" α 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 seahorse fossil's age is unknown, we add a parameter for it called `fossil age`. As prior knowledge we can use the fact that it must have evolved after the ray finned fish fossil 30 Ma after the bony fish fossil , but before modern seahorses 450 Ma after the bony fish fossil . \"\"\" model function horsetations ts α ~ prior alpha prior distribution of parameter N zeros length ts output variable in this model we have multiple values, so we need to preallocate a vector for i in 1 length ts N average α ts i N i ~ Poisson N average end fossil age ~ Uniform 30, 450 horse mutations ~ Poisson α fossil age return N end md\"Then we simply repeat model instantation, conditioning and sampling \" horse model horsetations times horseditioned model horse model | N observed mutations, horse mutations 156 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 md\" The essentials\" md\"This section again reitarates the essential code for this practical without much explanations.\" let model function mutations ts α ~ Exponential 2 prior distribution of parameter N zeros length ts output variable in this model we have multiple values, so we need to preallocate a vector for i in 1 length ts N average α ts i N i ~ Poisson N average end return N end mutation model mutations times instantiate model conditioned model mutation model | N observed mutations, condition model mind the `,` after `observed mutations` mutation chain sample conditioned model, NUTS , 2000 run inference with an appropriate sampler α sp mutation chain α get sample of posterior distribution of α histogram α sp, title \"Posterior distribution of mutation rate α\" end "},{"url":"exercises/MCMC_2-basics/","title":"5. MCMC basics","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"28\" title \"5. MCMC basics\" tags \"exercises\" layout \"layout.jlhtml\" description \"MCMC basics\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots using PlutoUI TableOfContents 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`. \"\"\" md\"\"\" Y1 Y2 Y3 \\ \\ \\ molehills ground | | | tunnel 0 X \"\"\" md\"\"\" We're no mole experts, but for our prior information we can suppose a mole would not make a tunnel much longer than a few 100 m probably a lot shorter . 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. You come back a day later and find even more molehills You now measure the following values for `Y` ` 3.0, 1.5, 0.9, 5.7 `. How long do you estimate the tunnel given this extra information? \"\"\" md\" 1 Prior plot\" X prior missing missing plot above md\" 2 Unconditional expected value of Y\" model function mole X ~ missing Y ~ missing return Y end molemodel mole Y samples missing E Y missing md\" 3 Conditional expected value of X\" cond mole missing molechain missing missing plot molechain X samplescondY missing E XcondY missing E X missing md\" 4 Conditional distribution of X\" histogram md\" 5 Conditional distribution of X with more data \" model function mole2 X ~ missing how to deal with a vector of values again... end Y obs 3.0, 1.5, 0.9, 5.7 missing some lines of code I estimate the tunnel to be about MISSING long 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`. Suppose the following priors `N ~ Poisson 10 `, `W ~ Uniform 150, 250 ` and the following relationship between expected and actual outcome `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 `. \"\"\" md\" 1 Conditional distribution of N\" model function potatoes N ~ missing W ~ missing T ~ missing end potato model missing potato cond missing potato chain missing be careful with your choice of sampling algorithm are all priors continuous? plot chain histogram of N md\" 2 Probability\" p potato1 missing md\" 3 Probability with more data \" potato cond2 missing alternative conditioned model now also given `W 220` pota2 chain missing plot chain p potato2 missing md\" 3 Lights out\" md\"\"\" You use 4 of the same LED light in your room. Define μ the average lifespan of your LED lights in khr or 1000 hours 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 Unconditional expected value\" lights prior missing E mu missing md\" 2 Choice of distribution\" md\"\"\" note See theory p. 96 for an overview of elementary distributions. \"\"\" A sensible distribution for the lifespan of a LED light is missing md\" 3 Conditional expected value\" model function lights missing end L obs 16, 20, 23, 41 E mu cond missing md\" 4 🌟🌟🌟 Working with censored data\" md\"\"\" note This question is way above the exam's difficulty level. Don't feel bad if you can't find the answer right away \"\"\" md\"\"\" hint You can model the number of lights that still work as a `Binomial` distribution, the success rate of which depends on `μ`. \"\"\" model function lights censored time observed missing end E mu cond🌟 missing 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 Prior distribution given `fs1`\" md\"\"\" hint The distribution of fish lengths can be modelled as a `MixtureModel`. \"\"\" lengthdist missing missing plot md\" 2 Conditional expected value\" 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 missing end fs1 est missing md\" 3🌟 Conditional expected value spicy \" model function fishmixture🌟 missing end p fish4 is species1 missing 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 md\" 1 All points\" model function circle σ 0.25 generate a circle center missing generate a radius missing three random points in polar coordinates P1 missing P2 missing P3 missing end circlemodel circle | x1 x1, y1 y1, x2 x2, y2 y2, x3 x3, y3 y3 circlechain missing 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 Some points\" circle1 missing given one point circle2 missing given two points chain1 missing chain2 missing 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":"5. MCMC advanced","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"29\" title \"5. MCMC advanced\" tags \"exercises\" layout \"layout.jlhtml\" description \"MCMC advanced\" 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 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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 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 `σ` a noise parameter, of which you only know that it's probably small. \"\"\" 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 missing end missing histogram of `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 missing model function petrigrowth missing end prob splittable missing md\" 2\" missing plot md\" 3🌟\" dropletdist🌟 missing 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🌟 missing end prob splittable🌟 missing "},{"url":"exercises/MCMC_4-review/","title":"5. MCMC review","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"30\" title \"5. MCMC review\" tags \"exercises\" layout \"layout.jlhtml\" description \"MCMC review\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Turing, StatsPlots function generate data n wasps 10 minbound 0, maxbound 1000 x n, y n rand DiscreteUniform minbound, maxbound , 2 xs, ys rand DiscreteUniform minbound, maxbound , n wasps for in 1 2 v wasps rand Uniform 5, 10 , n 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 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 feeder stations with the return times of the hornets marked there. \"\"\" xs, ys, ts, true location generate data 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. \"\"\" x nest sp missing vector with possible values of the nest's x coordinate y nest sp missing vector with possible values of the nest's y coordinate begin scatter x nest sp, y nest sp, opacity 0.1, color blue, label \"Estimated nest locations\", xlims 0, 1000 , ylims 0, 1000 , markershape square scatter xs, ys, color orange, label \"wasp locations\", marker z ts scatter true location 1 1 , true location 2 2 , color RGB 0, 1, 0 , label \"True nest location\", markershape square end "},{"url":"exercises/calib_fermenter_monod/","title":"6. Calibration fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"32\" title \"6. Calibration fermenter monod\" tags \"exercises\" layout \"layout.jlhtml\" description \"Calibration fermenter monod\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using Catalyst, OrdinaryDiffEq using Turing, StatsPlots, 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 \\mathrm g L grows by breaking down substrate S \\mathrm g L . The reactor is fed with an inlet flow rate Q in \\mathrm L h , which consists of a manipulable input concentration of substrate S in \\mathrm g L . This process was modelled using Monod kinetics \\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 species missing parameters missing missing missing missing end convert missing, missing 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 has been collected of the substrate S and biomass X concentration at an interval of 5\\ \\mathrm h within 100\\ \\mathrm 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 . \"\"\" begin missing missing end md\"\"\" We have previously used the following parameter values `μmax 0.40` \\mathrm h^ 1 , `Ks 0.015` \\mathrm g L , `Sin 0.022` \\mathrm g L `Y 0.67`, `Q 2.0` \\mathrm L h , `V 40.0` \\mathrm 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` \\mathrm 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 ` \\mathrm h . Take the values above as initial values for \\mu max and K s . \"\"\" md\"\"\" Declare the Turing model. Assume the following for the priors The measurement error is an unknown positive value, but probably near 0 . The parameters `μmax` and `K` are both positive and expected to be in 0.0, 1.0 , presumably around 0.1 . \"\"\" model function fermenter inference t meas σ S ~ missing σ X ~ missing μmax ~ missing Ks ~ missing parms missing oprob missing osol missing S ~ missing X ~ missing end md\"\"\" tip This model can change between being non stiff and stiff based on the sampled parameter values. You can use an auto switching solver such as `AutoTsit5 Rosenbrock23 ` here to make calibration more stable. \"\"\" md\"\"\" Provide the measurements to the Turing model. \"\"\" fermenter inf missing md\"\"\" Optimize the likelihood of the parameters \\sigma S , \\sigma X , \\mu max and K s using the NelderMead optimizer. Store the optimization results in `results mle`. Optionally, you can specify starting points for \\sigma S , \\sigma X , \\mu max and K s to the optimizer to improve the consistency of the results. For the gaussian noise you can just use a value of 0.1. \"\"\" results mle missing md\"\"\" Visualize a summary of the optimized parameters. Beware that this may take a lot of time... \"\"\" missing md\"\"\" Get the optimized values and assign them to `μmax opt` and `Ks opt`. \"\"\" μmax opt missing Ks opt missing 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 md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveat 0.5`. \"\"\" oprob opt missing osol opt missing md\"\"\" Plot S and X simulated with the optimized parameter values together with the measured data. \"\"\" begin 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 about the meaning of the estimated parameters and their impact on the variables S and X . \"\"\" "},{"url":"exercises/calib_intro/","title":"6. Calibration intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"31\" title \"6. Calibration intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Calibration intro\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using Catalyst, ModelingToolkit, OrdinaryDiffEq using ModelingToolkit t nounits as t, D nounits as D using Turing, StatsPlots, 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, we always knew the values of all parameters. In reality, the value of a parameter has to be calibrated, hence, 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, which respectively try to maximize the likelihood function and the posterior probability of the data. In order to understand the difference, recall Bayes' theorem applied to a set of parameters θ and data D ```math P θ \\mid D \\frac P D \\mid θ \\, P θ P D ``` In the MLE method, the likelihood function P D \\mid θ the probability of the data given the parameters 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, the posterior probability P θ \\mid D the probability of the parameters given the data is maximized instead. 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 MLE and MAP methods are that they only return a point estimate of the optimal value, which gives no information on how certain we are about this value. A more informative, yet computationally exhaustive method is using the MCMC Markov chain Monte Carlo sampling methods, which approximate the entire posterior distribution of the unknown values. \"\"\" md\"\"\" In this notebook we will calibrate the different parameters involved in the grass growth models. To illustrate this concept, we first revisit the three simple models modelling the 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 some typical parameter values and initial conditions for grasslands similar to the one we observed, which we can use as prior information. | | \\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\"\"\" Variables containing the initial condition and parameters values will be defined later in the objective function. \"\"\" md\"\"\" The measurement data \"\"\" md\"\"\" Assume that the measured grass yields of a certain plant type 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\" Calibration with Turing\" md\"\"\" We will be using the familiar Turing framework to perform optimisation, which consists roughly of the following steps Define your model as a Turing model, with the following components The observed input data as input of the model Priors for all unknown values such as initial values, parameter values and the measurement error An ODE based model that predicts values of the output variable based on the observed input values and the unknown initial values, parameter values, etc. The relationship between the observed outputs and the predicted outputs, which often comes down to specifying the measurement error Instantiate the Turing model and condition it on the observed values of the output variable Call an optimisation algorithm on the Turing model Extract the optimized parameters Visualise the results \"\"\" md\" The priors\" md\"\"\" During calibration, the prior distributions have two roles 1. Define for every unknown quantity the bounds the optimizer will only search within the domain of the prior distribution. Note you can add additional bounds to any existing distribution using the `truncated` function. 1. Define part of the posterior probability of observing the data given the parameter values. In other words, the theoretically optimal values depend on your prior distributions. Note that the second only applies to methods that take prior information into account, which Maximum Likelihood Estimation MLE does not. \"\"\" md\" Choice of priors for the grass growth models\" md\"\"\" In general for our grass models, we will need to define priors for the following the measurement error standard deviation \\sigma W . the initial condition W 0 . the parameters \\mu and either W f or d depending on the model . \"\"\" md\"\"\" For the measurement error \\sigma W , a distribution with most of its probability density around 0 and a long positive tail is often a good choice. We will use the Exponential distribution here for this purpose. As for its parameters, considering our yield is in the order of 1 to 10 t ha , an expected value and therefore standard deviation of around 1 seems appropriate. \"\"\" bind example mean error Slider 0.01 0.01 10.0, show value true, default 1 plot Exponential example mean error , title \"Example prior for the measurement error\", legend false md\"\"\" For the initial and parameter values, we will default to log normal distributions. These have the following properties that generally fit well with biological parameters, such as is the case in this notebook A positive domain, which bounds possible values to the positive numbers Most of the probability density around the expected value A long postive tail, which allows for outliers As the distribution of a variable whose logarithm is normally distributed, it does have some strange behaviour the arguments of the distribution are the mean and standard deviation of the logarithm . To clarify, for X \\sim \\mathrm LogNormal μ, σ μ E \\mathrm log X σ \\sqrt \\mathrm Var \\mathrm log X Practically, this means you need to specify the logarithm of the desired expected value and play around with the standard deviation . For this exercise, we will choose an expected value based on the table discussed earlier in this section and keep the standard deviation to the default value of 1. Additionally, for some parameters we may want to truncate them to to keep them within biologically and numerically sensible bounds. \"\"\" bind example log μ Slider 0.01 0.01 10.0, default 0.1, show value true bind example log σ Slider 0.01 0.01 3.0, default 1.0, show value true begin example param prior LogNormal log example log μ , example log σ println \"\"\"Look at this strange distribution with mean mean example param prior median median example param prior σ std example param prior \"\"\" plot example param prior, xlims 0, 5 example log μ , title \"Example prior for the model parameters\", legend false end md\"\"\" Example the Logistic growth model \"\"\" md\"\"\" We will illustrate the calibration with the logistic growth model. The latter can be done via a Catalyst reaction network model or via a model built with ModelingToolkit. As an example, we will show both possibilities for the logistic growth model. \"\"\" md\"\"\" 1 Catalyst based model \"\"\" growth log reaction network begin species W t 2.0 parameters μ 0.07 Wf 10.0 μ 1 W Wf , W 2W end md\"\"\" ̇2 ModelingToolkit based model \"\"\" variables W t parameters μ Wf d IF YOU UNCOMMENT THE FOLLOWING LINE, PLEASE COMMENT THE CATALYST MODEL FIRST mtkbuild growth log ODESystem D W ~ μ 1 W Wf W , t md\"\"\" Declaration of the Turing model \"\"\" model function growth log inference t meas σ W ~ Exponential W0 ~ LogNormal log 1 μ ~ LogNormal log 0.1 Wf ~ truncated LogNormal log 10 , lower 0.1 prevent zeros in denominator of our model u0 log W W0 parms log μ μ, Wf Wf oprob log ODEProblem growth log, u0 log, tspan, parms log osol log solve oprob log, AutoTsit5 Rosenbrock23 , saveat t meas W s ~ MvNormal osol log W , σ W end md\"\"\" Some remarks The time points are the ones from the measurements, therefore, we set `saveat t meas`. Depending on the priors, the parameter values of our ODE may vary wildly during calibration. As this can influence the stiffness of the system, it can be beneficial to use an ODE solver that automatically detects the stiffness of the system and switches to a stiff solver if necessary, such as `AutoTsit5 Rosenbrock23 `. We don't expect you to know when this is necessary, but if your calibration regularly gives instability errors, this may help \"\"\" md\"\"\" We will provide the measurements to the Turing model \"\"\" growth log inf growth log inference 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`. \"\"\" results log mle optimize growth log inf, MLE , NelderMead md\"\"\" note \"NelderMead algorithm\" We optimize the likelihood using the Nelder–Mead method, which is deterministic given the same starting point, it will always return the same result for a fixed problem. However, runs may still differ because in the Turing model the initial values are randomly sampled from the specified distributions. Providing explicit starting points to the optimizer can therefore improve consistency. ``` begin init params 1, 2, 0.07, 10 results log mle optimize growth log inf, MLE , init params, NelderMead end ``` \"\"\" md\"\"\" You can visualize a summary of the optimized parameters by piping them to `coeftable`. Beware that this can take a lot of time... \"\"\" results log mle | coeftable 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 \"\"\" parms 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, parms 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 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`. \"\"\" results log map optimize growth log inf, MAP , NelderMead md\"\"\" You can visualize a summary of the optimized parameters by piping them to `coeftable`. Beware that this can take a lot of time... \"\"\" results log map | coeftable 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 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 \"\"\" parms 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, parms 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 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 inf, NUTS , 500 plot results log nuts check convergence summarize results log nuts W0 opt3 log mean results log nuts W0 μ opt3 log mean results log nuts μ 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 \"\"\" parms 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, parms opt3 log osol opt3 log solve oprob opt3 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 opt3 log, label \"Logistic growth\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 scatter 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\"\"\" Implement a reaction network object using Catalyst for the exponential growth model \"\"\" growth exp reaction network begin missing missing end growth exp reaction network begin μ Wf, 0 W μ, W 0 end md\"\"\" Convert the reaction network model into an ODE system to verify. \"\"\" missing convert ODESystem, growth exp md\"\"\" Declare the Turing model. Try to find sensible prior distributions. \"\"\" model function growth exp inference t meas, W meas σ W ~ missing W0 ~ missing μ ~ missing Wf ~ missing u0 exp missing parms exp missing oprob exp missing osol exp missing W meas ~ missing end model function growth exp inference t meas σ W ~ Exponential W0 ~ LogNormal log 1 μ ~ LogNormal log 0.1 Wf ~ LogNormal log 10 u0 exp W W0 parms exp μ μ, Wf Wf oprob exp ODEProblem growth exp, u0 exp, tspan, parms exp osol exp solve oprob exp, AutoTsit5 Rosenbrock23 , saveat t meas W s ~ MvNormal osol exp W , σ W end md\"\"\" Provide the measurements to the Turing model. \"\"\" growth exp inf missing growth exp inf growth exp inference t meas | W s W meas, md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and W f . Do this with `MLE` method and Nelder Mead. Store the optimization results in `results exp mle`. \"\"\" results exp mle missing results exp mle optimize growth exp inf, MLE , NelderMead md\"\"\" Visualize a summary of the optimized parameters. Beware this can take a lot of time... \"\"\" missing results exp mle | coeftable md\"\"\" Get the optimized values and assign them to `W0 opt exp`, `μ opt exp` and `Wf opt exp`. \"\"\" W₀ opt exp missing W0 opt exp coef results exp mle W0 μ opt exp missing μ opt exp coef results exp mle μ Wf opt exp missing 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 \" u₀ opt exp missing u0 opt exp W W0 opt exp md\"\"\" Set up parameter values with optimized parameter values \"\"\" parms opt exp missing parms opt exp μ μ opt exp, Wf Wf opt exp md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveas 0.5`. \"\"\" oprob opt exp missing oprob opt exp ODEProblem growth exp, u0 opt exp, tspan, parms opt exp osol opt exp missing osol opt exp solve oprob opt exp, Tsit5 , saveat 0.5 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. \"\"\" begin missing missing end begin plot osol opt exp, label \"Exponential growth\", xlabel \"t\", xlims 0, 100 ,ylims 0, 14 scatter t meas, W meas, label \"Yield\" 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\"\"\" Implement a system using MTK for the Gompertz growth model.\\ Hint no need to redefine the variable `W t ` and the parameters `μ` and `d` because they are defined at the beginning of this notebook. \"\"\" mtkbuild growth gom ODESystem D W ~ μ d log W W , t md\"\"\" Declare the Turing model. Try to find sensible prior distributions. \"\"\" model function growth gom inference t meas, W meas σ W ~ missing W0 ~ missing μ ~ missing d ~ missing u0 gom missing parms gom missing oprob gom missing osol gom missing W meas ~ missing end model function growth gom inference t meas σ W ~ Exponential W0 ~ truncated LogNormal log 1.0 , lower 1e 5 prevent log 0 μ ~ truncated LogNormal log 0.1 , upper 1.0 prevent overly large exponential growth from crashing the solver this model has no carrying capacity to slow things down d ~ LogNormal log 0.1 u0 gom W W0 parms gom μ μ, d d oprob gom ODEProblem growth gom, u0 gom, tspan, parms gom osol gom solve oprob gom, AutoTsit5 Rosenbrock23 , saveat t meas W s ~ MvNormal osol gom W , σ W end md\"\"\" Provide the measurements to the Turing model. \"\"\" growth gom inf missing growth gom inf growth gom inference t meas | W s W meas, md\"\"\" Optimize the priors \\sigma W , W 0 , \\mu and D . Do this with `MLE` method and Nelder Mead. Store the optimization results in `results gom mle`. \"\"\" results gom mle missing results gom mle optimize growth gom inf, MLE , NelderMead md\"\"\" Visualize a summary of the optimized parameters. Beware this can take a lot of time... \"\"\" missing results gom mle | coeftable md\"\"\" Get the optimized values and assign them to `W0 opt gom`, `μ opt gom` and `D opt gom`. \"\"\" W₀ opt gom missing W0 opt gom coef results gom mle W0 μ opt gom missing μ opt gom coef results gom mle μ D opt gom missing 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 \"\"\" u₀ opt gom missing u0 opt gom W W0 opt gom md\"\"\" Set up parameter values with optimized parameter values \"\"\" parms opt gom missing parms opt gom μ μ opt gom, d d opt gom md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveas 0.5`. \"\"\" oprob opt gom missing oprob opt gom ODEProblem growth gom, u0 opt gom, tspan, parms opt gom osol opt gom missing 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. \"\"\" begin missing missing end begin plot osol opt gom, label \"Gompertz growth\", xlabel \"t\", xlims 0, 100 , ylims 0, 14 scatter t meas, W meas, label \"Yield\" end md\"\"\" Which grass growth model fits best these data? How can you prove this numerically? \"\"\" md\" Answer missing\" "},{"url":"exercises/calib_irrigation/","title":"6. Calibration irrigation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"33\" title \"6. Calibration irrigation\" tags \"exercises\" layout \"layout.jlhtml\" description \"Calibration irrigation\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using ModelingToolkit, OrdinaryDiffEq using ModelingToolkit t nounits as t, D nounits as D using StatsPlots, StatsBase, Turing using LinearAlgebra, 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 . \"\"\" variables missing parameters missing change S1 missing change S2 missing mtkbuild sys irrigation missing 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 . \"\"\" tspan missing md\"\"\" Declare the Turing model. Make sure you take both experiments into account for optimizing k and S max . Based on literature, you can assume that the value of S max lies somewhere between 100 and 200 mm. \"\"\" model function irrigation inference t meas σ S1 ~ missing σ S2 ~ missing k ~ missing Smax ~ missing parms missing For experiment 1 u01 missing oprob1 missing osol1 missing S1 ex1 ~ missing S2 ex1 ~ missing For experiment 2 u02 missing oprob2 missing osol2 missing S1 ex2 ~ missing S2 ex2 ~ missing end md\"\"\" Instantiate the model and condition it with the measurements of S 1 and S 2 from both experiments \"\"\" irrigation inf missing 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 md\"\"\" Visualize a summary of the optimized parameters. Beware that this may take a lot of time... \"\"\" missing md\"\"\" Get the optimized values and assign them to `k opt` and `Smax opt`. \"\"\" k opt missing Smax opt missing 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 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 oprob1 opt missing osol1 opt missing 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 oprob2 opt missing osol2 opt missing begin missing missing missing end md\"\"\" question Do your simulations fit well the measurements? \"\"\" md\" Answer missing\" "},{"url":"exercises/dje_model_bike_sharing/","title":"3. DJE_model_bike_sharing","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"21\" title \"3. DJE model bike sharing\" tags \"exercises\" layout \"layout.jlhtml\" description \"Discrete jump model of a simple bike sharing system\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using Catalyst, JumpProcesses, StatsPlots, StatsBase using PlutoUI TableOfContents md\"\"\" Exercise Modeling a simple Bike Sharing System \"\"\" https www.rete8.it wp content uploads 2016 04 ciclostazione 777x437.jpg md\"\"\" Bike sharing station https www.rete8.it wp content uploads 2016 04 ciclostazione 777x437.jpg \"\"\" md\"\"\" Imagine a bike sharing system for students traveling between Olin College and Wellesley College, which are about three miles apart in eastern Massachusetts. Suppose the system contains 12 bikes and two bike racks, one at Olin and one at Wellesley, each with the capacity to hold 12 bikes. As students arrive, check out a bike, and ride to the other campus, the number of bikes in each location changes. Initially there are `10` bikes at Olin and, hence, `2` bikes at Wellesley. For this simple model, we will also assume that the changes in the number of bikes at both locations is instantaneous. The rate at which a bike is moved from Olin to Wellesley is denoted as p 1 \\ bikes\\ min^ 1 the rate at which a bike is moved from Wellesley to Olin is denoted as p 2 \\ bikes\\ min^ 1 . Both processes are zeroth order and we want to see the evolution of bikes during 1\\,h 60\\,min . This is a discreet and stochastic problem and you need to solve it with SSA. \"\"\" md\"\"\" Create a reaction network object model for the aforementioned problem in order to simulate the evolution of the number of bikes at Olin O and Wellesley W with time. Name it `bike sharing`. In order to make sure that O does not become negative, you can use either `ifelse O 0, 1, 0 ` or ` O, 0 ` as a multiplication factor to the rate `p₁`. A similar multiplication factor must be applied to the other rate `p₂`. \"\"\" bike sharing reaction network begin species missing missing missing end md\"\"\" tip \"Tip\" Subscripts 1, 2, etc, can be visualized by typing, after the letter, a backslash followed by an underscore and then the TAB key. For example `p\\ 1` followed by the TAB key will result in `p₁`. \"\"\" md\"\"\" Convert the system to a symbolic differential equation model and verify, by analyzing the differential equation, that your model is correctly implemented. \"\"\" missing md\"\"\" Part 1 Simulation for different p₁ values \"\"\" md\"\"\" Simulate the evolution of O and W for values of `p₁` in the range ` 0.0, 1.0 ` with a stepsize of `0.1` using a slider. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing md\"\"\" Set the timespan for the simulation \"\"\" tspan missing floats md\"\"\" Create a slider for the variable `p₁` in the range of `0.0` and `1.0` with a step of `0.1`. Take a default value of `0.0`. \"\"\" bind missing md\" Initialize vector `parms` with parameter values, `p₁` is the slider value and assign a constant value of `0.3` to `p₂`. \" parms missing md\"\"\" Create a DiscreteProblem and store it in `dprob` \"\"\" dprob missing md\"\"\" Create a JumpProblem and store it in `jdprob`. Use the simulation method `Direct `.\"\"\" jdprob missing md\"\"\" Solve the problem and store it in `jdsol`. \"\"\" jdsol missing md\"\"\" Plot the solution. Limit the plot to ` 0, 12 ` for the vertical axis. \"\"\" missing md\"\"\" Analyse the results. See what happens when you run the notebook cell with the `solve` function repeatly change the value of `p₁` using the slider \"\"\" md\"\"\" question \"Question\" From what value of `p₁` do you start to get empty bike racks at Olin? \"\"\" md\" Answer missing\" md\"\"\" Part 2 Mean zero counts at Olin \"\"\" md\"\"\" We now want to have an idea of the mean zero counts at Olin for `p₁` values in the range ` 0.0, 1.0 `. \"\"\" md\"\"\" You can inspect the actual number of bike values at Olin by using `jdsol O ` \"\"\" missing md\"\"\" If you want to have a `true` boolean value on positions where the vector value is zero and `false` on non zero values , then you would compare `jdsol O ` element wise with `0`. In Julia, if you want to do element wise operations with on vectors, you always need to place a dot `.` in front of the operator, like for example `. `. Compare in that way `jdsol O ` with `0` \"\"\" missing md\"\"\" Furthermore, if you want the count the number of `true` values in the latter hence, the zero element values , you can simply use the function `count ... `. Count the number of zeros \"\"\" missing md\"\"\" Using the aforementioned way to count zeros in a vector, we will now count the zeros for a range of p values. Because of the stochastic behaviour of the system, for each p values we will count the zeros for a 1000 simulations and then storing only the average value. To introduce a new value for p 1 you can take a deepcopy of the problem and remake the problem like this `jdprob re remake deepcopy jdprob p p₁ p val ` and then solving the problem and store it in `jdsol re`. In the layout below, `mean zero counts` while contain the final mean values of the averaged numbers of zeros from a `1000` simulations using a specific p value, `zero counts p val` will contain the actual number of zeros for a `1000` simulations using a specific p value. Use the layout below to fill in `mean zero counts`. \"\"\" md\"\"\" warning \"Important note\" The SSA solver only saves the state when something changes for example, when a bike arrives or leaves . It does not automatically store values in between events, so it does not explicitly keep track of how long the system stays in the same state. If we want to estimate how long there were zero bikes, we need information at regular time intervals. We can do this by setting `saveat 0.1`. This forces the solver to record the state every 0.1 time units, thereby approximating the time that there are 0 bikes at the campus. Because the timepoints at which a state changes are random, we will still have a small error due to the number of bikes changing in between our chosen time intervals. Choosing a small time interval will help reduce this error. \"\"\" begin p values 0.0 0.1 1.0 different p values mean zero counts vector to store the corresponding mean zero values for p val in p values p val will be each of the p values zero counts p val vector to store the zeros for the 1000 simulations for i missing do a 1000 simulation take a deepcopy and remake the problem for the specific p value jdprob re missing solve the problem jdsol re missing append the number of zeros to zero counts p val missing end append the mean number of zeros to mean zero counts missing end end md\"\"\" Have a look at the mean zero counts by typing `mean zero counts` \"\"\" missing md\"\"\" Plot the mean zero counts as a function of the p values. \"\"\" missing md\"\"\" question \"Questions\" 1. From what value of p do the empty number of bike racks at Olin clearly begin to rise? 2. Reflect on this, does this make sense? Hint change the value of p 2 and observe what happens. \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/dje_model_catalyst_intro/","title":"3. DJE_model_Catalyst_intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"20\" title \"3. DJE model Catalyst intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Introduction to solving discrete jump problems with Catalyst\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using PlutoUI TableOfContents using Catalyst using JumpProcesses, StatsPlots md\"\"\" Introduction Solving discrete jump problems with Catalyst \"\"\" md\"\"\" In this practical, we will revisit the infection model which we solved last week using Catalyst.jl. Last week we assumed that the number of people is a continuous variable, while in practice this is not the case ever seen 46.3 persons . In this session, we will instead use a Stochastic Simulation Algorithm SSA , which is better suited for models with discrete variables, such as the number of infected individuals. \"\"\" md\"\"\" For the sake of clarity we restate the describtion 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\"\"\" 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\"\"\" 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 \"\"\" parms α 0.15, β 0.1, r 0.2, m 0.6 md\"\"\" Setting the timespan \"\"\" md\"\"\" note When working with JumpProblems always use floating point values when defining the time span. Practically this means that you write `tspan 0.0, 60.0 ` instead of `tspan 0, 60 `, indicating to Julia that time points should be represented at floating points. Using integers may lead to errors for JumpProblems when the solver encounters non integer time points e.g. 1.6seconds . \"\"\" 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, parms 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. \"\"\" dsol solve jprob dsol solve jprob, SSAStepper also possible 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 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/dje_model_festival_toilet/","title":"3. DJE_model_festival_toilet","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"22\" title \"3. DJE model festival toilet\" tags \"exercises\" layout \"layout.jlhtml\" description \"Discrete jump model of a festival toilet queue\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Catalyst, StatsPlots, JumpProcesses using PlutoUI TableOfContents md\"\"\" Exercise Modelling a Festival Toilet \"\"\" https www.linda.nl lindanl assets uploads 2024 06 03140318 festivaltoilet 1800x1013.png md\"\"\" Festival toiletten https www.linda.nl lindanl assets uploads 2024 06 03140318 festivaltoilet 1800x1013.png \"\"\" md\"\"\" A good festival provides sufficient food and drink. This also means that there will be a lot of discharge. As a student worker, you are responsible for determining the toilet capacity by means of a simulation. The festival has ten mobile unisex toilets and only one queue. Given that people are discrete and unpredictable, a Jump model seems appropriate. These are the stocks in your model L t the number of people in the queue wacht L ijn initially zero V t the number of free V rije toilets B t the number of occupied B ezette toilets initially zero . You simulate minute by minute from 0 to 360 minutes corresponding to 6 p.m. to 12 p.m. . You consider the following flows processes People are constantly joining the queue zero order process , at an average rate of 1.5 per minute. When a cubicle is free, one person from the queue enters and the cubicle is occupied. Only one person per cubicle is allowed. This happens quickly, as the kinetics have already been established. An occupied cubicle is occupied for an average of five minutes, then it is free again and the person goes to do something else . Use the following symbols for the parameters `kₐ` rate at which people arrive in the queue `kₒ` rate at which someone from the queue will occupy a free cubicle `kₑ` rate at which an occupied cubicle becomes available. \"\"\" md\"\"\" The time interval used is \"\"\" tspan missing use floats md\"\"\" Part 1 Simulating L , V and B . \"\"\" md\"\"\" Complete the model, create the discrete jump problem, solve it, and create a graph with the simulation. \"\"\" toilet reaction network begin species L t missing V t missing B t missing parameters kₐ missing kₒ 10 kₑ missing missing kₒ, L V B missing end missing md\"\"\" Create the discrete jump problem \"\"\" dprob missing jprob missing md\"\"\" Solve the problem \"\"\" sol1 missing md\"\"\" Make the graph containing L , V and B \"\"\" missing md\"\"\" Part 2 Maximum Length of the Queue \"\"\" md\"\"\" Calculate the maximum length of the queue wacht L ijn . Run your model `1000` times and save the maximum length each time in a vector `line maxima` via a `for` loop . Create a histogram of those maximum lengths. \"\"\" md\"\"\" Calculate the maximum length of the queue L using the solution `sol1` from Part 1 \"\"\" missing md\"\"\" Run your model 12 times and each time save the maximum length via a `for` loop in a vector `line maxima`. \"\"\" begin line maxima for i 1 missing sol2 missing missing end end line maxima md\"\"\" Make the histogram \"\"\" missing md\"\"\" Part 3 Introducing a Discrete Event \"\"\" md\"\"\" Between 8 p.m. and 9 p.m. is a period that many people use to relieve themselves. During that period, an average of three people arrive per minute and then returns to normal . Implement this with an event, simulate and create a plot. \"\"\" md\"\"\" Create the conditions, add the condition to the model, and recreate the discrete jump problem. \"\"\" rush event missing named toilet with rush missing dprob3 missing jprob3 missing md\"\"\" Los het probleem op \"\"\" sol3 missing md\"\"\" Create a graph showing L , V and B \"\"\" missing md\"\"\" Part 4 Public Urination Events \"\"\" md\"\"\" When the queue becomes too long if there are more than 15 people in the queue , people start urinating in public on average 1 per minute . These people disappear from the queue. Based on your model from 1 , create a new model that simulates this. Create a new variable W t initially zero that keeps track of the total number of public urination events W ildplasevents . Plot the total number of public urination events over time.\\ Hint ` L, 15 ` is 1 when `L` is greater than 15, and zero in other cases. Alternatively `ifelse L 15, 1, 0 ` is also 1 when `L` is greater than 15, and zero in other cases. \"\"\" toilet wp reaction network begin species L t missing V t missing B t missing W t missing parameters kₐ missing kₒ 10 kₑ missing missing kₒ, L V B missing missing end dprob wp missing jprob wp missing sol wp missing voer enkele keren uit 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":"8. Model selection intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"42\" title \"8. Model selection intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Model selection intro\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils using PlutoUI TableOfContents using OrdinaryDiffEq using Catalyst using Turing, StatsPlots, 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 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_XTRA_anaerobic_fermentation/","title":"2. ODE_model_Xtra_anaerobic_fermentation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"14\" title \"2. ODE model Xtra anaerobic fermentation\" tags \"exercises\" layout \"layout.jlhtml\" description \"Extra exercise on anaerobic fermentation\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst md\"\"\" Exercise Anaerobic fermentation \"\"\" md\"\"\" https users.ugent.be ~gvhaelew fig anaerobic fermentation.png \"\"\" 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 ..., ..., ... ` https docs.sciml.ai Catalyst stable api Catalyst.mmr . \"\"\" Uncomment and complete the instruction anaerobic fermentation1 reaction network begin species missing missing missing end md\"\"\" Check out the species. \"\"\" missing md\"\"\" Convert the system to a symbolic differential equation model and inspect your differential equations. \"\"\" osys1 missing md\"\"\" Initialize a vector `u01` with the initial conditions \"\"\" u01 missing md\"\"\" Set the timespan for the simulation \"\"\" tspan1 missing md\"\"\" Initialize a vector `parms1` with the parameter values \"\"\" parms1 missing md\"\"\" Create the ODE problem and store it in `oprob1` \"\"\" oprob1 missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol1` \"\"\" osol1 missing md\"\"\" Plot the results. Use a line width of 2 `linewidth ...` . \"\"\" missing md\"\"\" Interprete the results. Try to come up with an answer to the following questions \"\"\" md\"\"\" question 1. Why is the concentration of invertase I constant, and the concentration of sucrose S becoming zero? \"\"\" md\" Answer missing\" md\"\"\" question 2. Try to explain the peak in the glucose G concentration. \"\"\" md\" Answer missing\" md\"\"\" question 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 md\"\"\" Make an exact copy of `u01` and rename it to `u02` with the initial conditions \"\"\" u02 missing md\"\"\" Make an exact copy of `tspan1` and rename it to `tspan2` \"\"\" tspan2 missing md\"\"\" Make a copy of `parms1`, rename it to `parms2` and supplement it with the new parameter values \"\"\" parms2 missing md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol2` \"\"\" osol2 missing 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 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 osol2 S end , ..., ..., ..., ... 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 md\"\"\" Calculate the steady state values of the species \"\"\" Sw2, Iw2, Gw2, Ew2, CO2w2 missing 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 md\"\"\" Include the condition into the reaction network model . \"\"\" named anaerobic fermentation3 c missing md\"\"\" Complete the reaction network model . \"\"\" anaerobic fermentation3 c com missing md\"\"\" Create a new ODE problem. \"\"\" oprob3 missing 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_XTRA_fermenter_firstorder/","title":"2. ODE_model_Xtra_fermenter_firstorder","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"13\" title \"2. ODE model Xtra fermenter firstorder\" tags \"exercises\" layout \"layout.jlhtml\" description \"Extra exercise on a fermenter with first order kinetics\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst 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 \\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. The reaction rate is actually \\begin eqnarray r \\beta \\, S \\end eqnarray 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 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 `u0` with the initial conditions \"\"\" u0 missing md\"\"\" Set the timespan for the simulation \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing 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 firstorder c`. \"\"\" 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\"\"\" questions Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in S in ? 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\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/ode_model_XTRA_soil_cont_plant_uptake/","title":"2. ODE_model_Xtra_soil_contamination","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"15\" title \"2. ODE model Xtra soil contamination\" tags \"exercises\" layout \"layout.jlhtml\" description \"Extra exercise on soil contamination with plant uptake\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst md\"\"\" Exercise Soil Contamination with Plant Uptake \"\"\" md\"\"\" https users.ugent.be ~gvhaelew fig soil plant cont model.png \"\"\" 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 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 md\"\"\" Set the timespan for the simulation \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol` \"\"\" osol missing md\"\"\" Plot the solutions \"\"\" missing md\"\"\" question 1. Interprate the simulation results cf. peak in C and increase of P in terms of the used parameter values. \"\"\" md\" Answer missing\" md\"\"\" question 2. How would you modify the basic model to make it a more realistic biological model cf. hill, monod, ... ? \"\"\" md\" Answer missing\" md\"\"\" question 3. What are the units of the parameters k₁, k₂ and k₃? \"\"\" md\" Answer missing\" "},{"url":"exercises/ode_model_XTRA_tank_T_h_mtk/","title":"1. ODE_model_Xtra_tank","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"6\" title \"1. ODE model Xtra tank\" tags \"exercises\" layout \"layout.jlhtml\" description \"extra exercises on modeling water height in a tank\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Cylindrical tank with cooling jacket \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" https users.ugent.be ~gvhaelew fig tank temp.png \"\"\" md\"\"\" Deriving the model equations \"\"\" md\"\"\" A cylindrical tank, surrounded by a cooling jacket, contains an initial amount of water initial temperature T c and initial height h 0 . The cooling jacket remains at a fixed temperature T c . At a certain moment t 0 , a stream of warmer water with flow rate Q in and temperature T in enters the tank, and at the same moment a valve at the bottom of the tank is opened. The water in the tank is well mixed at all times. We are interested in the evolution of the temperature T of the water in the tank and its height h . We denote \\rho as the mass density of the water, c p as the specific heat capacity of the water, U c the heat transfer coefficient, V h \\cdot A the volume of the water in the tank and Q out \\sqrt h R the outgoing flow at the bottom of the tank. The total amount of heat energy of the water in the tank is H c p \\rho V T in J . The following quantities are important for setting up the model equations c p \\rho Q in T in rate of heat flow due to the incoming water Js^ 1 c p \\rho Q out T rate of heat flow due to the outgoing water Js^ 1 U c A \\pi d h T T c rate of heat transfer due to the cooling jacket Js^ 1 For the last quantity, we have assumed that the heat transfer only occurs at the side walls and bottom where the water is in direct contact with the tank. Hence, no heat transfer at the water surface inside the tank. \"\"\" md\"\"\" task Derive the model equations for the total heat energy H and the volume V of the water inside the tank. \"\"\" md\"\"\" hint Use the 'important' quantities mentioned above. Set up ``` math \\begin align H & c p \\rho V T \\\\ V & h A \\\\ \\frac d H dt & \\dots \\\\ \\frac d V dt & \\dots \\end align ``` Think well before putting a or sign before the terms while setting up the differential equations \"\"\" solution md\"\"\" ``` math \\begin align H & c p \\rho V T \\\\ V & h A \\\\ \\frac d H dt & c p \\rho Q in T in c p \\rho \\cfrac \\sqrt h R T U c A \\pi d h T T c \\\\ \\cfrac dV dt & Q in \\cfrac \\sqrt h R \\end align ``` \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Define variables for the temperature T , the height h , the total heat energy H and the volume V of the water in the tank. Use the following variable names `T`, `h`, `H` and `V`. Mention the dependency on the time t . \"\"\" variables missing md\"\"\" Define the parameters for this model and assign their corresponding values. | Parameter | Value | Unit | Meaning | | | | | | | A | 0.283 | m^2 | cross sectional area | | d | 0.6 | m | diameter of the tank | | \\rho | 1000.0 | kg m^3 | water density | | Q in | 0.01 | m^3 s | inlet flow | | T in | 30.0 | ^ \\circ C | temperature of the incoming water stream | | U c | 4000 | J s m^2\\,^ \\circ C | heat transfer coefficient | | T c | 15.0 | ^ \\circ C | temperature of the cooling water | | c p | 4200 | J kg ^ \\circ C | heat capacity | | R | 200 | s m^ 5 2 | res. coeff. orifice | \"\"\" parameters missing md\"\"\" Set up the equation for total heat energy, the volume, the rate of change in the total heat energy and the rate of change in volume. \"\"\" eq heat missing eq volume missing change heat missing change volume missing md\"\"\" Bundle the equations. \"\"\" eqns tank missing md\"\"\" Part 1 simple simulation \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model. Name it `sys1 tank`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. Take as initial temperature of the water `15.0` ^ \\circ C and initial height of the water `0.8` m . Simulate for `2400.0` s . \"\"\" oprob1 tank missing md\"\"\" Solve the ODE problem. Use `saveat 1` and `reltol 1e 9`. \"\"\" sol1 tank missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the temperature with the option `idxs T ` and the height with the option `idxs h ` . Use `twinx ` as first argument in the second plot instruction to plot the height on the right y axis. \"\"\" begin plot missing plot missing end md\"\"\" question Why do you think the temperature first rises, goes to a maximum and then decreases to go to an equilibrium value? \"\"\" md\"\"\" Answers missing \"\"\" md\"\"\" Part 2 effect of the resistance coefficient of the orifice \"\"\" md\"\"\" In this part the resistance coefficient of the orifice will be increased to `250` at the time instant of `1200.0` s . In order to achieve this a discrete event will be used. \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the new model that includes the discrete event. Name it `sys2 tank`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the new ODE problem. Use the same initial values and simulation time as in the previous part. \"\"\" oprob2 tank missing md\"\"\" Solve the ODE problem. Use `saveat 1` and `reltol 1e 9`. Don't forget to take a `deepcopy` of the ODE problem. \"\"\" sol2 tank missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the temperature and the height. Use `twinx ` as first argument in the second plot instruction to plot the height on the right y axis. \"\"\" begin plot plot end md\"\"\" question Is the evolution of T and h after the increase of R according to your exceptations? Explain it. \"\"\" md\"\"\" Answer missing \"\"\" "},{"url":"exercises/ode_model_XTRA_temp_reactors_mtk/","title":"1. ODE_model_temp_reactor","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"7\" title \"1. ODE model temp reactor\" tags \"exercises\" layout \"layout.jlhtml\" description \"modeling temperature in a CSTR\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Temperature evolution in a set of reactors \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" Deriving the model equations \"\"\" md\"\"\" Consider two reactors in series with constant volumes of liquid V 1 and V 2 in a room at a constant ambient temperature T a . A fluid with temperature T in , adjustable by the control engineer, flows through both reactors at a constant flow rate Q . The temperature of the fluid in the first and second reactors are T 1 and T 2 respectively. The heat loss through the walls of the reactors is proportional to the wall areas A 1 first reactor and A 2 second reactor . The heat transfer coefficient of both reactor walls is \\lambda . In the second reactor a heater with an adjustable power P h delivers heat to the fluid. The density of the fluid is \\rho and its thermal heat capacity is c p . \"\"\" PlutoUI.LocalResource \"fig temperature reactors.png\" md\"\"\" Temperature reators https users.ugent.be ~gvhaelew fig temperature reactors.png \"\"\" md\"\"\" The following quantities are important for setting up the model equations c p \\rho V 1 T 1 the amount of heat energy in reactor 1 c p \\rho V 2 T 2 the amount of heat energy in reactor 2 c p \\rho Q \\left T in T 1 \\right rate of heat flow entering reactor 1 c p \\rho Q \\left T 1 T 2 \\right rate of heat flow entering reactor 2 \\lambda A 1 \\left T 1 T a\\right rate of heat flow dissipating from reactor 1 \\lambda A 2 \\left T 2 T a\\right rate of heat flow dissipating from reactor 2 P h rate of heat supplied to reactor 2 \"\"\" md\"\"\" task Derive the model equations for the fluid temperatures T 1 and T 2 . \"\"\" md\"\"\" hint Use the 'important' quantities mentioned above. Set up ``` math \\begin align c p\\,\\rho\\,V 1\\,\\cfrac dT 1 dt & \\dots \\\\ c p\\,\\rho\\,V 2\\,\\cfrac dT 2 dt & \\dots \\end align ``` Think well before putting a or sign before the terms while setting up the differential equations \"\"\" solution md\"\"\" ``` math \\left\\ \\begin array l c p\\,\\rho\\,V 1\\,\\cfrac dT 1 dt & c p\\,\\rho\\,Q \\left T in T 1 \\right \\lambda\\,A 1 \\left T 1 T a\\right \\\\ c p\\,\\rho\\,V 2\\,\\cfrac dT 2 dt & c p\\,\\rho\\,Q \\left T 1 T 2 \\right \\lambda\\,A 2 \\left T 2 T a\\right P h \\end array \\right. ``` \"\"\" md\"\"\" Temperature sensors measures the fluid temperatures T 1 and T 2 in both reactors. We are interested in the evolution of T 1 and T 2 as a function of time. \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" The values of the parameters are listed here | Parameter | Value | Unit | Meaning | | | | | | | `Q` | 0.05e 3 | m^3 s | flow rate | | `V₁` | 0.004 | m^3 | volume of liquid in reactor 1 | | `V₂` | 0.006 | m^3 | volume of liquid in reactor 2 | | `Tin` | 40.0 | ^ \\circ C | temperature of incoming liquid | | `λ` | 240 | W m^2\\,^ \\circ C | heat transfer coefficient | | `A₁` | 0.1 | m^2 | wall area of reactor 1 | | `A₂` | 0.2 | m^2 | wall area of reactor 2 | | `cₚ` | 4200 | J kg\\,^ \\circ C | thermal heat capacity of fluid | | `ρ` | 1000 | kg m^3 | density of fluid | | `Tₐ` | 20.0 | ^ \\circ C | ambient temperature | | `Pₕ` | 5000 | W | power of heater in reactor 2 | The parameters T in and P h will be called manipulable parameters since they can be suddenly changed by a control engineer. \"\"\" md\"\"\" Define the variables for the temperature in both reactors. Use `T₁` and `T₂`. \"\"\" variables md\"\"\" Define the parameters for this model and assign their corresponding values. \"\"\" parameters md\"\"\" Set up the equation that describes the change in T 1 . \"\"\" eq T1 missing md\"\"\" Set up the equation that describes the change in T 2 . \"\"\" eq T2 missing md\"\"\" Bundle both equations. \"\"\" eqns reactors missing md\"\"\" Part 1 simple simulation \"\"\" md\"\"\" In this part we will assume that the manipulable parameters remain constant with values as listed before. \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model. Name it `sys1 reactors`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. Assume an initial temperature of 15.0 ^ \\circ C for T 1 and T 2 and simulate during 600.0 seconds. Use ` ` for the parameters argument since their values have been set before. \"\"\" oprob1 reactors missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1`. \"\"\" sol1 reactors missing md\"\"\" Plotting results \"\"\" md\"\"\" Make a plot of T 1 and T 2 over time. \"\"\" missing md\"\"\" Retrieve the final values of T 1 and T 2 in the solution i.e., at t 600\\ s . \"\"\" missing missing md\"\"\" Calculating steady state values \"\"\" md\"\"\" Calculate the steady state values using `SteadyStateProblem` and `solve`. Use the above retrieved final values as a first guess. \"\"\" equil T vals missing md\"\"\" Display the steady state values. \"\"\" missing missing md\"\"\" questions 1. Why is the steady state value of T 1 not equal to T in ? 2. Why is the steady state value of T 2 much larger than T 1 ? \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" md\"\"\" Part 2 sudden change in T in and P h \"\"\" md\"\"\" In this part we will assume that both manipulable parameters T in and P h suddenly change at some point in time `Tin` changes from 40.0 ^ \\circ C to 45.0 ^ \\circ C , and `Pₕ` changes from 5000 W to 2250 W , at t 600 seconds. \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build a new model where you include both discrete events. Name it `sys2 reactors`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. Take the same initial values for the temperatures as before but now simulate for 1200 seconds. \"\"\" oprob2 reactors missing md\"\"\" Solve the ODE problem. Make a deepcopy of the ODE problem, use `Tsit5 ` and `saveat 1`. \"\"\" sol2 reactors missing md\"\"\" Plotting results \"\"\" md\"\"\" Make a plot of T 1 and T 2 over time. \"\"\" missing md\"\"\" question 1. Is the variable T 1 affected by A only T in , B only P h , C both T in and P h , or D none of them? Try to reason using the model equation for the rate of change of T 1 . 2. Is the variable T 2 affected by A only T in , B only P h , C both T in and P h , or D none of them? Try to reason using the model equation for the rate of change of T 2 . \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/ode_model_XTRA_water_evap_infil/","title":"2. ODE_model_Xtra_evaporation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"16\" title \"2. ODE model Xtra evaporation\" tags \"exercises\" layout \"layout.jlhtml\" description \"Extra exercise on water evaporation and infiltration in a reservoir\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Plots md\"\"\" Exercise Water evaporation and infiltration \"\"\" hint text Markdown.MD Markdown.Admonition \"hint\", \"Hint\", text md\"\"\" https users.ugent.be ~gvhaelew fig water evap infil model.png \"\"\" md\"\"\" 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\" using Catalyst using OrdinaryDiffEq, Catalyst "},{"url":"exercises/ode_model_XTRA_water_evap_infil_mtk/","title":"1. ODE_model_Xtra_evaporation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"8\" title \"1. ODE model Xtra evaporation\" tags \"exercises\" layout \"layout.jlhtml\" description \"modeling evaporation and infiltration in ground\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown InteractiveUtils using StatsPlots, PlutoUI using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Water evaporation and infiltration \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" https users.ugent.be ~gvhaelew fig water evap infil model.png \"\"\" md\"\"\" 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\"\"\" task Set up a system of differential equations modelling the above problem. \"\"\" solution 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 ModelingToolkit. \"\"\" md\"\"\" Define the variables with ` variables`. \"\"\" missing md\"\"\" Define the parameters with ` parameters`. \"\"\" missing md\"\"\" Set up the equations. Name the set of equations `eqs wei`. \"\"\" eqs wei missing md\"\"\" Part 1 \"\"\" md\"\"\" 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. The latter can be handled using the continuous event. Use the following option when creating an `ODESystem` `continuous events W ~ 0 O ~ 0 `. This will make sure that when W hits zero, then O will be put to zero. \"\"\" md\"\"\" Build a system of equations with ` mtkbuild`. Name it `sys1 wei`. \"\"\" missing 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 md\"\"\" Set the timespan for the simulation \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing md\"\"\" Create the ODE problem and store it in `oprob1 wei` \"\"\" oprob1 wei missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 `, `saveat 0.1` and `reltol 1e 9`. Store the solution in `osol1 wei` \"\"\" osol1 wei missing md\"\"\" Plot the results \"\"\" missing 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\" md\"\"\" Part 2 \"\"\" md\"\"\" Copy the above system of equations that you have built with ` mtkbuild`. Name it `sys2 wei`. Extend this system with two discrete events so that the pump is switched on 20\\ L min at time 10 and time 15. Use the following additional option when creating an `ODESystem` \\ `discrete events 10 O ~ 20 , 15 O ~ 20 `. \"\"\" missing md\"\"\" We will use the same values for the initial conditions, time span and parameter, so we don't need to redefine them. \"\"\" md\"\"\" Create the ODE problem and store it in `oprob2 wei` \"\"\" oprob2 wei missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 `, `saveat 0.1` and `reltol 1e 9`. Store the solution in `osol2 wei` \"\"\" osol2 wei missing md\"\"\" Plot the results \"\"\" missing md\"\"\" Interpret the results. \"\"\" md\"\"\" missing \"\"\" md\"\"\" Part 3 \"\"\" md\"\"\" Solve for the equilibrium values using a `SteadyStateProblem`. You can using either `sys1 wei` or `sys2 wei` to do that. Provide a vector with an initial guess for the equibibrium values and vector for the parameter values where O is zero cf. pump is switched off \"\"\" eq val missing Weq missing Geq missing "},{"url":"exercises/ode_model_birth_death/","title":"2. ODE_model_birth_death","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"10\" title \"2. ODE model birth death\" tags \"exercises\" layout \"layout.jlhtml\" description \"Simple birth death model for a mice population\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst md\"\"\" Exercise Simple birth death model for mice \"\"\" md\"\"\" https users.ugent.be ~gvhaelew fig mice model part1.png \"\"\" md\"\"\" 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`. \"\"\" 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 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 md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing md\"\"\" Set the timespan for the simulation \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol` \"\"\" osol missing md\"\"\" Plot the results \"\"\" missing md\"\"\" question Interpret the results. Ask yourself the following questions 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 md\"\"\" Make a new reaction system where the discrete event is included. Name it `birth death2`. \"\"\" named birth death2 missing md\"\"\" Complete the new reaction system . Name it `birth death2 com`. \"\"\" birth death2 com missing md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 1.0`. Store the solution in `osol2` \"\"\" osol2 missing md\"\"\" Plot the results \"\"\" missing md\"\"\" questions Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of the increase in the death rate? 2. If the death rate increases at a different timepoint, would you reach the same steady state value for X ? Explain. \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/ode_model_catalyst_intro/","title":"2. ODE_model_Catalyst_intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"9\" title \"2. ODE model Catalyst intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Introduction to Catalyst as an alternative to ModelingToolkit\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using PlutoUI TableOfContents using Catalyst using OrdinaryDiffEq, StatsPlots md\"\"\" Introduction to Catalyst ODE \"\"\" md\"\"\" In practical 1, we learned to solve ODEs by making use of ModelingToolkit.jl MTK . In this practical we will learn to use Catalyst.jl which is an alternative for defining ODEs. So bear in mind that all problems you will solve in this practical can also be solved by making use of ModelingToolkit.jl this could be a nice test for yourself . Catalyst.jl is a symbolic modelling package for construction, analysis and high performance simulation of chemical reaction networks. In essence, the package simply provides an alternative way for defining ModelingToolkit's symbolic systems, using the notation of chemical reaction networks. These 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 species S t 9 999 000.0 I t 0.0 parameters α 1e 6 α β, 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\"\"\" tip \"Tip\" 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 is essentially a `ModelingToolkit` `ODESystem` with some extra information added on top such as what to return when you call the function `species` on it . It can be converted to a classic `ODESystem` via \"\"\" osys convert ODESystem, infection model md\"\"\" Note that the model equations are essentially \\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 `OrdinaryDiffEq` and `StatsPlots` packages, which are required for simulating the system and plotting the results. \"\"\" md\"\"\" Now we wish to simulate our model. To do this, we need to provide 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 persons 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\"\"\" Creating the ODEProblem \"\"\" md\"\"\" Creating the ODE problem works the same as when working with a `ModelingToolkit` model we use the function `ODEProblem` and provide the symbolic system, the initial conditions, the time span, and the parameters. There is one important difference as we defined our variables and parameters inside the Catalyst model, they are generally not defined in the global environment . We have a few options to solve this 1. Use the variable names instead as `Symbol`s, for example ` S` 1. Specify the variable or parameter as an element of the Catalyst model using the following syntax `model.X`, for example `infection model.S` 1. Use ` unpack` to bring the variables into the global environment does not work for the parameters . \"\"\" u0 S 9 999 000.0, I 1 000.0, D 0.0, R 0.0 tspan 0.0, 90.0 parms α 0.08, β 1.0e 6, r 0.2, m 0.4 oprob ODEProblem infection model, u0, tspan, parms md\"\"\" note The time span is given in parentheses ` ` rather than square brackets ` ` to make it into a `Tuple` rather than a `Vector`. `Tuple`s are very similar to `Vector`s, but are used when the amount of elements inside is important. This is the case here because our time span always needs exactly 2 elements a starting time and an ending time. The parameters and initial conditions, on the other hand, could contain any amount of elements, and therefore we use `Vector`s for them. \"\"\" md\"\"\" Solving the ODEProblem \"\"\" md\"\"\" We can now simulate our model. Solving the model is again done using `OrdinaryDiffEq`, and is therefore exactly the same as the first practical. If you'd like some more examples, there are some examples https docs.sciml.ai DiffEqDocs stable getting started online on how to solve ODE problems with Julia's ecosystem for differential equations. Additionally, if you're interested in all the ODE solvers available, there is a nice overview https docs.sciml.ai DiffEqDocs stable solvers ode solve Full List of Methods available. \"\"\" 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 phase 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 add discrete and continuous events. These are used to 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 of being contagious. \"\"\" md\"\"\" We will create a 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. For the value of parameter `r`, we define a slider a little further on. \"\"\" 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 parms1 α 0.08, β 1.0e 6, r r, m 0.4 type a semi colon at end of an instruction to avoid seeing its output oprob1 ODEProblem infection model, u0, tspan, parms1 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\"\"\" note You can see here we use the value of `r` in the parameter vector before defining it in the slider. This is possible in Pluto because it knows what code cells depend on each other, and it will simply run the definition of `r` before running the code that uses `r`, even though the definition appears later in the notebook. \"\"\" 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. \"\"\" md\"\"\" We need to state that the parameter \\beta needs to be reduced by 50\\% at time t 14 . We include this condition in a variable named `condition2` in the following way \"\"\" condition2 14.0 infection model.β ~ infection model.β 2 md\"\"\" note Events only recognize symbolic variables, not variable names, so we need to specify `β` as `infection model.β` see the problem creation section . \"\"\" md\"\"\" The discrete time event needs to be now 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 , so that the model can be simulated. \"\"\" infection model2 com complete infection model2 md\"\"\" Then we need to create a new ODE problem. \"\"\" oprob2 ODEProblem infection model2 com, u0, tspan, parms 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. \"\"\" We can compare the result now to the solution without contact reduction begin plot osol2 plot osol linestyle dash, label none, color grey, lw 0.5 end md\"\"\" If you want to see the final values of S , I , D and R , type \"\"\" osol2.u end md\"\"\" question How do we interpret this event? Is the number of deceased and infections reduced? How much? \"\"\" osol2 D end osol D end 1 The number of deceased is reduced 13% osol2 S end osol S end 1 6 times less infections with contact measures osol2 R end osol R end 1 13% less recoveries due to fewer infections 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 999\\,000 is subtracted 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 `thr` a short for thr eshold and we set its default value to `1e6`. \"\"\" infection model3 reaction network begin species thr t 1e6 α β, 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 `thr` is `1e6` then I will change at some point and also `thr` will become `1e9`, so that the condition happens only once. By the way, I will never reach 1\\,000\\,000\\,000 \"\"\" condition3 infection model3.I ~ infection model3.thr infection model3.I ~ infection model3.I 999 000, infection model3.thr ~ 1e9 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, parms 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.5 md\"\"\" Now we can plot the results. \"\"\" begin plot osol3 idxs S, I, D, R plot osol linestyle dash, label none, color grey, lw 0.5 end md\"\"\" If you want to see the final values of S , I , D , R and `pwc`, type \"\"\" osol3.u end md\"\"\" question How do we interpret this new event? Is this a better measure than contact reduction alone? Would you know how we call such an event? \"\"\" 0.4 0.999e6 Amount of people deceased during isolation infected osol3 D end 0.4 0.999e6 osol D end 1 Deceased are reduced only 1% osol3 S end osol S end 1 56% less infections wrt. example 1 osol3 R end 0.6 0.999e6 osol R end 1 1% less recoveries md\"\"\" Answer \"\"\" md\"\"\" hint Isolation alone is not effective to protect the part of the population that's already been infected. The peak of infections is not avoided, only delayed. However, the proportion of the population exposed is much lower thanks to isolation. Would then a combined set of rules be best in that case? \"\"\" "},{"url":"exercises/ode_model_diver_mtk/","title":"1. ODE_model_diver","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"3\" title \"1. ODE model diver\" tags \"exercises\" layout \"layout.jlhtml\" description \"modeling of pressure on diver with MTK\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Diver \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" A diver is pulled up from a depth of 30\\ m to a ship with a constant velocity v . The pressure change that takes place in the body is proportional to the difference between the ambient pressure at a certain depth and the internal pressure in the diver's body through a coefficient gradient of c 2\\ s^ −1 . We are interested in the rate of pressure change in the body, as it has to stay below a certain critical value estimated to be 0.02\\ bar s if we want to avoid the Caisson disease. \"\"\" PlutoUI.LocalResource \"fig diver position.png\" https users.ugent.be ~gvhaelew fig diver position.png md\"\"\" Diver https users.ugent.be ~gvhaelew fig diver position.png \"\"\" md\"\"\" Consider a Z axis pointing upwards with the water surface at position z 0\\ m . Hence, the position of the diver beneath the water surface is negative z 0 . The pressure that the water exerts on the diver's body is given by Pascal's law p p a \\rho g z , with p a the air pressure. It is easy to verify that p p a at z 0 . Important remaks When the diver is residing for a relatively long period of time at a certain position z , its internal body pressure p b will equal the ambient pressure p p a \\rho \\, g \\, z . When the diver is suddenly pulled up, it takes time for the internal body pressure of the diver to adapt to the new ambient pressure. Assume a linear relationship between the rate of change in internal body pressure and the difference between the ambient and the internal body pressure \\cfrac dp b dt c 2 \\left p p b\\right \\, , with c 2 the adaption coefficient. Since the adaption of p b is not instant cf. adaptation coefficient c 2 , the factor p p b will be negative, and hence, also \\cfrac dp b dt . It is the absolute value of \\cfrac dp b dt that must stay below a certain critical value in order to avoid the Caisson disease. \"\"\" md\"\"\" Part 1 constant speed \"\"\" md\"\"\" Deriving the model equations \"\"\" md\"\"\" task Derive the model equations the position z and the body pressure p b . \"\"\" md\"\"\" hint Set up ``` math \\begin align \\cfrac d z dt & \\cdots \\\\ \\cfrac d p b dt & \\cdots \\end align ``` \"\"\" solution md\"\"\" ``` math \\begin align \\cfrac d z dt & v \\\\ \\cfrac d p b dt & c 2 \\left p a \\rho g z p b\\right \\end align ``` \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Consider for this part the velocity v being constant with a value of 0.15\\ m s . Set up a model for the position z\\ m and the body pressure p b\\ bar . Include in your model equations also a variable keeping track of the rate of change in body pressure dp b dt\\ bar s . \"\"\" md\"\"\" Define variables for the position z , the body pressure p b and the rate of change in body pressure dp b dt . Use the following variable names `z`, `pb` and `dpbdt`. \"\"\" variables missing md\"\"\" Define the parameters for this model and assign their corresponding values. | Parameter | Value | Unit | Meaning | | | | | | | p a | 101325 1e 5 | bar | Air pressure | | \\rho | 1000.0 | kg m^3 | water density | | g | 9.81 | m s^2 | gravitational constant | | c 2 | 0.05 | 1 s | coefficient gradient | | v | 0.15 | m s | pull up velocity | \"\"\" md\"Use the following names `pa`, `ρ`, `g`, `c₂` and `v const`.\" parameters missing md\"\"\" Define the initial diver position. \"\"\" z₀ missing md\"\"\" Define the initial body pressure. Don't forget to include ` 1e 5` in the hydrostatic term in order to have everything in bar . \"\"\" pb₀ missing md\"\"\" In order to have an idea of this initial body pressure, it is preferable to calculate it in a `let` ... `end` block. Everything in this block will be local scope and won't interfere with variable names defined elsewhere. \"\"\" let pa 101325 1e 5 ρ 1000.0 g 9.81 missing end md\"\"\" Set up the equation for the rate of change in position. \"\"\" eq1 position missing md\"\"\" Set up the equation for the rate of change in body pressure. Don't forget to include ` 1e 5` in one of the terms in order to have the pressure in bar . \"\"\" eq1 body pressure missing md\"\"\" Set up the equation that will keep track of the rate of change in body pressure. \"\"\" eq1 dpbdt missing md\"\"\" Bundle all equations. \"\"\" eqns1 diver missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model and include the continuous event that when z hits zero, the pull up velocity must become zero as well. Name the model `sys1 diver`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem, specifying the initial values for the diver's position and internal body pressure. Use a simulation time span of `500.0` seconds. Use an empty vector ` ` for the parameters argument to use their default values. \"\"\" oprob1 diver missing md\"\"\" Solve the ODE problem. Make a deepcopy of the ODE problem, use `Tsit5 ` and `saveat 1`. \"\"\" sol1 diver missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the position of the diver over time. Include a y label cf. `ylabel \"...\"` and a title cf. `title \"...\"` in all plots. \"\"\" missing md\"\"\" Plot the body pressure of the diver over time. \"\"\" missing md\"\"\" Plot the rate of change in body pressure over time. \"\"\" missing md\"\"\" Calculate the maximum rate of change in the body pressure in absolute value. \"\"\" missing md\"\"\" question Is this maximum rate of change in body pressure for a diver that is pulled up at a speed of 0.15\\ m s a safe value? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Part 2 external forces \"\"\" md\"\"\" Suppose that the diver is now attached with a rope to a motor on a boat crane to be pulled up. Four forces now act on the diver gravity, a frictional force, the force exerted by acceleration and the Archimedean force. The frictional force on the diver is proportional to the speed of the diver through a coefficient c 1, kg s , while the upward Archimedean force is given by F arch \\rho V g , with V the volume of the diver. \"\"\" md\"\"\" Deriving the model equations \"\"\" md\"\"\" In this case the velocity v won't be constant in the beginning but will be governed by the external force. You need to take into account the following forces The external force \\vec F ext F ext \\, \\vec e z . This force is directed upward. The buoyuancy force \\vec F b \\rho \\, V \\, g \\, \\vec e z . This force is always directed upward. The weight of the diver \\vec F g m\\,g\\,\\vec e z . This force is always directed downward. The friction force between the diver and the water \\vec F w c 1\\,v \\,\\vec e z . This force is always opposite the movement direction of the diver. In our case the diver is pulled up, hence, this force is directed downward remember that v \\cfrac dz dt 0 . \"\"\" md\"\"\" task Derive the model equations for the position z , the velocity v and the body pressure p b . \"\"\" md\"\"\" hint Start setting up ``` math \\begin align m\\cfrac d^2 z dt^2 & \\cdots \\\\ \\cfrac d p b dt & \\cdots \\end align ``` and work toward ``` math \\begin align \\cfrac d z dt & \\cdots \\\\ m \\cfrac d v dt & \\cdots \\\\ \\cfrac d p b dt & \\cdots \\end align ``` \"\"\" solution md\"\"\" ``` math \\begin align \\cfrac d z dt & v \\\\ m \\cfrac d v dt & F ext \\left \\rho V m \\right g c 1 v\\\\ \\cfrac d p b dt & c 2 \\left p a \\rho g z p b\\right \\end align ``` \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Define an additional variable for the velocity v . Use the following variable name `v`. \"\"\" variables missing md\"\"\" Define the additional parameters for this model and assign their corresponding values. | Parameter | Value | Unit | Meaning | | | | | | | m | 100 | kg | mass of the diver | | V | 0.082 | m^3 | volume of the diver | | c 1 | 20.0 | kg s | friction coefficient | \"\"\" md\"Use the following names `m`, `V` and `c₁`.\" parameters missing md\"\"\" In order to have an idea of what external force you need to pull up the diver at a constant velocity of 0.15 m s, you can calculate it by setting \\cfrac dv dt to zero in the equation for the rate of change in the velocity, and solving for F ext . \"\"\" let ρ 1000.0 g 9.81 v const 0.15 m 100.0 V 0.082 c₁ 20.0 missing end md\"\"\" Set up the additional parameter for the external force with a default value of 180. Name it `Fext`. \"\"\" parameters missing md\"\"\" Set up the equation for the rate of change in position. Multiply the right handside of the equation with z 0 to make sure that after z hits 0 , z remains constant and, hence, also v remains 0 . \"\"\" eq2 position missing md\"\"\" Set up the equation for the rate of change in velocity. This includes the external force, the buoyuancy force, the weight of the diver and the frictional force. \"\"\" eq2 velocity missing md\"\"\" Set up the equation for the rate of change in body pressure. Don't forget to include ` 1e 5` in one of the terms in order to have everything in bar . \"\"\" eq2 body pressure missing md\"\"\" Set up the equation that will keep track of the rate of change in body pressure. \"\"\" eq2 dpbdt missing md\"\"\" Bundle all equations. \"\"\" eqns2 diver missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model and include the continuous event that when z hits zero, v must become zero as well. Name the model `sys2 diver`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. The initial conditions for the position and body pressure are the same as before. For v , assume that the diver is initially at rest. Use a simulation time span of `500.0` seconds. Use the default values of the parameters again. \"\"\" oprob2 diver missing md\"\"\" Solve the ODE problem. Make a deepcopy of the ODE problem, use `Tsit5 ` and `saveat 1`. \"\"\" sol2 diver missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the position of the diver over time. Include a y label cf. `ylabel \"...\"` and a title cf. `title \"...\"` in all plots. \"\"\" missing md\"\"\" Plot the velocity of the diver over time. \"\"\" missing md\"\"\" Plot the body pressure of the diver over time. \"\"\" missing md\"\"\" Plot the rate of change in body pressure over time. \"\"\" missing md\"\"\" Calculate the maximum rate of change in the body pressure in absolute value. \"\"\" missing md\"\"\" question Is this maximum rate of change in body pressure for a diver that is pulled up with F ext 180.0\\ N a safe value? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Part 3 plot of maximum dpb dt vs Fext \"\"\" md\"\"\" Make a plot of the maximum rate of change in body pressure versus the external force in the range 177, 187 N with a step size of 0.5 N . Use the same initial conditions and time span as in Part 2 but iteratively modify the parameter value of F ext . Instead of `Tsit5 `, use now the `Rosenbrock32 ` solver. \"\"\" md\"\"\" hints Append during each iteration the maximum rate of change in body pressure to the vector `dpbdt max vals`. Define a range object for the external forces as `Fext vals 178 0.5 184`. Use the model `sys2 diver` from Part 2, but name the ODE problem as `oprob3 diver`. Change the parameter value of `Fext` at each iteration step. \"\"\" begin dpbdt max vals Fext vals missing for Fext val in Fext vals oprob3 diver missing sol3 diver missing append dpbdt max vals, missing end end md\"\"\" Plot the maximum rate of change in body pressure vs the external force. \"\"\" missing md\"\"\" task Incept graphically from the plot the value of the external force so that the rate of change in body pressure is 0.02. \"\"\" md\"\"\" Response missing \"\"\" md\"\"\" Part 4 more accurate value of Fext \"\"\" md\"\"\" Find a more accurate value for the external force so that the maximum rate of change in body pressure is 0.02. \"\"\" md\"\"\" hints Use a `while` loop. The start value of the external force is defined as `Fext val 180.0`, its step size as `DFext val 0.05`. Assign within the `while` loop the value of the maximum rate of change in body pressure to `dpbdt max val`. Use the model `sys2 diver` from Part 2, but name the ODE problem as `oprob4 diver`. Within the `while` loop you need to put `global` before `dpbdt max val` and `Fext val` because they were define outside the `while` loop. \"\"\" begin Fext val missing DFext val missing dpbdt max val 0.00 while missing oprob4 diver missing sol4 diver missing global dpbdt max val missing global Fext val missing end end md\"\"\" Show the value of the retrieved external force. \"\"\" missing md\"\"\" Part 5 above the water surface \"\"\" md\"\"\" Suppose that the diver is now pulled up to a height of 5\\ m above the water surface. The velocity of the diver should be constant and the same as when the diver hits the surface. \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Set up the equation for the rate of change in position. Multiply the right handside of the equation with ` z 5 ` to make sure that after z hits 5 , z remains constant and, hence, also v remains 0 . \"\"\" eq5 position missing md\"\"\" Set up the equation for the rate of change in velocity. This equation only holds when z 0 , hence multiply it with ` z 0 `. When 0 \\leq z 5 , the velocity should remain constant, hence, \\cfrac dv dt 0 . \"\"\" eq5 velocity missing md\"\"\" Set up the equation for the rate of change in body pressure. The term with \\rho\\,g\\,z has to do with the hydrostatic pressure and should vanish when the diver is above the water surface. Hence, include the factor ` z 0 ` to this term. \"\"\" eq5 body pressure missing md\"\"\" Set up the equation that will keep track of the rate of change in body pressure. \"\"\" eq5 dpbdt missing md\"\"\" Bundle all equations. \"\"\" eqns5 diver missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model and include the continuous event that when z hits 5 , v must become zero. Name the model `sys5 diver`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. All initial conditions are the same as before. Use a simulation time span of `500.0` seconds. Use the default parameter values. \"\"\" oprob5 diver missing md\"\"\" Solve the ODE problem. Make a deepcopy of the ODE problem, use `Tsit5 `, `saveat 1` and `reltol 1e 9`. \"\"\" sol5 diver missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the position of the diver over time. Include y labels cf. `ylabel \"...\"` and titles cf. `title \"...\"` . \"\"\" missing missing missing missing md\"\"\" questions 1. Which plots do you expect to be slightly different compared to those in Part 2. 2. Are the plots that are different according to your expectations? \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/ode_model_fermenter_monod/","title":"2. ODE_model_fermenter_monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"11\" title \"2. ODE model fermenter monod\" tags \"exercises\" layout \"layout.jlhtml\" description \"Fermenter with biomass growing on substrate through Monod kinetics\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst md\" Exercise Fermenter Monod kinetics \" md\"\"\" https users.ugent.be ~gvhaelew fig fermenter model 2nd.png \"\"\" 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 X \\xrightarrow \\quad\\quad k 1 Y \\, X \\quad\\quad\\quad\\quad \\textrm with \\quad k \\cfrac \\mu max S K s \\, . \\end eqnarray Note that we also make the assumption that the reaction kinetics follow the rate equation https en.wikipedia.org wiki Rate equation , and therefore the reaction's speed is also first order in S and X . The quantity \\mu k\\,S \\mu max \\, \\cfrac S S K s is called the specific growth rate h^ 1 . Therein, \\mu max is the maximum specific 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 . Note that in Catalyst you can write this as `mm S, μmax, Ks `, where the function `mm` stands for the Michaelis Menten kinetics, which is equivalent to Monod kinetics. 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`. \"\"\" fermenter monod reaction network begin species missing parameters missing missing When S and X meet, then Y X X are created missing S is created at a rate Q V Sin missing S is degraded at a rate Q V S missing X is degraded at a rate Q V X 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 md\"\"\" Set the timespan for the simulation \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing md\"\"\" Part 1 Solve the ODE problem. Use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol1` \"\"\" osol1 missing md\"\"\" Plot the results \"\"\" missing md\"\"\" Inspect the final values in both the S and X vector.\\ Tip use something like ` osol1 ... ... , osol1 ... ... ` \"\"\" osol1 ... ... , osol1 ... ... md\"\"\" We will now create a `SteadyStateProblem` to determine the steady state values for S and X under the current conditions cf. current initial values and current parameter values . \"\"\" md\"\"\" Initialize a vector `u guess1` with the final values for S and X \"\"\" u guess1 S missing, X missing md\"\"\" Make and solve the SteadyStateProblem. Use `u guess1` as initial conditions and the parameter values previously defined. The outputs are the steady state values for S and X which we have denoted as `Seq1` and `Xeq1`. \"\"\" equil val monod missing Seq1 missing Xeq1 missing md\"\"\" Next, we can just inspect these values \"\"\" missing md\"\"\" questions 1. Explain why S first increases and then decreases while X only increases during the first 50 hours. 2. What are the steady state values of S and X . \"\"\" md\"\"\" Answers 1. missing 2. 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 md\"\"\" Make a new reaction system where the discrete event is included. Name it `fermenter monod2`. \"\"\" named fermenter monod2 missing md\"\"\" Complete the new reaction system . Name it `fermenter monod2 com`. \"\"\" fermenter monod2 com missing md\"\"\" Create the ODE problem and store it in `oprob2` \"\"\" oprob2 missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol2` \"\"\" osol2 missing 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 ... ... md\"\"\" Initialize a vector `u guess2` with the final values for S and X \"\"\" u guess2 missing md\"\"\" Initialize a vector `parms mod` with the parameter values. Notice that all parameter values will be the same, except the one of S in . \"\"\" parms mod missing md\"\"\" Make and solve the steady state problem. Call the output values `Seq2` and `Xeq2`. \"\"\" eq2 missing Seq2 missing Xeq2 missing md\"\"\" Inspect those values. \"\"\" missing md\"\"\" questions 1. Can you clearly see the effect of the increase in S in ? 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. 3. Can you explain why X increased permanently? \"\"\" md\"\"\" Answers 1. missing 2. missing 3. 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 md\"\"\" Make a new reaction system where the discrete event is included. Name it `fermenter monod3`. \"\"\" named fermenter monod3 missing md\"\"\" Complete the new reaction system . Name it `fermenter monod3 com`. \"\"\" fermenter monod3 com missing md\"\"\" Create the ODE problem and store it in `oprob3` \"\"\" oprob3 missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol3` \"\"\" osol3 missing md\"\"\" Plot the results \"\"\" missing md\"\"\" questions Interpret the results. Ask yourself the following questions 1. Can you clearly see the effect of doubling of Q ? 2. Can you argue, by means of reasoning, why S increases and X decreases? \"\"\" md\"\"\" Answers 1. missing 2. missing \"\"\" "},{"url":"exercises/ode_model_infection/","title":"2. ODE_model_infection","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"12\" title \"2. ODE model infection\" tags \"exercises\" layout \"layout.jlhtml\" description \"Infection model built as a reaction network\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, Catalyst md\" Exercises infection model \" md\"\"\" https users.ugent.be ~gvhaelew fig infection model.png \"\"\" md\"\"\" We will work here with the same infection model as in the Introdution to Catalyst revisit the concerned notebook if necessary . 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 person and also becomes infected. The infection rate is \\alpha \\beta . Decease , where an infected person dies. The death rate is m r . Recovery , where an infected person recovers. The recovery rate is 1 m r . \"\"\" md\"\"\" The involved reactions are S I \\xrightarrow \\alpha \\beta 2I I \\xrightarrow mr D I \\xrightarrow 1 m r R \"\"\" md\"\"\" Examples \"\"\" md\" Implementation of the system \" md\"\"\" Implement the reaction model in Catalyst \"\"\" 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\"\"\" We can get 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 \"\"\" 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 parms α 0.08, β 1.0e 6, r 0.2, m 0.4 md\"\"\" Creating and solving the ODE problem and plotting results \"\"\" oprob ODEProblem infection model, u0, tspan, parms osol solve oprob, Tsit5 , saveat 0.5 plot osol plot osol, idxs S, I only S and I plot osol, idxs S, I also possible when S and I are unpacked plot osol, idxs S, I , xlab \"S\", ylab \"I\" phase plot I vs S osol.u end final values in the order of defined species md\"\"\" Example 1 Influence of r Influence of the duration of infection, 1 r , for average infection periods between 1 and 10 days of being contagious r between 0.1 and 1.0 , with a step of 0.1 , and default value 0.1 . \"\"\" bind r Slider 0.1 0.1 1, default 0.1, show value true parms1 α 0.08, β 1.0e 6, r r, m 0.4 r specified by slider oprob1 ODEProblem infection model, u0, tspan, parms1 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, parms osol2 solve deepcopy oprob2 , Tsit5 , saveat 0.5 We can compare the result now to the solution without contact reduction begin plot osol2 plot osol linestyle dash, label none, color grey, lw 0.5 end osol2.u end md\"\"\" question How do we interpret this event? Is the number of deceased and infections reduced? How much? \"\"\" osol2 D end osol D end 1 The number of deceased is reduced 13% osol2 S end osol S end 1 6 times less infections with contact measures osol2 R end osol R end 1 13% less recoveries due to fewer infections 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 thr t 1e6 α β, S I 2I r m, I D r 1 m , I R end condition3 infection model3.I ~ infection model3.thr infection model3.I ~ infection model3.I 999 000, infection model3.thr ~ 1e9 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, parms osol3 solve deepcopy oprob3 , Tsit5 , saveat 0.5 begin plot osol3 idxs S, I, D, R plot osol linestyle dash, label none, color grey, lw 0.5 end osol3.u end md\"\"\" question How do we interpret this new event? Is this a better measure than contact reduction alone? Would you know how we call such an event? \"\"\" 0.4 0.999e6 Amount of people deceased during isolation infected osol3 D end 0.4 0.999e6 osol D end 1 Deceased are reduced only 1% osol3 S end 0.999e6 osol S end 1 Infections have increased 56% wrt. example 1 osol3 R end 0.6 0.999e6 osol R end 1 1% less recoveries md\"\"\" Answer \"\"\" md\"\"\" hint Isolation alone is not effective to protect the part of the population that's already been infected. The peak of infections is not avoided, only delayed. However, the proportion of the population exposed is much lower thanks to isolation. Would then a combined set of rules be best in that case? \"\"\" 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 10^ 6 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 md\" Initialize vector `parms ex1` with parameter values \" parms ex1 missing md\"\"\" Create the ODE problem and store it in `oprob ex1` \"\"\" oprob ex1 missing md\"\"\" Solve the ODE problem and store the solution in `osol ex1` \"\"\" osol ex1 missing md\"\"\" Plot the solutions \"\"\" missing md\"\"\" Change the value of \\alpha in the `params ex1` vector to visualize the effect in the plot. \"\"\" md\"\"\" warning \"Tip\" You can move the cell containing the slider next to the figure to visualize the changes in \\alpha . \"\"\" md\"\"\" Try to interpret the results yourself. Ask yourself the following questions question 1. What are the trends in the obtained results? \"\"\" md\"\"\" Answer \"\"\" md\"\"\" question 2. How can this be explained from the model structure? \"\"\" md\"\"\" Answer \"\"\" 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 ..., I R end md\"\"\" Convert to an ODE system. Check the differential equations and make sure you understand each term. \"\"\" osys ex2 missing md\"\"\" Set up parameter values \"\"\" parms ex2 missing md\"\"\" Create the ODE problem and store it in `oprob ex2` \"\"\" oprob ex2 missing md\"\"\" Solve the ODE problem and store the solution in `osol ex2` \"\"\" osol ex2 missing md\"\"\" Plot the solutions \"\"\" missing 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 question Why does the peak in the number of infected individuals shift to the right when the value of b increases? \"\"\" md\"\"\" Answer \"\"\" md\"\"\" Check the number of fatalities \"\"\" missing ...% less deaths for b 0.2 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 . 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 ..., I R ..., ... ... end md\"\"\" Convert to an ODE system. Check the differential equations and make sure you understand each term. \"\"\" osys ex3 missing 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 md\"\"\" Set up parameter values \"\"\" parms ex3 missing md\"\"\" Create the ODE problem and store it in `oprob ex3` \"\"\" oprob ex3 missing md\"\"\" Solve the ODE problem for step wise increasing values of v and store the solution in `osol ex3 vac`. \"\"\" osol ex3 no vac missing md\"\"\" First, put the value of b in Exercise 2 to 0.2 . 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 returning 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 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 md\"\"\" Make a new reaction system where the discrete event is included. Name it `infection med vac c`. \"\"\" named infection med vac c missing md\"\"\" Complete the new reaction system . Name it `infection med vac c com`. \"\"\" infection med vac c com missing md\"\"\" Create the ODE problem and store it in `oprob ex3 c` \"\"\" oprob ex3 c missing md\"\"\" Solve the ODE problem. Make a deepcopy and use `Tsit5 ` and `saveat 0.5`. Store the solution in `osol ex3`. \"\"\" osol ex3 missing md\"\"\" Plot the solutions \"\"\" missing md\"\"\" Check the number of fatalities now and compare to the case without vaccination. \"\"\" missing md\"\"\" question What can you say about the rate of infection in the case of no vaccination? How would you measure this in the plot? \"\"\" md\"\"\" Answer \"\"\" md\"\"\" hint The rate of infection has to do with how many people get infected during a certain period... \"\"\" "},{"url":"exercises/ode_model_irrigation_mtk/","title":"1. ODE_model_irrigation","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"2\" title \"1. ODE model irrigation\" tags \"exercises\" layout \"layout.jlhtml\" description \"Modeling of irrigation with MTK\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Irrigation experiment \"\"\" md\"\"\" https users.ugent.be ~gvhaelew fig irrigation model.png \"\"\" 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 r \\cfrac S 1 S 1,res S max , The underground outflow into groundwater O v\\,S 2^2 , The amount of percolation to deeper soil layers P \\cfrac k S max S 1 . The latter three are called observables . \"\"\" md\"\"\" Model the aforementioned system of differential equations using ModelingToolkit. \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Define the variables S 1 and S 2 with their corresponding initial values. Consider the observables R , O and P also as variables, as well as the 'parameter' r . \"\"\" variables missing md\"\"\" Define the parameters for this model and assign their corresponding values. Mind that r is a variable and, hence, should not be listed as a parameter. | Parameter | Value | Unit | | | | | | `k` | 3 | mm\\,h^ 1 | | `v` | 1.0e 3 | h^ 1 \\,mm^ 1 | | `S₁res`| 10 | mm | | `Smax` | 150 | mm | \"\"\" parameters missing md\"\"\" Set up de model equations. \"\"\" change S1 missing change S2 missing md\"\"\" Set up an equation for the variable r . hint The equation contains two terms a constant term 5 and a another term that will add 5 when t 60 . \"\"\" eq flow rate missing md\"\"\" Set up the equations for the runoff R , the outflow O and the percolation P . \"\"\" eq runoff missing eq outflow missing eq percolation missing md\"\"\" Bundle all equations. \"\"\" eqs irrigation missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build a system of equations with ` mtkbuild`. Name it `sys irrigation`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem for your system and name it `oprob irrigation`. \"\"\" oprob irrigation missing md\"\"\" Solve the ODE problem. Use `Tsit5 `, `saveat 1.0` and `reltol 1e 9`. Store the solution in `osol irrigation` \"\"\" osol irrigation missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot R , O and P . \"\"\" missing md\"\"\" Check out the solution for S 1 and S 2 . \"\"\" missing missing md\"\"\" What is the value of S 1 for t 100\\ h ? \"\"\" missing md\"\"\" Calculate the value of R for t 100\\ h using the value for S 1 at t 100\\ h and the parameters. \"\"\" missing md\"\"\" question Does this value correspond to the value you can determine from the plot of R ? \"\"\" md\"\"\" Answers missing \"\"\" md\"\"\" Make a plot of S 1 and S 2 . \"\"\" missing "},{"url":"exercises/ode_model_mtk_intro/","title":"1. ODE_model_MTK_intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"1\" title \"1. ODE model MTK intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Introduction to ModelingToolkit\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents packages needed for example to make plots using OrdinaryDiffEq, ModelingToolkit packages needed to solve differential equations with ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D t will be the symbol for the time D will be the differentiation operator md\"\"\" Introduction to ModelingToolkit ODE \"\"\" TableOfContents displays that table of contents on the right md\"\"\" ModelingToolkit.jl is a Julia package for symbolic and numeric modeling of complex systems , such as those described by for example differential equations and algebraic equations . It’s part of the SciML ecosystem Scientific Machine Learning . At its core, it lets you 1. Define models symbolically – You describe variables, parameters, and equations symbolically, rather than writing them out procedurally. 2. Automatically generate efficient code – The toolkit simplifies, differentiates, and compiles your model into optimized Julia code for simulation or analysis. 3. Compose models – You can build complex systems by connecting smaller subsystems useful in fields like biology, mechanics, or electrical circuits . 4. Perform advanced analyses – It supports symbolic simplification, Jacobian and sensitivity computation, structural analysis, and automatic differentiation. In short, ModelingToolkit bridges the gap between symbolic math and high performance simulation — enabling you to describe models declaratively, and then solve them efficiently using Julia’s differential equation solvers. This is a barebones tutorial to ModelingToolkit, helping you to build simple models based on ODEs. See the documentations https docs.sciml.ai ModelingToolkit stable for more details. Below we will illustrate the use of ModelingToolkit for three different cases. \"\"\" md\"\"\" Case 1 Conical tank https i.ibb.co 7xYxhD7f conical tank.png \"\"\" md\"\"\" As the figure here above illustrates, a conical tank with height H and base radius R has a constant inlet flow rate q in q . At the bottom, the outlet flow rate is proportional proportionality factor k to the height h of the liquid in the tank. At the liquid's surface level in the tank its radius is denoted as r . Hence, the liquid's volume is V \\frac 1 3 r^2 h \\pi . It also hold that r \\frac Rh H . The rate of change in V is given by the following differential equation ``` math \\cfrac d V t dt q in q out q k h t ``` We are not going to substitute r in the equation for the volume and or simplify \\frac dV t dt . Instead, we are going to define the relevant variables that change over time and define their relation to eachother. \"\"\" md\"\"\" Defining variables and parameters \"\"\" md\"\"\" We define variables for the height h , the radius r and the volume V as follow. Mind that you need to explicitely write their dependency on the time t . Here we have also provided a default initial value initial condition for V . This is optional as later when creating the ODE problem you can overwrite these values . The initial values for h and r will be calculated when solving the problem. \"\"\" variables h t r t V t 0.1 md\"\"\" We define the parameters. Optionally, as with the variables, you can also provide default values for the parameters and or overwrite them later when creating the ODE problem. Here we have provided default parameter values for H and R . \"\"\" parameters k q H 5.0 R 2.0 md\"\"\" Defining the equations \"\"\" md\"\"\" Now we will define all necessary equations. \"\"\" md\"\"\" We define the equation that relates r to h . The equation is named `eq radius`. You can choose an arbitrary name but use one that makes sense. Mind that between the lefthandside and the righthandside a `~` tilde is used and not a ` ` equal sign . The ` ` is only used to name the equation. Furthermore, write the name of the variables without explicitely stating their dependency on the time. \"\"\" eq radius r ~ h R H md\"\"\" Next, we define the equation that relates V to r and h . \"\"\" eq volume V ~ π r^2 h 3 md\"\"\" Finally, we will define the differential equation that relates the rate of change in V to the inlet flow rate and the outlet flow rate. The symbolic operator `D` takes the symbolic differentiation of `V` in `D V `. \"\"\" change vol D V ~ q h k change in volume inflow outflow md\"\"\" After all equations have been defined, we will bundle them in a vector using square brackets as below. \"\"\" eqns tank eq radius, eq volume, change vol md\"\"\" Building the MTK system and creating the ODE problem \"\"\" md\"\"\" We will build an ODE system for MTK ModelingToolkit . You need to provide the list of equations cf. here `eqns tank` and the symbol `t` for the time to the function `ODESystem`. Optionally you can also provide so called continuous and discrete events, but this will be illustrated later. The name of the MTK model or system given is here `tank`. \"\"\" mtkbuild tank ODESystem eqns tank, t md\"\"\" After having built the MTK model we need to create the ODE problem. You need to provide the MTK model cf. here `tanks` , a vector with the initial conditions, a time span and a vector of parameter values to the function `ODEProblem`. Because V , r and h are related to each other, we provide the initial condition for V only and provide guessed values for r and h . Some remarks Since the initial value for V was set when defining the variables, we could as well have written ` ` instead of ` V 0.1 ` if you want to use the default value. You can overwrite the initial value here if you wish. Beware that V , r and h are related with eachother and that only one of them must be given an initial value. The other two values need to be 'guessed' cf. the keyword argument `guesses ...` . We have intentionally not specified the parameters `R` and `H` in the vector of parameters, because we want to use their default values. You can overwrite the default parameter values here if you wish. \"\"\" tank prob ODEProblem tank, V 0.1 , 0.0, 100.0 , q 0.1, k 0.05 , guesses r 0.1, h 0.1 md\"\"\" Solving and plotting \"\"\" md\"\"\" We will now solve the ODE problem using the function `solve`. The function expects two inputs an `ODEProblem` and an ODE solver. It is also possible to call `solve` with just the `ODEProblem`, in which case it will try to automatically select a suitable solver for the ODE problem. This function also has many keyword arguments, which you can find in the docs page of the function type `?solve` in a cell or write in directly in the `🔍 Live docs` in the bottom right corner . One we will use often is `saveat`, which decides at what timesteps the solution will be returned. For example, if you provide the option `saveat 1`, then the solution will be approximated every time unit. \"\"\" tank sol solve tank prob tank sol solve tank prob, saveat 1 md\"\"\" If you want to check the number of time instances at which the solution was approximated, you can use the function `length` \"\"\" length tank sol md\"\"\" If you plan on further analyzing the solution for one of the variables, you can access their values by indexing the solution object with the symbolic variable. For example \"\"\" tank sol V md\"You can also use the variable name as a `Symbol` created by adding a leading ` ` \" tank sol V tank sol V md\"\"\" You can plot the evolution of V , h and r using the function `plot` and providing the name of the solution object. The keyword argument `idxs` allows you to choose what variables to plot. \"\"\" plot tank sol, idxs V, r, h plot tank sol md\"\"\" You can calculate the steady state equilibrium values by solving a steady state problem in the way below. The first argument to `SteadyStateProblem` is the name of the MTK system model, the second argument consists of inital guesses of the steady state values and the third argument consists of the parameter values. \"\"\" equil val tank solve SteadyStateProblem tank, V 1.0, h 2.0, r 0.5 , q 0.1, k 0.05 md\"\"\" Then you can retrieve the steady state value of each variable in the following way \"\"\" equil val tank V equil val tank h md\"\"\" You can also provide an expression to `idxs` consisting of variables and or parameters. Below, we show the ratio V h . \"\"\" plot tank sol, idxs V h md\"\"\" Case 2 Mass and spring system https i.ibb.co MDTGngfq damped spring.png \"\"\" md\"\"\" In this case we will simulate an under damped harmonic oscillation. In an under damped harmonic oscillator, the system experiences oscillatory motion while the amplitude gradually decreases over time due to damping. The deviation y t describes the displacement of the oscillator from its equilibrium position as a function of time, typically following an exponentially decaying sinusoidal form. The total mechanical energy E t , composed of both kinetic and potential contributions, also decays exponentially as energy is dissipated by the damping mechanism. Studying y t and E t provides insight into how the oscillation amplitude and system energy evolve under the influence of damping. The motion of an under damped harmonic oscillator is governed by Newton’s second law, leading to the differential equation ``` math m y'' t k y t \\mu y' t ``` where m is the mass, \\mu the damping coefficient, and k the spring constant. The under damped condition corresponds to \\mu^2 4mk , resulting in oscillatory motion with an exponentially decaying amplitude. We will make sure that the parameter values in this example meets this condition. The total mechanical energy is given by ``` math E t \\cfrac 1 2 m y' t ^2 \\cfrac 1 2 k y t ^2 ``` and, due to damping, it decreases over time as energy is continuously dissipated by the resistive force. \"\"\" md\"\"\" Defining variables and parameters \"\"\" md\"\"\" We define variables for the position y and the total energy E as follow. Don't forget to mention the dependency on the time t . Here, you could provide a default initial condition for y but this is optional. E doesn't need an initial condition because E will be calculated at each iteration step when y and y' were computed. \"\"\" variables y t E t position and energy md\"\"\" We define the parameters. Optionally, as with the variables, you can also provide default values for the parameters and or overwrite them later when creating the ODE problem. An other option is to provide an informative description string with the meaning of each of the parameters. \"\"\" md\"\"\" note We can't define a parameter `k` here because we already defined one in the previous section, and Pluto doesn't allow variables to be defined multiple times in different places this guarantees the code returns the same output no matter what order it is run in . Because of this, we call it `k s` instead. \"\"\" parameters μ description \"friction\" m description \"mass\" k s description \"spring constant\" md\"\"\" Defining the equations \"\"\" md\"\"\" In the differential equation Newton's second law we use the second derivative of y y'' \\cfrac d^2y dt^2 . You can use the following notations to write the symbolic second derivative of y \"\"\" D D y second order derivative wrt time D^2 y second order derivative wrt time md\"\"\" We will define the second order differential equation for the position i.e. the equation based on Newton’s second law . \"\"\" spring eq m D D y ~ k s y μ D y eq spring m D^2 y ~ k s y μ D y md\"\"\" Next, we will define the expression for the total energy of the system. \"\"\" exp energy E ~ m D y ^2 2 y^2 k s 2 we can add quantities to keep track off. md\"\"\" Building the MTK system and creating the ODE problem \"\"\" md\"\"\" We will now build an ODE system for MTK ModelingToolkit . You will need to provide the vector of equations and the symbol `t` for the time to the function `ODESystem`. In addition we will introduce a discrete event at time t `40`, the mass is reduced by 75%. You could image that part of the mass fell from the spring during the movement. The name of the MTK model or system given is here `spring ode`. If you have multiple discrete events you can include them in the following way ` ... ...~... , ... ...~... , ... `. \"\"\" mtkbuild spring ode ODESystem eq spring, exp energy , t discrete events 40 m~ 1 0.75 m md\"\"\" As you can notice, the second order ODE has been converted into a system of two first order ODEs. A new variable `y t` was introduced such that `y t` \\cfrac dy dt . This new variable is nothing else than y' . \"\"\" md\"\"\" After having built the MTK model we need to create the ODE problem. You need to provide the name of the MTK model cf. here `spring ode` , a vector with the initial conditions, a time span and a vector of parameter values to the function `ODEProblem`. Remark that since the second order ODE was converted into a system of two ODEs, you need to provide two initial conditions one for y and one for y' . The symbolic notation for y' is `D y ` and the latter should be used while creating the ODE problem. \"\"\" spring prob ODEProblem spring ode, y 2.0, D y 1.0 , 0.0, 100. , m 3, k s 0.6, μ 1e 1 md\"\"\" Solving and plotting \"\"\" md\"\"\" We will now solve the ODE problem using the function `solve`. In this case we have provided a solver cf. `Tsit5 ` and a relative tolerance to be met by the solver cf. `reltol 1e 9` . The solver `Tsit5 ` is a recommended solver for non stiff problems. See the documentation https docs.sciml.ai DiffEqDocs dev solvers ode solve for more details. \"\"\" sol spring solve deepcopy spring prob , Tsit5 , reltol 1e 9 md\"\"\" note When working with events, it is good practice to take a `deepcopy` of the ODE problem before solving it. This is because events can change parameter values of the problem while solving, which do not reset when solving is done. Therefore solving the same problem a second time would use a changed set of initial parameters, and give different results. Copying the problem before solving prevents this issue. \"\"\" md\"\"\" You can plot the evolution of y and y' using the function `plot` by just providing the name of the solution object. \"\"\" plot sol spring md\"\"\" You can clearly notice some change in the oscillatory at t 40. \"\"\" md\"\"\" If you want to see the evolution of E , you can provide it to the `idxs` keyword argument. \"\"\" plot sol spring, idxs E md\"\"\" Case 3 Lotka Volterra Classical model for prey predator relations \"\"\" md\"\"\" The Lotka–Volterra model , also known as the predator–prey model , is a pair of first order, nonlinear differential equations that describe the dynamic interaction between two biological species one as a prey population and the other as its predator. Developed independently by Alfred J. Lotka and Vito Volterra in the 1920s, the model captures the cyclical nature of population sizes — with predator numbers rising and falling in response to changes in prey abundance, and vice versa. Despite its simplicity, the Lotka–Volterra framework remains a cornerstone of theoretical ecology, providing insight into population oscillations, stability, and the balance of ecosystems. In this case we will consider a population of rabbits R as prey and foxes F as predators. Suppose that their evolution over time is governed by the following equations ``` math \\begin align \\cfrac dR dt & \\alpha \\left 1 \\cfrac R K \\right R \\beta R F \\\\ \\cfrac dF dt & \\gamma R F \\delta F \\end align ``` The rabbit population grows at rate with \\alpha but is limited by a carrying capacity K , while predation by foxes reduces it at a rate \\beta . The fox population increases proportionally to the number of hunts \\gamma R F and declines naturally at rate \\delta . \"\"\" md\"\"\" Defining variables and parameters \"\"\" md\"\"\" We will define the variables for this model. For the rabbits we will use the symbol 🐰 and for the foxes the symbol 🦊. In order to get the first symbol, type a back slash `\\` and then type ` rabbit ` followed by the TAB key. The second symbol you can get analogously, type `\\` and then type ` fox face ` followed by the TAB key. If you hit the TAB key before finishing the name of the symbol, you can see different options. Don't forget to specify the dependence on the time `t`. \"\"\" variables 🐰 t 🦊 t md\"\"\" Next, we will define the parameters. To get the greek letters is similar to getting the rabbit and foxes symbols. For example, to get α, type `\\` and then `alpha` followed by the TAB key. The other ones you can get with `beta`, `gamma` and `delta`. \"\"\" parameters α β γ δ K md\"\"\" Defining the equations \"\"\" md\"\"\" Now we will define the equations. We will readily define and bundle both equations simultanuously and name the vector of equations `LV eqs`. \"\"\" LV eqs D 🐰 ~ α 🐰 1 🐰 K β 🐰 🦊, D 🦊 ~ γ 🐰 🦊 δ 🦊 md\"\"\" Building the MTK system and creating the ODE problem \"\"\" md\"\"\" In this example we will introduce a so called continuous event. The continuous event is formulated here below. It states that when the population of rabbits hits 300, then its population is brought back to 100. You can imagine that if you have enough rabbits, say 300, hunters will hunt the rabbits and bring its population back to 100. In our case there is only one continuous event, but it is possible to include multiple continuous events in the following way ` ...~... ...~... , ...~... ...~... , ... ` \"\"\" rabbitmanagment 🐰 ~ 300.0 🐰 ~ 100.0 md\"\"\" We will now build an ODE system for MTK. You will need to provide the list of equations, the symbol `t` for the time and the continuous event to the function `ODESystem`. \"\"\" mtkbuild lv sys ODESystem LV eqs, t continuous events rabbitmanagment md\"\"\" Next, we will create the ODE problem by providing the name of the MTK system, the initial conditions for 🐰 and 🦊, the time span and the parameter values. \"\"\" lv prob ODEProblem lv sys, 🐰 1.0, 🦊 1e 2 , 0, 100 , α 0.6, β 0.6, γ 0.04, δ 0.5, K 1000 lv prob ODEProblem lv sys, 🐰 100, 🦊 4 , 0.0, 100.0 , α 0.6, β 0.016, γ 0.004, δ 0.6, K 1000 md\"\"\" Solving and plotting \"\"\" md\"\"\" We will now solve the ODE problem using the function `solve`. In this case we have provided a solver cf. `Tsit5 ` and a relative tolerance to be met by the solver cf. `reltol 1e 9` . Also notice the `deepcopy` of the ODE problem when dealing with events. \"\"\" sol LV solve deepcopy lv prob , Tsit5 , reltol 1e 9 md\"\"\" You can plot the evolution of 🐰 and 🦊 using the function `plot` by just providing the name of the solution object. You can clearly see that when 🐰 hits 300, its population is brought back to 100. This happens three times. In this period of time the population of 🦊 has grown in a way that the 🐰 cannot reach a population of 300 anymore. Instead, both populations go to steady state values. We have provided written labels this time because the fancy symbols don't come through in the legend of the plot. \"\"\" plot sol LV, label \"rabbits\" \"foxes\" md\"\"\" Here below we calculate the steady state values for the rabbits and the foxes. \"\"\" equil val LV solve SteadyStateProblem lv sys, 🐰 100, 🦊 40 , α 0.6, β 0.016, γ 0.004, δ 0.6, K 1000 md\"\"\" The steady state value of the rabbits \"\"\" equil val LV 🐰 md\"\"\" The steady state value of the foxes \"\"\" equil val LV 🦊 md\"\"\" You can also plot the ratio of the rabbits over the foxes in the following way. \"\"\" plot sol LV, idxs 🐰 🦊, label \"rabbits foxes\" "},{"url":"exercises/ode_model_tank_h_mtk/","title":"1. ODE_model_tank_h_mtk","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"4\" title \"1. ODE model tank h mtk\" tags \"exercises\" layout \"layout.jlhtml\" description \"modeling the height of the water in a tank\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq using ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Cylindrical tank water level \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" https users.ugent.be ~gvhaelew fig tank height.png \"\"\" md\"\"\" Deriving the model equations \"\"\" md\"\"\" A cylindrical tank is filled with water its density is denoted \\rho with a constant flow rate of Q in . The outlet flow rate Q out depends on the square root of the water level height h in the tank in the following manner ``` math Q out \\cfrac \\sqrt h R ``` where R is the resistence coefficient of the orifice. The cross section A of the tank is constant, hence, the mass M of water in the tank only varies with the height of the water ``` math M \\rho\\,V \\rho\\,A\\,h ``` \"\"\" md\"\"\" task Derive as an exercise a single model equation for the height h of the water inside the tank. \"\"\" md\"\"\" hint Start by setting up the rate of change of the mass of water in the tank. ``` math \\cfrac dM dt \\cdots ``` work toward ``` math \\cfrac dh dt \\cdots ``` \"\"\" solution md\"\"\" ``` math \\cfrac dh dt \\cfrac Q in A \\cfrac \\sqrt h AR ``` \"\"\" md\"\"\" Apart from the height of the water, we are also interested in the hydrostatic pressure p in bar , note that 1\\ Pa 10^ 5 \\ bar at the bottom of the tank ``` math p \\rho\\,g\\,h ``` where g is the gravitational constant. \"\"\" md\"\"\" Define variables for the height h of the water in the tank and the hydrostatic pressure p . Name them `h` and `p`. Mention the dependency on the time t . \"\"\" variables missing md\"\"\" Define the parameters for this model and assign their corresponding default values. | Parameter | Value | Unit | Meaning | | | | | | | A | 0.152 | m^2 | cross sectional area | | \\rho | 1000.0 | kg m^3 | water density | | g | 9.81 | m s^2 | gravitational constant | | Q in | | m^3 s | inlet flow | | R | | s m^ 5 2 | res. coeff. orifice | The values of Q in and R will be set later when creating the ODE problem using values from sliders. \"\"\" parameters missing md\"\"\" Part 1 filling up \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" In this part we will analyze the height of the water h over time for different values of the initial height of the water h 0 , the inlet flow Q in and the resistance coefficient R of the orifice. The latter three will be set using values from sliders see below, just above the plot of the variables . \"\"\" md\"\"\" Set up the equation for the rate of change in height. Multiply the term \\cfrac \\sqrt h R with h 0 . Think about why this must be done. \"\"\" change height missing md\"\"\" Set up the equation that will keep track of the hydrostatic pressure. Don't forget to include ` 1e 5` in the term in order to have it in bar . \"\"\" eq pressure missing md\"\"\" Bundle the equations. \"\"\" eqns tank missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model. Name it `sys1 tank`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. Use the variables `h₀`, `QinLmin` and `R val` to set the initial height, the inlet flow and the resistance coefficient, respectively. Important remarks The value of `QinLmin` is in liters per minute L min . Hence, you need to multiply it with `1e 3 60` to have it in m^3 s before assigning it to `Qin`. You can use the default values for the parameters `A`, `ρ` and `g`, which were defined earlier. \"\"\" oprob1 tank missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1`. \"\"\" sol1 tank missing md\"\"\" Plotting results \"\"\" bind h₀ Slider 0 0.1 2.5, default 0, show value true bind QinLmin Slider 0 10 120, default 100, show value true bind R val Slider 400 10 800, default 600, show value true md\"\"\" Plot the height and the pressure. Fix the limits of the y axis to 0.0, 2.6 by specifying `ylim 0.0, 2.6 `. Play with the sliders above to see their effect. \"\"\" missing md\"\"\" questions 1. On what parameters does the equilibrium value for the height depend? 2. Can you calculate this equilibrium value analytically using the equation for the rate of change in height? How does the expression look like? 3. To what values do you need to set `h₀` and `Qin` in order to completely drain the tank? \"\"\" md\"\"\" Answers 1. missing 2. missing 3. missing \"\"\" md\"\"\" Calculating the steady state value \"\"\" md\"\"\" Calculate the steady state value using `SteadyStateProblem`. Use `1.0` as a first guess for h . Make this calculation only when `QinLmin` is greater than 30\\ L min . \"\"\" equil val missing md\"\"\" Display the steady state value. \"\"\" missing md\"\"\" Part 2 filling up and draining \"\"\" md\"\"\" In this part the tank will be filled up to a certain height and then completely drained. In order to achieve this, we will use a continuous event. \"\"\" md\"\"\" The tank is initially empty and filled up with an inlet flow Q in of 100 L min . Take 600 for the value of R . The continuous event should state that when h becomes 0.99 , the value of Q in should be set to 0 . \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the new model that includes the continuous event. Name it `sys2 tank`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the new ODE problem. Set the correct values for the initial height and the parameters. The simulation time is the same as in Part 1. \"\"\" oprob2 tank missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1`. Don't forget to make a `deepcopy` of the ODE problem. \"\"\" sol2 tank missing md\"\"\" Plotting results \"\"\" md\"\"\" Plot the height and the pressure. \"\"\" missing md\"\"\" questions 1. What is the time needed to drain the tank after the height became `0.99`? Derive this numerically using the results. 2. Can you calculate this time also analytically? 3. Do both numerical and analytical draining times more or less correspond? Hints You can you `findfirst x , V ` to find the first index of the value `x` in the vector `V`. To slice a vector `V` from a certain index `i` to the `end`, do `V i end `. You can use `findfirst 0 , V ` to find the first index of the value less than `0` in the vector `V`. Don't use `findfirst 0 , V ` because the numerical values will never be exactly `0` in the tail of the height vector. \"\"\" md\"\"\" Numerical derivation \"\"\" i max missing Δi zero missing missing md\"\"\" Analytical calculation optional \"\"\" missing md\"\"\" Answers 1. missing 2. missing 3. missing \"\"\" md\"\"\" Part 3 modifying Qin and R \"\"\" md\"\"\" In this part the values of Q in and R will be modified at distinct moments in time. In order to achieve this, we will use discrete events. The tank is initially empty and filled up with an inlet flow Q in of 100 L min . Take 600 for the value of R . At the time instant 600 s the value of Q in will increase by 10\\ \\% i.e., multiplied by `1.1` . At the time instant `1200` s the value of `R` will be decreased by 25\\ \\% i.e., multiplied by `0.75` . \"\"\" md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the new model that includes both discrete events. Name it `sys3 tank`. \"\"\" mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the new ODE problem. Set the correct values for the initial height and the parameters. The simulation time is the same as in Part 1 and 2. \"\"\" oprob3 tank missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 1`. Don't forget to take a `deepcopy` of the ODE problem. \"\"\" sol3 tank missing md\"\"\" Plotting results \"\"\" missing md\"\"\" question Is the evolution of the height accoding to your intuition? Think about what should happen to the height when you increase Q in and decrease R . \"\"\" md\"\"\" Answer missing \"\"\" "},{"url":"exercises/ode_model_tractor_seat_mtk/","title":"1. ODE_model_tractor_seat","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.13 frontmatter order \"5\" title \"1. ODE model tractor seat\" tags \"exercises\" layout \"layout.jlhtml\" description \"modeling the movement of a oscilatory tractor seat\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using StatsPlots, PlutoUI TableOfContents using OrdinaryDiffEq, ModelingToolkit using ModelingToolkit t nounits as t, D nounits as D md\"\"\" Exercise Tractor seat \"\"\" solution text Markdown.MD Markdown.Admonition \"hint\", \"Solution\", text md\"\"\" https users.ugent.be ~gvhaelew fig tractor seat all.png \"\"\" md\"\"\" Deriving the model equation \"\"\" md\"\"\" We want to model the movement of a tractor seat when, at time t 1 , a person goes to sit on the seat, suddenly increasing its mass. First, derive the equation relating the vertical position y of a tractor seat with its mass m . The tractor seat consists of a spring and a shock absorber. The spring constant is denoted k and the natural length of the spring is denoted L . The shock absorber has a damping effect which is proportional proportionality factor b , but opposite, to the velocity of the seat when moving. Pay attention to the sense of forces Assume that the seat was initially in a rest position y 0 and not moving v 0 0 . \"\"\" md\"\"\" task Derive the model equation for the position y of the seat. Use Newton's second law. \"\"\" md\"\"\" hint Set up ``` math \\begin align m\\cfrac d^2 y dt^2 & \\cdots \\\\ \\end align ``` \"\"\" solution md\"\"\" ``` math \\begin align m\\cfrac d^2 y dt^2 & mg k\\left L y\\right bv \\end align ``` \"\"\" md\"\"\" Setting up the equations \"\"\" md\"\"\" Define the variable for the position y . Name it `y`, and mention the dependency on the time t . \"\"\" variables missing md\"\"\" Define the parameters for this model and assign their corresponding values. | Parameter | Value | Unit | Meaning | | | | | | | m | 23.0 | kg | mass of the seat | | g | 9.81 | m s^2 | gravitational constant | | k | 22570 | N m | spring constant | | L | 0.40 | m | natural length of the spring | | b | 900 | Ns m | damping constant | \"\"\" md\"Use the following names `m`, `g`, `k`, `L` and `b`.\" parameters missing md\"\"\" In order to know the initial position y 0 of the seat, calculate the steady state of y . Hints Set both \\cfrac d^2 y dt^2 and v \\cfrac d y dt to zero in the model equation and determine an expression for the steady state of y . Once you have the expression it is preferable to calculate the actual value it a `let` ... `end` block. Everything in this block will be local scope and won't interfere with variable names defined elsewhere. Round to two digits after the decimal point with the function `round ..., digits 2 `. \"\"\" let m 23.0 g 9.81 k 22570 L 0.40 missing end md\"\"\" Set up the model equation. \"\"\" eq position missing md\"\"\" Building the ODE system \"\"\" md\"\"\" Build the model and include the discrete event that when the time is 1 second, the mass needs to be incremented with the mass of the person. Assume the person weighs 80 kg . Name your model `sys tractor seat`. \"\"\" M 80 mtkbuild missing md\"\"\" Create and solve the ODE problem \"\"\" md\"\"\" Create the ODE problem. Use the steady state value that you calculated before for the initial position y 0 . As mentioned before v 0 0 . The symbol for \\frac dy dt in the vector of initial conditions is `D y `. Use a simulation time span of `5.0` seconds. Assign the value `b val` to the damping constant parameter b . `b val` is defined later in the notebook and bound to a slider, so you can see how it influences the results. \"\"\" oprob treactor seat missing md\"\"\" Solve the ODE problem. Make a deepcopy of the ODE problem, use `Tsit5 ` and `saveat 0.01`. \"\"\" sol tractor seat missing bind b val Slider 200 50 4000, default 900, show value true md\"\"\" Plotting results \"\"\" md\"\"\" Plot the position y of the seat over time. Use the option `idxs y ` and `ylim 0.32, 0.392 `. Play with the sliders above to see their effect. \"\"\" missing md\"\"\" Check the final value of y . \"\"\" missing md\"\"\" questions 1. Can you interprete the plot? 2. What is the approximate value of b in order to have a critically damped motion? A critically damped motion is when a system returns to its equilibrium position as quickly as possible without oscillating or overshooting . 3. Does the steady state value depend on the value of b ? \"\"\" md\"\"\" Answers 1. missing 2. missing 3. missing \"\"\" md\"\"\" Plot the velocity v of the seat over time. Use `idxs D y `. \"\"\" missing md\"\"\" question If you use a considerable value for b , will the seat still be moving at the end of the time span? \"\"\" md\"\"\" Answer missing \"\"\" md\"\"\" Calculating the steady state values \"\"\" md\"\"\" Create a steady state problem and solve it. Hint set the mass `m` to the correct value \"\"\" stst val missing md\"\"\" Show the steady state value for `y` and `D y `. \"\"\" missing missing md\"\"\" question Do the steady state values correspond to the ones you can derive from the plots? \"\"\" md\"\"\" Answer missing \"\"\" "},{"url":"exercises/optim_wastewater_treatment/","title":"6. Optimisation wastewater treatment","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"34\" title \"6. Optimisation wastewater treatment\" tags \"exercises\" layout \"layout.jlhtml\" description \"Optimisation wastewater treatment\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using ModelingToolkit, OrdinaryDiffEq using ModelingToolkit t nounits as t, D nounits as D using Turing, StatsPlots, StatsBase, Optim using PlutoUI TableOfContents md\"\"\" Exercise Wastewater treatment Optimisation \"\"\" md\"\"\" Consider a wastewater treatment plant where wastewater circulates through cylindrical tanks, allowing microorganisms to break down the organic material present. At the top of such a tank with volume V\\ \\mathrm m^3 , wastewater enters at a flow rate q\\ \\mathrm m^3 h . The concentration of organic material in the inflow is known and equal to C in \\ \\mathrm kg m^3 . At the bottom of the tank, wastewater and microorganisms leave the tank at the same flow rate q\\ \\mathrm 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\\ \\mathrm kg m^3 and the concentration of microorganisms is denoted as X\\ \\mathrm 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 \\ \\mathrm m^3\\,h^ 1 \\,kg^ 1 with yield coefficient Y . The factor K s\\ \\mathrm kg m^3 is the concentration of C where the rate is half its maximum rate and r\\ \\mathrm m^3\\,h^ 1 \\,kg^ 1 is the maximum growth rate coefficient. Furthermore, the microorganisms degrade with a rate coefficient k d . In the middle of the tank, a mixing system ensures that wastewater and microorganisms are thoroughly 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 | | | | | 3.0 | 0.5 | | 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 being broken down by microorganisms depends on the flow rate q . First Part 1 , we will simulate the system with the parameters given above. Second 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\\ \\mathrm kg\\,m^ 3 . \"\"\" md\"\"\" Part 1 In this part, we will simulate the system with the parameters given above. \"\"\" md\"\"\" Implementation of the system \"\"\" md\"\"\" Model the system by means of ModelingToolkit. \"\"\" md\"\"\" Define the variables and assign them to their default values. \"\"\" variables missing md\"\"\" Define the parameters and assign them to their default values. \"\"\" parameters missing md\"\"\" Set up the equations for the change in C and X . \"\"\" change C missing change X missing md\"\"\" Build the ODE system and name it `sys ww treat`. \"\"\" mtkbuild missing md\"\"\" Setting up initial conditions, timespan and parameter values \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions \"\"\" u0 missing md\"\"\" Set the timespan to 72 hours \"\"\" tspan missing md\"\"\" Initialize a vector `parms` with the parameter values \"\"\" parms missing md\"\"\" Creating an ODE problem, solve the problem and plot results \"\"\" md\"\"\" Create the ODE problem and store it in `oprob` \"\"\" oprob missing md\"\"\" Solve the ODE problem. Use `Tsit5 ` and `saveat 0.1`. Store the solution in `osol` \"\"\" osol missing md\"\"\" Plot the results. Use `ylim 0, 4 ` and `linewidth 2` as options. \"\"\" begin missing plot tspan 1 , tspan 2 , 0.28, 0.28 , linestyle dash, linewidth 2, linecolor green, label \"\" end md\"\"\" Check out the end value of the organic waste. \"\"\" missing md\"\"\" Part 2 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\\ \\mathrm kg\\,m^ 3 . \"\"\" md\"\"\" Declare the Turing model function. Sample the flow rate q prior from an uniform distribution in the range 0, 5 \\ \\mathrm kg\\,m^ 3 . Suppose therein that the desired end value of the organic waste i.e. 0.28\\ \\mathrm kg\\,m^ 3 is normally distributed with mean the end value obtained from the solution and standard deviation 10^ 3 \\ \\mathrm kg\\,m^ 3 . \"\"\" model function wastewater treatment inference q ~ missing u0 missing tspan missing params missing oprob missing osol missing C ~ missing end md\"\"\" Define the desired value for the organic waste with the variable name `C val`. \"\"\" missing md\"Instantiate the Turing model and condition it with the observed value of C \" wastewater treatment cond mod missing md\"\"\" Optimize the prior for q . Do this with the `MLE` method and Nelder Mead. Store the optimization results in `results mle`. \"\"\" results mle missing md\"\"\" Get the optimized value for q and assign it to `q opt`. \"\"\" q opt missing md\"\"\" Set up parameter values with the optimized parameter value. \"\"\" parms opt missing md\"\"\" Create an ODEProblem and solve it. Use `Tsit5 ` and `saveat 0.1`. \"\"\" oprob opt missing osol opt missing md\"\"\" Plot C and X simulated with the optimized parameter value. Use `ylim 0, 4 ` and `linewidth 2` as options. The dashed line indicates C 0.28\\ \\mathrm kg\\,m^ 3 . \"\"\" begin missing plot osol, linestyle dash, linewidth 1, label none, color orange blue hline 0.28 , linestyle dash, linewidth 2, color orangered, label \"C 0.28\" end md\"\"\" question Does the value of C now respect the limit in the concentration? Draw your conclusion. \"\"\" md\"\"\" Conclusion missing \"\"\" "},{"url":"exercises/probabilistic_selection/","title":"8. Probability selection","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"43\" title \"8. Probability selection\" tags \"exercises\" layout \"layout.jlhtml\" description \"Probability selection\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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 ~ missing pearls zeros num pearls for i in 1 num pearls pearls i ~ missing end end model function lognorm num pearls μ lognorm ~ missing σ lognorm ~ missing pearls zeros num pearls for i in 1 num pearls pearls i ~ missing end end md\"Instantiate the models and condition them on the available data.\" expmodel missing lognormmodel missing 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 missing exp mean missing lognorm res missing lognorm mean missing lognorm spread missing begin histogram pearlsizes, normalize pdf add plot of best fit exponential distribution end begin histogram pearlsizes, normalize pdf add plot of best fit LogNormal distribution 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 missing for pearlsize in pearlsizes likelihood missing prior missing 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 missing for pearlsize in pearlsizes likelihood missing prior missing 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 missing 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 missing 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 missing AIC exp missing AIC lognorm missing 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 missing BIC exp missing BIC lognorm missing 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 n is number of points xm ~ missing ym ~ missing missing 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 n is number of points missing end md\"Instantiate and condition the models.\" singlemodel missing doublemodel missing 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 missing begin single xm missing single ym missing end doubleres missing begin double xm1 missing double xm2 missing double ym1 missing double ym2 missing end 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 missing AIC double missing "},{"url":"exercises/probmod_1-intro/","title":"4. ProbMod intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"23\" title \"4. ProbMod intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Introduction to the sampling practicals\" 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 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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. To start off, let's load the required packages. \"\"\" md\" Problem description\" md\"\"\" Most students of our beautiful campus Coupure had to endure the bitter task of cycling through the rain. What's worse, even when the rain has stopped, one is not safe from the wetness get too close behind a fellow cyclist and their back wheel will pelt your face with dirty water droplets. In order to prevent this tragic fate, we must find how far we must stay away from the cyclist in front of us to keep our faces safe. We will do this by simulating the trajectories of the water droplets and estimating the probability that they will coincide with our face . \"\"\" md\"\"\" Problem illustration https i.imgur.com 7TDkD08.png Figure 1 An illustration of the problem. \"\"\" md\" The model\" md\"\"\" We consider a simple model for a droplet's motion it is launched into the air by the back wheel giving it an initial velocity, and then falls down due to gravity. We neglect other factors such as wind resistance. \"\"\" md\"\"\" After simulating the droplet's motion, we need to check whether it hits our face. We'll assume it's 1.5 m to 1.7 m above the ground and at some horizontal distance x f away from the cyclist in front of us. We then get hit if the droplet's altitude is in the range 1.5, 1.7 when it has travelled a distance x f . \"\"\" md\" Mathematical description\" md\"\"\" We can describe the horizontal position x and vertical position y through time t using simple equations from the physics of projectile motion https en.wikipedia.org wiki Projectile motion Trajectory in vacuum see also Figure 2 for an illustration ```math \\begin align x t & \\mathrm cos α \\, v 0 \\, t \\, , \\\\ y t & \\mathrm sin α \\, v 0 \\, t \\frac g 2 \\, t^2 \\, , \\end align ``` where the parameters are v 0 the initial velocity. \\alpha the launching angle. g 9.8 the gravitational acceleration. \"\"\" md\"\"\" Checking for droplet collision then comes down to finding the time t f the droplet has travelled a distance x f ```math t f \\frac x f \\mathrm cos \\alpha \\, v 0 \\, , ``` evaluating it's y coordinate there ```math y f \\mathrm sin α \\, v 0 \\, t f \\frac g 2 \\, t f^2 \\, , ``` and checking if it's in the range of our face ```math y f ∈ 1.5, 1.7 \\, . ``` \"\"\" md\"\"\" Model illustration https i.imgur.com fMyFGn4.png Figure 2 An illustration of the droplet launching model. Note one vector represents a velocity while the other represents an acceleration \"\"\" md\" The variables\" md\"\"\" To reiterate, we have three variables in our model 1. The initial droplet velocity v 0 . 1. The droplet launching angle \\alpha . 1. The horizontal position of our face x f . The first two of these are stochastic variables they don't have a single, exact value, but can rather take a range of different values following some underlying distribution. This can be either because we simply don't know the real value, or there really are random fluctuations on the value of the variable. For our model, we need to specify what these distributions are. We will assume for now we magically know the distributions of these variables, though they are usually inferred from data as we will see next practical. \"\"\" md\"For the initial droplet velocity v 0 , we will assume a normal distribution with mean 5 m s and a standard deviation of 1 \" plot Normal 5, 1 , xlabel \"v0 m s \", set x label ylabel \"Probability density\", set y label legend false, remove legend from figure title \"Initial velocity prior\" set title md\"For the launching angle \\alpha , we will assume a symmetric triangular distribution going from 0 radians to \\pi 2 radians \" plot TriangularDist 0, pi 2 , xlabel \"α rad \", set x label ylabel \"Probability density\", set y label legend false, remove legend from figure title \"Launching angle prior\" set title md\"The third is an input variable when cycling, we can choose how far away we stay from the cyclist in front of us. The x position of our face x f is therefore an input to the model.\" md\"\"\" note As you can see, to visualize a distribution you can simply call the `plot` function on the distribution and set extra keyword arguments as desired . \"\"\" 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\"First, we define the constant parameter `g` outside of our model, so we can use it later on for plotting \" const g 9.8 md\"\"\" note Using a global variable a variable not defined inside a function inside of a function, such as our Turing model, will slow down your code. We can fix this by declaring that the value of this global variable is constant with `const`. But don't worry this is no programming course, so it's no problem if you forget this on your exam. \"\"\" md\"\"\" Our model can be defined as follows \"\"\" note x f is an input variable, so we add it as an input to the model model function splash x f define the initial velocity v0 ~ Normal 5, 1 define the launching angle α ~ TriangularDist 0, pi 2 get the time it takes the droplet to travel the distance x f t f x f cos α v0 get the droplet's altitude at this time y f sin α v0 t f g 2 t f^2 to respect the ground, set altitude to 0 if it would be negative y f max 0, y f return y f end md\"This function does not yet know the value of the input variable x f , and is therefore an incomplete model. Calling it with a value for all its inputs will return a complete Turing model. Let's set it to 1 for now.\" splash model splash 1 md\"\"\" note Some models don't have any input variables. In this case, you still need to call it to create a \"completed\" model, though you will simply call it with no input arguments. \"\"\" md\" Sampling the model\" md\"\"\" Now that we have defined our model, we can use it to answer our question. We will estimate the probability a droplet hits our face from a distance x f by sampling a large number of different trajectories and checking in which fraction the droplet hits us. \"\"\" md\" Generating samples\" md\"We sample different sets of values for the model's parameters using the `sample` function \" chain sample splash model, the Turing model we sample Prior , the sampling algorithm we will always use `Prior ` this practical 500 the amount of samples chain md\"\"\" note The `logprior` column gives the log prior probability of a sample. For example, for a sample with v 0 6.1 and \\alpha 0.9 , this column will equal \\mathrm log P v 0 6.1 P \\alpha 0.9 , where P v 0 6.1 equals the probability density of its prior distribution, a Normal 5, 1 , at x 6.1 and analogous for \\alpha . \"\"\" md\" Getting the model output\" md\"To answer our question, we are interested in the `y f` variable returned by the model. We can extract the output variable from our chain using the `generated quantities` function \" y f samples generated quantities splash model, chain md\"It's often useful to visualize the distribution of our variable of interest. We can use the `histogram` function for this \" histogram y f samples, bins 15 md\"Checking whether the droplet altitude matches our face's can be done with a simple inequality operation \" face hit samples 1.5 y f 1.7 for y f in y f samples md\"Finally, we can estimate the probability of our face being hit by taking the mean amount of times we got hit in our sample.\" mean face hit samples md\"\"\"And that's the answer to our question If we're 1m behind the cyclist in front of us, we can expect to get hit by about ~1% of the droplets they launch exact number depends on the random samples . You can manually change the value of x f to investigate for other distances, or scroll down to the Essentials Essentials section for a handy slider.\"\"\" md\"\"\" note Do you like your code sleek? You can also calculate this probability from your samples with just one line `mean 1.5 . y f samples . 1.7 ` \"\"\" md\" Getting the stochastic variables\" md\"Often, it's enough to get a sample of the variable of interest we `return` in our Turing model. Sometimes, however, it can be useful to also get sampled values for the stochastic variables used to calculate the variable of interest. In this case, we could make a nice plot of the droplet's trajectories if we have all the sampled values of v 0 and \\alpha .\" md\"Getting the values of a model's stochastic variable can be done simply by indexing the chain with the name of that variable as a `String` or `Symbol` whichever you prefer \" begin v0 samples chain \"v0\" α samples chain α end md\"\"\" To plot the trajectories, we want an equation for y in function of x . We can do this by substituting x t into the equation for y t to acquire the following \"\"\" md\"\"\" ```math y x \\frac \\mathrm sin α \\mathrm cos α \\, x \\frac g 2 \\, \\left \\frac x \\mathrm cos α \\, v 0 \\right ^2 ``` \"\"\" md\"\"\" note You can also use this equation to find y x f in the Turing model. \"\"\" md\"First we define our trajectory function in function of x and our stochastic variables.\" y x, α, v0 sin α cos α x g 2 x cos α v0 ^2 md\"Then we get the specific trajectory for every sample of our chain by filling in the stochastic variables with the values we sampled.\" trajectories note every trajectory in our vector is a function of x so we will write an anonymous function x function x,...,... to plot the trajectory in function of x x y x, α samples i , v0 samples i for i in 1 length v0 samples md\"Finally, we plot all sampled trajectories.\" begin plot trajectories, xlims 0, 3 , ylims 0, 2 , label false, color blue, alpha 0.3 plot 1, 1 , 1.5, 1.7 , linewidth 5, color orange, label \"face\" end md\"\"\" note We set a lot of keyword arguments of our plot to make the figure pretty here, but don't panic this is still no programming course, so we will always clearly provide which one s you need if you need to plot something. If you ever do want to look for one yourself, you can find them on the function's help page `?plot` or the `🔍 Live docs` in the bottom right corner . \"\"\" md\" Sampling alternative\" md\"It's also possible to skip the chain construction entirely and simply call your model as a function to get a sample of the model output. Note that it's not possible to get the corresponding values of the stochastic variables using this approach, so only use this method if you only care about the model output.\" get a sample of the output 500 times y f samples alternative splash model for in 1 500 histogram y f samples alternative, bins 15 yup, that's the same distribution md\" For loops for many variables\" md\"\"\" Sometimes you need to define a large number of stochastic variables following the same distribution. Let's say, for example, you want to model the total mass of water being splashed on your face assuming that 10 droplets will hit you, each having a mass that is exponentially distributed with a mean of 30 mg. This can be done using a for loop as follows \"\"\" model function splash mass droplet masses zeros 10 instantiate vector of 10 zeros for i in 1 length droplet masses droplet masses i ~ Exponential 30 each element of the vector follows an Exponential distribution end total mass sum droplet masses return total mass end mass model splash mass mass chain sample mass model, Prior , 1000 total mass samples generated quantities mass model, mass chain md\" 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. Additionally, x f has been assigned to a slider so you can more easily explore at what distance your face is safe. Side note the code is wrapped in a let block so Pluto won't complain about the same variable names being used again. \"\"\" md\"\"\" note If you enjoy playing with the x f slider, the code will run much faster if you comment out the problem definition in the code below select the code and press `CTRL ` . This will make it reuse the same model defined previously and saves the time to run the model generation. \"\"\" bind x f Slider 0 0.1 5, default 1.0, show value true let model definition comment out for speed model function splash x f v0 ~ Normal 5, 1 α ~ TriangularDist 0, pi 2 t f x f cos α v0 y f sin α v0 t f g 2 t f^2 y f max 0, y f return y f end get samples splash model splash x f chain sample splash model, Prior , 500 y f samples generated quantities splash model, chain v0 samples chain \"v0\" α samples chain α calculate interesting things prob face hit mean 1.5 y f 1.7 for y f in y f samples y x, α, v0 sin α cos α x g 2 x cos α v0 ^2 trajectories x y x, α samples i , v0 samples i for i in 1 length v0 samples plot trajectories, xlims 0, 5 , ylims 0, 2 , legend false, color blue, alpha 0.3, title \"Probability of getting hit ≈ round 100 prob face hit, digits 3 %\" annotate x f, 1.6, text \"☺\", 30 end "},{"url":"exercises/probmod_2-basics/","title":"4. ProbMod basics","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"24\" title \"4. ProbMod basics\" tags \"exercises\" layout \"layout.jlhtml\" description \"Basic sampling exercises\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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 dpchain missing Y samples missing missing plot of X missing histogram of Y 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 ✅ `Y samples . 1` compares every element of `Y samples` to `1` ❌ `Y samples 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 `X samples` based on `Y samples` and vice versa for \\text var Y ∣ X . \"\"\" X samples missing varXcondY missing varYcondX missing missing analytical answer of missing md\"\"\" warning Starting here, only part of the answer's structure will be given. You are therefore expected to add more code cells yourself, using the ` ` at the left in between two cells. \"\"\" md\"\"\" 2 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\"\"\" tip 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 watercube samples missing samples of dicesum for watercube p watercube kills missing probability that watercube kills the monster missing histogram md\" 2 Dirtprism\" model function dirtprism check the \"For loops for many variables\" section from the intro notebook dicesum missing return dicesum end p dirtprism kills missing probability that dirtprism kills the monster missing histogram md\" 3 Comparison\" p watercube is better missing md\" 3 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, goes 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 10. 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 5 year old chickens. \"\"\" md\" 1 Probability\" md\"\"\" tip 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\" 4 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? Assume the probability for a person to be born is equal on every day of the year. \"\"\" 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":"4. ProbMod advanced","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"25\" title \"4. ProbMod advanced\" tags \"exercises\" layout \"layout.jlhtml\" description \"Advanced sampling exercises\" 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 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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 that 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`. \"\"\" missing plot md\" 2 Probability\" logistic t, P0, r, K K 1 K P0 P0 exp r t model function petrigrowth t missing return splittable end prob splittable missing md\" 3 Plot\" md\"\"\" tip Remember anonymous functions can be defined using `myfun x ...`, and can be visualized using `plot myfun `. The same syntax applies if `myfun` is a vector of functions. However, don't forget it was asked to plot only 100 functions. \"\"\" logfuns missing 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 applications 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 xe . 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\"\"\" You can sample the gravitational force between both cubes 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 total force between the two cubes? Is this the same as if you had treated the cubes as point masses? 1. To estimate the net force, you take the average of n samples. Of course, the result will vary every time you take a new sample if you take the average of only 10 samples, your estimated total force will vary wildly We can quantify how much the estimated total force varies by taking a sample of sample averages and then calculating the variance. Assuming you want your sample average to have a variance of no more than 0.01 , how many samples do you need? Visualise the distribution of the sample average. EXTRA How does the central limit theorem https en.wikipedia.org wiki Central limit theorem apply to this question? Can you use it to answer the question with less trial and error? \"\"\" md\" 1 Net Force\" model function cubeforce F missing return F end cubemodel cubeforce estimated force missing pointmass force missing doesn't require Turing, only maths md\" 2 Variance of Estimator\" bind n Slider 10 10 200, show value true force samples missing multiple samples of your estimated force, using `n` samples missing histogram of estimated force samples force samples var missing variance of the estimated force "},{"url":"exercises/probmod_4-review/","title":"4. ProbMod review","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"26\" title \"4. ProbMod review\" tags \"exercises\" layout \"layout.jlhtml\" description \"Review sampling exercise\" frontmatter.author name \"Bram Spanoghe\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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 . One 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.\" html\"\"\" img src \"https upload.wikimedia.org wikipedia commons thumb 5 58 Buffon needle.svg 1920px Buffon needle.svg.png\" style \"background color white \" alt \"Buffon's needles\" \"\"\" 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_mtk/","title":"3. SDE_model_aging","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"18\" title \"3. SDE model aging\" tags \"exercises\" layout \"layout.jlhtml\" description \"Aging with saturated repair, modelled as an SDE\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using ModelingToolkit, StochasticDiffEq using ModelingToolkit t nounits as t, D nounits as D using StatsPlots, PlutoUI, StatsBase TableOfContents md\"\"\" Exercise Aging and saturated repair \"\"\" md\"\"\" Aging https www.biotechniques.com wp content uploads 2024 11 aging 800x344.png \"\"\" 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 \\cfrac dW dt Lets denote the amount of senescent cells as 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 \\mu 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. W is a Wiener process. 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 using MTK. Take a default initial value X t 0 `0.0` for the species `X`, and default values of `μ 0.00558`, `β 0.4364`, `κ 1.116` for the parameters in the model. Use a noise scaling parameter `n 0.2`. \"\"\" md\"\"\" Define the only variable X with ` variables`. \"\"\" missing md\"\"\" Define the parameters with ` parameters`. \"\"\" missing md\"\"\" Instantiate a source `B` of Brownian noise a Wiener process by default with ` brownian`. \"\"\" missing md\"\"\" Set up the model equation with the noise `n B` added as a term in the equation and put it in a vector array. \"\"\" eq scs missing md\"\"\" Define the expression for the diffusion noise term and put it in a vector array. \"\"\" md\"\"\" Build a system of equations with ` mtkbuild` and the function `System`. Provide the following arguments to `System` the model equation and `t`. Name the system `sys scs`. \"\"\" missing 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.0` y to `120.0` y , and initialize a vector `parms` 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 tspan missing parms missing md\"\"\" Simulating the system as an SDE problem \"\"\" md\"\"\" Create the SDE problem. \"\"\" sprob scs missing md\"\"\" Solve the SDE problem using `EM `as solver and time step `dt 0.1`. \"\"\" ssol scs missing md\"\"\" Plot the solutions. Use the option `ylim 0, 6 ` in order to limit the range of X . \"\"\" missing 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 scs missing md\"\"\" Solve the ensemble problem. Use `EM ` as solver, take a time step `dt 0.1`, use the option `trajectories 100`. \"\"\" 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 scs missing md\"\"\" Plot the solutions. Use the option `ylim 0, 6 ` in order to limit the range of X . \"\"\" missing 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 is 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 ` \"\"\" begin times missing make empty vector for i missing for loop from 1 to 100, default step is 1 find index of first element that is greater than 5 j 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 ` or `bins 0 120`. \"\"\" missing md\"\"\" Check the mean. \"\"\" missing md\"\"\" Check the standard deviation. \"\"\" missing md\"\"\" Check the minimum value. \"\"\" missing md\"\"\" Check the maximum value. \"\"\" missing md\"\"\" question 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? 2. What is the effect of halving the damage rate \\mu ? 3. What is the effect of doubling the damage removal rate \\beta ? 4. What is the effect of halving the noise? \"\"\" md\"\"\" Answers 1. missing 2. missing 3. missing 4. missing \"\"\" "},{"url":"exercises/sde_model_fermenter_secondorder_mtk/","title":"3. SDE_model_fermenter_secondorder","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"19\" title \"3. SDE model fermenter secondorder\" tags \"exercises\" layout \"layout.jlhtml\" description \"Fermenter with second order kinetics, modelled as an SDE\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using ModelingToolkit, StochasticDiffEq using ModelingToolkit t nounits as t, D nounits as D using StatsPlots, StatsBase using PlutoUI TableOfContents 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 resulting in the following system of ODEs \\begin eqnarray \\cfrac dS dt & \\cfrac Q V \\,\\left S in S\\right k\\,X\\,S \\\\ \\cfrac dX dt & \\cfrac Q V \\,X Y\\,k\\,X\\,S \\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\"\"\" Implementation of the system \"\"\" md\"\"\" Create a MTK 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 a noisy input concentration of the substrate S in . \"\"\" md\"\"\" The parameter values are `k 0.2`, `Y 0.76`, `Q 2.0`, `V 40.0` and `Sin 2.2`. 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.1` \\ g L . Simulate the evolution of S and X during `120.0` hours. \"\"\" md\"\"\" Define the variables `S` and `X` with ` variables`. You can also add the default initial conditions. \"\"\" missing md\"\"\" Define the parameters of the system with ` parameters`. Add a noise scaling parameter `n 0.5`. \"\"\" missing md\"\"\" Instantiate a source `B` of Brownian noise a Wiener process by default with ` brownian`. \"\"\" missing md\"\"\" Set up the model equations with the noise `n B` added to `Sin` and put them in a vector array. Name the array `eqns ferm`. \"\"\" eqns ferm missing md\"\"\" Build a system of equations with ` mtkbuild` and the function `System`. Provide the following arguments to `System` the model equations and `t`. Name the system `sys ferm`. \"\"\" missing 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.0` y to `120.0` y , and initialize a vector `parms` 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 tspan missing parms missing md\"\"\" Simulating the system as an SDE problem \"\"\" md\"\"\" Create the SDE problem and store it in `sprob ferm`. \"\"\" sprob ferm missing md\"\"\" Solve the SDE problem. Use `EM ` with `dt 0.1`. Store the solution in `ssol ferm` \"\"\" ssol ferm missing md\"\"\" Plot the results with the option `ylim 0.0, 2.0 ` \"\"\" missing md\"\"\" Simulating the system as an EnsembleProblem. \"\"\" md\"\"\" Create an `EnsembleProblem` in order to visualize a multiple solutions. Store it in `esprob ferm`. \"\"\" esprob ferm missing md\"\"\" Solve the `EnsembleProblem` using the same solver and time step as before, for 100 trajectories. Store the solution in `essol ferm`. \"\"\" essol ferm missing md\"\"\" Plot the results. Use as option again `ylim 0.0,2.0 ` and also `linealpha 0.5` or `la 0.5` to modify the line boldness. \"\"\" missing md\"\"\" Check out the end value of X in the first simulation with `essol ferm.u 1 X end `. \"\"\" missing md\"\"\" Create an array vector with all the end values of X . Use therefore a `for` loop and the `append ` function. Put all values in `Xeq values`. \"\"\" begin Xeq values missing for i missing missing end end md\"\"\" Plot a histogram in the range ` 1.0, 1.8 ` with a bin size of `0.01` cf. `bins 1.0 0.01 1.8` . \"\"\" missing md\"\"\" Calculate the mean. \"\"\" missing md\"\"\" Calculate the standard deviation. \"\"\" missing "},{"url":"exercises/sde_model_heston_mtk_intro/","title":"3. SDE_model_Heston_intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"17\" title \"3. SDE model Heston intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Introduction to solving SDE problems with ModelingToolkit\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using StatsPlots, StatsBase using ModelingToolkit, StochasticDiffEq using ModelingToolkit t nounits as t, D nounits as D using PlutoUI TableOfContents md\"\"\" Introduction Solving SDE problems with ModelingToolkit \"\"\" https www.goldavenue.com next image?url https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fyrlg7o79%2Fproduction%2F2974fbf9471a13b1a69294cfd81839266c779303 2560x1638.jpg&w 750&q 75 md\"\"\" Stock and volatility https www.goldavenue.com next image?url https%3A%2F%2Fcdn.sanity.io%2Fimages%2Fyrlg7o79%2Fproduction%2F2974fbf9471a13b1a69294cfd81839266c779303 2560x1638.jpg&w 750&q 75 \"\"\" 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 a simplified Heston model .\\ \"\"\" md\"\"\" In finance, the Heston model, named after Steven L. Heston, is a mathematical model that describes the evolution of the volatility of an underlying stock. It is a stochastic volatility model such a model assumes that the volatility of the stock is not constant, nor even deterministic, but follows a random process. Stock the goods or merchandise or asset something having value Volatility a tendency to change quickly and unpredictably \"\"\" md\"\"\" Our simplified Heston model assumes that S , the price of the stock, is determined by a stochastic process \\cfrac dS dt \\mu\\,S\\,dt \\sqrt V \\,S\\,\\cfrac dW dt where the volatility \\sqrt V is given by a Cox Ingersoll Ross CIR process \\cfrac dV dt \\kappa\\, \\theta V \\,dt \\sigma\\,\\sqrt V \\,\\cfrac dW dt and W is a Wiener process i.e., continuous random walk . The value V , being the square of the volatility, is called the instantaneous variance . \"\"\" md\"\"\" There are five parameters \\mu the expected return drift of the stock \\kappa the rate at which the variance of the price reverts to long term mean \\theta \\theta long term mean of the variance of the price. \\sigma the volatility of the volatility \"\"\" md\"\"\" The values of the parameters are summerized in the following table | Parameter | Value | | | | | `μ` | 0.03 | | `κ` | 0.90 | | `θ` | 0.04 | | `σ` | 0.08 | We will use `100.0` and `0.04` as initial values for `S` and `V` respectively. \"\"\" md\"\"\" Implementation of the system \"\"\" md\"\"\" We will create a MTK model for the aforementioned problem in order to simulate the evolution of the price of the stock S with time as a Stochastic Differential Equation SDE problem with the noisy factors in the equations above. \"\"\" md\"\"\" We define the variables `S` and `V` with ` variables` and we can immediately add default initial conditions. \"\"\" variables S t 100.0 V t 0.04 md\"\"\" We define the parameters `μ`, `κ`, `θ` and `σ` with ` parameters` and we can immediately add their default values. In addition we add the scaling parameter `n` and we set it to `0.5`. We this parameter you can literally scale the noise. \"\"\" parameters μ 0.03 κ 0.90 θ 0.04 σ 0.08 n 0.5 md\"\"\" Next, we instantiate a source `B` of Brownian noise a Wiener process by default with ` brownian`. \"\"\" brownian B md\"\"\" Now we set up the model equations including the noise terms `sqrt V S n B` and ` σ sqrt V n B`, and put them in a vector array. We have named the array `eqs spsv`. \"\"\" eqs spsv D S ~ μ S sqrt V S n B, D V ~ κ θ V σ sqrt V n B md\"\"\" Now we are ready to build a system of equations with ` mtkbuild` and the function `System`. We provide the following arguments to `System` the model equations and `t`. We have named our system `sys spsv`. \"\"\" mtkbuild sys spsv System eqs spsv, t md\"\"\" Simulating the system as an SDE problem \"\"\" md\"\"\" If you want to modify the default initial conditions or parameter values, you can define new vectors for them. For the sake of completeness, we will define new vectors `u0` and `parms` but using the same default values as were set before. \"\"\" md\"\"\" Setting initial conditions \"\"\" u0 S 100.0, V 0.04 md\"\"\" Setting the timespan \"\"\" md\"\"\" We will simulate the price of the stock in the time interval ` 0.0, 5.0 `. \"\"\" tspan 0.0, 5.0 md\"\"\" Setting parameter values \"\"\" parms μ 0.03, κ 0.90, θ 0.04, σ 0.08, n 0.5 md\"\"\" Creating an SDEProblem \"\"\" md\"\"\" We will create the SDE problem using the function `SDEProblem`. As with the function `ODEProblem` you need to provide the same kind of arguments the name of the MTK system, the vector of initial values, the time span and the vector of parameters. Since the initial conditions and the parameter values were given as default cf. when we defined them with ` variables` and ` parameters` , we could just put empty vectors cf. ` ` if we like. We have named our SDE problem `sprob spsv`. \"\"\" sprob spsv SDEProblem sys spsv, u0, tspan, parms using values in u0 and parms sprob spsv SDEProblem sys spsv, , tspan, using the default values 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.01` that will introduce some randomness at every time step. We have stored the solution in `ssol spsv`. \"\"\" ssol spsv solve sprob spsv, EM , dt 0.01 md\"\"\" We will only plot the variable `S` using the function `plot` with the option `idxs S `. In our simplified model, the graph of `V` will be exactly the negative of `S` but on a much smaller scale. \"\"\" plot ssol spsv idxs S md\"\"\" Simulating the system as an EnsembleProblem. In practice, modeling and simulation are used to draw conclusions or gain insights into a system. Unlike ordinary differential equations ODEs , simulations based on stochastic differential equations SDEs produce different outcomes each time they are run due to their inherent randomness. Besides, we are often interested in the edge cases what are the chances of losing more than 20% of my budget or what are the chances of doubling it? 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 and gain insights in the general behaviour of the system. In order to create an EnsembleProblem , you need to create an SDEProblem first. Since we already have our SDEProblem called `sprob spsv`, we can readily create an EnsembleProblem from this. All you need to do is call the function `EnsembleProblem` with the name of the SDE problem object cf. `sprob spsv` as argument. \"\"\" esprob spsv EnsembleProblem sprob spsv 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 how many trajectories simulations you want to make. If you want an ensemble of 100 simulations, you can put `trajectories 100`. \"\"\" essol spsv solve esprob spsv, EM , dt 0.01, trajectories 100 md\"\"\" We can now plot all simulations of `S` simply as before with the function `plot`. \"\"\" plot essol spsv idxs S md\"\"\" If you want to inspect the end value of S of the 32th simulation, you can do as follow \"\"\" essol spsv 32 S end md\"\"\" In order to have an idea of the spread of the end value of S after 5 units of time for all simulations, we will store all the end values in a vector `S end vals` using a `for` loop as illustrated in the following code cell \"\"\" begin S end vals define an empty vector for i 1 length essol spsv loop index wise over the end values append the end value of S of the i th simulation to S end vals append S end vals, essol spsv i S end end end md\"\"\" Next, we will plot a histogram using the end values. Hereby, we will group our data in `bins` of size 5 in the range 0 to 200 cf. the option `bins 0 5 200` . \"\"\" histogram S end vals bins 0 5 200 md\"\"\" You can determine the mean value of all end values of S in the following way \"\"\" mean S end vals md\"\"\" You can determine the standard deviation of all end values of S in the following way \"\"\" std S end vals "},{"url":"exercises/sens_bitrophic_model/","title":"7. Sensitivity bitrophic model","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"37\" title \"7. Sensitivity bitrophic model\" tags \"exercises\" layout \"layout.jlhtml\" description \"Sensitivity bitrophic model\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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":"7. Sensitivity fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"36\" title \"7. Sensitivity fermenter monod\" tags \"exercises\" layout \"layout.jlhtml\" description \"Sensitivity fermenter monod\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using Catalyst, OrdinaryDiffEq using StatsPlots, PlutoUI TableOfContents using ForwardDiff using Turing md\"\"\" Exercise Fermenter Monod kinetics Sensitivity analysis \"\"\" md\"\"\" In one of the previous practica, 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\"\"\" Suppose that at t 0 no substrate S is present in the reactor but that there is initially some biomass with a concentration of 0.0005\\ g L . The default parameter values are Q 2.0 , V 40.0 and Y 0.67 . The parameters \\mu max , K s and S in will be assigned later. \"\"\" md\"\"\" The reaction network object model for this problem could be defined as \"\"\" fermenter monod reaction network begin species S t 0.0 X t 0.0005 parameters Q 2.0 V 40.0 Y 0.67 Sin μmax Ks μmax S Ks , S X 1 Y X Alternative mm S, μmax, Ks X, S Y X Q V, S, X ∅ Q V Sin, ∅ 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. Keep in mind that `mm S, μmax, Ks ` stands for \\mu max \\, \\cfrac S S K s . \"\"\" osys missing md\"\"\" Goals of this exercise \"\"\" md\"\"\" Compute the following in a timespan of ` 0.0, 100.0 ` \\,h The sensitivities of S and X w.r.t. \\mu max , K s and S in . Plot the following A figure with the sensitivity functions of S and X w.r.t. S in . A figure with the sensitivity functions of S w.r.t. \\mu max , K s and S in . A figure with the sensitivity functions of X w.r.t. \\mu max , K s and S in . Interpret your results. Use the following parameter values \\mu max `0.40`, K s `0.015` and S in `0.022` for the calculations of the sensitivities. \"\"\" md\"\"\" Initialize a vector `u0` with the initial conditions, set the timespan and initialize a vector `parms` with the parameter values \"\"\" u0 missing in principle not necessary since we will use the default tspan missing md\"\"\" For practical reasons, we will define the time step size as `dt 0.5`. \"\"\" dt missing md\"\"\" For practical reasons, we will use the variables `μmax val`, `Ks val`, and `Sin val` to store the parameter values that are used for the calculation of the sensitivity functions. \"\"\" begin μmax val missing Ks val missing Sin val missing end parms missing no need to include Q, V, and Y since we will use the default md\"\"\" Preliminary simulation \"\"\" md\"\"\" Create the ODE problem and store it in `oprob`. Next, solve the ODE problem using `Tsit5 ` and `saveat dt`, and store the solution in `osol`. Finally plot the results. \"\"\" oprob missing osol missing missing md\"\"\" Local Sensitivity Analysis LSA \"\"\" md\"\"\" Setting up function \"\"\" md\"\"\" Write a solution function with as argument a vector of the parameters that you want the sensitivity on , and that returns the outputs. \"\"\" function fermenter monod sim parms missing ... end md\"\"\" Make two functions based on the solution function that 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 parms missing fermenter monod sim X params missing md\"\"\" Compute the outputs \"\"\" md\"\"\" Make the time vector. \"\"\" t vals missing md\"\"\" Compute the two outputs S and X for the given parameter values. \"\"\" S sim missing X sim missing md\"\"\" Compute the sensitivities \"\"\" md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouputs S and X . Hence, you need to call `ForwardDiff.jacobian` twice. \"\"\" md\"\"\" Absolute sensitivities \"\"\" sens S missing sens X missing md\"\"\" Extract the absolute sensitivities of the outputs on the different parameters. \"\"\" begin sens S on μmax missing sens S on Ks missing sens S on Sin missing end begin sens X on μmax missing sens X on Ks missing sens X on Sin missing end md\"\"\" Normalized sensitivities \"\"\" md\"\"\" Compute the normalized sensitivities. \"\"\" begin sens S on μmax rel missing sens S on Ks rel missing sens S on Sin rel missing end begin sens X on μmax rel missing sens X on Ks rel missing sens X on Sin rel missing end md\"\"\" Plotting questions \"\"\" md\"\"\" Plot the sensitivity functions of S and X on S in . Provide a suitable title `title \"...\"` , labels `label \"...\" \"...\" ` and an x label `xlabel \"...\"` , and set the line width to 2 `linewidth ...` . \"\"\" missing md\"\"\" questions Interpret your results. Try to answer the following question s Which output variable S or X is most sensitive to S in in steady state? Why is the sensitivity function of S on S in at first positive but then becomes zero? \"\"\" md\"\"\" Answers missing missing \"\"\" md\"\"\" Plot the sensitivity functions of S 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\"\"\" questions Interpret your results. Try to answer the following question s Which parameter \\mu max , K s or S in affects the output S the most in steady state? Why is the sensitivity function of S w.r.t. K s positive? Why is the sensitivity function of S w.r.t. \\mu max negative? \"\"\" md\"\"\" Answers missing missing \"\"\" It seems like μmax is affecting S the most in steady state, and its influence is negative hence, the larger μmax, the smaller S. The sensitivity function of S w.r.t. Ks is positive, because the larger Ks, the smaller the reaction rate r S Y X . Hence, less X will be produced so less S will be consumed. Remember that r μmax S X S Ks and Ks is in the denominator. The sensitivity function of S w.r.t. μmax is negative, because the larger μmax, the larger the reaction rate r S Y X . Hence, more X will be produced so more S will be consumed. Remember that r μmax S X S Ks and μmax is in the numerator. 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\"\"\" questions Interpret your results. Try to answer the following question s Which parameter \\mu max , K s or S in affects the output X the most in steady state? Why is the sensitivity function of X w.r.t. K s negative? Why is the sensitivity function of X w.r.t. \\mu max positive? \"\"\" md\"\"\" Answers missing missing missing \"\"\" It seems like Sin is affecting X the most in steady state, and its influence is positive hence, the larger Sin, the larger X. The sensitivity function of X w.r.t. Ks is negative, because the larger Ks, the smaller the reaction rate r S Y X . Hence, less X will be produced. Remember that r μmax S X S Ks and Ks is in the denominator. The sensitivity function of X w.r.t. μmax is positive, because the larger μmax, the larger the reaction rate r S Y X . Hence, more X will be produced. Remember that r μmax S X S Ks and μmax is in the numerator. md\"\"\" Monte Carlo error propagation \"\"\" md\"\"\" Let's take a look at how the uncertainty propagates in your model by using Monte Carlo simulations. Based on some literature search you can assume that the parameter `Ks` is normally distributed around the original value with a standard deviation of 20%. \"\"\" md\"\"\" Start with making a Turing model in which you implement the prior and return the solution of the solved problem. \"\"\" md\"\"\" note Instead of creating a new ODEProblem everytime, you can simply remake an old ODEProblem by using `new prob remake oprob, p Ks Ks `. \"\"\" model function monod deviation Ks dev ~ missing 20% standard deviation new prob missing return missing end md\"\"\" Now get sample 100 solutions from your monod deviation model. \"\"\" missing md\"\"\" We can now visualise the results of our Monte Carlo simulation by looping through our solutions and plotting them. It is advised to put `label false` avoids 100 labels , `color gray` neutral color and `alpha 0.4` makes it more transparent and `linestyle dot`. \"\"\" begin p1 plot title \"Monte Carlo of logistic growth\" make an empty plot for missing in missing missing end plot osol, label \"Original simulation\", linewidth 3 Plots original result p1 end md\"\"\" As can be seen from the graph, the uncertainty is rather high, especially if we want to carefully monitor the concentration of biomass through time. Note that, since we have a sample of values for S and X at every time step, it is also perfectly possible here to quantify the uncertainty by calculating the standard deviation at every time step, but the graph already gives a clear indication. \"\"\" md\"\"\" Now create a vector x1 that stores the values of the biomass at the end of the simulations and make a histogram of it. Put `xlims 0.010, 0.015 `. \"\"\" missing missing md\"\"\" Determininig optimal measurements \"\"\" md\"\"\" As a process operator, you may want to reduce the uncertainty in biomass simulations. However, since taking measurements can be costly, it is important to perform them at the most informative time points. Local sensitivity analysis is a useful tool in this context, as it indicates when the model output is most sensitive to changes in parameter values. At these time points, accurate measurements provide the greatest insight into the true parameter values. Start off by identifying the time at which the biomass concentration is most sensitive to variations in `Ks`. \"\"\" md\"\"\" note You can find the index of the maximum value in a vector with the `argmax ` function. For example `argmax 1, 2, 5, 4 ` will give you 3. \"\"\" begin t star idx missing t star t vals t star idx plot t vals, abs. sens X on Ks rel , title \"Sensitivity of Ks wrt. X\", xlabel \"Time h \", ylabel \"normalized sensitivity\" vline t star , label \"t round t star, digits 2 h\", linestyle dash, color red end begin μmax real 0.42 Ks real 0.0165 Sin real 0.0207 real solution solve remake oprob, p μmax μmax real, Ks Ks real, Sin Sin real end md\"\"\" We have defined a function `real solution t ` in this notebook that returns a hypothetical measurement `S meas, X meas` at the given timepoint `t`. Use this function to get a measurement at the optimal time `t star` . \"\"\" S meas, X meas real solution t star md\"\"\" Conditioning on the measurements \"\"\" md\"\"\" Now we can use this measurement to calibrate our model. In this part we will use Monte Carlo Markov chain to show the decrease in uncertainty. We start by creating a Turing model, for which you can use the priors from the first part of this exercise. For the standard deviation `σ X` you can assume a value of 0.0002 and a value of 0.00055 for `σ S`. Make sure to truncate the domain of your parameters to a reasonable domain or you might run into issues with the solver. \"\"\" model function monod meas σ X missing σ S missing Ks dev ~ missing sol missing S pred, X pred sol t meas X meas ~ missing S meas ~ missing end monod cond model missing md\"\"\" MCMC \"\"\" monod chain missing md\"\"\" Plot the chain to check if it properly converged to the posterior distribution. \"\"\" missing md\"\"\" Now, get the sampled posterior solutions and afterwards plot the resulting simulations. \"\"\" missing md\"\"\" Plot the resulting Monte Carlo simulation with the original simulations. \"\"\" begin p plot ylabel \"concentration g l \", xlabel \"Time hours \" for missing in missing missing end plot osol, label \"Original simulation S\" \"Original simulation X\" , linewidth 3 p end md\"\"\" Lastly, create a histogram of the value of X at the end of the simulations based on your posterior distribution of Ks. Put the `xlims 0.010, 0.015 ` to compare with the prior distribution. \"\"\" missing missing "},{"url":"exercises/sens_insuline/","title":"7. Sensitivity insuline","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"38\" title \"7. Sensitivity insuline\" tags \"exercises\" layout \"layout.jlhtml\" description \"Sensitivity insuline\" 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 Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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":"7. Sensitivity intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.19.46 frontmatter order \"35\" title \"7. Sensitivity intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Sensitivity intro\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown using InteractiveUtils 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 implicitely used to estimate parameters and explicitely in the context of optimal experimental design. \"\"\" md\"\"\" The sensitivity function that measures how sensitive output y j is to changes in parameter \\theta i is given by the partial derivative \\cfrac \\partial \\hat y i \\theta \\partial \\theta j \\tag 1 \"\"\" md\"\"\" Try to understand why the above expression 1 does indeed give us the information we were promised in the first paragraph. How will we be able to see from the value determined by the above expression 1 that whether or not output y j is sensitive to a change in \\theta i ? \"\"\" 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 i and \\theta i \\Delta\\theta i and the difference between these evaluations is taken. \"\"\" md\"\"\" Since quantity 1 is dependent on the units, a normalized variant is often used \\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 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 the concepts, we first consider three simple models modelling 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 show 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 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 We will start by modelling our system and simulating using the aforementioned parameters values, initial condition and timespan. \"\"\" 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 μ W, 0 W μ Wf W, W 0 μ 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 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 that you want the sensitivity on , 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 based on the solution function that returns a single output. \"\"\" 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 Alternative t vals log tspan 1 0.5 tspan 2 md\"\"\" Compute the single output with the given parameter values. \"\"\" W log growth sim W log μ log, Wf log md\"\"\" Use 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 sensitivities of W on \\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 on \\mu . `sens W log ,2 ` gives the absolute sensitivity of W on 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 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\"\"\" Alternative For each plot command we need to provide the time vector first argument and the column vector with the local sensitivity second argument . Additionally, you can provide a title, a legend label and a x and or y label. Beware that if you want to execute multiple commands here `plot` in a single cell, you need to put them in a `begin` and `end` block. Also if you want to visualize subsequent graphs in the same plot, the forthcoming plot command names should be followed by a ` `, as in `plot ... `. \"\"\" ╠═╡ ╠═╡ begin plot t vals log, sens W on μ rel log, title \"Normalized sensitivities\", label \"W on μ\", xlabel \"Time day \" plot t vals log, sens W on Wf rel log, label \"W on Wf\" end ╠═╡ md\"\"\" Conclusions From the sensitivity plot of W on \\mu it can be seen that W is most sensitive to \\mu in the time region 0, 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 on 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\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 Sensitivity analysis of the exponential growth model \"\"\" 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 growth exp reaction network begin species W t 2.0 parameters μ 0.02 Wf 10.0 μ Wf, μ , 0 W Alternative μ Wf, 0 W μ, W 0 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 osys exp convert ODESystem, growth exp md\"\"\" Initialize a vector `u0 exp` with the initial condition \"\"\" u0 exp missing Uncomment and complete the instruction u0 exp W 2.0 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 0.02 Wf exp 10.0 md\"\"\" Initialize a vector `params exp` with the parameter values \"\"\" params exp missing Uncomment and complete the instruction params exp μ μ exp, Wf Wf exp md\"\"\" Create the ODE problem and store it in `oprob exp` \"\"\" oprob exp missing Uncomment and complete the instruction oprob exp ODEProblem growth exp, u0 exp, tspan, params exp 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 osol exp solve oprob exp, Tsit5 , saveat 0.5 md\"\"\" Plot the result \"\"\" missing Uncomment and complete the instruction plot osol exp 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 growth sim exp params missing ... end function growth sim exp params μ, Wf params u0 exp W 2.0 tspan 0.0, 100.0 oprob exp ODEProblem growth exp, u0 exp, tspan, μ μ, Wf Wf osol exp solve oprob exp, Tsit5 , saveat 0.5 return osol exp 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 growth sim W exp params growth sim exp params W md\"\"\" Make the time vector. \"\"\" t vals missing Uncomment and complete the instruction t vals exp 0 0.5 100.0 md\"\"\" Compute the output for the given parameter values. \"\"\" W exp missing Uncomment and complete the instruction W exp growth sim W exp μ exp, Wf exp md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouput s . \"\"\" sens W exp missing Uncomment and complete the instruction sens W exp ForwardDiff.jacobian growth sim W exp, μ exp, Wf exp 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 μ exp sens W exp ,1 sens W on Wf exp missing Uncomment and complete the instruction sens W on Wf exp sens W exp ,2 md\"\"\" Compute the normalized sensitivities. \"\"\" sens W on μ rel exp missing Uncomment and complete the instruction sens W on μ rel exp sens W on μ exp . μ exp . W exp sens W on Wf rel exp missing Uncomment and complete the instruction sens W on Wf rel exp sens W on Wf exp . Wf exp . W exp md\"\"\" Plot both normalized sensitivity functions with appropriate title and labels \"\"\" missing Uncomment and complete the instruction plot t vals exp, sens W on μ rel exp, sens W on Wf rel exp , title \"Normalized sensitivities\", label \"W on μ\" \"W on Wf\" , xlabel \"Time day \" md\" Draw your conclusions missing missing \" md\"\"\" Exercise 2 Sensitivity analysis of the Gompertz growth model \"\"\" 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 growth gom reaction network begin species W t 2.0 parameters μ 0.09 D 0.04 μ D log W , W 2 W Alternative μ, W 2 W D log W , W 0 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 osys gom convert ODESystem, growth gom md\"\"\" Initialize a vector `u0 gom` with the initial condition \"\"\" u0 gom missing Uncomment and complete the instruction u0 gom W 2.0 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 0.09 D gom 0.04 md\"\"\" Initialize a vector `params gom` with the parameter values \"\"\" params gom missing Uncomment and complete the instruction params gom μ μ gom, D D gom md\"\"\" Create the ODE problem and store it in `oprob gom` \"\"\" oprob gom missing Uncomment and complete the instruction oprob gom ODEProblem growth gom, u0 gom, tspan, params gom 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 osol gom solve oprob gom, Tsit5 , saveat 0.5 md\"\"\" Plot the result \"\"\" missing Uncomment and complete the instruction plot osol gom 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 growth sim gom params missing ... end function growth sim gom params μ, D params u0 gom W 2.0 tspan 0.0, 100.0 oprob gom ODEProblem growth gom, u0 gom, tspan, μ μ, D D osol gom solve oprob gom, Tsit5 , saveat 0.5 return osol gom 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 growth sim W gom params growth sim gom params W md\"\"\" Make the time vector. \"\"\" t vals gom missing Uncomment and complete the instruction t vals gom 0 0.5 100.0 md\"\"\" Compute the output for the given parameter values. \"\"\" W gom missing Uncomment and complete the instruction W gom growth sim W gom μ gom, D gom md\"\"\" Using `ForwardDiff.jacobian` to compute the sensitivities for the single ouput s . \"\"\" sens W gom missing Uncomment and complete the instruction sens W gom ForwardDiff.jacobian growth sim W gom, μ gom, D gom 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 μ gom sens W gom ,1 sens W on D gom missing Uncomment and complete the instruction sens W on D gom sens W gom ,2 md\"\"\" Compute the normalized sensitivities. \"\"\" sens W on μ rel gom missing Uncomment and complete the instruction sens W on μ rel gom sens W on μ gom . μ gom . W gom sens W on D rel gom missing Uncomment and complete the instruction sens W on D rel gom sens W on D gom . D gom . W gom md\" Plot both sensitivity functions with appropriate title and labels \" missing Uncomment and complete the instruction plot t vals gom, sens W on μ rel gom, sens W on D rel gom , title \"Normalized sensitivities\", label \"W on μ\" \"W on D\" , xlabel \"Time day \" md\" Draw your conclusions missing missing \" "},{"url":"exercises/uncert_bitrophic_model/","title":"7. Uncertainty bitrophic model","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"41\" title \"7. Uncertainty bitrophic model\" tags \"exercises\" layout \"layout.jlhtml\" description \"Uncertainty bitrophic model\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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":"7. Uncertainty fermenter monod","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.4 frontmatter order \"40\" title \"7. Uncertainty fermenter monod\" tags \"exercises\" layout \"layout.jlhtml\" description \"Uncertainty fermenter monod\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" 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":"7. Uncertainty intro","tags":["exercises"],"text":" A Pluto.jl notebook v0.20.21 frontmatter order \"39\" title \"7. Uncertainty intro\" tags \"exercises\" layout \"layout.jlhtml\" description \"Uncertainty intro\" frontmatter.author name \"Gauthier Vanhaelewyn\" using Markdown using InteractiveUtils Running this yourself? Point this at your own environment — we advise one shared project in the parent folder Pkg.activate \"..\" using Pkg Pkg.activate \".. .. pluto deployment environment\" using Markdown, InteractiveUtils using OrdinaryDiffEq, Catalyst using Measurements using StatsPlots, PlutoUI TableOfContents using Turing md\"\"\" Introduction to uncertainty analysis \"\"\" Catalyst.ModelingToolkit.NaNMath.log x Measurement Measurements.log x using `log` on a Measurement type variable in the reaction rate of a Catalyst model will otherwise error in the current Catalyst version 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, often incorporating parameters representing various aspects of the system's characteristics. However, these parameters are rarely known with absolute certainty and often carry inherent uncertainty due to measurement errors, variability in real world conditions, or incomplete knowledge of 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 replected as error bars assuming some uncertainty in the model parameters. To illustrate this concept, we first revisit the three simple models modelling 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 show some typical values for the parameters together with their uncertainties | | \\mu | W f | d | | | | | | | Logistic | 0.07 \\pm 0.02 | 10.0 \\pm 0.15 | | | Exponential | 0.02 \\pm 0.01 | 10.0 \\pm 0.15 | | | Gompertz | 0.09 \\pm 0.01 | | 0.040 \\pm 0.002 | 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 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\"\"\" Uncertainty analysis of the logistic growth model \"\"\" md\"\"\" We will start by modelling our system and simulating using the aforementioned parameter values and uncertainties, initial condition, and timespan. \"\"\" md\"\"\" Implementation of the system \"\"\" growth mod log reaction network begin species W t 2.0 parameters μ Wf μ 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` see above . \"\"\" md\"\"\" Setting initial conditions \"\"\" u0 log W 2.0 md\"\"\" Set the timespan for the simulation \"\"\" tspan 0.0, 100.0 md\"\"\" 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 0.15 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 0.15 . The last case study is the true case because, in reality, all uncertainties will contribute simultaneously. \"\"\" md\"\"\" Case study 1 \"\"\" md\"\"\" We initialize a vector `parms1 uncert log` with the parameter values and uncertainty standard deviation only in the first parameter \"\"\" parms1 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, parms1 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 de largest error bars largest uncentrainty in the output variable occurs at the timepoints where the sensitivity is strongest. \" md\"\"\" Alternative Monte Carlo uncertainty propagation \"\"\" md\"\"\" In uncertainty propagation, we are interested in the effect of uncertainties or errors on the final output of the model. Monte Carlo simulations are a straightforward way for testing different parameters and checking the results of the outcomes. Below a Turing model is defined that samples values from a normal distribution with a 5% standard deviation. This is our Prior belief of how accurate the estimate of our intial parameters is. Remember that this is similar to what we did in practical 4. \"\"\" model function logistic deviation μ dev ~ Normal 0.07, 0.0035 5% standard deviation Wf dev ~ Normal 10, 0.5 5% standard deviation return solve remake oprob log, p Wf Wf dev, μ μ dev , saveat 0.5 end md\"\"\" Using the `sample ` function we can sample values from our distribution. \"\"\" μ model logistic deviation chain sample μ model, Prior , 500 solutions generated quantities μ model, chain md\"\"\" Finally, we can use theses perturbed values to model the effects of these small perturbations on the output of the model. \"\"\" md\"\"\" Firstly, solve the original system Wf 10 and μ 0.07 and afterwards we can compare this with the outputs of the Monte Carlo simulations. \"\"\" oprob log ODEProblem growth mod log, u0 log,tspan, μ 0.07, Wf 10 osol log solve oprob log, Tsit5 , saveat 0.5 md\"\"\" Let's now plot the solution of the Monte Carlo experiments. \"\"\" begin p1 plot title \"Monte Carlo of logistic growth\" for solution in solutions plot solution, label false, color gray, alpha 0.4, linestyle dot end plot osol log, label \"Original simulation\", linewidth 3 p1 end md\"\"\" An advantage of using Monte Carlo over the error bars, is that the resulting graphs create a distribution from which we can calculate probabilities. We could now for example calculate what the average yield is after 50 days \"\"\" md\"\"\" It is worth noting that you can obtain the solution at a specific time point by indexing it using parentheses ` `, as shown below. However, the result is still returned as a vector indicated by the square brackets ` ` . To extract the actual value, you need to index the first element of that vector. \"\"\" osol log 50 gives a vector with inside the value of W at time 50 osol log 50 1 This gives the value of W at time 50 x solution 50 1 for solution in solutions md\"\"\" Looking at the histogram, we observe that the yield is approximately normally distributed at 50 days, near the inflection point where logistic growth begins to plateau. This is intuitive, as both the carrying capacity W f and the growth rate μ are normally distributed, and at this point in the growth curve their combined influence on the yield remains approximately linear, preserving normality. Note, however, that this need not hold at other time points or for other models \"\"\" histogram x, xlabel \"W\", ylabel \"Count\", title \"Histogram of W at t 50\" p mean x md\"\"\" What can you see on the resulting graph? What does this tell us about the uncertainty? missing missing missing \"\"\" md\"\"\" Case study 2 \"\"\" md\"\"\" We initialize a vector `parms2 uncert log` with the parameter values and uncertainty standard deviation only in the second parameter \"\"\" parms2 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, parms2 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 to 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 the largest uncertainty in the output variable occur at the tail of the curve, where the sensitivity is strongest. \"\"\" md\" Case study 3 \" md\"\"\" We initialize a vector `parms uncert log` with the parameter values and uncertainty standard deviation in all parameters \"\"\" parms 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, parms 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 you have the combined effect of the uncertainty in the parameter \\mu as well as in the parameter W f . \"\"\" md\"\"\" Exercises \"\"\" md\"\"\" Exercise 1 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 species W t 2.0 parameters μ Wf μ Wf, 0 W μ, W 0 end md\" The vector `u0 exp` with the initial condition is \" u0 exp W 2.0 md\"\"\" Initialize a vector `parms 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. \"\"\" parms uncert exp missing md\"\"\" Create the corresponding ODE problem and store it in `oprob uncert exp` \"\"\" oprob uncert exp missing md\" Solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert exp` \" osol uncert exp missing md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" missing md\"\"\" Draw your conclusions missing \"\"\" md\"\"\" Exercise 2 Uncertainty analysis of the exponential Gompertz model Perform an uncertainty analysis of the Gompertz growth model. Use the parameter uncertainties mentioned in the Table in the Grass growth models sections. \"\"\" md\" A possible reaction network object for the Gompertz growth model can be implemented as follows \" growth gom reaction network begin species W t 2.0 parameters μ d μ d log W , W 2W end md\" The vector `u0 gom` with the initial condition is \" u0 gom W 2.0 md\"\"\" Initialize a vector `parms uncert gom` 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. \"\"\" parms uncert gom missing md\"\"\" Create the corresponding ODE problem and store it in `oprob uncert log` \"\"\" oprob uncert gom missing md\" Solve the ODE problem. Use `Tsit5 ` and `saveat 2.0`. Store the solution in `osol uncert gom` \" osol uncert gom missing md\"\"\" Plot the results simulation of the output variable W and uncertainty band \"\"\" missing md\"\"\" Draw your conclusions 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"}]