Spaces:
Runtime error
Runtime error
File size: 10,666 Bytes
458aa75 c9063e5 458aa75 c9063e5 458aa75 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
import pandas as pd
import numpy as np
from random import normalvariate, random
import plotly.express as px
from radcad import Model, Simulation, Experiment
import streamlit as st
# Additional dependencies
# For analytics
import numpy as np
# For visualization
import plotly.express as px
from PIL import Image
# Additional dependencies
pd.options.plotting.backend = "plotly"
st.header('DeSci Value Flow Model')
image = Image.open('desci.png')
st.image(image, caption='DeSci value flow schema')
def p_researcher1(params, substep, state_history, previous_state):
losses = 0
to_market = 0
to_researcher = 0
to_treasury = 0
to_other_researcher = 0
salary = 0
funding = 0
if (previous_state['timestep'] < params['timestep_switch']) and (previous_state['funding_pool'] > params['funding_round']):
funding = params['funding_round']
to_treasury -= funding
research_value = funding * (1-params['epsilon'])
losses += funding - research_value
salary = research_value * params['beta']
to_market = research_value
if (random() < params['probability_buying']) and (previous_state['researcher1_value'] > params['cost_buying']):
salary = salary - params['cost_buying']
tx_fee = params['cost_buying'] * params['tx_fee']
salary -= tx_fee
to_treasury += tx_fee
to_other_researcher += params['cost_buying']
to_researcher += salary + research_value
elif (previous_state['timestep'] > params['timestep_switch']) and (previous_state['researcher1_value'] > params['cost_buying']):
tx_fee = params['cost_buying'] * params['tx_fee']
to_researcher -= params['cost_buying'] - tx_fee
to_other_researcher += params['cost_buying']
to_treasury += tx_fee
return {'update_researcher1_funding': funding,
'update_researcher1_salary': salary,
'update_researcher1_value': to_researcher,
'update_funding_pool': to_treasury,
'update_market': to_market,
'update_researcher2_value': to_other_researcher,
'update_losses': losses}
def p_researcher2(params, substep, state_history, previous_state):
losses = 0
to_market = 0
to_researcher = 0
to_treasury = 0
to_other_researcher = 0
salary = 0
funding = 0
if (previous_state['timestep'] > params['timestep_switch']) and (previous_state['funding_pool'] > params['funding_round']):
funding = params['funding_round']
to_treasury -= funding
research_value = funding * (1-params['epsilon'])
losses += funding - research_value
salary = research_value * params['beta']
to_market = research_value
if (random() < params['probability_buying']) and (previous_state['researcher2_value'] > params['cost_buying']):
salary = salary - params['cost_buying']
tx_fee = params['cost_buying'] * params['tx_fee']
salary -= tx_fee
to_treasury += tx_fee
to_other_researcher += params['cost_buying']
to_researcher += salary + research_value
elif (previous_state['timestep'] < params['timestep_switch']) and (previous_state['researcher2_value'] > params['cost_buying']):
tx_fee = params['cost_buying'] * params['tx_fee']
to_researcher -= params['cost_buying'] - tx_fee
to_other_researcher += params['cost_buying']
to_treasury += tx_fee
return {'update_researcher2_funding': funding,
'update_researcher2_salary': salary,
'update_researcher2_value': to_researcher,
'update_funding_pool': to_treasury,
'update_market': to_market,
'update_researcher1_value': to_other_researcher,
'update_losses': losses}
def s_timestep(params, substep, state_history, previous_state, policy_input):
updated_timestep = previous_state['timestep'] + 1
return 'timestep', updated_timestep
def s_funding_pool(params, substep, state_history, previous_state, policy_input):
funding_pool = previous_state['funding_pool']
updated_funding_pool = funding_pool + policy_input['update_funding_pool']
return 'funding_pool', updated_funding_pool
def s_researcher1_value(params, substep, state_history, previous_state, policy_input):
r_value = previous_state['researcher1_value']
updated_researcher1_value = r_value + policy_input['update_researcher1_value']
return 'researcher1_value', updated_researcher1_value
def s_researcher1_funding(params, substep, state_history, previous_state, policy_input):
r_funding = previous_state['researcher1_funding']
updated_researcher1_funding = r_funding + policy_input['update_researcher1_funding']
return 'researcher1_funding', updated_researcher1_funding
def s_researcher1_salary(params, substep, state_history, previous_state, policy_input):
r_salary = previous_state['researcher1_salary']
updated_researcher1_salary = r_salary + policy_input['update_researcher1_salary']
return 'researcher1_salary', updated_researcher1_salary
def s_researcher2_value(params, substep, state_history, previous_state, policy_input):
r_value = previous_state['researcher2_value']
updated_researcher2_value = r_value + policy_input['update_researcher2_value']
return 'researcher2_value', updated_researcher2_value
def s_researcher2_funding(params, substep, state_history, previous_state, policy_input):
r_funding = previous_state['researcher2_funding']
updated_researcher2_funding = r_funding + policy_input['update_researcher2_funding']
return 'researcher2_funding', updated_researcher2_funding
def s_researcher2_salary(params, substep, state_history, previous_state, policy_input):
r_salary = previous_state['researcher2_salary']
updated_researcher2_salary = r_salary + policy_input['update_researcher2_salary']
return 'researcher2_salary', updated_researcher2_salary
def s_knowledge_market(params, substep, state_history, previous_state, policy_input):
value = previous_state['knowledge_market_value']
updated_market_value = value + policy_input['update_market']
return 'knowledge_market_value', updated_market_value
def s_losses(params, substep, state_history, previous_state, policy_input):
losses = previous_state['losses']
updated_losses = losses + policy_input['update_losses']
return 'losses', updated_losses
st.subheader('Initial Value Allocation')
funding_pool = st.slider('Initial Funding Pool', min_value=1000, max_value=10000, value=1000, step=10)
researcher1_value = st.slider('Researcher1 Tokens', min_value=0, max_value=1000, value=0, step=1)
researcher2_value = st.slider('Researcher2 Tokens', min_value=0, max_value=1000, value=0, step=1)
st.subheader('Simulation Parameters')
tx_fee = st.slider('Transaction fee collected by DAO treasury during each transaction in the knowledge market', min_value=0., max_value=1., value=0.1, step=0.0001)
st.write('Set the funding disbursed each round from the funding pool')
funding_round = st.slider('Funding Round', min_value=100, max_value=1000, value=100, step=1)
st.write('Set the relative value leakages in the model.')
epsilon = st.slider('Work Inefficiency Weight', min_value=0., max_value=1., value=0.1, step=0.0001)
st.write('Set the portion of grant funding to be used as researcher salary.')
beta = st.slider('Salary Weight', min_value=0., max_value=1., value=0.4, step=0.0001)
st.write('Set the cost of getting access to papers in the knowledge market.')
cost_buying = st.slider('Cost of Buying', min_value=10., max_value=100., value=10., step=0.1)
st.write('Set the probability a researcher will buy access to a paper at each timestep.')
probability_buying = st.slider('Researcher Probability of Buying', min_value=0., max_value=1., value=0.1, step=0.0001)
st.write('Set the number of timesteps in the simulation.')
timesteps = st.slider('Timesteps', min_value=10, max_value=1000, value=100, step=1)
initial_state = {
'funding_pool': funding_pool,
'researcher1_value': researcher1_value,
'researcher1_funding': 0,
'researcher1_salary': 0,
'researcher2_value': researcher2_value,
'researcher2_funding': 0,
'researcher2_salary': 0,
'knowledge_market_value': 0,
'timestep': 0,
'losses': 0
}
ts = int(timesteps/2)
system_params = {
'funding_pool': [funding_pool],
'funding_round': [funding_round],
'beta': [beta],
'epsilon': [epsilon],
'cost_buying': [cost_buying],
'probability_buying': [probability_buying],
'timestep_switch': [ts],
'tx_fee': [tx_fee]
}
def configure_and_run_experiment(initial_state,
partial_state_update_blocks,
timesteps):
model = Model(
# Model initial state
initial_state=initial_state,
# Model Partial State Update Blocks
state_update_blocks=partial_state_update_blocks,
# System Parameters
params=system_params
)
simulation = Simulation(
model=model,
timesteps=timesteps, # Number of timesteps
runs=1 # Number of Monte Carlo Runs
)
result = simulation.run()
return result
partial_state_update_blocks = [
{
'policies': {
'p_researcher1': p_researcher1,
'p_researcher2': p_researcher2
},
'variables': {
'timestep': s_timestep,
'funding_pool': s_funding_pool,
'researcher1_value': s_researcher1_value,
'researcher1_funding': s_researcher1_funding,
'researcher1_salary': s_researcher1_salary,
'researcher2_value': s_researcher2_value,
'researcher2_funding': s_researcher2_funding,
'researcher2_salary': s_researcher2_salary,
'knowledge_market_value': s_knowledge_market,
'losses': s_losses
}
}
]
if st.button('Run Simulation'):
raw_result = configure_and_run_experiment(initial_state, partial_state_update_blocks, timesteps)
df = pd.DataFrame(raw_result)
fig1 = df.plot(kind='line', x='timestep', y=['funding_pool', 'researcher1_value', 'researcher2_value'], width=1000)
fig2 = df.plot(kind='line', x='timestep', y=['funding_pool','knowledge_market_value'], width=1000)
fig3 = df.plot(kind='line', x='timestep', y=['funding_pool', 'losses'], width=1000)
fig4 = df.plot(kind='line', x='timestep', y=['researcher1_value', 'researcher2_value', 'losses'], width=1000)
st.subheader('Results')
st.plotly_chart(fig1)
st.plotly_chart(fig2)
st.plotly_chart(fig3)
st.plotly_chart(fig4)
|