|
| 1 | +# SimmCAD Documentation |
| 2 | + |
| 3 | +## Introduction |
| 4 | + |
| 5 | +A blockchain is a distributed ledger with economic agents transacting in a network. The state of the network evolves with every new transaction, which can be a result of user behaviors, protocol-defined system mechanisms, or external processes. |
| 6 | + |
| 7 | +It is not uncommon today for blockchain projects to announce a set of rules for their network and make claims about their system level behvaior. However, the validity of those claims is hardly validated. Furthermore, it is difficult to know the potential system-level impact when the network is considering an upgrade to their system rules and prameters. |
| 8 | + |
| 9 | +To rigorously and reliably analyze, design, and improve cryptoeconomic networks, we are introducing this Computer Aided Design Engine where we define a cryptoeconomic network with its state and exogneous variables, model transactions as a result of agent behaviors, state mechanisms, and environmental processes. We can then run simulations with different initial states, mechanisms, environmental processes to understand and visualize network behavior under different conditions. |
| 10 | + |
| 11 | +## State Variables and Transitions |
| 12 | + |
| 13 | +We now define variables and different transition mechanisms that will be inputs to the simulation engine. |
| 14 | + |
| 15 | +- ***State variables*** are defined to capture the shape and property of the network, such as a vector or a dictionary that captures all user balances. |
| 16 | +- ***Exogenous variables*** are variables that represent external input and signal. They are only affected by environmental processes and are not affected by system mechanisms. Nonetheless, exgoneous variables can be used as an input to a mechanism that impacts state variables. They can be considered as read-only variables to the system. |
| 17 | +- ***Behaviors per transition*** model agent behaviors in reaction to state variables and exogenous variables. The resulted user action will become an input to state mechanisms. Note that user behaviors should not directly update value of state variables. |
| 18 | +- ***State mechanisms per transition*** are system defined mechanisms that take user actions and other states as inputs and produce updates to the value of state variables. |
| 19 | +- ***Exogenous state updates*** specify how exogenous variables evolve with time which can indirectly impact state variables through behavior and state mechanisms. |
| 20 | +- ***Environmental processes*** model external changes that directly impact state or exogenous variables at specific timestamps or conditions. |
| 21 | + |
| 22 | +A state evolves to another state via state transition. Each transition is composed of behavior and state mechanisms as functions of state and exogenous variables. A flow of the state transition is as follows. |
| 23 | + |
| 24 | +Given some state and exogenous variables of the system at the onset of a state transition, agent behavior takes in these variables as input and return a set of agent actions. This models after agent behavior and reaction to a set of variables. Given these agent actions, state mechanism, as defined by the protocol, takes these actions, state, and exogenous variables as inputs and return a new set of state variables. |
| 25 | + |
| 26 | +## System Configuration File |
| 27 | + |
| 28 | +Simulation engine takes in system configuration files, e.g. `config.py`, where all the above variables and mechanisms are defined. The following import statements should be added at the beginning of the configuration files. |
| 29 | +```python |
| 30 | +from decimal import Decimal |
| 31 | +import numpy as np |
| 32 | +from datetime import timedelta |
| 33 | + |
| 34 | +from SimCAD import configs |
| 35 | +from SimCAD.configuration import Configuration |
| 36 | +from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \ |
| 37 | + ep_time_step |
| 38 | +``` |
| 39 | + |
| 40 | +State variables and their initial values can be defined as follows. Note that `timestamp` is a required field for this iteration of SimCAD for `env_proc` to work. Future iterations will strive to make this more generic and timestamp optional. |
| 41 | +```python |
| 42 | +genesis_dict = { |
| 43 | + 's1': Decimal(0.0), |
| 44 | + 's2': Decimal(0.0), |
| 45 | + 's3': Decimal(1.0), |
| 46 | + 'timestamp': '2018-10-01 15:16:24' |
| 47 | +} |
| 48 | +``` |
| 49 | + |
| 50 | +Each potential transition and its state and behavior mechanisms can be defined in the following dictionary object. |
| 51 | +```python |
| 52 | +transitions = { |
| 53 | + "m1": { |
| 54 | + "behaviors": { |
| 55 | + "b1": b1m1, |
| 56 | + "b2": b2m1 |
| 57 | + }, |
| 58 | + "states": { |
| 59 | + "s1": s1m1, |
| 60 | + "s2": s2m1 |
| 61 | + } |
| 62 | + }, |
| 63 | + "m2": {...} |
| 64 | +} |
| 65 | +``` |
| 66 | +Every behavior per transition should return a dictionary as actions taken by the agents. They will then be aggregated through addition in this version of SimCAD. Some examples of behaviors per transition are as follows. More flexible and user-defined aggregation functions will be introduced in future iterations but no example is provided at this point. |
| 67 | +```python |
| 68 | +def b1m1(step, sL, s): |
| 69 | + return {'param1': 1} |
| 70 | + |
| 71 | +def b1m2(step, sL, s): |
| 72 | + return {'param1': 'a', 'param2': 2} |
| 73 | + |
| 74 | +def b1m3(step, sL, s): |
| 75 | + return {'param1': ['c'], 'param2': np.array([10, 100])} |
| 76 | +``` |
| 77 | +State mechanism per transition on the other hand takes in the output of behavior mechanisms (`_input`) and returns a tuple of the name of the variable and the new value for the variable. Some examples of a state mechanism per transition are as follows. Note that each state mechanism is supposed to change one state variable at a time. Changes to multiple state variables should be done in separate mechanisms. |
| 78 | +```python |
| 79 | +def s1m1(step, sL, s, _input): |
| 80 | + y = 's1' |
| 81 | + x = _input['param1'] + 1 |
| 82 | + return (y, x) |
| 83 | + |
| 84 | +def s1m2(step, sL, s, _input): |
| 85 | + y = 's1' |
| 86 | + x = _input['param1'] |
| 87 | + return (y, x) |
| 88 | +``` |
| 89 | +Exogenous state update functions, for example `es3p1`, `es4p2` and `es5p2` below, update exogenous variables at every timestamp. Note that every timestamp is consist of all behaviors and state mechanisms in the order defined in `transitions` dictionary. If `exo_update_per_ts` is not used, exogenous state updates will be applied at every mechanism step (`m1`, `m2`, etc). Otherwise, exogenous state updates will only be applied once for every timestamp after all the mechanism steps are executed. |
| 90 | +```python |
| 91 | +exogenous_states = exo_update_per_ts( |
| 92 | + { |
| 93 | + "s3": es3p1, |
| 94 | + "s4": es4p2, |
| 95 | + "timestamp": es5p2 |
| 96 | + } |
| 97 | +) |
| 98 | +``` |
| 99 | +To model randomness, we should also define pseudorandom seeds in the configuration as follows. |
| 100 | +```python |
| 101 | +seed = { |
| 102 | + 'z': np.random.RandomState(1), |
| 103 | + 'a': np.random.RandomState(2), |
| 104 | + 'b': np.random.RandomState(3), |
| 105 | + 'c': np.random.RandomState(3) |
| 106 | +} |
| 107 | +``` |
| 108 | +SimCAD currently supports generating random number from a normal distribution through `bound_norm_random` with `min` and `max` values specified. Examples of environmental processes with randomness are as follows. We also define timestamp format with `ts_format` and timestamp changes with `t_delta`. Users can define other distributions to update exogenous variables. |
| 109 | +```python |
| 110 | +proc_one_coef_A = 0.7 |
| 111 | +proc_one_coef_B = 1.3 |
| 112 | + |
| 113 | +def es3p1(step, sL, s, _input): |
| 114 | + y = 's3' |
| 115 | + x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) |
| 116 | + return (y, x) |
| 117 | + |
| 118 | +def es4p2(step, sL, s, _input): |
| 119 | + y = 's4' |
| 120 | + x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) |
| 121 | + return (y, x) |
| 122 | + |
| 123 | +ts_format = '%Y-%m-%d %H:%M:%S' |
| 124 | +t_delta = timedelta(days=0, minutes=0, seconds=1) |
| 125 | +def es5p2(step, sL, s, _input): |
| 126 | + y = 'timestamp' |
| 127 | + x = ep_time_step(s, s['timestamp'], fromat_str=ts_format, _timedelta=t_delta) |
| 128 | + return (y, x) |
| 129 | +``` |
| 130 | +User can also define specific external events such as market shocks at specific timestamps through `env_processes` with `proc_trigger`. An environmental process with no `proc_trigger` will be called at every timestamp. In the example below, it will return the value of `s3` at every timestamp. Logical event triggers, such as a big draw down in exogenous variables, will be supported in a later version of SimCAD. |
| 131 | +```python |
| 132 | +def env_a(x): |
| 133 | + return x |
| 134 | +def env_b(x): |
| 135 | + return 10 |
| 136 | + |
| 137 | +env_processes = { |
| 138 | + "s3": env_a, |
| 139 | + "s4": proc_trigger('2018-10-01 15:16:25', env_b) |
| 140 | +} |
| 141 | +``` |
| 142 | + |
| 143 | +Lastly, we set the overall simulation configuration and initialize the `Configuration` class with the following. `T` denotes the time range and `N` refers to the number of simulation runs. Each run will start from the same initial states and run for `T` time range. Every transition is consist of behaviors, state mechanisms, exogenous updates, and potentially environmental processes. All of these happen within one time step in the simulation. |
| 144 | +```python |
| 145 | +sim_config = { |
| 146 | + "N": 2, |
| 147 | + "T": range(5) |
| 148 | +} |
| 149 | + |
| 150 | +configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) |
| 151 | +``` |
0 commit comments