Barlin Cleanup
This commit is contained in:
parent
dd0b50faf8
commit
7a84af853f
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
|
|
@ -0,0 +1,482 @@
|
|||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## SimCAD Application Notebook\n",
|
||||
"## Experiment Type 2"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"### Name of Config File or System Description\n",
|
||||
"#### 20 MonteCarlo Runs \n",
|
||||
"#### Behaviors: EMHers, Herders, HODLers, EIUers, and Human EIUers"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"Simulation Run 1\n",
|
||||
"single_proc: [<SimCAD.Configuration object at 0x000001EA1AAA6630>]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "unsupported operand type(s) for *: 'float' and 'decimal.Decimal'",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[1;32m<ipython-input-1-0d9ea96d7f5c>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 15\u001b[0m \u001b[0msingle_proc_ctx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mExecutionContext\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mexec_mode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msingle_proc\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 16\u001b[0m \u001b[0mrun1\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mExecutor\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msingle_proc_ctx\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msingle_config\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 17\u001b[1;33m \u001b[0mrun1_raw_result\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mrun1\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 18\u001b[0m \u001b[0mdf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpd\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mDataFrame\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mrun1_raw_result\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[1;31m# print(tabulate(result, headers='keys', tablefmt='psql'))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\__init__.py\u001b[0m in \u001b[0;36mexecute\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexec_context\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mExecutionMode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msingle_proc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 73\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0msingle_proc_exec\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulation_execs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_lists\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs_structs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mTs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mNs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 74\u001b[0m \u001b[1;32melif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexec_context\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mExecutionMode\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmulti_proc\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 75\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfigs\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m>\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\__init__.py\u001b[0m in \u001b[0;36msingle_proc_exec\u001b[1;34m(simulation_execs, states_lists, configs_structs, env_processes_list, Ts, Ns)\u001b[0m\n\u001b[0;32m 67\u001b[0m \u001b[0msimulation\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mN\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlist\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmap\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ml\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 68\u001b[0m \u001b[1;31m# print(states_list)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 69\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0msimulation\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mN\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 70\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mflatten\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mresult\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 71\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36msimulation\u001b[1;34m(self, states_list, configs, env_processes, time_seq, runs)\u001b[0m\n\u001b[0;32m 100\u001b[0m \u001b[1;31m# print(\"Run: \"+str(run))\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 101\u001b[0m \u001b[0mstates_list_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdeepcopy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;31m# WHY ???\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 102\u001b[1;33m \u001b[0mhead\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mtail\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpipe\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mstates_list_copy\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 103\u001b[0m \u001b[0mgenesis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhead\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 104\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'mech_step'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'time_step'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mgenesis\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'run'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mpipe\u001b[1;34m(self, states_list, configs, env_processes, time_seq, run)\u001b[0m\n\u001b[0;32m 86\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mtime_step\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mtime_seq\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 87\u001b[0m \u001b[1;31m# print(run)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 88\u001b[1;33m \u001b[0mpipe_run\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mblock_gen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0msimulation_list\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtime_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 89\u001b[0m \u001b[0m_\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m*\u001b[0m\u001b[0mpipe_run\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mpipe_run\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 90\u001b[0m \u001b[0msimulation_list\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpipe_run\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mblock_gen\u001b[1;34m(self, states_list, configs, env_processes, t_step, run)\u001b[0m\n\u001b[0;32m 72\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mconfig\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mconfigs\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 73\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb_conf\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfig\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 74\u001b[1;33m \u001b[0mstates_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmech_step\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mstates_list\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0ms_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mb_conf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0menv_processes\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mt_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mrun\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 75\u001b[0m \u001b[0mm_step\u001b[0m \u001b[1;33m+=\u001b[0m \u001b[1;36m1\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 76\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mmech_step\u001b[1;34m(self, m_step, sL, state_funcs, behavior_funcs, env_processes, t_step, run)\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;31m# *** add env_proc value here as wrapper function ***\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mlast_in_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mstate_funcs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36m<listcomp>\u001b[1;34m(.0)\u001b[0m\n\u001b[0;32m 42\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 43\u001b[0m \u001b[1;31m# *** add env_proc value here as wrapper function ***\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 44\u001b[1;33m \u001b[0mlast_in_copy\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mf\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mstate_funcs\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 45\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 46\u001b[0m \u001b[1;32mfor\u001b[0m \u001b[0mk\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mlast_in_obj\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\SimCAD\\engine\\simulation.py\u001b[0m in \u001b[0;36mexception_handler\u001b[1;34m(self, f, m_step, sL, last_mut_obj, _input)\u001b[0m\n\u001b[0;32m 28\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mexception_handler\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_mut_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 29\u001b[0m \u001b[1;32mtry\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 30\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mf\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mm_step\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0msL\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mlast_mut_obj\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0m_input\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 31\u001b[0m \u001b[1;32mexcept\u001b[0m \u001b[0mKeyError\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 32\u001b[0m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m\"Exception\"\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;32m~\\staging\\21f1155\\sandboxUX\\config6b.py\u001b[0m in \u001b[0;36ms2m3\u001b[1;34m(step, sL, s, _input)\u001b[0m\n\u001b[0;32m 179\u001b[0m \u001b[0my\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'Price'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 180\u001b[0m \u001b[1;31m#var1 = Decimal.from_float(s['Buy_Log'])\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 181\u001b[1;33m \u001b[0mx\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Price'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Buy_Log'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m/\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Z'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m-\u001b[0m \u001b[1;36m0.1\u001b[0m \u001b[1;33m*\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Sell_Log'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Z'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Buy_Log'\u001b[0m\u001b[1;33m]\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0ms\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;34m'Sell_Log'\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 182\u001b[0m \u001b[1;31m#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 183\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0my\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
|
||||
"\u001b[1;31mTypeError\u001b[0m: unsupported operand type(s) for *: 'float' and 'decimal.Decimal'"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"import pandas as pd\n",
|
||||
"from tabulate import tabulate\n",
|
||||
"\n",
|
||||
"from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
|
||||
"from sandboxUX import config6b #, config2\n",
|
||||
"from SimCAD import configs\n",
|
||||
"\n",
|
||||
"# ToDo: pass ExecutionContext with execution method as ExecutionContext input\n",
|
||||
"\n",
|
||||
"exec_mode = ExecutionMode()\n",
|
||||
"\n",
|
||||
"print(\"Simulation Run 1\")\n",
|
||||
"# print()\n",
|
||||
"single_config = [configs[0]]\n",
|
||||
"single_proc_ctx = ExecutionContext(exec_mode.single_proc)\n",
|
||||
"run1 = Executor(single_proc_ctx, single_config)\n",
|
||||
"run1_raw_result = run1.main()\n",
|
||||
"df = pd.DataFrame(run1_raw_result)\n",
|
||||
"# print(tabulate(result, headers='keys', tablefmt='psql'))\n",
|
||||
"# print()\n",
|
||||
"\n",
|
||||
"# print(\"Simulation Run 2: Pairwise Execution\")\n",
|
||||
"# print()\n",
|
||||
"# multi_proc_ctx = ExecutionContext(exec_mode.multi_proc)\n",
|
||||
"# run2 = Executor(multi_proc_ctx, configs)\n",
|
||||
"# run2_raw_results = run2.main()\n",
|
||||
"# for raw_result in run2_raw_results:\n",
|
||||
"# result = pd.DataFrame(raw_result)\n",
|
||||
"# print(tabulate(result, headers='keys', tablefmt='psql'))\n",
|
||||
"# print()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#df = pd.DataFrame(run1_raw_result)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# Standard Library Imports\n",
|
||||
"#import pandas as pd\n",
|
||||
"import numpy as np\n",
|
||||
"import matplotlib as mpl\n",
|
||||
"import matplotlib.pyplot as plt\n",
|
||||
"import seaborn as sns\n",
|
||||
"#from tabulate import tabulate\n",
|
||||
"\n",
|
||||
"sns.set_style('whitegrid')\n",
|
||||
"\n",
|
||||
"%matplotlib inline"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# SimCAD Specific\n",
|
||||
"# from SimCAD.engine import ExecutionMode, ExecutionContext, Executor\n",
|
||||
"# from sandboxUX import config1 , config2\n",
|
||||
"# from SimCAD import configs"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Convert data type of output to float. MPL works OK with strings, seaborn does not\n",
|
||||
"names = df.keys()[:-3] # [:-3] only affects state variables\n",
|
||||
"for n in names:\n",
|
||||
" df[n]=df[n].apply(float)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"#Check\n",
|
||||
"df.head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df.iloc[2995:3005]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df.tail(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df.corr()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"aggregate_dimension = 'time_step'\n",
|
||||
"\n",
|
||||
"mean_df = df.groupby(aggregate_dimension).mean().reset_index()\n",
|
||||
"median_df = df.groupby(aggregate_dimension).median().reset_index()\n",
|
||||
"std_df = df.groupby(aggregate_dimension).std().reset_index()\n",
|
||||
"min_df = df.groupby(aggregate_dimension).min().reset_index()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"mean_df.head(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"scrolled": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"mean_df.tail(10)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def dist_plot(x, y,lx=False,ly=False, suppMin=False): \n",
|
||||
" plt.figure(figsize=(12,8))\n",
|
||||
" if not(suppMin):\n",
|
||||
" plt.plot(mean_df[x].values, mean_df[y].values,\n",
|
||||
" mean_df[x].values,median_df[y].values,\n",
|
||||
" mean_df[x].values,mean_df[y].values+std_df[y].values,\n",
|
||||
" mean_df[x].values,min_df[y].values)\n",
|
||||
" plt.legend(['mean', 'median', 'mean+ 1*std', 'min'],bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\n",
|
||||
" \n",
|
||||
" else:\n",
|
||||
" plt.plot(mean_df[x].values, mean_df[y].values,\n",
|
||||
" mean_df[x].values,median_df[y].values,\n",
|
||||
" mean_df[x].values,mean_df[y].values+std_df[y].values,\n",
|
||||
" mean_df[x].values,mean_df[y].values-std_df[y].values)\n",
|
||||
" plt.legend(['mean', 'median', 'mean+ 1*std', 'mean - 1*std'],bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)\n",
|
||||
"\n",
|
||||
" plt.xlabel(x)\n",
|
||||
" plt.ylabel(y)\n",
|
||||
" if lx:\n",
|
||||
" plt.xscale('log')\n",
|
||||
" \n",
|
||||
" if ly:\n",
|
||||
" plt.yscale('log')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dist_plot('time_step', 'P_Ext_Markets',suppMin=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"dist_plot('time_step', 'Price',suppMin=True)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(mean_df['time_step'][1:],mean_df['Price'][1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
|
||||
"plt.plot(mean_df['time_step'][1:],(1/250)*mean_df['P_Ext_Markets'][1:])\n",
|
||||
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
|
||||
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"print(np.std(mean_df))"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(mean_df['time_step'][1:],mean_df['Buy_Log'][1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
|
||||
"plt.plot(mean_df['time_step'][1:],mean_df['Sell_Log'][1:])\n",
|
||||
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
|
||||
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"buy_delta = mean_df['Buy_Log'].diff()\n",
|
||||
"sell_delta = mean_df['Sell_Log'].diff()\n",
|
||||
"ext_delta = mean_df['P_Ext_Markets'].diff()\n",
|
||||
"# df_delta['Buy_Log'] = buy_delta\n",
|
||||
"# df_delta['Sell_Log'] = sell_delta\n",
|
||||
"# df_delta = df_delta.append(ext_delta)\n",
|
||||
"# df_delta.head()\n",
|
||||
"sell_delta.head(20)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(mean_df['time_step'][1:],buy_delta[1:]) #, df['Zeus_LT']], figsize=(15,10)) #, logy=True)\n",
|
||||
"plt.plot(mean_df['time_step'][1:],sell_delta[1:])\n",
|
||||
"plt.plot(mean_df['time_step'][1:],ext_delta[1:])\n",
|
||||
"plt.ylim(-400,400)\n",
|
||||
"#plt.plot(df['time_step'],df['Zeus_LT'])\n",
|
||||
"plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"sns.pairplot(mean_df)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(mean_df['time_step'],mean_df['Z']/mean_df['P_Ext_Markets'])\n",
|
||||
"plt.title('Z per External Stock Market Price')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# plt.figure(figsize=(12,8))\n",
|
||||
"# plt.plot(df['time_step'],(df['TDR_Int']-df['TDR_Ext'])/df['TDR_Ext'])\n",
|
||||
"# plt.title('Availability of TDR arbitrage opportunity')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# plt.figure(figsize=(12,8))\n",
|
||||
"# plt.plot(df['time_step'],(df['Zeus_LT']/df['Zeus_ST']-1))\n",
|
||||
"# plt.title('Availability of LT vs ST arbitrage opportunity')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# vol_df = df.rolling(window = 21).mean()\n",
|
||||
"vol_df = pd.DataFrame()\n",
|
||||
"rolling_days = 63 # days = number * mechanisms\n",
|
||||
"for n in names:\n",
|
||||
" vol_df[n] = mean_df[n].rolling(rolling_days).mean().shift()\n",
|
||||
" \n",
|
||||
"vol_df = vol_df.dropna() #(vol_df.iloc[0:rolling_days])\n",
|
||||
"# vol_df[n].iloc[:rolling_days], axis=1)\n",
|
||||
"vol_df.head()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(vol_df['Z'])\n",
|
||||
"plt.title('Rolling Average of Z')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(vol_df['P_Ext_Markets'])\n",
|
||||
"plt.title('Rolling Average of External Stock Market Price')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"plt.figure(figsize=(12,8))\n",
|
||||
"plt.plot(vol_df['Price'])\n",
|
||||
"plt.plot(vol_df['P_Ext_Markets']/250)\n",
|
||||
"plt.legend()\n",
|
||||
"plt.title('Rolling Average of Zeus Price')"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[\"Price\"].min()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"df[\"Price\"].max()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"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.6.5"
|
||||
}
|
||||
},
|
||||
"nbformat": 4,
|
||||
"nbformat_minor": 2
|
||||
}
|
||||
|
|
@ -0,0 +1,265 @@
|
|||
from decimal import Decimal
|
||||
import numpy as np
|
||||
|
||||
from SimCAD import Configuration, configs
|
||||
from SimCAD.utils.configuration import exo_update_per_ts, proc_trigger, bound_norm_random, \
|
||||
ep_time_step
|
||||
|
||||
seed = {
|
||||
'z': np.random.RandomState(1)
|
||||
}
|
||||
|
||||
# Signals
|
||||
# Pr_signal
|
||||
beta = Decimal('0.25') # agent response gain
|
||||
beta_LT = Decimal('0.1') # LT agent response gain
|
||||
# alpha = .67, 2 block moving average
|
||||
alpha = Decimal('0.67') # 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
|
||||
max_withdraw_factor = Decimal('0.9')
|
||||
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
|
||||
|
||||
|
||||
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
|
||||
|
||||
# Stochastic process factors
|
||||
correction_factor = Decimal('0.01')
|
||||
volatility = Decimal('5.0')
|
||||
|
||||
# Buy_Log_signal =
|
||||
# Z_signal =
|
||||
# Price_signal =
|
||||
# TDR_draw_signal =
|
||||
# P_Ext_Markets_signal =
|
||||
|
||||
# Behaviors per Mechanism
|
||||
|
||||
# BEHAVIOR 1: EMH Trader
|
||||
EMH_portion = Decimal('0.250')
|
||||
EMH_Ext_Hold = Decimal('42000.0')
|
||||
|
||||
|
||||
def b1m1(step, sL, s):
|
||||
# print('b1m1')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
return {'buy_order1': buy}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'buy_order1': 0}
|
||||
else:
|
||||
return {'buy_order1': 0}
|
||||
|
||||
|
||||
def b1m2(step, sL, s):
|
||||
# print('b1m2')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'sell_order1': 0}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
return {'sell_order1': sell}
|
||||
else:
|
||||
return {'sell_order1': 0}
|
||||
|
||||
# BEHAVIOR 3: Herding
|
||||
Herd_portion = Decimal('0.250')
|
||||
Herd_Ext_Hold = Decimal('42000.0')
|
||||
Herd_UB = Decimal('0.10') # UPPER BOUND
|
||||
Herd_LB = Decimal('0.10') # LOWER BOUND
|
||||
def b3m2(step, sL, s):
|
||||
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
|
||||
|
||||
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
return {'herd_sell': sell, 'herd_buy': 0}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
|
||||
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
return {'herd_sell': 0, 'herd_buy': buy}
|
||||
else:
|
||||
return {'herd_sell': 0, 'herd_buy': 0}
|
||||
|
||||
# BEHAVIOR 4: HODLers
|
||||
HODL_belief = Decimal('10.0')
|
||||
HODL_portion = Decimal('0.250')
|
||||
HODL_Ext_Hold = Decimal('4200.0')
|
||||
|
||||
|
||||
def b4m2(step, sL, s):
|
||||
# print('b4m2')
|
||||
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
|
||||
return {'sell_order2': sell}
|
||||
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
return {'sell_order2': 0}
|
||||
else:
|
||||
return {'sell_order2': 0}
|
||||
|
||||
# BEHAVIOR 7: Endogenous Information Updating (EIU)
|
||||
EIU_portion = Decimal('0.250')
|
||||
EIU_Ext_Hold = Decimal('42000.0')
|
||||
EIU_UB = Decimal('0.50') # UPPER BOUND
|
||||
EIU_LB = Decimal('0.50') # LOWER BOUND
|
||||
def b7m2(step, sL, s):
|
||||
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
|
||||
|
||||
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
|
||||
return {'EIU_sell': sell, 'EIU_buy': 0}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
|
||||
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
|
||||
return {'EIU_sell': 0, 'EIU_buy': buy}
|
||||
else:
|
||||
return {'EIU_sell': 0, 'EIU_buy': 0}
|
||||
|
||||
# STATES
|
||||
# ZEUS Fixed Supply
|
||||
def s1m1(step, sL, s, _input):
|
||||
y = 'Z'
|
||||
x = s['Z'] #+ _input # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s2m1(step, sL, s, _input):
|
||||
# y = 'Price'
|
||||
# x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
|
||||
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
# return (y, x)
|
||||
|
||||
|
||||
def s3m1(step, sL, s, _input):
|
||||
y = 'Buy_Log'
|
||||
x = _input['buy_order1'] + _input['herd_buy'] + _input['EIU_buy'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
def s4m2(step, sL, s, _input):
|
||||
y = 'Sell_Log'
|
||||
x = _input['sell_order1'] + _input['sell_order2'] + _input['herd_sell'] + _input['EIU_sell'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s3m3(step, sL, s, _input):
|
||||
# y = 'Buy_Log'
|
||||
# x = s['Buy_Log'] + _input # / Psignal_int
|
||||
# return (y, x)
|
||||
|
||||
|
||||
# Price Update
|
||||
def s2m3(step, sL, s, _input):
|
||||
|
||||
y = 'Price'
|
||||
#var1 = Decimal.from_float(s['Buy_Log'])
|
||||
x = s['Price'] + s['Buy_Log'] /s['Z'] /(Decimal('0.10') * s['Price']) - s['Sell_Log'] / s['Z'] / (Decimal('0.10')*s['Price'])
|
||||
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
def s5m3(step, sL, s, _input):
|
||||
y = 'Price_Signal'
|
||||
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
|
||||
return (y, x)
|
||||
|
||||
def s6m1(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] - _input
|
||||
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
return (y, x)
|
||||
|
||||
|
||||
def s2m2(step, sL, s, _input):
|
||||
y = 'Price'
|
||||
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
|
||||
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
return (y, x)
|
||||
|
||||
# Exogenous States
|
||||
proc_one_coef_A = -125
|
||||
proc_one_coef_B = 125
|
||||
|
||||
# A change in belief of actual price, passed onto behaviors to make action
|
||||
def es4p2(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
|
||||
|
||||
return (y,x)
|
||||
|
||||
|
||||
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
|
||||
y = 'timestamp'
|
||||
x = ep_time_step(s, s['timestamp'], seconds=1)
|
||||
return (y, x)
|
||||
|
||||
#Environment States
|
||||
# NONE
|
||||
|
||||
# Genesis States
|
||||
state_dict = {
|
||||
'Z': Decimal(21000000.0),
|
||||
'Price': Decimal(100.0), # Initialize = Z for EMA
|
||||
'Buy_Log': Decimal(0.0),
|
||||
'Sell_Log': Decimal(0.0),
|
||||
'Price_Signal': Decimal(100.0),
|
||||
'Trans': Decimal(0.0),
|
||||
'P_Ext_Markets': Decimal(25000.0),
|
||||
'timestamp': '2018-10-01 15:16:24'
|
||||
}
|
||||
|
||||
def env_proc_id(x):
|
||||
return x
|
||||
|
||||
env_processes = {
|
||||
# "P_Ext_Markets": env_proc_id
|
||||
}
|
||||
|
||||
exogenous_states = exo_update_per_ts(
|
||||
{
|
||||
"P_Ext_Markets": es4p2,
|
||||
"timestamp": es5p2
|
||||
}
|
||||
)
|
||||
|
||||
sim_config = {
|
||||
"N": 100,
|
||||
"T": range(1000)
|
||||
}
|
||||
|
||||
# test return vs. non-return functions as lambdas
|
||||
# test fully defined functions
|
||||
mechanisms = {
|
||||
"m1": {
|
||||
"behaviors": {
|
||||
"b1": b1m1,
|
||||
"b3": b3m2,
|
||||
"b7": b7m2
|
||||
},
|
||||
"states": {
|
||||
"Z": s1m1,
|
||||
"Buy_Log": s3m1
|
||||
}
|
||||
},
|
||||
"m2": {
|
||||
"behaviors": {
|
||||
"b1": b1m2,
|
||||
"b3": b3m2,
|
||||
"b4": b4m2,
|
||||
"b7": b7m2
|
||||
},
|
||||
"states": {
|
||||
"Sell_Log": s4m2
|
||||
}
|
||||
},
|
||||
"m3": {
|
||||
"behaviors": {
|
||||
},
|
||||
"states": {
|
||||
"Price": s2m3,
|
||||
"Price_Signal": s5m3
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))
|
||||
|
|
@ -0,0 +1,298 @@
|
|||
from decimal import Decimal
|
||||
import numpy as np
|
||||
|
||||
from SimCAD import Configuration, configs
|
||||
from SimCAD.utils.configuration import exo_update_per_ts, proc_trigger, bound_norm_random, \
|
||||
ep_time_step
|
||||
|
||||
seed = {
|
||||
'z': np.random.RandomState(1)
|
||||
}
|
||||
|
||||
# Signals
|
||||
# Pr_signal
|
||||
beta = Decimal('0.25') # agent response gain
|
||||
beta_LT = Decimal('0.1') # LT agent response gain
|
||||
# alpha = .67, 2 block moving average
|
||||
alpha = Decimal('0.67')
|
||||
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
|
||||
# 21 * 3 mech steps, 2/64 = 0.03125
|
||||
alpha_2 = Decimal('0.03125')
|
||||
max_withdraw_factor = Decimal('0.9')
|
||||
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
|
||||
|
||||
|
||||
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
|
||||
|
||||
# Stochastic process factors
|
||||
correction_factor = Decimal('0.01')
|
||||
volatility = Decimal('5.0')
|
||||
|
||||
# Buy_Log_signal =
|
||||
# Z_signal =
|
||||
# Price_signal =
|
||||
# TDR_draw_signal =
|
||||
# P_Ext_Markets_signal =
|
||||
|
||||
# Behaviors per Mechanism
|
||||
|
||||
# BEHAVIOR 1: EMH Trader
|
||||
EMH_portion = Decimal('0.20')
|
||||
EMH_Ext_Hold = Decimal('42000.0')
|
||||
|
||||
|
||||
def b1m1(step, sL, s):
|
||||
# print('b1m1')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
return {'buy_order1': buy}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'buy_order1': 0}
|
||||
else:
|
||||
return {'buy_order1': 0}
|
||||
|
||||
|
||||
def b1m2(step, sL, s):
|
||||
# print('b1m2')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'sell_order1': 0}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
return {'sell_order1': sell}
|
||||
else:
|
||||
return {'sell_order1': 0}
|
||||
|
||||
# BEHAVIOR 3: Herding
|
||||
Herd_portion = Decimal('0.20')
|
||||
Herd_Ext_Hold = Decimal('42000.0')
|
||||
Herd_UB = Decimal('0.10') # UPPER BOUND
|
||||
Herd_LB = Decimal('0.10') # LOWER BOUND
|
||||
def b3m2(step, sL, s):
|
||||
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
|
||||
|
||||
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
return {'herd_sell': sell, 'herd_buy': 0}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
|
||||
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
return {'herd_sell': 0, 'herd_buy': buy}
|
||||
else:
|
||||
return {'herd_sell': 0, 'herd_buy': 0}
|
||||
|
||||
# BEHAVIOR 4: HODLers
|
||||
HODL_belief = Decimal('10.0')
|
||||
HODL_portion = Decimal('0.20')
|
||||
HODL_Ext_Hold = Decimal('4200.0')
|
||||
|
||||
|
||||
def b4m2(step, sL, s):
|
||||
# print('b4m2')
|
||||
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
|
||||
return {'sell_order2': sell}
|
||||
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
return {'sell_order2': 0}
|
||||
else:
|
||||
return {'sell_order2': 0}
|
||||
|
||||
# BEHAVIOR 7: Endogenous Information Updating (EIU)
|
||||
# Short Term Price Signal, Lower Threshold = BOT-like
|
||||
EIU_portion = Decimal('0.20')
|
||||
EIU_Ext_Hold = Decimal('42000.0')
|
||||
EIU_UB = Decimal('0.50') # UPPER BOUND
|
||||
EIU_LB = Decimal('0.50') # LOWER BOUND
|
||||
def b7m2(step, sL, s):
|
||||
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
|
||||
|
||||
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
|
||||
return {'EIU_sell': sell, 'EIU_buy': 0}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
|
||||
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
|
||||
return {'EIU_sell': 0, 'EIU_buy': buy}
|
||||
else:
|
||||
return {'EIU_sell': 0, 'EIU_buy': 0}
|
||||
|
||||
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
|
||||
# Longer Term Price Signal, Higher Threshold = Human-Like
|
||||
HEIU_portion = Decimal('0.20')
|
||||
HEIU_Ext_Hold = Decimal('42000.0')
|
||||
HEIU_UB = Decimal('2.0') # UPPER BOUND
|
||||
HEIU_LB = Decimal('2.0') # LOWER BOUND
|
||||
def b7hm2(step, sL, s):
|
||||
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
|
||||
|
||||
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
|
||||
return {'HEIU_sell': sell, 'HEIU_buy': 0}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
|
||||
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
|
||||
return {'HEIU_sell': 0, 'HEIU_buy': buy}
|
||||
else:
|
||||
return {'HEIU_sell': 0, 'HEIU_buy': 0}
|
||||
|
||||
# STATES
|
||||
# ZEUS Fixed Supply
|
||||
def s1m1(step, sL, s, _input):
|
||||
y = 'Z'
|
||||
x = s['Z'] #+ _input # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s2m1(step, sL, s, _input):
|
||||
# y = 'Price'
|
||||
# x = (s['P_Ext_Markets'] - _input['buy_order1']) / s['Z'] * 10000
|
||||
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
# return (y, x)
|
||||
|
||||
|
||||
def s3m1(step, sL, s, _input):
|
||||
y = 'Buy_Log'
|
||||
x = _input['buy_order1'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
def s4m2(step, sL, s, _input):
|
||||
y = 'Sell_Log'
|
||||
x = _input['sell_order1'] + _input['sell_order2'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s3m3(step, sL, s, _input):
|
||||
# y = 'Buy_Log'
|
||||
# x = s['Buy_Log'] + _input # / Psignal_int
|
||||
# return (y, x)
|
||||
|
||||
|
||||
# Price Update
|
||||
def s2m3(step, sL, s, _input):
|
||||
|
||||
y = 'Price'
|
||||
#var1 = Decimal.from_float(s['Buy_Log'])
|
||||
x = s['Price'] + s['Buy_Log'] /s['Z']/(Decimal('1.25') ) - s['Sell_Log']/s['Z']/(Decimal('1.25') )
|
||||
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
def s5m3(step, sL, s, _input):
|
||||
y = 'Price_Signal'
|
||||
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
|
||||
return (y, x)
|
||||
|
||||
def s6m3(step, sL, s, _input):
|
||||
y = 'Price_Signal_2'
|
||||
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
|
||||
return (y, x)
|
||||
|
||||
def s6m1(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] - _input
|
||||
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
return (y, x)
|
||||
|
||||
|
||||
def s2m2(step, sL, s, _input):
|
||||
y = 'Price'
|
||||
x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
|
||||
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
return (y, x)
|
||||
|
||||
# Exogenous States
|
||||
proc_one_coef_A = -125
|
||||
proc_one_coef_B = 125
|
||||
|
||||
# A change in belief of actual price, passed onto behaviors to make action
|
||||
def es4p2(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
|
||||
|
||||
return (y,x)
|
||||
|
||||
|
||||
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
|
||||
y = 'timestamp'
|
||||
x = ep_time_step(s, s['timestamp'], seconds=1)
|
||||
return (y, x)
|
||||
|
||||
#Environment States
|
||||
# NONE
|
||||
|
||||
# Genesis States
|
||||
state_dict = {
|
||||
'Z': Decimal(21000000.0),
|
||||
'Price': Decimal(100.0), # Initialize = Z for EMA
|
||||
'Buy_Log': Decimal(0.0),
|
||||
'Sell_Log': Decimal(0.0),
|
||||
'Price_Signal': Decimal(100.0),
|
||||
'Price_Signal_2': Decimal(100.0),
|
||||
'Trans': Decimal(0.0),
|
||||
'P_Ext_Markets': Decimal(25000.0),
|
||||
'timestamp': '2018-10-01 15:16:24'
|
||||
}
|
||||
|
||||
def env_proc_id(x):
|
||||
return x
|
||||
|
||||
env_processes = {
|
||||
# "P_Ext_Markets": env_proc_id
|
||||
}
|
||||
|
||||
exogenous_states = exo_update_per_ts(
|
||||
{
|
||||
"P_Ext_Markets": es4p2,
|
||||
"timestamp": es5p2
|
||||
}
|
||||
)
|
||||
|
||||
sim_config = {
|
||||
"N": 100,
|
||||
"T": range(1000)
|
||||
}
|
||||
|
||||
# test return vs. non-return functions as lambdas
|
||||
# test fully defined functions
|
||||
mechanisms = {
|
||||
"m1": {
|
||||
"behaviors": {
|
||||
"b1": b1m1,
|
||||
"b3": b3m2,
|
||||
"b7": b7m2,
|
||||
"b7h": b7hm2
|
||||
},
|
||||
"states": {
|
||||
"Z": s1m1,
|
||||
"Buy_Log": s3m1
|
||||
}
|
||||
},
|
||||
"m2": {
|
||||
"behaviors": {
|
||||
"b1": b1m2,
|
||||
"b3": b3m2,
|
||||
"b4": b4m2,
|
||||
"b7": b7m2,
|
||||
"b7h": b7hm2
|
||||
},
|
||||
"states": {
|
||||
"Sell_Log": s4m2
|
||||
}
|
||||
},
|
||||
"m3": {
|
||||
"behaviors": {
|
||||
},
|
||||
"states": {
|
||||
"Price": s2m3,
|
||||
"Price_Signal": s5m3,
|
||||
"Price_Signal_2": s6m3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))
|
||||
|
|
@ -0,0 +1,307 @@
|
|||
from decimal import Decimal
|
||||
import numpy as np
|
||||
|
||||
from SimCAD import Configuration, configs
|
||||
from SimCAD.utils.configuration import exo_update_per_ts, proc_trigger, bound_norm_random, \
|
||||
ep_time_step
|
||||
|
||||
seed = {
|
||||
'z': np.random.RandomState(1)
|
||||
}
|
||||
|
||||
# Signals
|
||||
# Pr_signal
|
||||
beta = Decimal('0.25') # agent response gain
|
||||
beta_LT = Decimal('0.1') # LT agent response gain
|
||||
# alpha = .67, 2 block moving average
|
||||
alpha = Decimal('0.67')
|
||||
# 21 day EMA forgetfullness between 0 and 1, closer to 1 discounts older obs quicker, should be 2/(N+1)
|
||||
# 21 * 3 mech steps, 2/64 = 0.03125
|
||||
alpha_2 = Decimal('0.03125')
|
||||
max_withdraw_factor = Decimal('0.9')
|
||||
external_draw = Decimal('0.01') # between 0 and 1 to draw Buy_Log to external
|
||||
|
||||
|
||||
#alpha * s['Zeus_ST'] + (1 - alpha)*s['Zeus_LT']
|
||||
|
||||
# Stochastic process factors
|
||||
correction_factor = Decimal('0.01')
|
||||
volatility = Decimal('5.0')
|
||||
|
||||
# Buy_Log_signal =
|
||||
# Z_signal =
|
||||
# Price_signal =
|
||||
# TDR_draw_signal =
|
||||
# P_Ext_Markets_signal =
|
||||
|
||||
# Behaviors per Mechanism
|
||||
|
||||
# BEHAVIOR 1: EMH Trader
|
||||
EMH_portion = Decimal('0.20')
|
||||
EMH_Ext_Hold = Decimal('42000.0')
|
||||
|
||||
|
||||
def b1m1(step, sL, s):
|
||||
# print('b1m1')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
buy = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
price = s['Price']
|
||||
return {'EMH_buy': buy, 'EMH_buy_P': price}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'EMH_buy': 0}
|
||||
else:
|
||||
return {'EMH_buy': 0}
|
||||
|
||||
|
||||
def b1m2(step, sL, s):
|
||||
# print('b1m2')
|
||||
theta = (s['Z']*EMH_portion*s['Price'])/(s['Z']*EMH_portion*s['Price'] + EMH_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
return {'EMH_sell': 0}
|
||||
elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)):
|
||||
sell = beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta))
|
||||
price = s['Price']
|
||||
return {'EMH_sell': sell, 'EMH_sell_P': price}
|
||||
else:
|
||||
return {'EMH_sell': 0}
|
||||
|
||||
# BEHAVIOR 3: Herding
|
||||
Herd_portion = Decimal('0.20')
|
||||
Herd_Ext_Hold = Decimal('42000.0')
|
||||
Herd_UB = Decimal('0.10') # UPPER BOUND
|
||||
Herd_LB = Decimal('0.10') # LOWER BOUND
|
||||
def b3m2(step, sL, s):
|
||||
theta = (s['Z']*Herd_portion*s['Price'])/(s['Z']*Herd_portion*s['Price'] + Herd_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - Herd_LB:
|
||||
|
||||
sell = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
price = s['Price'] - (s['Price_Signal'] / s['Price'])
|
||||
return {'herd_sell': sell, 'herd_buy': 0, 'herd_sell_P': price}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > Herd_UB:
|
||||
buy = beta * theta*Herd_Ext_Hold * s['P_Ext_Markets']/(s['Price']*Herd_portion*(1-theta))
|
||||
price = s['Price'] + (s['Price'] / s['Price_Signal'])
|
||||
return {'herd_sell': 0, 'herd_buy': buy, 'herd_buy_P': price}
|
||||
else:
|
||||
return {'herd_sell': 0, 'herd_buy': 0}
|
||||
|
||||
# BEHAVIOR 4: HODLers
|
||||
HODL_belief = Decimal('10.0')
|
||||
HODL_portion = Decimal('0.20')
|
||||
HODL_Ext_Hold = Decimal('4200.0')
|
||||
|
||||
|
||||
def b4m2(step, sL, s):
|
||||
# print('b4m2')
|
||||
theta = (s['Z']*HODL_portion*s['Price'])/(s['Z']*HODL_portion*s['Price'] + HODL_Ext_Hold * s['P_Ext_Markets'])
|
||||
if s['Price'] < 1/HODL_belief*(theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
sell = beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta))
|
||||
price = s['Price']
|
||||
return {'HODL_sell': sell, 'HODL_sell_P': price}
|
||||
elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)):
|
||||
return {'HODL_sell': 0}
|
||||
else:
|
||||
return {'HODL_sell': 0}
|
||||
|
||||
# BEHAVIOR 7: Endogenous Information Updating (EIU)
|
||||
# Short Term Price Signal, Lower Threshold = BOT-like
|
||||
EIU_portion = Decimal('0.20')
|
||||
EIU_Ext_Hold = Decimal('42000.0')
|
||||
EIU_UB = Decimal('0.50') # UPPER BOUND
|
||||
EIU_LB = Decimal('0.50') # LOWER BOUND
|
||||
def b7m2(step, sL, s):
|
||||
theta = (s['Z']*EIU_portion*s['Price'])/(s['Z']*EIU_portion*s['Price'] + EIU_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal']) < - EIU_LB:
|
||||
|
||||
sell = beta * theta*EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EIU_portion*(1-theta))
|
||||
price = s['Price'] + (s['Price_Signal'] / s['Price'])
|
||||
return {'EIU_sell': sell, 'EIU_buy': 0, 'EIU_sell_P': price}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal']) > EIU_UB:
|
||||
buy = beta * theta* EIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* EIU_portion*(1-theta))
|
||||
price = s['Price'] - (s['Price'] / s['Price_Signal'])
|
||||
return {'EIU_sell': 0, 'EIU_buy': buy, 'EIU_buy_P': price}
|
||||
else:
|
||||
return {'EIU_sell': 0, 'EIU_buy': 0}
|
||||
|
||||
# BEHAVIOR 7b: Endogenous Information Updating (EIU)
|
||||
# Longer Term Price Signal, Higher Threshold = Human-Like
|
||||
HEIU_portion = Decimal('0.20')
|
||||
HEIU_Ext_Hold = Decimal('42000.0')
|
||||
HEIU_UB = Decimal('2.0') # UPPER BOUND
|
||||
HEIU_LB = Decimal('2.0') # LOWER BOUND
|
||||
def b7hm2(step, sL, s):
|
||||
theta = (s['Z']*HEIU_portion*s['Price'])/(s['Z']*HEIU_portion*s['Price'] + HEIU_Ext_Hold * s['P_Ext_Markets'])
|
||||
# if s['Price'] - s['Price_Signal'] < (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)) - Herd_LB:
|
||||
if (s['Price'] - s['Price_Signal_2']) < - HEIU_LB:
|
||||
|
||||
sell = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HEIU_portion*(1-theta))
|
||||
price = s['Price'] + (s['Price_Signal_2'] / s['Price'])
|
||||
return {'HEIU_sell': sell, 'HEIU_buy': 0, 'HEIU_sell_P': price}
|
||||
# elif s['Price'] > Herd_UB - (theta*Herd_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*Herd_portion*(1-theta)):
|
||||
elif (s['Price'] - s['Price_Signal_2']) > HEIU_UB:
|
||||
buy = beta * theta* HEIU_Ext_Hold * s['P_Ext_Markets']/(s['Price']* HEIU_portion*(1-theta))
|
||||
price = s['Price'] - (s['Price'] / s['Price_Signal_2'])
|
||||
return {'HEIU_sell': 0, 'HEIU_buy': buy, 'HEIU_buy_P': price}
|
||||
else:
|
||||
return {'HEIU_sell': 0, 'HEIU_buy': 0}
|
||||
|
||||
# STATES
|
||||
# ZEUS Fixed Supply
|
||||
def s1m1(step, sL, s, _input):
|
||||
y = 'Z'
|
||||
x = s['Z'] #+ _input # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s2m1(step, sL, s, _input):
|
||||
# y = 'Price'
|
||||
# x = (s['P_Ext_Markets'] - _input['EMH_buy']) / s['Z'] * 10000
|
||||
# #x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
# return (y, x)
|
||||
|
||||
|
||||
def s3m1(step, sL, s, _input):
|
||||
y = 'Buy_Log'
|
||||
x = _input['EMH_buy'] + _input['herd_buy'] + _input['EIU_buy'] + _input['HEIU_buy'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
def s4m2(step, sL, s, _input):
|
||||
y = 'Sell_Log'
|
||||
x = _input['EMH_sell'] + _input['HODL_sell'] + _input['herd_sell'] + _input['EIU_sell'] + _input['HEIU_sell'] # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s3m3(step, sL, s, _input):
|
||||
# y = 'Buy_Log'
|
||||
# x = s['Buy_Log'] + _input # / Psignal_int
|
||||
# return (y, x)
|
||||
|
||||
|
||||
# Price Update
|
||||
def s2m3(step, sL, s, _input):
|
||||
|
||||
y = 'Price'
|
||||
#var1 = Decimal.from_float(s['Buy_Log'])
|
||||
x = s['Price'] + (s['Buy_Log'] /s['Z'] ) - (s['Sell_Log']/s['Z'] )
|
||||
#+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int
|
||||
return (y, x)
|
||||
|
||||
def s5m3(step, sL, s, _input):
|
||||
y = 'Price_Signal'
|
||||
x = alpha * s['Price'] + (1 - alpha)*s['Price_Signal']
|
||||
return (y, x)
|
||||
|
||||
def s6m3(step, sL, s, _input):
|
||||
y = 'Price_Signal_2'
|
||||
x = alpha_2 * s['Price'] + (1 - alpha_2)*s['Price_Signal_2']
|
||||
return (y, x)
|
||||
|
||||
def s6m1(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] - _input
|
||||
#x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
return (y, x)
|
||||
|
||||
|
||||
# def s2m2(step, sL, s, _input):
|
||||
# y = 'Price'
|
||||
# x = (s['P_Ext_Markets'] - _input) /s['Z'] *10000
|
||||
# x= alpha * s['Z'] + (1 - alpha)*s['Price']
|
||||
# return (y, x)
|
||||
|
||||
# Exogenous States
|
||||
proc_one_coef_A = -125
|
||||
proc_one_coef_B = 125
|
||||
|
||||
# A change in belief of actual price, passed onto behaviors to make action
|
||||
def es4p2(step, sL, s, _input):
|
||||
y = 'P_Ext_Markets'
|
||||
x = s['P_Ext_Markets'] + bound_norm_random(seed['z'], proc_one_coef_A, proc_one_coef_B)
|
||||
|
||||
return (y,x)
|
||||
|
||||
|
||||
def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params
|
||||
y = 'timestamp'
|
||||
x = ep_time_step(s, s['timestamp'], seconds=1)
|
||||
return (y, x)
|
||||
|
||||
#Environment States
|
||||
# NONE
|
||||
|
||||
# Genesis States
|
||||
state_dict = {
|
||||
'Z': Decimal(21000000.0),
|
||||
'Price': Decimal(100.0), # Initialize = Z for EMA
|
||||
'Buy_Log': Decimal(0.0),
|
||||
'Sell_Log': Decimal(0.0),
|
||||
'Price_Signal': Decimal(100.0),
|
||||
'Price_Signal_2': Decimal(100.0),
|
||||
'Trans': Decimal(0.0),
|
||||
'P_Ext_Markets': Decimal(25000.0),
|
||||
'timestamp': '2018-10-01 15:16:24'
|
||||
}
|
||||
|
||||
def env_proc_id(x):
|
||||
return x
|
||||
|
||||
env_processes = {
|
||||
# "P_Ext_Markets": env_proc_id
|
||||
}
|
||||
|
||||
exogenous_states = exo_update_per_ts(
|
||||
{
|
||||
"P_Ext_Markets": es4p2,
|
||||
"timestamp": es5p2
|
||||
}
|
||||
)
|
||||
|
||||
sim_config = {
|
||||
"N": 1,
|
||||
"T": range(1000)
|
||||
}
|
||||
|
||||
# test return vs. non-return functions as lambdas
|
||||
# test fully defined functions
|
||||
mechanisms = {
|
||||
"m1": {
|
||||
"behaviors": {
|
||||
"b1": b1m1,
|
||||
"b3": b3m2,
|
||||
"b7": b7m2,
|
||||
"b7h": b7hm2
|
||||
},
|
||||
"states": {
|
||||
"Z": s1m1,
|
||||
"Buy_Log": s3m1
|
||||
}
|
||||
},
|
||||
"m2": {
|
||||
"behaviors": {
|
||||
"b1": b1m2,
|
||||
"b3": b3m2,
|
||||
"b4": b4m2,
|
||||
"b7": b7m2,
|
||||
"b7h": b7hm2
|
||||
},
|
||||
"states": {
|
||||
"Sell_Log": s4m2
|
||||
}
|
||||
},
|
||||
"m3": {
|
||||
"behaviors": {
|
||||
},
|
||||
"states": {
|
||||
"Price": s2m3,
|
||||
"Price_Signal": s5m3,
|
||||
"Price_Signal_2": s6m3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms))
|
||||
Loading…
Reference in New Issue