Lately, there have been many economic attacks on smart contracts, and I've found myself wanting some tool to let me see how different DeFi components work and interact under varying circumstances. Then one day, someone mentioned cadCAD, which fits the purpose perfectly!

I dove into the field of systems thinking, design and simulations, and I'm super excited about it. so I decided to write this article to share what it's all about!

1. It simulates complex systems
3. It's a system for Markov Chain Monte Carlo methods (MCMC)
4. It's a system for simulating differential multi-agent games

## What is it really?

If you're familiar with formal methods and model checking, then the way of thinking behind cadCAD will very likely come naturally to you. Put shortly; it's a library to help build stochastic models (Markov chains) and perform simulations on those models.

More on Markov chains and stochastic models in a bit!

In other words, cadCAD is a python library that does system simulations. You define a model of a system in python and then simulate it with cadCAD.

These models will have two main components (there are really four components, more on that later):

• state
• state transition logic

The state describes the systems' current situation. In cadCAD, you'll use python dictionaries for this. For example:

``````state = {
'variableA': 0,
}
``````

The state transition logic describes how the system changes over time. During simulations, cadCAD will iteratively use it to compute the next state of the system.

``````def state_transition(state):
new_state = deepcopy(state)
new_state['variableA'] += 1
return new_state``````
Each iteration is one “time step” in the system.

Running a simulation using this model we’ll end up with a series of states:

``````states = {
0: {'variableA': 0},
1: {'variableA': 1},
2: {'variableA': 2},
3: {'variableA': 3},
4: {'variableA': 4},
5: {'variableA': 5},
}
``````

We can even make a nice chart to visualise the behaviour of our model!

## Markov Chains and Monte Carlo simulations

We just covered a straightforward model. There is one variable, and each timestep, we perform the exact same update. The real world is usually much different, much more random!

Models which have non-deterministic state transitions (i.e. some randomness) are commonly referred to as Markov chains.

📚 non-deterministic processes will sometimes be called stochastic processes

Monte Carlo methods are an approach to simulating these stochastic processes. It simply means we'll perform many simulations instead of just one. Due to the randomness in the system, each simulation will (probably) be slightly different from all the others. By increasing our simulation sample size, and looking at all of the results, we get a more accurate view of how the system behaves.

You can read more about Monte Carlo simulations in the context of economics here.

## Diving deeper

You can describe any complex system using the state and state transition logic components above. That said, cadCAD is nudging you to use a particular modelling pattern that works quite well.

In this pattern, we have four different aspects to a model:

• state
• policy
• mechanism
• metrics

### State

The state is the same as described above; it’s just a python object representing the system's state at a single point in time.

### Policy and Mechanism

Policies and mechanisms are where things start to divert from the original approach. For a reason! The separation of policies and mechanisms is great when we're trying to model systems with multiple actors.

In short, policies describe what "action" each actor will decide to take, while mechanisms execute the actions of each actor.

So why is this useful? First of all, it's a clean separation between the strategies of actors and the system's mechanics. It helps us keep things clean.

More importantly, however, it allows us to express models in the same way that differential games are commonly defined. There often is a phase where every player needs to simultaneously decide on a course of action (policy) and then reveal that decision to act on it (a mechanism).

An impressive example of applied game theory is his Goldenballs player who "solved" the prisoners' dilemma: https://www.youtube.com/watch?v=S0qjK3TWZE8

#### Example

Let’s explore a small example to see what policies and mechanisms look like.

Assume that we have a game with two players, Alice and Bob, and they’re playing a game of rock paper scissors.

#### Naive

They count; one … two … three
First Bob goes (Alice could do anything ,so he picks something at random):… scissors
Now it’s Alices turn (Bob just used scissors, so Alice goes for it!): … rock
Alice wins!
But it’s not fair! Alice saw Bob making the scissors sign.

#### With Policies

Let’s change our model and use policies and mechanisms. Instead of deciding and going one at a time, Alice and Bob first write down their decision on a piece of paper (the policy) and then play out the game (the mechanism).

Alice and Bob decide (spoiler: they both decide rock) and then write it down on a piece of paper. Once they're ready, they both flip their piece of paper and say what they chose:
Bob goes: … rock
Alice goes: … rock
It’s a tie!

I know that Alice could have played fair even in the first example; you write the model in cadCAD, so you could have made Alice play fair. That’s not the point though. The story illustrates how we can effectively (and easily) describe how multiple actors decide on an action based on the same prior knowledge (also referred to as prior).

### Metrics

Metrics are not really part of a model but just a supplement to make interpreting results easy. All they do is record what’s happening in state variables.
You don’t need them, though, as you could measure the same values post-simulation. I personally don’t use metrics to keep my models clean.

## But, why?

Complex systems are complex (the name gives it away 😉), and it can be difficult to assess how the system will actually behave when it’s put in the wild.

How do you know how arbitrage will affect a particular type of market?
How do you know how slippage affects incentives?
Will a system reach an equilibrium? Or just oscillate/ go off and move towards infinity.

Next to the questions that you have about your own systems, there are emergent properties. These properties don’t just arise from single system components but come from multiple sub-systems affecting each other. A prime example of an undesired emergent property is the possibility of using flash loans for oracle manipulation, resulting in a significant profit opportunity for an attacker.

### Digital Twins

The way that the cadCAD community proposes you use cadCAD is to build a digital twin. A digital twin is nothing more than a model of another existing system.

Using the digital twin, you can perform experiments to see how specific changes/features you want to make can affect the system overall.

## Where to get started

This article just scratches the surface, so be on the lookout for more articles on this topic on this blog!  In the meanwhile I’ve prepared a reading list of further material that you can read (I learned a lot of things from these articles; big thanks to the authors 🙏):

Systems Thinking
Tools for Systems Thinkers: Systems Mapping
Tools for Systems Thinkers: The 6 Fundamental Concepts of Systems Thinking
Thinking in Systems - Donella H. Meadows

Modelling and simulation is essentially a tool in the systems thinker toolbox!