-
Notifications
You must be signed in to change notification settings - Fork 0
/
simulation.py
75 lines (62 loc) · 2.68 KB
/
simulation.py
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
from readers.csv_reader import *
import random
import pyarrow as pa
import pyarrow.parquet as pq
from analyze import *
tenors = ['0D', '1D', '5D', '10D', '1M', '2M', '3M', '4M', '5M', '6M', '1Y', '2Y', '5Y', '10Y']
def get_scale(i):
scale = 1
if i != 0:
if i % 98 == 0:
scale = 2500
if i % 56 == 0:
scale = -300
if i % 67 == 0:
scale = 9000
return scale
def get_portfolio_simulation(prefix, paths):
print("generating simulation for " + prefix)
simulation_df = None
rows = []
for p in range(paths):
cur_row = {'Path': p}
for i, j in enumerate(tenors):
scale = i * 3
if scale % 4 == 0:
scale = scale / 4
if scale == 0:
scale = 1
cur_row[j] = random.randint(-1000000, 1000000) * scale
rows.append(cur_row)
simulation_df = simulation_df.append(pd.DataFrame(rows), ignore_index=True) if simulation_df is not None else pd.DataFrame(rows)
return simulation_df
def get_position_simulation(max_row, prefix, paths):
print("generating simulation for " + prefix)
simulation_df = None
rows = []
for i in range(int(max_row)):
scale = get_scale(i)
for p in range(paths):
cur_row = {'PositionId': i, 'Path': p}
for j in tenors:
cur_row[j] = random.randint(-1000000, 1000000) * scale
rows.append(cur_row)
if i % 100 == 0:
simulation_df = simulation_df.append(pd.DataFrame(rows), ignore_index=True) if simulation_df is not None else pd.DataFrame(rows)
rows = []
print('i is ' + str(i))
simulation_df = simulation_df.append(pd.DataFrame(rows), ignore_index=True) if simulation_df is not None else pd.DataFrame(rows)
dd = simulation_df.groupby(['PositionId']).mean().reset_index()
del dd['Path']
pp = pd.DataFrame(simulation_df.sum(), columns=['sum'])
return simulation_df
def write_position_simulation(position_sims, position_paths, directory):
position_simulation_df = get_position_simulation(position_sims, "Position", position_paths)
print(position_simulation_df)
position_simulation = pa.Table.from_pandas(position_simulation_df)
pq.write_table(position_simulation, os.path.join(directory, 'position_simulation_eod.parquet'))
def write_portfolio_simulation(portfolio_paths, directory):
portfolio_simulation_df = get_portfolio_simulation("Portfolio", portfolio_paths)
print(portfolio_simulation_df)
portfolio_simulation = pa.Table.from_pandas(portfolio_simulation_df)
pq.write_table(portfolio_simulation, os.path.join(directory, 'portfolio_simulation_eod.parquet'))