{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# CIC Current System Network Graph"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Graph overview \n",
    "\n",
    "Modeling as a weighted directed graph with agents as nodes. A network is a set of items (nodes or vertices) connected by edges or links. \n",
    "We represent a network by a graph (N, g), which consists of a set of nodes N = {1, . . . , n}.\n",
    "\n",
    "#### Node types\n",
    "* Agent\n",
    "\n",
    "An agent is a user of the CIC system.\n",
    "* Chama\n",
    "\n",
    "A chama is a savings group consisting of multiple agents. Redemptions of CICs for fiat occur through chamas.\n",
    "* Trader\n",
    "\n",
    "A trader is an agent interacting with the bonding curve for investment/arbitrage opportunities.\n",
    "* Cloud\n",
    "\n",
    "The cloud is a representation of the open boundary to the world external to the model.\n",
    "* Contract\n",
    "\n",
    "The contract is the smart contract of the bonding curve.\n",
    "\n",
    "### Edges between agents\n",
    "The edge weight gij > 0 takes on non-binary values, representing the intensity of the interaction, so we refer to (N, g) as a weighted graph.\n",
    "E is the set of “directed” edges, i.e., (i, j) ∈ E\n",
    "\n",
    "#### Edge types\n",
    "* Demand\n",
    "* Fraction of demand in CIC\n",
    "* Utility - stack ranking. Food/Water is first, shopping, etc farther down\n",
    "* Spend\n",
    "* Fraction of actual in CIC\n",
    "\n",
    "![](images/dualoperator.png)\n",
    "\n",
    "\n",
    "![](images/v3differentialspec.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Assumptions\n",
    "(Defining data structures, not just initialization. Baking in degrees of freedom for future experimentation)\n",
    "\n",
    "* agents = a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p\n",
    "* Agent starting native currency is picked from a uniform distribution with a range of 20 to 500. Starting tokens is 400.\n",
    "* system = external,cic\n",
    "* chama = chama_1,chama_2,chama_3,chama_4\n",
    "\n",
    "Chamas are currently set to zero, it can be configured for more detailed analysis later on.\n",
    "* traders = ta,tb,tc\n",
    "\n",
    "Traders are currently set to zero, it can be configured for more detailed analysis later on.\n",
    "* Utility Types Ordered:\n",
    " * Food/Water\n",
    " * Fuel/Energy\n",
    " * Health\n",
    " * Education\n",
    " * Savings Group\n",
    " * Shop\n",
    "* Utility Types Probability \n",
    "  * 0.6\n",
    "  * 0.10\n",
    "  * 0.03\n",
    "  * 0.015\n",
    "  * 0.065\n",
    "  * 0.19\n",
    "* R0 = 500\n",
    "* S0 = 200000\n",
    "* P = 1\n",
    "* priceLevel = 100\n",
    "* fractionOfDemandInCIC = 0.5\n",
    "* fractionOfActualSpendInCIC = 0.5 # if an agent is interacting with the external environment, then the actual spend is 100% shilling.\n",
    "* kappa = 4\n",
    "\n",
    "\n",
    "## Initial State Values\n",
    "\n",
    "# Equations\n",
    "\n",
    "## Generators\n",
    "* Agent generation for each time step: Random choice of all agents minus 2 for both paying and receiving.  \n",
    "\n",
    "* Agent demand each time: Uniform distribution with a low value of 1 and a high of 500. \n",
    " \n",
    "### Red Cross Drip\n",
    "Every 30 days, the Red Cross drips 4000 shilling to the grassroots operator fiat balance.  \n",
    "\n",
    "### Spend Allocation \n",
    "\n",
    "#### Parameters:\n",
    "* Agent to pay: $i$\n",
    "* Agent to receive: $j$\n",
    "* Rank Order Demand: $\\frac{v_{i,j}}{d_{i,j}}$\n",
    "* Amount of currency agent $i$ has to spend, $\\gamma$\n",
    "* Amount of cic agent $i$ has to spend, $\\gamma_\\textrm{cic}$\n",
    "* Percentage of transaction in cic, $\\phi$\n",
    "* Spend, $\\zeta$\n",
    "\n",
    "\n",
    "if $\\frac{v_{i,j}}{d_{i,j}} * 1-\\phi > \\gamma_{i} \\textrm{and} \\frac{v_{i,j}}{d_{i,j}} * \\phi > \\gamma_\\textrm{cic} \\Rightarrow \\zeta = \\frac{v_{i,j}}{d_{i,j}}$ \n",
    "\n",
    "else $ \\Rightarrow \\zeta = \\gamma$\n",
    "\n",
    "Allocate utility type by stack ranking in. Allocate remaining fiat and cic until all demand is met or i runs out.\n",
    "\n",
    "\n",
    "### Withdraw calculation\n",
    "\n",
    "The user is able to withdraw up to 50% of the their CIC balance if they have spent 50% of their balance within the last 30 days at a conversion ratio of 1:1, meaning that for every one token withdraw, they receive 1 in native currency. We are assuming that agents want what to withdraw as much as they can.\n",
    "This is one of the most important control points for Grassroots economics. The more people withdraw CIC from the system, the more difficult it is on the system. The more people can withdraw, the better the adoption however. The inverse also holds true: the less individuals can withdraw, the lower the adoption.\n",
    "\n",
    "## Distribution to agents\n",
    "#### Parameters\n",
    "FrequencyOfAllocation = 45 # frequency of allocation of drip to agents\n",
    "* idealFiat = 5000\n",
    "* idealCIC = 200000\n",
    "* varianceCIC = 50000\n",
    "* varianceFiat = 1000\n",
    "* unadjustedPerAgent = 50\n",
    "\n",
    "```\n",
    "# agent:[centrality,allocationValue]\n",
    "agentAllocation = {'a':[1,1],'b':[1,1],'c':[1,1], \n",
    "                   'd':[1,1],'e':[1,1],'f':[1,1],\n",
    "                   'g':[1,1],'h':[1,1],'i':[1,1],\n",
    "                   'j':[1,1],'k':[1,1],'l':[1,1],\n",
    "                   'm':[1,1],'o':[1,1],'p':[1,1]}\n",
    "```\n",
    "\n",
    "Every 15 days, a total of unadjustedPerAgent * agents will be distributed among the agents. Allocation will occur based off of the the agent allocation dictionary allocation value. We can optimize the allocation overtime and make a state variable for adjustment overtime as a result of centrality. We are currently assuming that all agents have the same centrality and allocation.\n",
    "\n",
    "Internal velocity is better than external velocity of the system. Point of leverage to make more internal cycles. Canbe used for tuning system effiency.\n",
    "![](images/agentDistribution.png)\n",
    "\n",
    "### Inventory Controller\n",
    "Heuristic Monetary policy hysteresis conservation allocation between fiat and cic reserves. We've created an inventory control function to test if the current balance is in an acceptable tolarance. For the calculation, we use the following 2 variables, current CIC balance and current fiat balance, along with 2 parameters, desired cic and variance.\n",
    "\n",
    "Below is \n",
    "```\n",
    "if idealCIC - variance <= actual <= ideal + (2*variance):\n",
    "    decision = 'none'\n",
    "    amount = 0\n",
    "else:\n",
    "    \n",
    "    if (ideal + variance) > actual :\n",
    "        decision = 'mint'\n",
    "        amount = (ideal + variance) - actual\n",
    "    else:\n",
    "        pass\n",
    "    if actual > (ideal + variance):\n",
    "        decision = 'burn'\n",
    "        amount = actual - (ideal + variance) \n",
    "    else:\n",
    "        pass\n",
    "\n",
    "if decision == 'mint':\n",
    "    if fiat < (ideal - variance):\n",
    "        if amount > fiat:\n",
    "            decision = 'none'\n",
    "            amount = 0\n",
    "        else:\n",
    "            pass\n",
    "if decision == 'none':\n",
    "    if fiat < (ideal - variance):\n",
    "        decision = 'mint'\n",
    "        amount = (ideal-variance)\n",
    "    else:\n",
    "        pass\n",
    "    \n",
    "\n",
    "```\n",
    "\n",
    "If the controller wants to mint, the amount decided from the inventory controller, $\\Delta R$ is inserted into the following minting equation:\n",
    "\n",
    "- Conservation equation, V0: $V(R+ \\Delta R', S+\\Delta S) = \\frac{(S+\\Delta S)^\\kappa}{R+\\Delta R'} =\\frac{S^\\kappa}{R}$\n",
    "- Derived Mint equation: $\\Delta S = mint\\big(\\Delta R ; (R,S)\\big)= S\\big(\\sqrt[\\kappa]{(1+\\frac{\\Delta R}{R})}-1\\big)$\n",
    " \n",
    "\n",
    "\n",
    "If the controller wants to burn, the amount decided from the inventory controller, $\\Delta S$ is inserted into the following minting equation:\n",
    " - Derived Withdraw equation: $\\Delta R = withdraw\\big(\\Delta S ; (R,S)\\big)= R\\big(1-(1-\\frac{\\Delta S}{S})^\\kappa \\big)$\n",
    " \n",
    "\n",
    "There is a built in process lag of 7 days before the newly minted or burned CIC is added to the respective operator accounts.\n",
    "\n",
    "### Velocity of Money \n",
    "\n",
    "Indirect measurement of velocity of money per timestep:\n",
    "\n",
    "$V_t = \\frac{PT}{M}$\n",
    "\n",
    "Where\n",
    "\n",
    "* $V_t$ is the velocity of money for all agent transaction in the time period examined\n",
    "* $P$ is the price level\n",
    "* $T$ is the aggregated real value of all agent transactions in the time period examined\n",
    "* $M$ is the average money supply in the economy in the time period examined.\n",
    "\n",
    "\n",
    "\n",
    "## Simulation run\n",
    "* 5 monte carlo runs with 100 timesteps. Each timestep is equal to 1 day.\n",
    "\n",
    "\n",
    "## Proposed Experiments\n",
    "![](images/experiments.png)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Run cadCAD model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/aclarkdata/anaconda3/lib/python3.7/site-packages/statsmodels/tools/_testing.py:19: FutureWarning: pandas.util.testing is deprecated. Use the functions in the public API at pandas.testing instead.\n",
      "  import pandas.util.testing as tm\n",
      "/home/aclarkdata/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:563: MatplotlibDeprecationWarning: \n",
      "The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.\n",
      "  if not cb.iterable(width):\n",
      "/home/aclarkdata/anaconda3/lib/python3.7/site-packages/networkx/drawing/nx_pylab.py:660: MatplotlibDeprecationWarning: \n",
      "The iterable function was deprecated in Matplotlib 3.1 and will be removed in 3.3. Use np.iterable instead.\n",
      "  if cb.iterable(node_size):  # many node sizes\n"
     ]
    },
    {
     "data": {
      "image/png": "[encoded data, 97736 characters]\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[{'N': 5, 'T': range(0, 100), 'M': [{}]}]\n"
     ]
    }
   ],
   "source": [
    "import math\n",
    "import pandas as pd\n",
    "from tabulate import tabulate\n",
    "import matplotlib.pyplot as plt\n",
    "import seaborn as sns\n",
    "import numpy as np\n",
    "\n",
    "from model.parts.supportingFunctions import *\n",
    "\n",
    "pd.options.display.float_format = '{:.2f}'.format\n",
    "\n",
    "%matplotlib inline\n",
    "from tabulate import tabulate\n",
    "from typing import Dict, List\n",
    "\n",
    "from ipywidgets import interact, interactive, fixed, interact_manual\n",
    "import ipywidgets as widgets\n",
    "from IPython.display import clear_output\n",
    "\n",
    "# The following imports NEED to be in the exact order\n",
    "from cadCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
    "from model import economyconfig\n",
    "from cadCAD import configs\n",
    "\n",
    "exec_mode = ExecutionMode()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "                            __________   ____ \n",
      "          ________ __ _____/ ____/   |  / __ \\\n",
      "         / ___/ __` / __  / /   / /| | / / / /\n",
      "        / /__/ /_/ / /_/ / /___/ ___ |/ /_/ / \n",
      "        \\___/\\__,_/\\__,_/\\____/_/  |_/_____/  \n",
      "        by BlockScience\n",
      "        \n",
      "Execution Mode: single_proc: [<cadCAD.configuration.Configuration object at 0x7fbf17ed0d50>]\n",
      "Configurations: [<cadCAD.configuration.Configuration object at 0x7fbf17ed0d50>]\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/aclarkdata/anaconda3/lib/python3.7/site-packages/cadCAD/utils/__init__.py:113: FutureWarning: The use of a dictionary to describe Partial State Update Blocks will be deprecated. Use a list instead.\n",
      "  FutureWarning)\n"
     ]
    }
   ],
   "source": [
    "single_proc_ctx = ExecutionContext(context=exec_mode.single_proc)\n",
    "run1 = Executor(exec_context=single_proc_ctx, configs=[configs[0]])\n",
    "run1_raw_result, raw_tensor_field = run1.execute()\n",
    "df = pd.DataFrame(run1_raw_result)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set subsetep to last\n",
    "df = df[df['substep'] == df['substep'].max()]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>network</th>\n",
       "      <th>KPIDemand</th>\n",
       "      <th>KPISpend</th>\n",
       "      <th>KPISpendOverDemand</th>\n",
       "      <th>VelocityOfMoney</th>\n",
       "      <th>startingBalance</th>\n",
       "      <th>30_day_spend</th>\n",
       "      <th>withdraw</th>\n",
       "      <th>outboundAgents</th>\n",
       "      <th>inboundAgents</th>\n",
       "      <th>operatorFiatBalance</th>\n",
       "      <th>operatorCICBalance</th>\n",
       "      <th>fundsInProcess</th>\n",
       "      <th>totalDistributedToAgents</th>\n",
       "      <th>totalMinted</th>\n",
       "      <th>totalBurned</th>\n",
       "      <th>run</th>\n",
       "      <th>substep</th>\n",
       "      <th>timestep</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>8</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 193, 'd': 484, 'e': 254,...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 193, 'd': 59, 'e': 254, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1.0, 'd': 0.121900826446...</td>\n",
       "      <td>14.98</td>\n",
       "      <td>{'a': 400, 'b': 400, 'c': 400, 'd': 400, 'e': ...</td>\n",
       "      <td>{}</td>\n",
       "      <td>0</td>\n",
       "      <td>[i, d, i, p, c, l, p, e, e, i, m, j, h, m]</td>\n",
       "      <td>[f, external, m, f, d, m, d, b, g, c, a, c, ex...</td>\n",
       "      <td>4500</td>\n",
       "      <td>200000.00</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>16</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 336, 'b': 0, 'c': 0, 'd': 768, 'e': 291,...</td>\n",
       "      <td>{'a': 336, 'b': 0, 'c': 0, 'd': 326.0, 'e': 29...</td>\n",
       "      <td>{'a': 1.0, 'b': 0, 'c': 0, 'd': 0.424479166666...</td>\n",
       "      <td>29.48</td>\n",
       "      <td>{'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....</td>\n",
       "      <td>{'c': 193, 'd': 59, 'e': 254, 'h': 365, 'i': 2...</td>\n",
       "      <td>0</td>\n",
       "      <td>[j, l, h, d, f, m, e, k, a, external, d, f, g, h]</td>\n",
       "      <td>[p, e, g, h, j, k, j, e, l, o, l, c, h, c]</td>\n",
       "      <td>4500</td>\n",
       "      <td>200000.00</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>2</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>24</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 334, 'c': 19, 'd': 463, 'e': 0, ...</td>\n",
       "      <td>{'a': 0, 'b': 334, 'c': 19, 'd': 7.5, 'e': 0, ...</td>\n",
       "      <td>{'a': 0, 'b': 1.0, 'c': 1.0, 'd': 0.0161987041...</td>\n",
       "      <td>22.56</td>\n",
       "      <td>{'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....</td>\n",
       "      <td>{'a': 336, 'f': 418.125, 'g': 198.5, 'k': 59, ...</td>\n",
       "      <td>0</td>\n",
       "      <td>[m, i, external, p, d, g, external, b, h, j, k...</td>\n",
       "      <td>[k, a, b, d, e, a, c, f, c, external, b, e, h, c]</td>\n",
       "      <td>4500</td>\n",
       "      <td>200000.00</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>32</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 443, 'c': 0, 'd': 496, 'e': 0, '...</td>\n",
       "      <td>{'a': 0, 'b': 443, 'c': 0, 'd': 15.75, 'e': 0,...</td>\n",
       "      <td>{'a': 0, 'b': 1.0, 'c': 0, 'd': 0.031754032258...</td>\n",
       "      <td>8.80</td>\n",
       "      <td>{'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....</td>\n",
       "      <td>{'b': 334, 'a': 336, 'f': 418.125, 'g': 406.5,...</td>\n",
       "      <td>0</td>\n",
       "      <td>[d, o, b, f, k, e, h, f, i, f, j, j, l, b]</td>\n",
       "      <td>[m, c, i, h, j, e, l, k, k, e, p, j, d, b]</td>\n",
       "      <td>4500</td>\n",
       "      <td>200000.00</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>4</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>40</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 213, 'b': 0, 'c': 60, 'd': 0, 'e': 0, 'f...</td>\n",
       "      <td>{'a': 213, 'b': 0, 'c': 60, 'd': 0, 'e': 0, 'f...</td>\n",
       "      <td>{'a': 1.0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 0, '...</td>\n",
       "      <td>5.19</td>\n",
       "      <td>{'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....</td>\n",
       "      <td>{'o': 108, 'b': 777, 'a': 336, 'f': 700.125, '...</td>\n",
       "      <td>0</td>\n",
       "      <td>[g, c, a, f, h, e, c, g, k, e, i, i, o, i]</td>\n",
       "      <td>[p, l, c, o, h, external, d, e, m, d, o, l, g, h]</td>\n",
       "      <td>4500</td>\n",
       "      <td>200000.00</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>1</td>\n",
       "      <td>8</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                                              network  \\\n",
       "8   (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "16  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "24  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "32  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "40  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "\n",
       "                                            KPIDemand  \\\n",
       "8   {'a': 0, 'b': 0, 'c': 193, 'd': 484, 'e': 254,...   \n",
       "16  {'a': 336, 'b': 0, 'c': 0, 'd': 768, 'e': 291,...   \n",
       "24  {'a': 0, 'b': 334, 'c': 19, 'd': 463, 'e': 0, ...   \n",
       "32  {'a': 0, 'b': 443, 'c': 0, 'd': 496, 'e': 0, '...   \n",
       "40  {'a': 213, 'b': 0, 'c': 60, 'd': 0, 'e': 0, 'f...   \n",
       "\n",
       "                                             KPISpend  \\\n",
       "8   {'a': 0, 'b': 0, 'c': 193, 'd': 59, 'e': 254, ...   \n",
       "16  {'a': 336, 'b': 0, 'c': 0, 'd': 326.0, 'e': 29...   \n",
       "24  {'a': 0, 'b': 334, 'c': 19, 'd': 7.5, 'e': 0, ...   \n",
       "32  {'a': 0, 'b': 443, 'c': 0, 'd': 15.75, 'e': 0,...   \n",
       "40  {'a': 213, 'b': 0, 'c': 60, 'd': 0, 'e': 0, 'f...   \n",
       "\n",
       "                                   KPISpendOverDemand  VelocityOfMoney  \\\n",
       "8   {'a': 0, 'b': 0, 'c': 1.0, 'd': 0.121900826446...            14.98   \n",
       "16  {'a': 1.0, 'b': 0, 'c': 0, 'd': 0.424479166666...            29.48   \n",
       "24  {'a': 0, 'b': 1.0, 'c': 1.0, 'd': 0.0161987041...            22.56   \n",
       "32  {'a': 0, 'b': 1.0, 'c': 0, 'd': 0.031754032258...             8.80   \n",
       "40  {'a': 1.0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 0, '...             5.19   \n",
       "\n",
       "                                      startingBalance  \\\n",
       "8   {'a': 400, 'b': 400, 'c': 400, 'd': 400, 'e': ...   \n",
       "16  {'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....   \n",
       "24  {'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....   \n",
       "32  {'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....   \n",
       "40  {'a': 415.0, 'b': 402.5, 'c': 420.5, 'd': 511....   \n",
       "\n",
       "                                         30_day_spend withdraw  \\\n",
       "8                                                  {}        0   \n",
       "16  {'c': 193, 'd': 59, 'e': 254, 'h': 365, 'i': 2...        0   \n",
       "24  {'a': 336, 'f': 418.125, 'g': 198.5, 'k': 59, ...        0   \n",
       "32  {'b': 334, 'a': 336, 'f': 418.125, 'g': 406.5,...        0   \n",
       "40  {'o': 108, 'b': 777, 'a': 336, 'f': 700.125, '...        0   \n",
       "\n",
       "                                       outboundAgents  \\\n",
       "8          [i, d, i, p, c, l, p, e, e, i, m, j, h, m]   \n",
       "16  [j, l, h, d, f, m, e, k, a, external, d, f, g, h]   \n",
       "24  [m, i, external, p, d, g, external, b, h, j, k...   \n",
       "32         [d, o, b, f, k, e, h, f, i, f, j, j, l, b]   \n",
       "40         [g, c, a, f, h, e, c, g, k, e, i, i, o, i]   \n",
       "\n",
       "                                        inboundAgents  operatorFiatBalance  \\\n",
       "8   [f, external, m, f, d, m, d, b, g, c, a, c, ex...                 4500   \n",
       "16         [p, e, g, h, j, k, j, e, l, o, l, c, h, c]                 4500   \n",
       "24  [k, a, b, d, e, a, c, f, c, external, b, e, h, c]                 4500   \n",
       "32         [m, c, i, h, j, e, l, k, k, e, p, j, d, b]                 4500   \n",
       "40  [p, l, c, o, h, external, d, e, m, d, o, l, g, h]                 4500   \n",
       "\n",
       "    operatorCICBalance                                     fundsInProcess  \\\n",
       "8            200000.00  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "16           200000.00  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "24           200000.00  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "32           200000.00  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "40           200000.00  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "\n",
       "    totalDistributedToAgents  totalMinted  totalBurned  run  substep  timestep  \n",
       "8                          0            0            0    1        8         1  \n",
       "16                         0            0            0    1        8         2  \n",
       "24                         0            0            0    1        8         3  \n",
       "32                         0            0            0    1        8         4  \n",
       "40                         0            0            0    1        8         5  "
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.head()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "df.reset_index(inplace=True)\n",
    "del df['index']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['agents'] = df.network.apply(lambda g: np.array([get_nodes_by_type(g,'Agent')][0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['agent_tokens'] = df.network.apply(lambda g: np.array([g.nodes[j]['tokens'] for j in get_nodes_by_type(g,'Agent')]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "df['agent_native_currency'] = df.network.apply(lambda g: np.array([g.nodes[j]['native_currency'] for j in get_nodes_by_type(g,'Agent')]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>network</th>\n",
       "      <th>KPIDemand</th>\n",
       "      <th>KPISpend</th>\n",
       "      <th>KPISpendOverDemand</th>\n",
       "      <th>VelocityOfMoney</th>\n",
       "      <th>startingBalance</th>\n",
       "      <th>30_day_spend</th>\n",
       "      <th>withdraw</th>\n",
       "      <th>outboundAgents</th>\n",
       "      <th>inboundAgents</th>\n",
       "      <th>...</th>\n",
       "      <th>fundsInProcess</th>\n",
       "      <th>totalDistributedToAgents</th>\n",
       "      <th>totalMinted</th>\n",
       "      <th>totalBurned</th>\n",
       "      <th>run</th>\n",
       "      <th>substep</th>\n",
       "      <th>timestep</th>\n",
       "      <th>agents</th>\n",
       "      <th>agent_tokens</th>\n",
       "      <th>agent_native_currency</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>495</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1, 'd': 0, 'e': 216, 'f'...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1, 'd': 0, 'e': 216, 'f'...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 1.0, '...</td>\n",
       "      <td>16.09</td>\n",
       "      <td>{'a': 1397.2279590304938, 'b': 919.89307291706...</td>\n",
       "      <td>{'a': 817, 'b': 383, 'c': 281, 'd': 2576.04463...</td>\n",
       "      <td>0</td>\n",
       "      <td>[k, m, j, f, c, e, k, o, k, p, g, j, d, c]</td>\n",
       "      <td>[g, k, o, j, external, b, a, h, k, i, j, l, g, c]</td>\n",
       "      <td>...</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>96</td>\n",
       "      <td>[a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]</td>\n",
       "      <td>[1371.5511663962209, 1304.8160607519264, 3067....</td>\n",
       "      <td>[1880.9234604645571, 2330.19597510887, 1045.05...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>496</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 586, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>{'a': 586, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>{'a': 1.0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>14.88</td>\n",
       "      <td>{'a': 1397.2279590304938, 'b': 919.89307291706...</td>\n",
       "      <td>{'a': 817, 'b': 383, 'c': 282, 'd': 2576.04463...</td>\n",
       "      <td>0</td>\n",
       "      <td>[l, o, p, external, o, m, a, k, a, o, external...</td>\n",
       "      <td>[h, c, i, c, m, c, g, b, e, k, m, c, c, i]</td>\n",
       "      <td>...</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>97</td>\n",
       "      <td>[a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]</td>\n",
       "      <td>[1078.5511663962209, 1510.8160607519264, 3442....</td>\n",
       "      <td>[1587.9234604645571, 2536.19597510887, 1420.05...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>497</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 56, 'd': 346, 'e': 406, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 56, 'd': 1360.3531211347...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1.0, 'd': 3.931656419464...</td>\n",
       "      <td>28.76</td>\n",
       "      <td>{'a': 1397.2279590304938, 'b': 919.89307291706...</td>\n",
       "      <td>{'a': 1403, 'b': 383, 'c': 282, 'd': 2576.0446...</td>\n",
       "      <td>0</td>\n",
       "      <td>[o, d, o, k, l, g, e, m, c, m, j, j, f, j]</td>\n",
       "      <td>[g, a, l, o, a, m, m, l, m, j, e, d, a, f]</td>\n",
       "      <td>...</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>98</td>\n",
       "      <td>[a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]</td>\n",
       "      <td>[2278.64406597355, 1510.8160607519264, 3414.44...</td>\n",
       "      <td>[2788.0163600418864, 2536.19597510887, 1392.05...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>498</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 514, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 514, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 0, 'f'...</td>\n",
       "      <td>34.34</td>\n",
       "      <td>{'a': 1397.2279590304938, 'b': 919.89307291706...</td>\n",
       "      <td>{'a': 1403, 'b': 383, 'c': 338, 'd': 3936.3977...</td>\n",
       "      <td>0</td>\n",
       "      <td>[k, external, h, f, i, g, k, c, c, g, f, i, c, l]</td>\n",
       "      <td>[j, i, a, j, k, m, d, f, j, k, f, k, l, e]</td>\n",
       "      <td>...</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>99</td>\n",
       "      <td>[a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]</td>\n",
       "      <td>[3093.590125261905, 1510.8160607519264, 3157.4...</td>\n",
       "      <td>[3602.9624193302416, 2536.19597510887, 1135.05...</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>499</th>\n",
       "      <td>(a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...</td>\n",
       "      <td>{'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...</td>\n",
       "      <td>3.51</td>\n",
       "      <td>{'a': 1397.2279590304938, 'b': 919.89307291706...</td>\n",
       "      <td>{'a': 1403, 'b': 383, 'c': 852, 'd': 3936.3977...</td>\n",
       "      <td>0</td>\n",
       "      <td>[l, k, g, j, a, f, l, h, j, f, o, f, b, b]</td>\n",
       "      <td>[e, external, g, f, o, i, o, b, c, h, a, a, o, l]</td>\n",
       "      <td>...</td>\n",
       "      <td>{'timestep': [], 'decision': [], 'cic': [], 's...</td>\n",
       "      <td>1500</td>\n",
       "      <td>0</td>\n",
       "      <td>0</td>\n",
       "      <td>5</td>\n",
       "      <td>8</td>\n",
       "      <td>100</td>\n",
       "      <td>[a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]</td>\n",
       "      <td>[3093.590125261905, 1510.8160607519264, 3157.4...</td>\n",
       "      <td>[3602.9624193302416, 2536.19597510887, 1135.05...</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "<p>5 rows × 22 columns</p>\n",
       "</div>"
      ],
      "text/plain": [
       "                                               network  \\\n",
       "495  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "496  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "497  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "498  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "499  (a, b, c, d, e, f, g, h, i, j, k, l, m, o, p, ...   \n",
       "\n",
       "                                             KPIDemand  \\\n",
       "495  {'a': 0, 'b': 0, 'c': 1, 'd': 0, 'e': 216, 'f'...   \n",
       "496  {'a': 586, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...   \n",
       "497  {'a': 0, 'b': 0, 'c': 56, 'd': 346, 'e': 406, ...   \n",
       "498  {'a': 0, 'b': 0, 'c': 514, 'd': 0, 'e': 0, 'f'...   \n",
       "499  {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...   \n",
       "\n",
       "                                              KPISpend  \\\n",
       "495  {'a': 0, 'b': 0, 'c': 1, 'd': 0, 'e': 216, 'f'...   \n",
       "496  {'a': 586, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...   \n",
       "497  {'a': 0, 'b': 0, 'c': 56, 'd': 1360.3531211347...   \n",
       "498  {'a': 0, 'b': 0, 'c': 514, 'd': 0, 'e': 0, 'f'...   \n",
       "499  {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...   \n",
       "\n",
       "                                    KPISpendOverDemand  VelocityOfMoney  \\\n",
       "495  {'a': 0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 1.0, '...            16.09   \n",
       "496  {'a': 1.0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f'...            14.88   \n",
       "497  {'a': 0, 'b': 0, 'c': 1.0, 'd': 3.931656419464...            28.76   \n",
       "498  {'a': 0, 'b': 0, 'c': 1.0, 'd': 0, 'e': 0, 'f'...            34.34   \n",
       "499  {'a': 0, 'b': 0, 'c': 0, 'd': 0, 'e': 0, 'f': ...             3.51   \n",
       "\n",
       "                                       startingBalance  \\\n",
       "495  {'a': 1397.2279590304938, 'b': 919.89307291706...   \n",
       "496  {'a': 1397.2279590304938, 'b': 919.89307291706...   \n",
       "497  {'a': 1397.2279590304938, 'b': 919.89307291706...   \n",
       "498  {'a': 1397.2279590304938, 'b': 919.89307291706...   \n",
       "499  {'a': 1397.2279590304938, 'b': 919.89307291706...   \n",
       "\n",
       "                                          30_day_spend withdraw  \\\n",
       "495  {'a': 817, 'b': 383, 'c': 281, 'd': 2576.04463...        0   \n",
       "496  {'a': 817, 'b': 383, 'c': 282, 'd': 2576.04463...        0   \n",
       "497  {'a': 1403, 'b': 383, 'c': 282, 'd': 2576.0446...        0   \n",
       "498  {'a': 1403, 'b': 383, 'c': 338, 'd': 3936.3977...        0   \n",
       "499  {'a': 1403, 'b': 383, 'c': 852, 'd': 3936.3977...        0   \n",
       "\n",
       "                                        outboundAgents  \\\n",
       "495         [k, m, j, f, c, e, k, o, k, p, g, j, d, c]   \n",
       "496  [l, o, p, external, o, m, a, k, a, o, external...   \n",
       "497         [o, d, o, k, l, g, e, m, c, m, j, j, f, j]   \n",
       "498  [k, external, h, f, i, g, k, c, c, g, f, i, c, l]   \n",
       "499         [l, k, g, j, a, f, l, h, j, f, o, f, b, b]   \n",
       "\n",
       "                                         inboundAgents  ...  \\\n",
       "495  [g, k, o, j, external, b, a, h, k, i, j, l, g, c]  ...   \n",
       "496         [h, c, i, c, m, c, g, b, e, k, m, c, c, i]  ...   \n",
       "497         [g, a, l, o, a, m, m, l, m, j, e, d, a, f]  ...   \n",
       "498         [j, i, a, j, k, m, d, f, j, k, f, k, l, e]  ...   \n",
       "499  [e, external, g, f, o, i, o, b, c, h, a, a, o, l]  ...   \n",
       "\n",
       "                                        fundsInProcess  \\\n",
       "495  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "496  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "497  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "498  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "499  {'timestep': [], 'decision': [], 'cic': [], 's...   \n",
       "\n",
       "     totalDistributedToAgents totalMinted  totalBurned  run  substep  \\\n",
       "495                      1500           0            0    5        8   \n",
       "496                      1500           0            0    5        8   \n",
       "497                      1500           0            0    5        8   \n",
       "498                      1500           0            0    5        8   \n",
       "499                      1500           0            0    5        8   \n",
       "\n",
       "     timestep                                         agents  \\\n",
       "495        96  [a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]   \n",
       "496        97  [a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]   \n",
       "497        98  [a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]   \n",
       "498        99  [a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]   \n",
       "499       100  [a, b, c, d, e, f, g, h, i, j, k, l, m, o, p]   \n",
       "\n",
       "                                          agent_tokens  \\\n",
       "495  [1371.5511663962209, 1304.8160607519264, 3067....   \n",
       "496  [1078.5511663962209, 1510.8160607519264, 3442....   \n",
       "497  [2278.64406597355, 1510.8160607519264, 3414.44...   \n",
       "498  [3093.590125261905, 1510.8160607519264, 3157.4...   \n",
       "499  [3093.590125261905, 1510.8160607519264, 3157.4...   \n",
       "\n",
       "                                 agent_native_currency  \n",
       "495  [1880.9234604645571, 2330.19597510887, 1045.05...  \n",
       "496  [1587.9234604645571, 2536.19597510887, 1420.05...  \n",
       "497  [2788.0163600418864, 2536.19597510887, 1392.05...  \n",
       "498  [3602.9624193302416, 2536.19597510887, 1135.05...  \n",
       "499  [3602.9624193302416, 2536.19597510887, 1135.05...  \n",
       "\n",
       "[5 rows x 22 columns]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Create dataframe variables   \n",
    "tokens = []\n",
    "for i in df.index:\n",
    "     tokens.append(sum(df['agent_tokens'][i]))\n",
    "        \n",
    "df['AggregatedAgentCICHolding'] = tokens  \n",
    "\n",
    "currency = []\n",
    "for i in df.index:\n",
    "     currency.append(sum(df['agent_native_currency'][i]))\n",
    "        \n",
    "df['AggregatedAgentCurrencyHolding'] = currency  \n",
    "\n",
    "AggregatedSpend = []\n",
    "for i in df.index:\n",
    "     AggregatedSpend.append(sum(df['KPISpend'][i].values()))\n",
    "\n",
    "df['AggregatedAgentSpend'] = AggregatedSpend   \n",
    "\n",
    "AggregatedDemand = []\n",
    "for i in df.index:\n",
    "     AggregatedDemand.append(sum(df['KPIDemand'][i].values()))\n",
    "        \n",
    "df['AggregatedAgentDemand'] = AggregatedDemand  \n",
    "\n",
    "\n",
    "AggregatedKPISpendOverDemand = []\n",
    "for i in df.index:\n",
    "     AggregatedKPISpendOverDemand.append(sum(df['KPISpendOverDemand'][i].values()))\n",
    "        \n",
    "df['AggregatedKPISpendOverDemand'] = AggregatedKPISpendOverDemand  \n",
    "\n",
    "\n",
    "AggregatedGapOfDemandMinusSpend = []\n",
    "for i in df.index:\n",
    "     AggregatedGapOfDemandMinusSpend.append(sum(df['KPIDemand'][i].values())- sum(df['KPISpend'][i].values()))\n",
    "        \n",
    "df['AggregatedGapOfDemandMinusSpend'] = AggregatedGapOfDemandMinusSpend  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "23"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df['KPISpend'][1]['external']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "subset = df[['timestep', 'VelocityOfMoney', 'operatorFiatBalance',\n",
    "       'operatorCICBalance', 'totalDistributedToAgents', 'totalMinted',\n",
    "       'totalBurned', 'run', 'substep', 'AggregatedAgentCICHolding',\n",
    "       'AggregatedAgentCurrencyHolding', 'AggregatedAgentSpend',\n",
    "       'AggregatedAgentDemand', 'AggregatedKPISpendOverDemand',\n",
    "       'AggregatedGapOfDemandMinusSpend']]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "mean_df,median_df,std_df,min_df = aggregate_runs(subset,'timestep')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>timestep</th>\n",
       "      <th>VelocityOfMoney</th>\n",
       "      <th>operatorFiatBalance</th>\n",
       "      <th>operatorCICBalance</th>\n",
       "      <th>totalDistributedToAgents</th>\n",
       "      <th>totalMinted</th>\n",
       "      <th>totalBurned</th>\n",
       "      <th>run</th>\n",
       "      <th>substep</th>\n",
       "      <th>AggregatedAgentCICHolding</th>\n",
       "      <th>AggregatedAgentCurrencyHolding</th>\n",
       "      <th>AggregatedAgentSpend</th>\n",
       "      <th>AggregatedAgentDemand</th>\n",
       "      <th>AggregatedKPISpendOverDemand</th>\n",
       "      <th>AggregatedGapOfDemandMinusSpend</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>95</th>\n",
       "      <td>96</td>\n",
       "      <td>12.79</td>\n",
       "      <td>16500.00</td>\n",
       "      <td>198500.00</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>8.00</td>\n",
       "      <td>-1451.11</td>\n",
       "      <td>18194.36</td>\n",
       "      <td>2081.82</td>\n",
       "      <td>2015.40</td>\n",
       "      <td>14.96</td>\n",
       "      <td>-66.42</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>96</th>\n",
       "      <td>97</td>\n",
       "      <td>24.53</td>\n",
       "      <td>16500.00</td>\n",
       "      <td>198500.00</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>8.00</td>\n",
       "      <td>-1294.82</td>\n",
       "      <td>18350.66</td>\n",
       "      <td>4124.56</td>\n",
       "      <td>3285.20</td>\n",
       "      <td>21.05</td>\n",
       "      <td>-839.36</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>97</th>\n",
       "      <td>98</td>\n",
       "      <td>21.78</td>\n",
       "      <td>16500.00</td>\n",
       "      <td>198500.00</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>8.00</td>\n",
       "      <td>-1336.96</td>\n",
       "      <td>18308.52</td>\n",
       "      <td>3758.32</td>\n",
       "      <td>2205.60</td>\n",
       "      <td>66.20</td>\n",
       "      <td>-1552.72</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>98</th>\n",
       "      <td>99</td>\n",
       "      <td>20.86</td>\n",
       "      <td>16500.00</td>\n",
       "      <td>198500.00</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>8.00</td>\n",
       "      <td>-1273.16</td>\n",
       "      <td>18372.32</td>\n",
       "      <td>3531.91</td>\n",
       "      <td>2682.40</td>\n",
       "      <td>8.81</td>\n",
       "      <td>-849.51</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>99</th>\n",
       "      <td>100</td>\n",
       "      <td>10.78</td>\n",
       "      <td>16500.00</td>\n",
       "      <td>198500.00</td>\n",
       "      <td>1500.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>0.00</td>\n",
       "      <td>3.00</td>\n",
       "      <td>8.00</td>\n",
       "      <td>-1283.46</td>\n",
       "      <td>18362.02</td>\n",
       "      <td>1833.99</td>\n",
       "      <td>1736.60</td>\n",
       "      <td>32.98</td>\n",
       "      <td>-97.39</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "    timestep  VelocityOfMoney  operatorFiatBalance  operatorCICBalance  \\\n",
       "95        96            12.79             16500.00           198500.00   \n",
       "96        97            24.53             16500.00           198500.00   \n",
       "97        98            21.78             16500.00           198500.00   \n",
       "98        99            20.86             16500.00           198500.00   \n",
       "99       100            10.78             16500.00           198500.00   \n",
       "\n",
       "    totalDistributedToAgents  totalMinted  totalBurned  run  substep  \\\n",
       "95                   1500.00         0.00         0.00 3.00     8.00   \n",
       "96                   1500.00         0.00         0.00 3.00     8.00   \n",
       "97                   1500.00         0.00         0.00 3.00     8.00   \n",
       "98                   1500.00         0.00         0.00 3.00     8.00   \n",
       "99                   1500.00         0.00         0.00 3.00     8.00   \n",
       "\n",
       "    AggregatedAgentCICHolding  AggregatedAgentCurrencyHolding  \\\n",
       "95                   -1451.11                        18194.36   \n",
       "96                   -1294.82                        18350.66   \n",
       "97                   -1336.96                        18308.52   \n",
       "98                   -1273.16                        18372.32   \n",
       "99                   -1283.46                        18362.02   \n",
       "\n",
       "    AggregatedAgentSpend  AggregatedAgentDemand  AggregatedKPISpendOverDemand  \\\n",
       "95               2081.82                2015.40                         14.96   \n",
       "96               4124.56                3285.20                         21.05   \n",
       "97               3758.32                2205.60                         66.20   \n",
       "98               3531.91                2682.40                          8.81   \n",
       "99               1833.99                1736.60                         32.98   \n",
       "\n",
       "    AggregatedGapOfDemandMinusSpend  \n",
       "95                           -66.42  \n",
       "96                          -839.36  \n",
       "97                         -1552.72  \n",
       "98                          -849.51  \n",
       "99                           -97.39  "
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mean_df.tail()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 76356 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "# plot of agent activity per timestep\n",
    "plot_median_with_quantiles_annotation(subset,'timestep','timestep','AggregatedAgentSpend')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 70772 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles_annotation(subset,'timestep','timestep','VelocityOfMoney')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 18584 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','operatorCICBalance')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 16436 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','operatorFiatBalance')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11656 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','totalMinted')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 11664 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','totalBurned')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 65872 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','VelocityOfMoney')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 67060 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "first_five_plot(subset,'timestep','timestep','AggregatedAgentCICHolding',5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 37512 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles_annotation(subset,'timestep','timestep','AggregatedAgentCICHolding')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 32668 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','AggregatedAgentCurrencyHolding')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 80724 characters]\n",
      "text/plain": [
       "<Figure size 720x432 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    }
   ],
   "source": [
    "plot_median_with_quantiles(subset,'timestep','timestep','AggregatedAgentDemand')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
