From d39bca6700f7947141831b4c728177b1f35822dc Mon Sep 17 00:00:00 2001 From: "Joshua E. Jodesty" Date: Mon, 7 Jan 2019 17:55:45 -0500 Subject: [PATCH] pre clean for az pt. 1 --- .gitignore | 3 +- simulations/scrapbox/config7c.py | 494 +++++++++++++++++++++++++++++ simulations/scrapbox/config8c.py | 443 ++++++++++++++++++++++++++ simulations/sim_test.py | 6 +- simulations/validation/config_1.py | 178 +++++++++++ simulations/validation/config_2.py | 180 +++++++++++ 6 files changed, 1297 insertions(+), 7 deletions(-) create mode 100644 simulations/scrapbox/config7c.py create mode 100644 simulations/scrapbox/config8c.py create mode 100644 simulations/validation/config_1.py create mode 100644 simulations/validation/config_2.py diff --git a/.gitignore b/.gitignore index 2728be8..e25ca00 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,4 @@ __pycache__ Pipfile Pipfile.lock results -.mypy_cache -simulations/scrapbox \ No newline at end of file +.mypy_cache \ No newline at end of file diff --git a/simulations/scrapbox/config7c.py b/simulations/scrapbox/config7c.py new file mode 100644 index 0000000..1374e1b --- /dev/null +++ b/simulations/scrapbox/config7c.py @@ -0,0 +1,494 @@ +from decimal import Decimal +import numpy as np + +from SimCAD import Configuration, configs +from SimCAD.configuration import exo_update_per_ts, bound_norm_random, \ + ep_time_step + +seed = { + 'z': np.random.RandomState(1) + # 'a': np.random.RandomState(2), + # 'b': np.random.RandomState(3), + # 'c': np.random.RandomState(3) +} + +#Signals +# Pr_signal +#if s['P_Ext_Markets'] != 0: +#Pr_signal = s['Z']/s['P_Ext_Markets'] +#else Pr_signal = 0 +# if Pr_signal < s['Z']/s['Buy_Log']: +beta = Decimal('0.25') #agent response gain +beta_LT = Decimal('0.1') #LT agent response gain +alpha = Decimal('0.091') #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 + +# 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): + # y = 'P_Ext_Markets' + # Psignal_ext = s['P_Ext_Markets'] / s['Z'] + # Psignal_int = s['Buy_Log'] / s['Z'] + # if Psignal_ext < Psignal_int: + # return beta*(Psignal_int - Psignal_ext) * s['Z'] # Deposited amount in TDR + # else: + # return 0 # Decimal(0.000001) + # return (y,x) + 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 beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta)) + elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)): + return 0 + else: + return 0 + +def b1m2(step, sL, s): + 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 0 + elif s['Price'] > (theta*EMH_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*EMH_portion*(1-theta)): + return beta * theta*EMH_Ext_Hold * s['P_Ext_Markets']/(s['Price']*EMH_portion*(1-theta)) + else: + return 0 + +# BEHAVIOR 3: Herding + + +# BEHAVIOR 4: HODLers +HODL_belief = Decimal('10.0') +HODL_portion = Decimal('0.250') +HODL_Ext_Hold = Decimal('4200.0') + +def b4m2(step, sL, s): + 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)): + return beta * theta*HODL_Ext_Hold * s['P_Ext_Markets']/(s['Price']*HODL_portion*(1-theta)) + elif s['Price'] > (theta*HODL_Ext_Hold * s['P_Ext_Markets'])/(s['Z']*HODL_portion*(1-theta)): + return 0 + else: + return 0 + + +# BEHAVIOR 2: Withdraw TDR and burn Zeus +# Selling Agent- Arbitrage on TDR ext v TDR int signals +# def b2m1(step, sL, s): +# Psignal_ext = s['P_Ext_Markets'] / s['Z'] +# Psignal_int = s['Buy_Log'] / s['Z'] +# if Psignal_ext > Psignal_int: +# # withdrawn amount in TDR, subject to TDR limit +# return - np.minimum(beta*(Psignal_ext - Psignal_int) * s['Z'],s['Buy_Log']*max_withdraw_factor) +# else: +# return 0 #- Decimal(0.000001) + # return 0 + +# BEHAVIOR 1: Deposit TDR and mint Zeus +# Buying Agent- Arbitrage on Price and Z signals +# def b1m2(step, sL, s): +# # Psignal_ext = s['P_Ext_Markets'] / s['Z'] +# # Psignal_int = s['Buy_Log'] / s['Z'] +# # if Psignal_ext > Psignal_int: +# # # withdrawn amount in TDR, subject to TDR limit +# # return - np.minimum(beta*(Psignal_ext - Psignal_int) * s['Z'],s['Buy_Log']*max_withdraw_factor) +# # else: +# # return 0 #- Decimal(0.000001) +# # +# # LT more valuable than ST = deposit TDR and mint Z +# Psignal_LT = s['Price'] / s['Z'] +# if Psignal_LT > 1: +# return beta_LT*(Psignal_LT - 1) * s['Z'] +# else: +# return 0 + +# Behavior will go here- b2m2, putting in mech 3: b1m3 for debugging +# def b2m2(step, sL, s): +# # Psignal_LT = s['Price'] / s['Z'] +# # if Psignal_LT > 1: +# test = np.arange(1,10) +# return test + +# Selling Agent- Arbitrage on Price and Z signals +# def b1m3(step, sL, s): +# Psignal_LT = s['Price'] / s['Z'] +# if Psignal_LT < 1: +# return - np.minimum(beta_LT*(Psignal_LT - 1) * s['Z'], s['Z']*max_withdraw_factor) +# else: +# return 0 + + +# def b2m3(step, sL, s): +# return 0 + +def dummy_behavior(step, sL, s): + return 0 +def s1_dummy(step, sL, s, _input): + y = 'Z' + x = s['Z'] + return (y, x) +def s2_dummy(step, sL, s, _input): + y = 'Price' + x = s['Price'] + return (y, x) +def s3_dummy(step, sL, s, _input): + y = 'Buy_Log' + x = s['Buy_Log'] + return (y, x) +def s4_dummy(step, sL, s, _input): + y = 'Sell_Log' + x = s['Sell_Log'] + return (y, x) +def s5_dummy(step, sL, s, _input): + y = 'Trans' + x = s['Trans'] + return (y, x) +def s6_dummy(step, sL, s, _input): + y = 'P_Ext_Markets' + x = s['P_Ext_Markets'] + return (y, x) +# Internal States per Mechanism +# Deposit TDR/Mint Zeus +# def s1m1(step, sL, s, _input): +# s['Z'] = s['Z'] + _input + + +# 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) /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 # / Psignal_int + return (y, x) + +def s4m2(step, sL, s, _input): + y = 'Sell_Log' + x = _input # / Psignal_int + print('s4m2 ',type(_input)) + 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): + print('s2m3 ') + print(type(s['Sell_Log'])) + print(type(s['Z'])) + + y = 'Price' + 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 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) + +# def s1m1(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] + _input / Psignal_int +# return (y, x) + +# def s2m1(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m1(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] + _input # Input already in TDR * s['Z'] +# return (y, x) + +# # Withdraw TDR/Burn Zeus +# def s1m2(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] #+ _input / Psignal_int +# return (y, x) + +# def s2m2(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m2(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] + _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s1m3(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] #+ _input / Psignal_int +# return (y, x) + +# def s2m3(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m3(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] #+ _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s3m4(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log']*(1-external_draw) + s['Sell_Log']*external_draw # _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s1m3(step, sL, s, _input): +# s['Z'] = s['Z'] + _input +# def s2m3(step, sL, s, _input): +# s['Price'] = s['Price'] + _input + +# Exogenous States +proc_one_coef_A = -125 +proc_one_coef_B = 125 +# def es3p1(step, sL, s, _input): +# s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) +# def es4p2(step, sL, s, _input): +# s['P_Ext_Markets'] = s['P_Ext_Markets'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) +# def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params +# s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1) +def es3p1(step, sL, s, _input): + y = 's3' + x = s['s3'] + 1 + return (y, x) +# def es4p2(step, sL, s, _input): +# y = 'P_Ext_Markets' +# # bound_norm_random defined in utils.py + +# #x = s['P_Ext_Markets'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) +# expected_change = correction_factor*(s['P_Ext_Markets']-s['Buy_Log']) +# vol = np.random.randint(1,volatility) +# change = expected_change * vol +# # change_float = (np.random.normal(expected_change,volatility*expected_change) #Decimal('1.0') +# #change = Decimal.from_float(change_float) +# x = s['P_Ext_Markets'] + change + +# return (y, x) + +# 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 +# def stochastic(reference, seed, correction = 0.01): +# series = np.zeros(len(reference)) +# series[0] = reference[0] +# for i in range(1,len(reference)): +# expected_change = correction*(reference[i]-series[i-1]) +# normalized_expected_change = np.abs(expected_change)*(reference[i])/(reference[i-1]) +# seed_int = seed.randint(1,10) +# change = np.random.normal(expected_change,seed_int*normalized_expected_change) + +# series[i] = series[i-1]+change +# # avoid negative series returns +# if series[i] <= 0: +# series[i] = .01 +# #series[i] = series[i-1]+change + +# return [series,seed_int] +# ref3 = np.arange(1,1000)*.1 +# test = stochastic(ref3,seed['b']) + +# def env_a(ref3,seed['b']): +# return stochastic(ref3,seed['b']) +def env_a(x): + return 100 +def env_b(x): + return 21000000 +# def what_ever(x): +# return x + 1 + +# 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), + 'Trans': Decimal(0.0), + 'P_Ext_Markets': Decimal(25000.0), + + # 's2': Decimal(0.0), + # 's3': Decimal(0.0), + # 's4': Decimal(0.0), + 'timestamp': '2018-10-01 15:16:24' +} + +# exogenous_states = { +# # "s3": es3p1, +# "P_Ext_Markets": es4p2, +# "timestamp": es5p2 +# } + +exogenous_states = exo_update_per_ts( + { + # "s3": es3p1, + "P_Ext_Markets": es4p2, + "timestamp": es5p2 + } +) + +env_processes = { + # "s3": env_proc('2018-10-01 15:16:25', env_a), +# "P_Ext_Markets": env_proc('2018-10-01 15:16:25', env_b) +} + +# test return vs. non-return functions as lambdas +# test fully defined functions +mechanisms = { + "m1": { + "behaviors": { + "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, +# "b2": b2m1 + }, + "states": { + "Z": s1m1, + "Price": s2_dummy, + "Buy_Log": s3m1, + "Sell_Log":s4_dummy, + "Trans": s5_dummy, + "P_Ext_Markets": s6_dummy + } + }, + "m2": { + "behaviors": { + "b1": b1m2, + "b4": b4m2 + }, + "states": { + "Z": s1_dummy, + "Price": s2_dummy, + "Buy_Log": s3_dummy, + "Sell_Log":s4m2, + "Trans": s5_dummy, + "P_Ext_Markets": s6_dummy + } + }, + "m3": { + "behaviors": { +# "b1": b1m2, +# "b4": b4m2 + }, + "states": { + "Z": s1_dummy, + "Price": s2m3, + "Buy_Log": s3_dummy, + "Sell_Log":s4_dummy, + "Trans": s5_dummy, + "P_Ext_Markets": s6_dummy + } + }, +# "m3": { +# "behaviors": { +# "b1": b1m3, +# "b2": b2m3 +# }, +# "states": { +# "Z": s1m3, +# "Price": s2m3, +# "Buy_Log": s3m3, +# "Sell_Log": s4_dummy, +# "Trans": s5_dummy, +# "P_Ext_Markets": s6_dummy +# } +# }, +# "m4": { +# "behaviors": { +# "dummy": dummy_behavior +# }, +# "states": { +# "Z": s1_dummy, +# "Price": s2_dummy, +# "Buy_Log": s3m4, +# "Sell_Log": s4_dummy, +# "Trans": s5_dummy, +# "P_Ext_Markets": s6_dummy +# } +# }, + # "m3": { + # "behaviors": { + # "b1": b1m3, + # "b2": b2m3 + # }, + # "states": { + # "Z": s1m3, + # "Price": s2m3, + # } + # } + #treat environmental processes as a mechanism + "ep": { + "behaviors": { + "dummy": dummy_behavior + }, + "states": { + "Z": s1_dummy, + "Price": s2_dummy, + "Buy_Log": s3_dummy, + "Sell_Log": s4_dummy, + "Trans": s5_dummy, + "P_Ext_Markets": es4p2 + } + } +} + +sim_config = { + "N": 1, + "T": range(1000) +} + +configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) diff --git a/simulations/scrapbox/config8c.py b/simulations/scrapbox/config8c.py new file mode 100644 index 0000000..2eecf45 --- /dev/null +++ b/simulations/scrapbox/config8c.py @@ -0,0 +1,443 @@ +from decimal import Decimal +import numpy as np + +from SimCAD import Configuration, configs +from SimCAD.configuration import exo_update_per_ts, bound_norm_random, \ + ep_time_step + +# behavior_ops = [] +# behavior_ops = [foldr(dict_elemwise_sum())] + +seed = { + 'z': np.random.RandomState(1) + # 'a': np.random.RandomState(2), + # 'b': np.random.RandomState(3), + # 'c': np.random.RandomState(3) +} + +#Signals +# Pr_signal +#if s['P_Ext_Markets'] != 0: +#Pr_signal = s['Z']/s['P_Ext_Markets'] +#else Pr_signal = 0 +# if Pr_signal < s['Z']/s['Buy_Log']: +beta = Decimal('0.25') #agent response gain +beta_LT = Decimal('0.1') #LT agent response gain +alpha = Decimal('0.091') #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 + +# 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') + # y = 'P_Ext_Markets' + # Psignal_ext = s['P_Ext_Markets'] / s['Z'] + # Psignal_int = s['Buy_Log'] / s['Z'] + # if Psignal_ext < Psignal_int: + # return beta*(Psignal_int - Psignal_ext) * s['Z'] # Deposited amount in TDR + # else: + # return 0 # Decimal(0.000001) + # return (y,x) + 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 + + +# 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 2: Withdraw TDR and burn Zeus +# Selling Agent- Arbitrage on TDR ext v TDR int signals +# def b2m1(step, sL, s): +# Psignal_ext = s['P_Ext_Markets'] / s['Z'] +# Psignal_int = s['Buy_Log'] / s['Z'] +# if Psignal_ext > Psignal_int: +# # withdrawn amount in TDR, subject to TDR limit +# return - np.minimum(beta*(Psignal_ext - Psignal_int) * s['Z'],s['Buy_Log']*max_withdraw_factor) +# else: +# return 0 #- Decimal(0.000001) + # return 0 + +# BEHAVIOR 1: Deposit TDR and mint Zeus +# Buying Agent- Arbitrage on Price and Z signals +# def b1m2(step, sL, s): +# # Psignal_ext = s['P_Ext_Markets'] / s['Z'] +# # Psignal_int = s['Buy_Log'] / s['Z'] +# # if Psignal_ext > Psignal_int: +# # # withdrawn amount in TDR, subject to TDR limit +# # return - np.minimum(beta*(Psignal_ext - Psignal_int) * s['Z'],s['Buy_Log']*max_withdraw_factor) +# # else: +# # return 0 #- Decimal(0.000001) +# # +# # LT more valuable than ST = deposit TDR and mint Z +# Psignal_LT = s['Price'] / s['Z'] +# if Psignal_LT > 1: +# return beta_LT*(Psignal_LT - 1) * s['Z'] +# else: +# return 0 + +# Behavior will go here- b2m2, putting in mech 3: b1m3 for debugging +# def b2m2(step, sL, s): +# # Psignal_LT = s['Price'] / s['Z'] +# # if Psignal_LT > 1: +# test = np.arange(1,10) +# return test + +# Selling Agent- Arbitrage on Price and Z signals +# def b1m3(step, sL, s): +# Psignal_LT = s['Price'] / s['Z'] +# if Psignal_LT < 1: +# return - np.minimum(beta_LT*(Psignal_LT - 1) * s['Z'], s['Z']*max_withdraw_factor) +# else: +# return 0 + + +# def b2m3(step, sL, s): +# return 0 + +# Internal States per Mechanism +# Deposit TDR/Mint Zeus +# def s1m1(step, sL, s, _input): +# s['Z'] = s['Z'] + _input + + +# 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'] # / Psignal_int + return (y, x) + +def s4m2(step, sL, s, _input): + y = 'Sell_Log' + x = _input['sell_order1'] + _input['sell_order2'] # / 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'] * 1/s['Z'] - s['Sell_Log']/s['Z'] + #+ np.divide(s['Buy_Log'],s['Z']) - np.divide() # / Psignal_int + 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) + +# def s1m1(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] + _input / Psignal_int +# return (y, x) + +# def s2m1(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m1(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] + _input # Input already in TDR * s['Z'] +# return (y, x) + +# # Withdraw TDR/Burn Zeus +# def s1m2(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] #+ _input / Psignal_int +# return (y, x) + +# def s2m2(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m2(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] + _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s1m3(step, sL, s, _input): +# Psignal_int = s['Buy_Log'] / s['Z'] +# y = 'Z' +# x = s['Z'] #+ _input / Psignal_int +# return (y, x) + +# def s2m3(step, sL, s, _input): +# y = 'Price' +# x= alpha * s['Z'] + (1 - alpha)*s['Price'] +# return (y, x) + +# def s3m3(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log'] #+ _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s3m4(step, sL, s, _input): +# y = 'Buy_Log' +# x = s['Buy_Log']*(1-external_draw) + s['Sell_Log']*external_draw # _input #* s['Z'] +# # y = 'Buy_Log' +# # x = s['Buy_Log'] + _input +# return (y, x) + +# def s1m3(step, sL, s, _input): +# s['Z'] = s['Z'] + _input +# def s2m3(step, sL, s, _input): +# s['Price'] = s['Price'] + _input + +# Exogenous States +proc_one_coef_A = -125 +proc_one_coef_B = 125 +# def es3p1(step, sL, s, _input): +# s['s3'] = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) +# def es4p2(step, sL, s, _input): +# s['P_Ext_Markets'] = s['P_Ext_Markets'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) +# def es5p2(step, sL, s, _input): # accept timedelta instead of timedelta params +# s['timestamp'] = ep_time_step(s, s['timestamp'], seconds=1) +def es3p1(step, sL, s, _input): + y = 's3' + x = s['s3'] + 1 + return (y, x) +# def es4p2(step, sL, s, _input): +# y = 'P_Ext_Markets' +# # bound_norm_random defined in utils.py + +# #x = s['P_Ext_Markets'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) +# expected_change = correction_factor*(s['P_Ext_Markets']-s['Buy_Log']) +# vol = np.random.randint(1,volatility) +# change = expected_change * vol +# # change_float = (np.random.normal(expected_change,volatility*expected_change) #Decimal('1.0') +# #change = Decimal.from_float(change_float) +# x = s['P_Ext_Markets'] + change + +# return (y, x) + +# 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 +# def stochastic(reference, seed, correction = 0.01): +# series = np.zeros(len(reference)) +# series[0] = reference[0] +# for i in range(1,len(reference)): +# expected_change = correction*(reference[i]-series[i-1]) +# normalized_expected_change = np.abs(expected_change)*(reference[i])/(reference[i-1]) +# seed_int = seed.randint(1,10) +# change = np.random.normal(expected_change,seed_int*normalized_expected_change) + +# series[i] = series[i-1]+change +# # avoid negative series returns +# if series[i] <= 0: +# series[i] = .01 +# #series[i] = series[i-1]+change + +# return [series,seed_int] +# ref3 = np.arange(1,1000)*.1 +# test = stochastic(ref3,seed['b']) + +# def env_a(ref3,seed['b']): +# return stochastic(ref3,seed['b']) +def env_a(x): + return 100 +def env_b(x): + return 21000000 +# def what_ever(x): +# return x + 1 + +# 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), + 'Trans': Decimal(0.0), + 'P_Ext_Markets': Decimal(25000.0), + + # 's2': Decimal(0.0), + # 's3': Decimal(0.0), + # 's4': Decimal(0.0), + 'timestamp': '2018-10-01 15:16:24' +} + +# exogenous_states = { +# # "s3": es3p1, +# "P_Ext_Markets": es4p2, +# "timestamp": es5p2 +# } + +exogenous_states = exo_update_per_ts( + { + # "s3": es3p1, + "P_Ext_Markets": es4p2, + "timestamp": es5p2 + } +) + +env_processes = { + # "s3": env_proc('2018-10-01 15:16:25', env_a), +# "P_Ext_Markets": env_proc('2018-10-01 15:16:25', env_b) +} + +# test return vs. non-return functions as lambdas +# test fully defined functions +mechanisms = { + "m1": { + "behaviors": { + "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, +# "b2": b2m1 + }, + "states": { + "Z": s1m1, +# "Price": s2_dummy, + "Buy_Log": s3m1, + } + }, + "m2": { + "behaviors": { + "b1": b1m2, + "b4": b4m2 + }, + "states": { + "Sell_Log":s4m2, + } + }, + "m3": { + "behaviors": { + }, + "states": { + "Price": s2m3, + } + }, +# "m3": { +# "behaviors": { +# "b1": b1m3, +# "b2": b2m3 +# }, +# "states": { +# "Z": s1m3, +# "Price": s2m3, +# "Buy_Log": s3m3, +# } +# }, +# "m4": { +# "behaviors": { +# }, +# "states": { +# } +# }, + # "m3": { + # "behaviors": { + # "b1": b1m3, + # "b2": b2m3 + # }, + # "states": { + # "Z": s1m3, + # "Price": s2m3, + # } + # } + #treat environmental processes as a mechanism + "ep": { + "behaviors": { + }, + "states": { + "P_Ext_Markets": es4p2 + } + } +} + +sim_config = { + "N": 1, + "T": range(1000) +} + +configs.append(Configuration(sim_config, state_dict, seed, exogenous_states, env_processes, mechanisms)) \ No newline at end of file diff --git a/simulations/sim_test.py b/simulations/sim_test.py index 1bbfcdc..ef1daa7 100644 --- a/simulations/sim_test.py +++ b/simulations/sim_test.py @@ -3,11 +3,7 @@ from tabulate import tabulate # The following imports NEED to be in the exact same order from SimCAD.engine import ExecutionMode, ExecutionContext, Executor -from simulations.validation import config1, config2 -# from simulations.validation import base_config1, base_config2 -# from simulations.barlin import config4 -# from simulations.zx import config_zx -# from simulations.barlin import config6atemp #config6aworks, +from simulations.validation import config_1, config_2 from SimCAD import configs # ToDo: pass ExecutionContext with execution method as ExecutionContext input diff --git a/simulations/validation/config_1.py b/simulations/validation/config_1.py new file mode 100644 index 0000000..7bdd5ac --- /dev/null +++ b/simulations/validation/config_1.py @@ -0,0 +1,178 @@ +from decimal import Decimal +import numpy as np +from datetime import timedelta + +from SimCAD import configs +from SimCAD.configuration import Configuration +from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \ + ep_time_step + +seed = { + 'z': np.random.RandomState(1), + 'a': np.random.RandomState(2), + 'b': np.random.RandomState(3), + 'c': np.random.RandomState(3) +} + +# Behaviors per Mechanism +# Different return types per mechanism ?? *** No *** +def b1m1(step, sL, s): + return {'param1': 1} +def b2m1(step, sL, s): + return {'param2': 4} + +def b1m2(step, sL, s): + return {'param1': 'a', 'param2': 2} +def b2m2(step, sL, s): + return {'param1': 'b', 'param2': 4} + +def b1m3(step, sL, s): + return {'param1': ['c'], 'param2': np.array([10, 100])} +def b2m3(step, sL, s): + return {'param1': ['d'], 'param2': np.array([20, 200])} + +# deff not more than 2 +# Internal States per Mechanism +def s1m1(step, sL, s, _input): + y = 's1' + x = _input['param1'] #+ [Coef1 x 5] + return (y, x) +def s2m1(step, sL, s, _input): + y = 's2' + x = _input['param2'] #+ [Coef2 x 5] + return (y, x) + +def s1m2(step, sL, s, _input): + y = 's1' + x = _input['param1'] + return (y, x) +def s2m2(step, sL, s, _input): + y = 's2' + x = _input['param2'] + return (y, x) + +def s1m3(step, sL, s, _input): + y = 's1' + x = _input['param1'] + return (y, x) +def s2m3(step, sL, s, _input): + y = 's2' + x = _input['param2'] + return (y, x) + +# Exogenous States +proc_one_coef_A = 0.7 +proc_one_coef_B = 1.3 + +def es3p1(step, sL, s, _input): + y = 's3' + x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + +def es4p2(step, sL, s, _input): + y = 's4' + x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + +ts_format = '%Y-%m-%d %H:%M:%S' +t_delta = timedelta(days=0, minutes=0, seconds=1) +def es5p2(step, sL, s, _input): + y = 'timestamp' + x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta) + return (y, x) + + +# Environment States +def env_a(x): + return 5 +def env_b(x): + return 10 +# def what_ever(x): +# return x + 1 + +# Genesis States +genesis_states = { + 's1': Decimal(0.0), + 's2': Decimal(0.0), + 's3': Decimal(1.0), + 's4': Decimal(1.0), + 'timestamp': '2018-10-01 15:16:24' +} + +# remove `exo_update_per_ts` to update every ts +exogenous_states = exo_update_per_ts( + { + "s3": es3p1, + "s4": es4p2, + "timestamp": es5p2 + } +) + +# ToDo: make env proc trigger field agnostic +# ToDo: input json into function renaming __name__ +env_processes = { + "s3": env_a, + "s4": proc_trigger('2018-10-01 15:16:25', env_b) +} + +# lambdas +# genesis Sites should always be there +# [1, 2] +# behavior_ops = [ foldr(_ + _), lambda x: x + 0 ] + +# [1, 2] = {'b1': ['a'], 'b2', [1]} = +# behavior_ops = [ behavior_to_dict, print_fwd, sum_dict_values ] +# behavior_ops = [foldr(dict_elemwise_sum())] +# behavior_ops = [foldr(lambda a, b: a + b)] + +# need at least 1 behaviour and 1 state function for the 1st mech with behaviors +# mechanisms = {} + +mechanisms = { + "m1": { + "behaviors": { + "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, + "b2": b2m1 + }, + "states": { # exclude only. TypeError: reduce() of empty sequence with no initial value + "s1": s1m1, + "s2": s2m1 + } + }, + "m2": { + "behaviors": { + "b1": b1m2, + "b2": b2m2 + }, + "states": { + "s1": s1m2, + "s2": s2m2 + } + }, + "m3": { + "behaviors": { + "b1": b1m3, + "b2": b2m3 + }, + "states": { + "s1": s1m3, + "s2": s2m3 + } + } +} + +sim_config = { + "N": 2, + "T": range(5) +} + +configs.append( + Configuration( + sim_config=sim_config, + state_dict=genesis_states, + seed=seed, + exogenous_states=exogenous_states, + env_processes=env_processes, + mechanisms=mechanisms + ) +) \ No newline at end of file diff --git a/simulations/validation/config_2.py b/simulations/validation/config_2.py new file mode 100644 index 0000000..6b9469e --- /dev/null +++ b/simulations/validation/config_2.py @@ -0,0 +1,180 @@ +from decimal import Decimal +import numpy as np +from datetime import timedelta + +from SimCAD import configs +from SimCAD.configuration import Configuration +from SimCAD.configuration.utils import exo_update_per_ts, proc_trigger, bound_norm_random, \ + ep_time_step + + +seed = { + 'z': np.random.RandomState(1), + 'a': np.random.RandomState(2), + 'b': np.random.RandomState(3), + 'c': np.random.RandomState(3) +} + +# Behaviors per Mechanism +# Different return types per mechanism ?? *** No *** +def b1m1(step, sL, s): + return {'param1': 1} +def b2m1(step, sL, s): + return {'param2': 4} + +def b1m2(step, sL, s): + return {'param1': 'a', 'param2': 2} +def b2m2(step, sL, s): + return {'param1': 'b', 'param2': 4} + + +def b1m3(step, sL, s): + return {'param1': ['c'], 'param2': np.array([10, 100])} +def b2m3(step, sL, s): + return {'param1': ['d'], 'param2': np.array([20, 200])} + + +# Internal States per Mechanism +def s1m1(step, sL, s, _input): + y = 's1' + x = _input['param1'] + return (y, x) +def s2m1(step, sL, s, _input): + y = 's2' + x = _input['param2'] + return (y, x) + +def s1m2(step, sL, s, _input): + y = 's1' + x = _input['param1'] + return (y, x) +def s2m2(step, sL, s, _input): + y = 's2' + x = _input['param2'] + return (y, x) + +def s1m3(step, sL, s, _input): + y = 's1' + x = _input['param1'] + return (y, x) +def s2m3(step, sL, s, _input): + y = 's2' + x = _input['param2'] + return (y, x) + +# Exogenous States +proc_one_coef_A = 0.7 +proc_one_coef_B = 1.3 + +def es3p1(step, sL, s, _input): + y = 's3' + x = s['s3'] * bound_norm_random(seed['a'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + +def es4p2(step, sL, s, _input): + y = 's4' + x = s['s4'] * bound_norm_random(seed['b'], proc_one_coef_A, proc_one_coef_B) + return (y, x) + +ts_format = '%Y-%m-%d %H:%M:%S' +t_delta = timedelta(days=0, minutes=0, seconds=1) +def es5p2(step, sL, s, _input): + y = 'timestamp' + x = ep_time_step(s, dt_str=s['timestamp'], fromat_str=ts_format, _timedelta=t_delta) + return (y, x) + + +# Environment States +def env_a(x): + return 10 +def env_b(x): + return 10 +# def what_ever(x): +# return x + 1 + +# Genesis States +genesis_states = { + 's1': Decimal(0.0), + 's2': Decimal(0.0), + 's3': Decimal(1.0), + 's4': Decimal(1.0), + 'timestamp': '2018-10-01 15:16:24' +} + +# remove `exo_update_per_ts` to update every ts +# why `exo_update_per_ts` here instead of `env_processes` +exogenous_states = exo_update_per_ts( + { + "s3": es3p1, + "s4": es4p2, + "timestamp": es5p2 + } +) + +# make env proc trigger field agnostic +env_processes = { + "s3": proc_trigger('2018-10-01 15:16:25', env_a), + "s4": proc_trigger('2018-10-01 15:16:25', env_b) +} + +# lambdas +# genesis Sites should always be there +# [1, 2] +# behavior_ops = [ foldr(_ + _), lambda x: x + 0 ] + + +# [1, 2] = {'b1': ['a'], 'b2', [1]} = +# behavior_ops = [behavior_to_dict, print_fwd, sum_dict_values] +# behavior_ops = [foldr(dict_elemwise_sum())] +# behavior_ops = [] + +# need at least 1 behaviour and 1 state function for the 1st mech with behaviors +# mechanisms = {} +mechanisms = { + "m1": { + "behaviors": { + "b1": b1m1, # lambda step, sL, s: s['s1'] + 1, + # "b2": b2m1 + }, + "states": { # exclude only. TypeError: reduce() of empty sequence with no initial value + "s1": s1m1, + # "s2": s2m1 + } + }, + "m2": { + "behaviors": { + "b1": b1m2, + # "b2": b2m2 + }, + "states": { + "s1": s1m2, + # "s2": s2m2 + } + }, + "m3": { + "behaviors": { + "b1": b1m3, + "b2": b2m3 + }, + "states": { + "s1": s1m3, + "s2": s2m3 + } + } +} + +sim_config = { + "N": 2, + "T": range(5) +} + +configs.append( + Configuration( + sim_config=sim_config, + state_dict=genesis_states, + seed=seed, + exogenous_states=exogenous_states, + env_processes=env_processes, + mechanisms=mechanisms + ) +) \ No newline at end of file