Model your first dynamic system with cadCAD
ETHCC 5 · July 2022 · 57:47
Bogdan-Radu Dumitru introduces cadCAD for modeling dynamic systems. The hands-on workshop covers its basic concepts and setting up a project with a simple simulation.
Speakers
Topics
From ETHCC 5, hosted by Ethereum France.
Transcript
is the mic positioning okay all good awesome so yeah like i was saying earlier um this is going to be a workshop so i'm hoping people want to play around and code with us and it's a workshop about dynamic system modeling and cad cad which is if you've read my abstract i said it sounds like something a pokemon would say cad cad i don't know i found that funny and i'm gonna use pokemons in the workshop for that because of that cool so yeah before we get started a little bit about myself i'm bogdan i'm the cto of mento which is a stable assets protocol on the cello blockchain
so we have cello dollars a low euro and solo real and have a stability mechanism i used to work for consensus i'm full stack engineer i really would like to know more math and i'm afraid of web 2. that's let's uh cool so yeah what does cad cats stand for it's this nice sentence of complex adaptive dynamics computer-aided design cool so complex adaptive dynamics what's this um this is basically systems that have a form of feedback to them that's how i'd like to think about them it's system in which things happen and the things that happened the agents
interact change the state of the system and that keeps on feeding back into itself and yeah growing and computer aided design is like you know everything we call things software that we use to design things and this is how i would like my job to look like i think it that would be like a nice everyday vibe but it's not really that makes me sad um cool so yeah actually if somebody has a better definition of this i'm curious but i've been hearing like complex and dynamic systems interchangeably in some situations and i was trying to figure out like what they actually mean and this is like the best thing i could come up with like i i feel like complex systems usually refers to something that's more broad
and real so like um oh the migration path of birds is an actual complex system that we look at and a dynamical system is actually a model of that of a complex system is a description in yeah just like with functions that describe the time dependency of a point in an ambient space so i can read that but it's more like an analytical description of a complex system and then what we're actually interested in modeling are non-layered linear dynamical systems which are like a subset of those and yeah differential equations that have non-linear terms and that's a nice little gif showing a pendulum which would like you know seemingly random motion that comes from the equations that describe it
cool oh yeah and i wanted to like just let you guys know that i'm not an economist by training i'm an engineer so like i'm kind of a noob at this but i've been using cat cat for like half a year now with economists to model our stability mechanism and things like that so i've been sort of learning these things but yeah i'm not gonna go in very deep things about complex systems just more about the engineering part of cad cad cool and like the first thing we're actually going to talk about is the predator preparing model which is like the most one of the most basic dynamic systems does anybody here know the predator prey
nope okay cool so uh yeah i'm gonna use pokemon for this because it's a bit more fun but the idea behind the predator prey model is that you have two populations that have a dependency on each other predators eat the prey and when you try to model the population growth you notice that you know as there is enough prey more predators will come and the growth of eating the prey will increase as well so the pre-population will decrease and as that happens predators will die out more because there's not enough food and as predators dies at some point the prey can again increase their population because they're not as hunted so we have these two populations that are interdependent and they will certify in the form of
equilibrium in time and the way i'm going to talk about it is like trainers that are trying to capture pokemon in a play in a place and pokemons grow if there's not enough trainers to capture them and as pokemon grow trainers migrate to that place and then you know they capture more pokemon and we're gonna see how that plays out and this is something you're actually gonna code but first we're gonna look at how it works cool so like i was saying what we're trying to model is the pokemon population and how it changes over time and this is like a nice equation that shows that the delta of the population over time for pokemon
is a function of the growth the natural growth of the pokemon which is given by this alpha term a decrease which is like the natural death rate of the pokemon by the beta term and then for the gamma term is the decrease that comes from a function of the pokemon and the trainers so that's like the interaction between those two populations and yeah does anybody have any questions here does this sort of make sense for now for the late joiners this is like the delta of a pokemon population in the lotka volterra predator prey model that's a fun sentence so yeah so in time the pokemon will
naturally grow will also decay from death and will decay as a function of how much they're captured by trainers and these are the three like variables that change how our system works and for the trainers it's a bit more simple trainers will migrate to this area as a function of how many pokemon they are and we also abandon the area in the absence of pokemon basically and we get these two terms here for them we're gonna have these equations to look at after so there's no need to yeah and okay we have these uh we have these equations and now the question is how does this map to kind of what cad cad is and cad cad is super
basic at heart it's just a system which has some initial parameters so some state that it models and some functions that change that state over time in incremental time steps and yeah kind of like what we're looking at here the state variables are like the state of the of the system that we want to model in this example the pokemon population the the yeah what we're calculating the deltas for before we have those two variables then the system parameters are all the alpha but all the greek letters earlier that kind of dictate how the system will evolve depending on how what values we set for them and the policy functions are those things
kind of the individual logic that we had had here so like this term you can be thought of a policy which is the trainer migration policy and that term there is the abandoned rate which is like a different policy of the system and they add up and the same for the pokemon this has like three policies that dictate the change in that state variable cool cool cool right and the state update functions uh it's usually just like we have those three policies so we computed those deltas and we change the state with that delta inside of a function and yeah cad cad had something that's called
partial state update blocks which just are the granularity of state updates so we can like sequence what happens in order and then cad cad just iterates over those state update blocks but like enough talking let's actually pull out our laptops and go to that link over there and start having fun in photoshop i don't want to see photoshop i don't know why and i forgot i was sharing
yeah so there's a readme in that repo that's really basic like has has everybody went to the link can i change or [Music] cool yeah so this is going to be like the awkward period when everybody's installing dependencies and like things are going to break so like let's give it a couple of minutes and you can call me if things are not working hmm
so but one of the first games one of the first games i played as a kid on the zx spectrum was a fox rabbit game that was yeah no i'm just saying i didn't know obviously when i was like eight years it's just a funny anecdote while
everyone's down like but you could change the parameters for the for the dynamics of the game and you can just see you know how quickly you can make the rabbits you know reproduce too much and all the foxes kill them um so yeah it's just a fun game yeah so we were talking about a game on what was the platform oh yeah the zx spectrum yeah that was basically implementing the predator prey model and yeah as a game that was fun yes
oh that's a good point let's see yeah so we need to do this who's on max or or if so yes i have forgotten about installing the large file system what is it like file storage forget but like if that doesn't work i can just send the large file to people around maybe
because for the second part of the workshop we're gonna look at the uni swap pool and we're gonna simulate the events coming so there's like 100 megabytes of uniswap events that we're going to simulate so that's stored using lfs but like it should be easy to install that and then pull the lfs what time is it okay cool anybody has any more problems that i can help with we're just waiting for
has anybody managed to open jupiter notebooks yet here no okay is anybody looking at meebs instead of installing software sorry which um i think three point eight should be okay or like three point so i don't need to actually check what's in here uh no there's one
seems like a fun error to have at this point oh look it's 3.8 here and this requirement because of course is
so if people use vs code there's a jupyter notebooks vs code plugin that helps i mean sometimes it acts up but you can actually open jupyter notebooks in vs code and edit them here but like you can also do it just like by running that command and they will open in the browser like so if people yeah either either option is okay is
in case people have problems with pdm this doesn't have like a lot of dependencies so these can also be installed via pip in your local vm and like you can go old school if you want to yeah i think so okay shhh
so has anybody passed step three by now has anybody passed step two and it's the problem are you having errors at step three or is it uh yeah okay so i just pushed uh to main requirements.txt file so you can do a pip install minus r that and we can like yeah or like if you're comfortable with virtual environments you can create a virtual m but yeah the
let me open the readme as well so so i added like a secondary option there let's see if this works easier so we have the first person that opened
the jupiter interface rejoice it's python are we having more success with pipe is it working for you not for you no i was like trying to get a sense like not working for him not working for you wait i'm gonna come and see what's up wow
is started
[Music] part for the course um
okay we have two what about on this side what's this accessory okay thumbs up there what's happening here okay so it feels like we have like a 50 50 success ratio on the install which is nice and we can go on with like i mean yeah the people will follow along um okay so i want to walk you through the predator prey part so it's like this one underscore better prey folder so you should open this in the text editor
whatever you're comfortable with and this is the basic cat cat experiment definition we have what's called the model with which which defines the simulation and it has an initial state which is what we were talking about the two populations pokemon and trainers and we can set them up with different values and we have parameters that we use inside of the simulation which are all those things that we talked about before as greek letters because you know the equation looked more fancy in math but here they're actually written down more humanely pokemon reproduction rate capture rate death rate trainer abandoned rate and trainer
growth rate um you'll notice that these are passed as lists this is because um cad cad allows you to specify multiple parameters and it will automatically run simulations for all those parameter combinations so if i want to try out what it would look like with like 0.3 and then like 0.2 over here then it will like running the simulation would mean it creates all of the parameter sweeps that's what they're called uh sets of just one value and then run all those simulations in parallel and then at the end i get all of the results and can analyze them yeah that's why these are defined as lists here but when we're
actually running the policy functions we're going to get just the current value for the simulation yeah and dt is like the delta time it's like the time step that we can define however we want like the cat cat just increments a new time step conceptually but the delta t gives us what we think as a time increment and the state update blocks gives us the policies that we're talking about like the ones that make use of the variables and create changes to the the pokemon and the trainer population deltas these are these are the things that we're actually gonna work at implementing now uh and we have these two variables which
which both change as a result of these policies having run and this is basically what it takes to define a model these state update blocks grow really complex as you have more behaviors and more state that you need to update but in general yeah the format is pretty self-evident and simple and then we define a simulation which we pass the model in and gives give it a number of time steps these are the increments that will that these are the number of like steps that will be simulated which means that these policy functions and state update function will run 3 000 times in order and runs
is what we used to tell it if we want to do multiple runs for each parameter set this is if we have randomness if we do monte carlo simulations and we have a source of randomness inside of the simulation we can tell okay do like 20 runs and each run will be slightly different and we can compare and see kind of the path of that simulation but in our case it's purely deterministic we don't have any stochastic process so we just do a single run and this is just like some other boilerplate code we create an experiment that has a simulation an experiment can actually have multiple simulations if we want to you know build something really crazy setting a back end and yeah we don't need to care about this part but now these functions are implemented
in this parts.pi file this is where we're going to work and this is where i've kind of rewritten the equations that were in the slides and what everything means over here and did a bit of a transformation because like we know we had our equation which was the delta pokemon over dt but then we can do delta pokemon equals this times dt yeah math and dt is part of our params all of these map to the greek letters they're also part of our params and for each step we want to calculate the delta in the end and the delta
is an aggregation of all the different behaviors so pokemon natural growth is basically this and we return an object saying that okay the delta has this pokemon capture is another behavior that should add up to this and what cat cat does is it aggregates all of these so if i have multiple policies that return the same variable name here like pokemon data pokemon delta pokemon delta at the end all of those are added up so what we get is kind of this equation basically but we write it as different behaviors of course we could have also done a single one but this kind of shows the fact that
you can have independent behaviors of the system that get aggregated by cad cad so it's kind of this is the nicer way to write it let's say and we have the same for all of the trainer rates and at the end we just take the from the policy input which is what all of these policies return we take the final delta and we add it to the previous state and return that as the as the updated pokemon population so this is kind of done so the only thing that we need to to do to make this work is actually implement this equations here but before we do that we can just like run this
let me go to here actually run everything let's go see what happens yeah so because all of those policies return zero um our populations kind of does nothing over time which is you know not that fun and the first thing you can actually do on your own is try to change those parts but in a linear fashion so i'm going to just say that the the pokemon population grows by one every time step and the trainer population grows by one as well wow lines
[Laughter] like i i like getting feedback right it's fun so this kind of works but you know there's no interdependency between these they just like grow linearly so we see that in the graph nicely oh yeah and i didn't really pass through this but this is this doesn't do much essentially just like some dependencies uh and it runs the experiment which for which we get back this data frame which if you're familiar a pandas data frame is just like a tabular collection of data that you can do fun things with and this is the head of that so it shows us those two variables as they grow and gives us some pointers so we have only one simulation but if we
had many that this would be the simulation index uh the subset refers to what i was talking about with the parameter sweeps if we have multiple parameter combinations that are run these would have been the subsets and the runs are those monte carlo runs if we had multiple i don't know why they chose to index those by zero and that by one but that's how it is and we're going to live with it and the time step is uh yeah the iterations of the state changes so we see like it starts with the initial state and then one gets added to these and they just grow grow grow over time and yeah here we're just doing a plot of this data frame which with some yeah we're naming the lines and doing
all of that cool so now i'm gonna like wait for a sec for people to actually try to implement this and i'm gonna do it as well um how are you sorry so that's the thing like the simulation runs inside of this notebook basically oh let's see i'm gonna come over yeah i mean if you manage to install jupiter and you open jupiter notebooks
you get this in your browser ah okay so it it works for him nice so so jupyter notebooks works there looks like it yeah okay so you can see the lines now there in your notebook yeah they look different on my machine but that's that's fine it didn't change
so does anybody need any help with this or people are good you know ah that's a good point i've actually so yeah that something actually didn't i could have used the mic stand now so yeah let's do this one together so basically if if we want to get the params we have this object pass as a function here and we can look up what we want so we look we need alpha here which is the pokemon reproduction rate
and let's look at the experiment oh nice cool so this is how we get the pokemon reproduction rate and this is how we get the dt these are all params and the pokemon population
is part of the state so essentially this is kind of what we wanted for this first element which was this and yeah let's see how it looks like if we simulate it just with this cool so yeah the pokemon kind of grow
exponentially because there's nobody to eat them yet and there's no death either they live in a fairytale land cool so
let's see what happens if we add the capture oh no this should be minus that's nice they're very much nerfed so how are we doing are people
writing the equations uh they got where how to get the state in the params everything's working yes yes are we getting close to the actual oscillating systems actually shhh
you know the greek letters are in the params the population is in the brief state because that's the one that changes woohoo so if everything works this is what you should see at some point which is a nice oscillating system oh it's so beautiful it only implemented the death rate and
the number of pokemons just increased it doesn't come with the minus so yes if you're reducing you have to put a minus oh yeah that could have been the case sorry um let's see so if you scroll up here you should see all of these yeah i had so much fun copying like greek
letters into the comments here yeah this is instead of the experiment this is the initial state and you can play around with these you can also play around with the variables here to see how like it kind of changes and also the number of time steps so if you want to simulate more time you can change that it's everything is in the experiment dot pi so who managed to get the wobbly lines
um yeah that's it's best to do a restart because um if you're changing like the logic then that needs to be re-imported by the kernel and all of that and that's cached into it so if you just rerun it won't re-import the function changes that you made so you have to do a restart when when you're changing things that are just in the notebook so if i change some code here i don't have to restart but but because those are in other files blah blah it needs a restart so
so is anybody else close or still working on it yes it's working you got it working let me take a look do anything
one of the comments the second uh pokemon capture okay the comment didn't have the dt in it uh okay oops cool so we're not going to have time to go through the second thing
but i want to like quickly run you through what it should have been for fun um but yeah i hope you get to the squiggly lines if not uh well what i had planned was a different sort of use of cad cat in which you can model and compare um like model um a system and then compare it with the actual state which was interesting so i modified an example from like the cat head repo and this includes a bunch of events emitted by the uni swap v1 die eath pool that we can use to model the behavior of the contract and then compare that model
to the reality essentially and we have a list of these events and these map to changes in that state we have three state items that we care about how much dye is in the pool how much east is in the pool and how much lp token was minted by the pool and we can process these events and make changes to that state and the parameters of the systems are this list of events and what feed we want the pool to take in the uni sub we want the fees hard code actually in the contract so it's like but you can experiment and play around with it and i'm gonna like go quickly through some math which is like given um
given a mountain eth you want to sell to the contract and the balances of the contract and a fee of 0.3 percent how much die do you get back from that contract and if you actually you know play around with the equations you end up with those as like some generalized formulas given that you rewrite the fee as the inverse and have it have a denominator in the denominator so like instead of thinking as the fee is 0.3 percent we think of like 1 minus 0.2 percent that's 997 over a thousand and we can have those nice formulas there for um the amount of die and this is what we need to use when we're doing like a
token purchase or any purchase because what we want to simulate is how much yeah how much does the pool balances change when token or ether are getting purchased of course adding liquidity and moving liquidity is kind of simple that just adds to the pool you know so you can play with this on your own time it's a kind of fun one let's i think i i went on a branch that has everything built in choo choo choo this also has more fun um visualizations than the first experiment so i encourage you all to play around with
it oh yeah this experiment takes a while because it's simulating all i don't know i think a couple hundred thousand events over this model but yeah while it's running i can quickly just go through the code so it kind of makes sense right experiment cool so we have only one policy which is decoding the event or the action and then we have these three variables well we have these four variables i actually didn't use price ratio in the end but i forgot to remove it from here which is the die balance date balance and the lp token supply
um this is the kind of like the date the the how an event is in this data source i found which is like this pickle which is why i had the lfs thing happening and oh yeah i also wanted to show like a cool thing you can do if you try to type your actual your params so here in the system params i i did it a bit nicer than it was in the first experiment so i actually created type dicts for the params and had the initial ones like this um that means that you can and also for the state variables created like a nice type dict which
means that then you can have that in your policy functions and actually type them and you know get autocomplete inside of the dix which is nice um but for this uni swap it's basically decoding the ac the event into an action and then outputting the deltas and my actions are the ones that we talked about so it can be either selling eth or selling tokens depositing withdrawing uh lp token minting or lp token burning which is happens for a transfer event because like the the old v1 pools implemented like erc20
interface 4 dlp tokens yeah and this includes some logic like the interesting one is here like the one that i was showing as math where we actually calculate how much to how much eat you get for diet and so on and let's see if this has finished running cool so actually the fun part is so what's what's being mapped here um it's the is the east supply i think wait oh yeah now the first one is the eath balance the second one is the die balance and the third one is the
lp token supply and actually the real history and the yellow are overlapped you can actually see them so like the values that we get from parsing the events and the values that we get from simulating it with 0.3 fee are the same but you can see like how the system would have behaved if if we had different fees inside of the pool so that's sort of fun to see like how with the same events but like a different fee structure and the other thing that you can actually see by doing this which i wanted to show and it's fun um is the return the roi of like being a 50 50 huddler just holding eth or being a
liquidity provider at the different fees and you can sort of graph that pretty easily now to have this data which is kind of nice and yeah of course like you get most money if you would be a liquid provider with like one percent fee that's sort of obvious but even like the eath holder sometimes you know does better that's worse but it's cool you know we have like we now have a model where we can um easily simulate an lp pool so there's actually a branch called solved where you can run this let's see what else they want to tell you about cool yeah and the last thing that i can show really fast is
that we have an actual we use this at mento to simulate our stability protocol um cool so in the same organization where the cad cad workshop are there's the simulation which is a really complex repo where we simulate our stability mechanism and i can show you really really fast something here which is kind of cool i'm not gonna run it now but it's
already run this is a stable token simulation with arbitrage and like the fun part is here which is like this is the actual stable token price that's simulated in this experiment and you can see like how it moves around one because it's back to the dollar but like in different arbitrage cycles the price gets back and close depending on market movements so yeah this is a pretty cool setup that we use with cat cat to be able to experiment with different components for our stable token mechanism yay and we're at time anybody has questions um yeah with the simulation you did by
reading events and replaying it with different fees um is that the part of cad cad that's just the dynamic modeling part or is that also got the agent part there's the the dynamic model where you and you have that structure there but there was the other part of cad could i understand is the agent models where you can say this is a type of behavior or are you using both sides of that or so so cad cat doesn't give you really primitives for agents yeah no it doesn't you have to kind of build that yourself uh the only primitive that you have is this idea of a state change if that state change comes from an agent that you're simulating that does a certain action that's kind of okay but it doesn't come with primitives right now was that with agents or oh that was yeah i mean it wasn't really
with agents because we're just like decoding an event this simulation has agents actually in it we have like different traders that behave with different parameters and they're like simulating as yeah more like individual entities and yeah it's closer to an agent-based simulation anybody else golden thank you very much for your time you