smejak commited on
Commit
458aa75
1 Parent(s): d83c2c5

Upload app.py

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