Files
IIabm/test.ipynb
T
2023-07-28 18:07:30 +08:00

25 KiB

In [14]:
import numpy as np

np.random.randint(0.5, 3.5)
p_remove = 0.9
np.random.choice([True, False], p=[p_remove, 1-p_remove])
rng = np.random.default_rng(0)
for _ in range(10):
    print(rng.integers(0,100))
np.random.choice([1, 2, 3], 2, p=[0.4, 0.4, 0.2])
Out [14]:
85
63
51
26
30
4
7
1
17
81
array([2, 2])
In [19]:
share = 0.8
list_succ_firms = [1, 1]
round(share * len(list_succ_firms)) if round(share * len(list_succ_firms)) > 0 else 1
Out [19]:
2
In [4]:
import math
size = [18,20,21,22,23]
p = [s / sum(size) for s in size]
print(p)
for beta in [0.1, 0.2, 0.3]:
    damp_size = [math.exp(beta*s) for s in size]
    print([s / sum(damp_size) for s in damp_size])
[0.17307692307692307, 0.19230769230769232, 0.20192307692307693, 0.21153846153846154, 0.22115384615384615]
[0.14899116146026878, 0.1819782155490595, 0.20111703154812216, 0.22226869439668717, 0.24564489704586234]
[0.10801741721030356, 0.16114305076975205, 0.19682056666851946, 0.2403971829915773, 0.29362178235984765]
[0.07643198434626533, 0.13926815562848321, 0.18799234648997357, 0.25376312466637047, 0.34254438886890737]
In [10]:
import math
size = [18,20,21,22,23]
p = [(s - min(size) + 1)/(max(size)-min(size)+1) for s in size]
print(p)
for beta in [0.1, 0.5, 0.8]:
    p = [((s - min(size) + 1)/(max(size)-min(size)+1))**beta for s in size]
    print(p)
[0.16666666666666666, 0.5, 0.6666666666666666, 0.8333333333333334, 1.0]
[0.8359588020779368, 0.9330329915368074, 0.960264500792218, 0.9819330445619127, 1.0]
[0.408248290463863, 0.7071067811865476, 0.816496580927726, 0.9128709291752769, 1.0]
[0.23849484685087588, 0.5743491774985174, 0.7229811807984657, 0.8642810744472068, 1.0]
In [1]:
import multiprocess as mp

print(mp.cpu_count())
32
In [18]:
from orm import engine
import pandas as pd
import pickle
str_sql = "select e_id, count, max_max_ts, dct_lst_init_remove_firm_prod from iiabmdb.without_exp_experiment as a " \
"inner join " \
"(select e_id, count(id) as count, max(max_ts) as max_max_ts from iiabmdb.without_exp_sample as a " \
"inner join (select s_id, max(ts) as max_ts from iiabmdb.without_exp_result where ts > 0 group by s_id) as b " \
"on a.id = b.s_id " \
"group by e_id) as b " \
"on a.id = b.e_id " \
"order by count desc;"
result = pd.read_sql(sql=str_sql, con=engine)
result['dct_lst_init_remove_firm_prod'] = result['dct_lst_init_remove_firm_prod'].apply(lambda x: pickle.loads(x))
# print(result)
list_dct = result.loc[result['count']>=9, 'dct_lst_init_remove_firm_prod'].to_list()
print(len(list_dct))
71
In [2]:
import numpy as np

np.random.choice([1], p=[0.9])
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[2], line 3
      1 import numpy as np
----> 3 np.random.choice([1], p=[0.9])

File mtrand.pyx:933, in numpy.random.mtrand.RandomState.choice()

ValueError: probabilities do not sum to 1
In [46]:
prob_remove = 0
prob_remove = np.random.uniform(
    prob_remove - 0.1, prob_remove + 0.1)
prob_remove = 1 if prob_remove > 1 else prob_remove
prob_remove = 0 if prob_remove < 0 else prob_remove
prob_remove
Out [46]:
0.004495606232695251
In [66]:
nprandom = np.random.default_rng(0)
lst_choose_firm = nprandom.choice(range(10),
                                       1,
                                       replace=False
                                       )
print(lst_choose_firm)
[8]
In [9]:
nprandom = np.random.default_rng(0)
lst_choose_firm = nprandom.choice([1,2],
                                  3,
                                  replace=False
                                  )
lst_choose_firm
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[9], line 2
      1 nprandom = np.random.default_rng(0)
----> 2 lst_choose_firm = nprandom.choice([1,2],
      3                                   3,
      4                                   replace=False
      5                                   )
      6 lst_choose_firm

File _generator.pyx:753, in numpy.random._generator.Generator.choice()

ValueError: Cannot take a larger sample than population when replace is False
In [3]:

for j in range(3):
    for k in range(3):
        print(j, k)
        if j == k == 1:
            print('break')
            break
    else:
        continue
    break

0 0
0 1
0 2
1 0
1 1
break
In [4]:
print(27 / (4 * 3))
print(27 / 4 / 3)
2.25
2.25
In [2]:
for i in range(1,1):
    print(i)
In [6]:
lst = list(range(1,11))
print(lst)

lst[-5:]
Out [6]:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[6, 7, 8, 9, 10]
In [7]:
with open('SQL_export_high_risk_setting.sql', 'r') as f:
    contents = f.read()

import pandas as pd
from orm import engine
result = pd.read_sql(sql=contents, con=engine)
result
Out [7]:
e_id n_disrupt_sample total_n_disrupt_firm_prod_experiment dct_lst_init_disrupt_firm_prod
0 383 50 300.0 b'\x80\x05\x95\x17\x00\x00\x00\x00\x00\x00\x00...
1 227 50 250.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
2 83 50 200.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
3 135 50 200.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
4 179 50 200.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
... ... ... ... ...
90 76 24 56.0 b'\x80\x05\x95\x14\x00\x00\x00\x00\x00\x00\x00...
91 89 24 54.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
92 90 24 54.0 b'\x80\x05\x95\x16\x00\x00\x00\x00\x00\x00\x00...
93 335 24 54.0 b'\x80\x05\x95\x17\x00\x00\x00\x00\x00\x00\x00...
94 449 24 53.0 b'\x80\x05\x95\x15\x00\x00\x00\x00\x00\x00\x00...

95 rows × 4 columns

In [5]:
import networkx as nx

G = nx.MultiDiGraph()
G.add_edge(1, 2)
nx.draw(G)
G.has_edge(1, 2, 1)
Out [5]:
False
In [17]:
s = set()
s.add(1)
s.add(2)
s.add(1)
len(s)
if s:
    print(1)
1
In [ ]: