-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathuniform_network_generator.py
152 lines (101 loc) · 3.22 KB
/
uniform_network_generator.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
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
import random
import sys
import numpy as np
import networkx as nx
import random
import scipy.sparse
import itertools
import sys
from scipy.sparse import csr_matrix
import networkx as nx
#TODO put it in the main
#define network parameters
# number of network participants (financial entities), e.g. banks
# density of the debpt network p
# number of reference entities
n_banks = 50
# notional values based on realisti values reported by Abaad et al.
max_notional = 50
mean_notional = 23
def is_naked(j, k, l, c):
if (l[k][j] < naked_cds(j,k,c)):
return 1
else:
return 0
def naked_cds(j,k,contract):
'''
This function is used to make a specific position either covered or naked
It finds, given holder and reference entities, the sum of cds notional
over all writers.
'''
summ = 0
for i in xrange(len(contract)):
summ = summ + contract[i][j][k]
return summ
def uniform_network_gen (n_banks, p):
'''
Return the uniform network generated given denisty (p), share of naked CDSs : 55-60 %
type:
debt_matrix, 2d-array of debt contracts
contract, 3d-array of cds contracts
ref_entities, list of reference entities
Parameters:
n_banks: int (number of banks, nodes, in the network)
p : float ( density of network should be in (0, 1))
'''
ref_entities = []
n_ref_entities = n_banks/3
n_c_d = 9
naked_cds_share = 0.6
size = n_ref_entities
while size > 0:
ref_entity = random.randint(0, n_ref_entities-1)
if ref_entity not in ref_entities: #// pathalogical cases are ommited
ref_entities.append(ref_entity)
size -= 1
# generate debt network
# erdos_rany game : G(n, m)
# m = (n(n-1))/2*p
m = n_banks*(n_banks-1)/2*p
G = nx.gnm_random_graph(n_banks, m, seed=3, directed=True)
debt_matrix = [[0 for i in range(n_banks)] for j in range(n_banks)]
A = nx.adjacency_matrix(G) # type A : scipy sparce scr matrix
cx = A.tocoo()
for i in range(n_banks):
for j in range(n_banks):
if i in ref_entities:
debt_matrix[i][j] = random.randint(1,mean_notional-5)
else :
debt_matrix[i][j] = random.randint(1,max_notional-10)
contract = [[[0 for i in range(n_banks)] for j in range(n_banks)] for k in range(n_banks)]
for k in ref_entities:
i = 0
j = 0
#TODO Define number of contracts written such that it is more acurate and realistic
n_contracts_written = random.randint(1,n_banks/n_c_d)
while n_contracts_written>0:
while i==j or i == k or j == k :
i = random.randint(0,n_banks-1)
j = random.randint(0,n_banks-1)
if contract[i][j][k] ==0 :#and has_liability(k, liability) :
contract[i][j][k] = random.randint(1, mean_notional)
n_contracts_written -=1
i = 0
j = 0
return debt_matrix, contract, ref_entities
#TO CHECK SHARE FO NAKED CDSs UNCOMMENT FOLLOWINGS.
# l,c,ref = uniform_network_gen(50, 0.3)
# n=0
# m=0
# counter = 0
# vec= []
# for x in range(10000):
# for i in range(50):
# for j in range(50):
# for k in ref:
# if c[i][j][k] !=0 :
# n+=is_naked(j,k,l,c)
# counter+=1
# r= float(n)/counter
# vec.append(r)
# print np.average(vec)