cadCAD/demos/ThreeSidedMarket.ipynb

683 lines
25 KiB
Plaintext

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Three Sided Market Model"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"ename": "FileNotFoundError",
"evalue": "[Errno 2] No such file or directory: 'threesidedmarket.jpg'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mFileNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-3-a7e03cd78ede>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mIPython\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisplay\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mImage\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mImage\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;34m'threesidedmarket.jpg'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m/anaconda3/lib/python3.7/site-packages/IPython/core/display.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, data, url, filename, format, embed, width, height, retina, unconfined, metadata)\u001b[0m\n\u001b[1;32m 1149\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0munconfined\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0munconfined\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1150\u001b[0m super(Image, self).__init__(data=data, url=url, filename=filename, \n\u001b[0;32m-> 1151\u001b[0;31m metadata=metadata)\n\u001b[0m\u001b[1;32m 1152\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1153\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwidth\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmetadata\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'width'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/anaconda3/lib/python3.7/site-packages/IPython/core/display.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, data, url, filename, metadata)\u001b[0m\n\u001b[1;32m 607\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mmetadata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;34m{\u001b[0m\u001b[0;34m}\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 608\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 609\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 610\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_check_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 611\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/anaconda3/lib/python3.7/site-packages/IPython/core/display.py\u001b[0m in \u001b[0;36mreload\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 1180\u001b[0m \u001b[0;34m\"\"\"Reload the raw data from file or URL.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1181\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0membed\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m-> 1182\u001b[0;31m \u001b[0msuper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mImage\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreload\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 1183\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mretina\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 1184\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_retina_shape\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m/anaconda3/lib/python3.7/site-packages/IPython/core/display.py\u001b[0m in \u001b[0;36mreload\u001b[0;34m(self)\u001b[0m\n\u001b[1;32m 632\u001b[0m \u001b[0;34m\"\"\"Reload the raw data from file or URL.\"\"\"\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 633\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilename\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 634\u001b[0;31m \u001b[0;32mwith\u001b[0m \u001b[0mopen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mfilename\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_read_flags\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 635\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdata\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mf\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 636\u001b[0m \u001b[0;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0murl\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mFileNotFoundError\u001b[0m: [Errno 2] No such file or directory: 'threesidedmarket.jpg'"
]
}
],
"source": [
"from IPython.display import Image\n",
"Image(filename=threesidedmarket.jpeg)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from decimal import Decimal\n",
"from datetime import timedelta\n",
"import pandas as pd\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline\n",
"import pandas as pd\n",
"from tabulate import tabulate\n",
"\n",
"from __future__ import print_function\n",
"from ipywidgets import interact, interactive, fixed, interact_manual\n",
"import ipywidgets as widgets\n",
"from IPython.display import clear_output\n",
"\n",
"from SimCAD.configuration import Configuration\n",
"from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \\\n",
" ep_time_step\n",
"from SimCAD.engine import ExecutionMode, ExecutionContext, Executor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"sim_config = {\n",
" 'N': 1,\n",
" 'T': range(50)\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Environmental Processes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"eta = 1\n",
"def tx_volume_generator(step, sL, s, _input):\n",
" y = 'tx_volume'\n",
" x = eta*s['tx_volume']\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"alpha = .999\n",
"beta = 1.0\n",
"def cost_of_production_generator(step, sL, s, _input):\n",
" y = 'cost_of_production'\n",
" x = alpha*s['cost_of_production']+beta\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#log quadratic overhead model; parameters\n",
"a = 1.0\n",
"b = 1.0\n",
"c = 1.0\n",
"d = 0\n",
"def overhead_cost_generator(step, sL, s, _input):\n",
" #unit fiat\n",
" y = 'overhead_costs'\n",
" q = a+b*s['tx_volume']+c*s['volume_production']+d*s['tx_volume']*s['volume_production']\n",
" x = np.log(q)\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#State Variables"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"seed = {}\n",
"env_processes = {}\n",
"initial_condition = {\n",
" 'fiat_reserve': float(2000),#unit: fiat\n",
" 'token_reserve': float(2000),#unit: tok\n",
" 'token_supply': float(5000),#unit: tok\n",
" 'tx_volume': float(2000), #unit: fiat\n",
" 'txo_fiat': float(1000), #unit: fiat\n",
" 'txo_token': float(1000), #unit: tok\n",
" 'txi_fiat': float(1000), #unit: fiat\n",
" 'txi_token': float(1000), #unit: tok\n",
" 'conversion_rate': float(1), #unit: tok/fiat\n",
" 'cost_of_production': float(25), #unit: fiat/labor \n",
" 'volume_of_production': float(100), #unit: labor\n",
" 'timestamp': '2019-01-01 00:00:00'\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#exogenous states\n",
"ts_format = '%Y-%m-%d %H:%M:%S'\n",
"t_delta = timedelta(days=0, minutes=0, seconds=1)\n",
"def time_model(step, sL, s, _input):\n",
" y = 'timestamp'\n",
" x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta)\n",
" return (y, x)\n",
"\n",
"exogenous_states = exo_update_per_ts(\n",
" {\n",
" 'timestamp': time_model\n",
" }\n",
")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Behavoirs (two types: controlled and uncontrolled)\n",
"#user behavoir is uncontrolled (estimated or modeled),\n",
"#governance policies are controlled (enforced)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#governance decision ~ system policy for token/fiat unit of value conversion\n",
"def conversion_policy(step, sL, s):\n",
" ncr = 1 #placeholder logic: fixed value\n",
" return {'new_conversion_rate': ncr}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#governance decision ~ determines the conditions or schedule of new tokens minted\n",
"\n",
"final_supply = 10000.0 #unit: tokens\n",
"release_rate = .01 #percent of remaining\n",
"\n",
"def mint_policy(step, sL, s):\n",
" mint = s['token_supply']\n",
" return {'new_conversion_rate': ncr}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#these are uncontrollerd choices of users in the provider consumer\n",
"\n",
"def consumer_choice(step, sL, s):\n",
" #fiat paid by consumers\n",
" #note: balance of consumption vol covered in tokens (computed later)\n",
" txi_fiat= \n",
" return {'txi_fiat': txi_fiat}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#these are uncontrollerd choices of users in the provider role\n",
"\n",
"def provider_choice(step, sL, s):\n",
" #fiat claimed by providers\n",
" #note: balance of provided vol covered in tokens (computed later)\n",
" txo_fiat = \n",
" return {'txo_fiat': txo_fiat}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#these are uncontrollerd choices of users in the producer role\n",
"\n",
"roi_threshold = 1.1 #estimatable parameter\n",
"\n",
"def producer_choice(step, sL, s):\n",
" #ROI heuristic\n",
" # add or remove resources until roi_threshold hit\n",
" return {'delta_labor': delta_labor}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#governance decision ~ system policy for compensating producers\n",
"#consider transaction volume, labor committed and token reserve and supply to determine payout\n",
"\n",
"def producer_compensation_policy(step, sL, s):\n",
" tokens_paid = \n",
" return {'tokens_paid': tokens_paid}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#used to trigger mechanisms with no explicit input\n",
"def dummy_behavior(step, sL, s):\n",
" return {'value': 0}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#Mechanisms"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def update_conversion_rate(step, sL, s):\n",
" y = 'conversion_rate'\n",
" x = _input['new_conversion_rate']\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#minting process mints into the reserve\n",
"def mint1(step, sL, s):\n",
" y = 'token_supply'\n",
" x = s['token_supply'] + _input['mint']\n",
" return (y, x)\n",
"\n",
"def mint2(step, sL, s):\n",
" y = 'token_reserve'\n",
" x = s['token_reserve'] + _input['mint']\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def commit_delta_production(step, sL, s, _input):\n",
" y = 'volume_of_production'\n",
" x = s['volume_of_production']+_input['delta_production']\n",
" return (y, x)\n",
"\n",
"def compensate_production(step, sL, s, _input):\n",
" y = 'token_reserve'\n",
" x = s['token_reserve']-_input['tokens_paid']\n",
" return (y, x)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"conversion_fee = .01\n",
"\n",
"def capture_consumer_payments1(step, sL, s, _input):\n",
" #fiat inbound\n",
" y = 'fiat_reserve'\n",
" x = s['fiat_reserve']+_input['txi_fiat']\n",
" return (y, x)\n",
"\n",
"def capture_consumer_payments2(step, sL, s, _input):\n",
" #tokens inbound\n",
" y = 'token_reserve'\n",
" fiat_eq = s['tx_volume']-_input['txi_fiat']\n",
" x = s['token_reserve']+s['conversion_rate']*fiat_eq*(1.0+conversion_fee)\n",
" return (y, x)\n",
"\n",
"platform_fee = 0.03\n",
"\n",
"def compensate_providers1(step, sL, s, _input):\n",
" #fiat outbound\n",
" y = 'fiat_reserve'\n",
" x = s['fiat_reserve']-_input['txo_fiat']*(1.0-platform_fee)\n",
" return (y, x)\n",
"\n",
"def compensate_providers2(step, sL, s, _input):\n",
" #tokens outbound\n",
" y = 'token_reserve'\n",
" fiat_eq = s['tx_volume']-_input['txo_fiat']\n",
" x = s['token_reserve']-s['conversion_rate']*fiat_eq*(1.0-platform_fee-conversion_fee)\n",
" return (y, x)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#~~~~ break point (copy pasted started code below)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Behaviors choose actions depending on states\n",
"# 1) increase contact rate, beta (create incentive to spread)\n",
"# 2) decrease recover rate, gamma (improve stickiness)\n",
"\n",
"def add_incentive(step, sL, s):\n",
" incentive_allocation_as_share_of_budget = 0.02\n",
" target_beta = .5\n",
" potential_spend = s['Budget']*incentive_allocation_as_share_of_budget\n",
" \n",
" potential_delta = target_beta-s['beta']\n",
" \n",
" cost_of_potential_delta = potential_delta * incentive_cost * s['Ps']\n",
" if cost_of_potential_delta <= potential_spend:\n",
" delta = potential_delta\n",
" else:\n",
" delta = potential_spend/(incentive_cost * s['Ps'])\n",
"\n",
" return {'delta': delta}\n",
"\n",
"def add_stickiness(step, sL, s):\n",
" stickiness_allocation_as_share_of_budget = 0.08\n",
" target_gamma = .1\n",
" potential_spend = s['Budget']*stickiness_allocation_as_share_of_budget\n",
" \n",
" potential_delta = s['gamma']-target_gamma\n",
" \n",
" cost_of_potential_delta = potential_delta * stickiness_cost * s['Pi']\n",
" if cost_of_potential_delta <= potential_spend:\n",
" delta = potential_delta\n",
" else:\n",
" delta = potential_spend/(stickiness_cost * s['Pi'])\n",
" \n",
" \n",
" return {'delta': delta}\n",
"\n",
"# def add_stickiness(step, sL, s):\n",
"# delta = 0.0\n",
"# potential_delta = s['gamma'] * delta_gamma\n",
"# if (s['Pr'] > 2 * s['Pi']) and s['Budget'] > \\\n",
"# abs(potential_delta * stickiness_cost * s['Pi']):\n",
"# delta = potential_delta\n",
"# return {'delta': delta}\n",
"\n",
"# def add_incentive(step, sL, s):\n",
"# delta = 0.0\n",
"# potential_delta = s['beta'] * delta_beta\n",
"# if (s['Ps'] > 3 * s['Pi']) and s['Budget'] > \\\n",
"# abs(potential_delta * incentive_cost * s['Ps']):\n",
"# delta = potential_delta\n",
"# return {'delta': delta}\n",
"\n",
"def dummy_behavior(step, sL, s):\n",
" return {'delta': 0.0}\n",
"\n",
"# Mechanisms incur cost to modify beta or gamma\n",
"# 1) incur cost to create incentive to spread\n",
"# 2) incur cost to improve stickiness\n",
"\n",
"def incur_incentive_cost(step, sL, s, _input):\n",
" y = 'Budget'\n",
" x = s['Budget'] - abs(_input['delta'] * s['Ps'] * incentive_cost)\n",
" return (y, x)\n",
"\n",
"def incur_stickiness_cost(step, sL, s, _input):\n",
" y = 'Budget'\n",
" x = s['Budget'] - abs(_input['delta'] * s['Pi'] * stickiness_cost)\n",
" return (y, x)\n",
"\n",
"def update_beta(step, sL, s, _input):\n",
" y = 'beta'\n",
" x = s['beta'] + _input['delta']\n",
" return (y, x)\n",
"\n",
"def update_gamma(step, sL, s, _input):\n",
" y = 'gamma'\n",
" x = s['gamma'] - _input['delta']\n",
" return (y, x)\n",
"\n",
"def S_model(step, sL, s, _input):\n",
" y = 'Ps'\n",
" x = s['Ps'] - s['beta'] * s['Ps']\n",
" return (y, x)\n",
"\n",
"def I_model(step, sL, s, _input):\n",
" y = 'Pi'\n",
" x = s['Pi'] + s['beta'] * s['Ps'] - s['gamma'] * s['Pi']\n",
" return (y, x)\n",
" \n",
"def R_model(step, sL, s, _input):\n",
" y = 'Pr'\n",
" x = s['Pr'] + s['gamma'] * s['Pi']\n",
" return (y, x)\n",
"\n",
"def collect_subscription(step, sL, s, _input):\n",
" y = 'Budget'\n",
" x = s['Budget'] + s['Pi'] * epsilon * subscription_fee\n",
" return (y, x)\n",
"\n",
"def incentive_degrade(step, sL, s, _input):\n",
" y = 'beta'\n",
" x = s['beta']*(1-incentive_degredation_rate)\n",
" return (y, x)\n",
"\n",
"def stickiness_degrade(step, sL, s, _input):\n",
" y = 'gamma'\n",
" x = (s['gamma']+stickiness_degredation_rate)/(1+stickiness_degredation_rate)\n",
" return (y, x)\n",
"\n",
"mechanisms = {\n",
" 'spread': {\n",
" 'behaviors': {\n",
" 'dummy': dummy_behavior\n",
" },\n",
" 'states': {\n",
" 'Ps': S_model,\n",
" 'Pi': I_model,\n",
" 'Pr': R_model,\n",
" 'Budget': collect_subscription,\n",
" 'beta': incentive_degrade,\n",
" 'gamma': stickiness_degrade \n",
" } \n",
" },\n",
" 'create_incentive': {\n",
" 'behaviors': {\n",
" 'action': add_incentive,\n",
" },\n",
" 'states': {\n",
" 'beta': update_beta,\n",
" 'Budget': incur_incentive_cost,\n",
" }\n",
" },\n",
" 'improve_stickiness': {\n",
" 'behaviors': {\n",
" 'action': add_stickiness\n",
" },\n",
" 'states': {\n",
" 'gamma': update_gamma,\n",
" 'Budget': incur_stickiness_cost,\n",
" }\n",
" }\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def widget_handler(beta=float(0.05), gamma=float(0.20),\n",
" subscription_fee=float(1.0), \n",
" incentive_cost=float(10.0), \n",
" stickiness_cost=float(5.0)):\n",
" initial_condition['beta'] = beta\n",
" initial_condition['gamma'] = gamma\n",
" subscription_fee = subscription_fee\n",
" incentive_cost = incentive_cost\n",
" stickiness_cost = stickiness_cost\n",
" \n",
" config = Configuration(\n",
" sim_config=sim_config,\n",
" state_dict=initial_condition,\n",
" seed=seed,\n",
" exogenous_states=exogenous_states,\n",
" env_processes=env_processes,\n",
" mechanisms=mechanisms)\n",
"\n",
" exec_mode = ExecutionMode()\n",
" exec_context = ExecutionContext(exec_mode.single_proc)\n",
" executor = Executor(exec_context, [config]) # Pass the configuration object inside an array\n",
" raw_result, tensor = executor.main()\n",
" df = pd.DataFrame(raw_result)\n",
" df['timestamp'] = pd.to_datetime(df['timestamp'], format=ts_format)\n",
" \n",
" fig = plt.figure(figsize=(7, 14))\n",
" \n",
" sir = fig.add_subplot(3, 1, 1)\n",
" sir.plot('timestamp', 'Ps', data=df, marker='', color='C0', linewidth=2)\n",
" sir.plot('timestamp', 'Pi', data=df, marker='', color='orange', linewidth=2)\n",
" sir.plot('timestamp', 'Pr', data=df, marker='', color='green', linewidth=2)\n",
" sir.legend()\n",
" \n",
" beta_gamma = fig.add_subplot(3, 1, 2)\n",
" beta_gamma.plot('timestamp', 'beta', data=df, marker='', color='C0', linewidth=2)\n",
" beta_gamma.plot('timestamp', 'gamma', data=df, marker='', color='orange', linewidth=2)\n",
" beta_gamma.legend()\n",
" \n",
" budget_pi = fig.add_subplot(3, 1, 3)\n",
" budget_pi.plot('timestamp', 'Budget', data=df, marker='', color='C0', linewidth=2)\n",
" budget_pi.plot('timestamp', 'Pi', data=df, marker='', color='orange', linewidth=2)\n",
" budget_pi.legend()\n",
" \n",
" plt.show()\n",
" \n",
"sliders = interact_manual(widget_handler, \n",
" beta=(0, 1, 0.01),\n",
" gamma=(0, 1, 0.01),\n",
" subscription_fee=(0, 10, 0.1),\n",
" incentive_cost=(0, 20, 0.5),\n",
" stickiness_cost=(0, 20, 0.5)\n",
" )\n",
"sliders"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"config = Configuration(\n",
" sim_config=sim_config,\n",
" state_dict=initial_condition,\n",
" seed=seed,\n",
" exogenous_states=exogenous_states,\n",
" env_processes=env_processes,\n",
" mechanisms=mechanisms)\n",
"\n",
"from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
"exec_mode = ExecutionMode()\n",
"exec_context = ExecutionContext(exec_mode.single_proc)\n",
"executor = Executor(exec_context, [config]) # Pass the configuration object inside an array\n",
"raw_result, tensor = executor.main()\n",
"df = pd.DataFrame(raw_result)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.plot('timestamp', ['Ps','Pi', 'Pr'])\n",
"df.plot('timestamp', ['beta', 'gamma'])\n",
"df.plot('timestamp', ['Budget', 'Pi'])\n",
"df[['Ps','Pi', 'Pr']].describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.beta.diff().describe()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.beta.diff().plot()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}