-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Andrew Clark
committed
Dec 14, 2020
1 parent
30c5bf2
commit 135cec2
Showing
45 changed files
with
7,264 additions
and
5,867 deletions.
There are no files selected for viewing
862 changes: 862 additions & 0 deletions
862
02 Reference Models/ThreeSided/Three Sided Market Model.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
2,353 changes: 500 additions & 1,853 deletions
2,353
02 Reference Models/ThreeSided/ThreeSidedMarket.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import math | ||
from decimal import Decimal | ||
from datetime import timedelta | ||
import numpy as np | ||
from typing import Dict, List | ||
|
||
from cadCAD.configuration import Experiment | ||
from cadCAD.configuration.utils import bound_norm_random, ep_time_step, config_sim, access_block | ||
from copy import deepcopy | ||
from cadCAD import configs | ||
from .state_variables import state_variables | ||
from .partial_state_update_block import partial_state_update_blocks | ||
from .parts.sys_params import * | ||
|
||
|
||
sim_config = config_sim({ | ||
'T': range(36), #day | ||
'N': 100, | ||
'M': params, | ||
}) | ||
|
||
seeds = { | ||
'a': np.random.RandomState(2), | ||
} | ||
|
||
exp = Experiment() | ||
|
||
exp.append_configs( | ||
sim_configs=sim_config, | ||
initial_state=state_variables, | ||
seeds=seeds, | ||
partial_state_update_blocks=partial_state_update_blocks | ||
) | ||
|
||
|
||
def get_configs(): | ||
''' | ||
Function to extract the configuration information for display in a notebook. | ||
''' | ||
|
||
|
||
return sim_config,state_variables,partial_state_update_blocks |
129 changes: 129 additions & 0 deletions
129
02 Reference Models/ThreeSided/model/partial_state_update_block.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
from .parts.exogenous import * | ||
from .parts.producers import * | ||
from .parts.providers import * | ||
from .parts.consumers import * | ||
from .parts.investors import * | ||
from .parts.governance import * | ||
from .parts.system import * | ||
|
||
|
||
# The Partial State Update Blocks | ||
partial_state_update_blocks = [ | ||
# exogenous.py: | ||
{ | ||
'policies': | ||
{ | ||
}, | ||
'variables': | ||
{ | ||
'cost_of_production': cost_of_production_generator, | ||
'tx_volume': tx_volume_generator, | ||
'overhead_cost': overhead_cost_generator | ||
} | ||
|
||
}, | ||
|
||
# producers.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': producer_choice | ||
}, | ||
'variables': | ||
{ | ||
'volume_of_production': commit_delta_production | ||
} | ||
}, | ||
|
||
# consumers.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': consumer_choice | ||
}, | ||
'variables': | ||
{ | ||
'fiat_reserve': capture_consumer_payments1, | ||
'token_reserve': capture_consumer_payments2 | ||
} | ||
}, | ||
# providers.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': provider_choice | ||
}, | ||
'variables': | ||
{ | ||
'fiat_reserve': compensate_providers1, | ||
'token_reserve': compensate_providers2 | ||
} | ||
}, | ||
# producers.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': producer_compensation_policy | ||
}, | ||
'variables': | ||
{ | ||
'token_reserve': compensate_production, | ||
'producer_roi_estimate': update_producer_roi_estimate | ||
} | ||
}, | ||
|
||
# governance.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': budgeting_policy | ||
}, | ||
'variables': | ||
{ | ||
'fiat_reserve': release_funds, | ||
'operational_budget': update_budget | ||
} | ||
}, | ||
|
||
# governance.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': minting_policy | ||
}, | ||
'variables': | ||
{ | ||
'token_reserve': mint1, | ||
'token_supply': mint2 | ||
} | ||
}, | ||
|
||
# system.py: | ||
{ | ||
'policies': | ||
{ | ||
}, | ||
'variables': | ||
{ | ||
'smooth_avg_fiat_reserve': update_smooth_avg_fiat_reserve, | ||
'smooth_avg_token_reserve':update_smooth_avg_token_reserve | ||
} | ||
}, | ||
|
||
# governance.py: | ||
{ | ||
'policies': | ||
{ | ||
'action': conversion_policy | ||
}, | ||
'variables': | ||
{ | ||
'conversion_rate': update_conversion_rate | ||
} | ||
} | ||
] | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
|
||
# Behaviors | ||
#these are uncontrollerd choices of users in the provider consumer | ||
def consumer_choice(params, step, history, current_state): | ||
#fiat paid by consumers | ||
#note: balance of consumption vol covered in tokens (computed later) | ||
|
||
#simple heuristic ~ the fraction of token payment is proportion to free supply share | ||
free_supply = current_state['token_supply']-current_state['token_reserve'] | ||
share_of_free_supply = free_supply/ current_state['token_supply'] | ||
|
||
txi_fiat= (1.0-share_of_free_supply)*current_state['tx_volume'] | ||
return {'txi_fiat': txi_fiat} | ||
|
||
|
||
# Mechanisms | ||
def capture_consumer_payments1(params, step, sL, s, _input): | ||
#fiat inbound | ||
y = 'fiat_reserve' | ||
x = s['fiat_reserve']+_input['txi_fiat'] | ||
return (y, x) | ||
|
||
def capture_consumer_payments2(params, step, sL, s, _input): | ||
#tokens inbound | ||
y = 'token_reserve' | ||
fiat_eq = s['tx_volume']-_input['txi_fiat'] | ||
x = s['token_reserve']+s['conversion_rate']*fiat_eq*(1.0+params['conversion_fee']) | ||
return (y, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import numpy as np | ||
|
||
|
||
# Exogenous Mechanisms | ||
def tx_volume_generator(params, step, sL, s, _input): | ||
y = 'tx_volume' | ||
x = s['tx_volume']*(1+2*params['eta']*np.random.rand()*(1-s['tx_volume']/params['tampw'])) | ||
return (y, x) | ||
|
||
def cost_of_production_generator(params, step, sL, s, _input): | ||
y = 'cost_of_production' | ||
x = params['alpha']*s['cost_of_production']+params['beta']*np.random.rand() | ||
return (y, x) | ||
|
||
def overhead_cost_generator(params, step, sL, s, _input): | ||
#unit fiat | ||
y = 'overhead_cost' | ||
q = params['a']+params['b']*s['tx_volume']+params['c']*s['volume_of_production']+params['d']*s['tx_volume']*s['volume_of_production'] | ||
x = params['flat']+np.log(q) | ||
return (y, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
|
||
# Behaviors | ||
def budgeting_policy(params, step, history, current_state): | ||
#governance decision ~ system policy for budgeting to cover overhead costs | ||
#note that this is a naive Heuristic control policy based on | ||
# Strategies described by human operators | ||
# there exist formal alternatives using stochastic optimal control | ||
|
||
#define an estimate of future overhead | ||
proj_overhead = current_state['overhead_cost'] #simple naive | ||
|
||
#simple threshold based conditional logic | ||
if current_state['operational_budget']< params['buffer_runway']*proj_overhead: | ||
target_release = params['buffer_runway']*proj_overhead-current_state['operational_budget'] | ||
if current_state['fiat_reserve']-target_release > params['reserve_threshold']*current_state['fiat_reserve']: | ||
budget_released = target_release | ||
else: | ||
budget_released = (1.0-params['reserve_threshold'])*current_state['fiat_reserve'] | ||
else: | ||
budget_released = params['min_budget_release']*current_state['fiat_reserve'] | ||
|
||
return {'budget_released': budget_released} | ||
|
||
def minting_policy(params, step, history, current_state): | ||
''' | ||
governance decision ~ determines the conditions or schedule of new tokens minted | ||
''' | ||
mint = (params['final_supply']-current_state['token_supply'])*params['release_rate'] | ||
return {'mint': mint} | ||
|
||
|
||
def conversion_policy(params, step, history, current_state): | ||
''' | ||
governance decision ~ system policy for token/fiat unit of value conversion | ||
''' | ||
ncr = params['conversion_rate_gain']*current_state['smooth_avg_token_reserve']/current_state['smooth_avg_fiat_reserve'] | ||
return {'new_conversion_rate': ncr} | ||
|
||
# Mechanisms | ||
def release_funds(params, step, sL, s, _input): | ||
#tokens outbound | ||
y = 'fiat_reserve' | ||
x = s['fiat_reserve'] - _input['budget_released'] | ||
return (y, x) | ||
|
||
def update_budget(params, step, sL, s, _input): | ||
#tokens outbound | ||
y = 'operational_budget' | ||
x = s['operational_budget'] + _input['budget_released'] | ||
return (y, x) | ||
|
||
def mint1(params, step, sL, s, _input): | ||
''' | ||
minting process mints into the reserve | ||
''' | ||
y = 'token_supply' | ||
x = s['token_supply'] + _input['mint'] | ||
return (y, x) | ||
|
||
def mint2(params, step, sL, s, _input): | ||
y = 'token_reserve' | ||
x = s['token_reserve'] + _input['mint'] | ||
return (y, x) | ||
|
||
def update_conversion_rate(params, step, sL, s, _input): | ||
y = 'conversion_rate' | ||
x = _input['new_conversion_rate'] | ||
return (y, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
|
||
# Behaviors | ||
def investors(params, step, history, current_state): | ||
# Pay relevant parties | ||
if current_state['timestep'] == 1: | ||
return {'Invest': 1} | ||
elif current_state['timestep'] == 10: | ||
return {'Invest': 1} | ||
else: | ||
return {'Invest': 0} | ||
|
||
|
||
# Mechanisms | ||
def receive_fiat_from_investors(params, step, sL, s, _input): | ||
y = 'fiat_reserve' | ||
if _input['Invest'] == 1: | ||
x = s['fiat_reserve'] + s['seed_money'] | ||
else: | ||
x = s['fiat_reserve'] | ||
return (y, x) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
import numpy as np | ||
#governance decision ~ system policy for compensating producers | ||
#consider transaction volume, labor committed and token reserve and supply to determine payout | ||
|
||
#this function is a parameter of this policy which determines diminishing value of more labor | ||
base_value = 1000.0 | ||
|
||
def marginal_utility_function(x): | ||
#this is how much the platform value a total amount of production (in fiat) | ||
return base_value+np.sqrt(x) | ||
|
||
|
||
# Behaviors | ||
def producer_choice(params, step, history, current_state): | ||
#ROI heuristic | ||
# add or remove resources based on deviation from threshold | ||
if current_state['producer_roi_estimate'] < 0: | ||
delta_labor = current_state['volume_of_production']*(params['attrition_rate']-1.0) | ||
else: | ||
ratio = current_state['producer_roi_estimate']/params['roi_threshold'] | ||
delta_labor = params['roi_gain']*current_state['volume_of_production']*(ratio-1.0) | ||
|
||
return {'delta_labor': delta_labor} | ||
|
||
def producer_compensation_policy(params, step, history, current_state): | ||
tokens_paid = current_state['conversion_rate']*marginal_utility_function(current_state['volume_of_production']) | ||
return {'tokens_paid': tokens_paid} | ||
|
||
# Mechanisms | ||
def commit_delta_production(params, step, sL, s, _input): | ||
y = 'volume_of_production' | ||
x = s['volume_of_production']+_input['delta_labor'] | ||
return (y, x) | ||
|
||
|
||
def compensate_production(params, step, sL, s, _input): | ||
y = 'token_reserve' | ||
x = s['token_reserve']-_input['tokens_paid'] | ||
return (y, x) | ||
|
||
def update_producer_roi_estimate(params, step, sL, s, _input): | ||
revenue = _input['tokens_paid']/s['conversion_rate'] | ||
cost = s['cost_of_production']*s['volume_of_production'] | ||
spot_ROI_estimate = (revenue-cost)/cost | ||
y = 'producer_roi_estimate' | ||
x = params['rho']*spot_ROI_estimate + s['producer_roi_estimate']*(1.0-params['rho']) | ||
return (y, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
|
||
# Behaviors | ||
def provider_choice(params, step, history, current_state): | ||
#fiat claimed by providers | ||
#note: balance of provided vol covered in tokens (computed later) | ||
txo_fiat = params['theta']*current_state['tx_volume']+ (1-params['theta'])*params['gamma']*current_state['volume_of_production']*current_state['cost_of_production'] | ||
return {'txo_fiat': txo_fiat} | ||
|
||
|
||
# Mechanisms | ||
def compensate_providers1(params, step, sL, s, _input): | ||
#fiat outbound | ||
y = 'fiat_reserve' | ||
x = s['fiat_reserve']-_input['txo_fiat']*(1.0-params['platform_fee']) | ||
return (y, x) | ||
|
||
def compensate_providers2(params, step, sL, s, _input): | ||
#tokens outbound | ||
y = 'token_reserve' | ||
fiat_eq = s['tx_volume']-_input['txo_fiat'] | ||
x = s['token_reserve']-s['conversion_rate']*fiat_eq*(1.0-params['platform_fee']-params['conversion_fee']) | ||
return (y, x) |
Oops, something went wrong.