def runModel(cashDistribution, leverageDistribution): # Set scale for distributions: cashScale = setCashScale(cashDistribution) leverageScale = setLeverageScale(leverageDistribution) # Generate cash vector cash_vector = generateCashVector(cashDistribution) * cashScale # print(cash_vector) cash_vector[cash_vector <= 0] = 1 * 10 ** -10 # print(cash_vector) # cash_vector[cash_vector > 5000] = 6500 cash_to_connectivity = lambda x: safe_ln(x) connectivity_vector = cash_to_connectivity(cash_vector) # Make the adjacency matrix mat = make_connections(connectivity_vector) mat = binarize_probabilities(mat) # Distribute liabilities leverage_ratios = generateLeverageRatios(leverageDistribution) * leverageScale leverage_ratios[leverage_ratios < 5] = 5 # print(leverage_ratios) liabilities = np.multiply(cash_vector, leverage_ratios) mat = distribute_liabilities(mat, liabilities) for i, cash in enumerate(cash_vector): mat[i, i] = cash defaults_to_freq = {} for z in tqdm(range(steps)): if network == 'TestNetwork': model = TestNetwork(size, mat) model.reset_net() step_result = model.step() defaults = step_result['cascade_defaults'] + step_result['ratio_defaults'] elif network == 'DeterministicRatioNetwork': model = DeterministicRatioNetwork(100, mat) model.reset_net() ratios, defaults = model.step() if defaults in defaults_to_freq: defaults_to_freq[defaults] += 1 else: defaults_to_freq[defaults] = 1 """ the below code saves the results and distribution configuration in a .json file with the date and time added to the name so as to prevent overwriting.""" with open(network + 'result_' + cashString + leverageString + str(time.strftime("%d_%m_%y_%H%M%S")) + '.json', 'w') as fp: json.dump(defaults_to_freq, fp)
def runModel(cashDistribution, leverageDistribution): cash_vector = generateCashVector(cashDistribution) cash_vector[cash_vector <= 0] = 1 * 10**-10 # cash_vector[cash_vector > 5000] = 6500 cash_to_connectivity = lambda x: safe_ln(x) connectivity_vector = cash_to_connectivity(cash_vector) # Make the adjacency matrix mat = make_connections(connectivity_vector) mat = binarize_probabilities(mat) # Distribute liabilities leverage_ratios = generateLeverageRatios(leverageDistribution) leverage_ratios[leverage_ratios < 5] = 5 liabilities = np.multiply(cash_vector, leverage_ratios) mat = distribute_liabilities(mat, liabilities) for i, cash in enumerate(cash_vector): mat[i, i] = cash defaults = np.zeros((steps, 2)) for z in tqdm(range(steps)): model = TestNetwork(100, mat) model.reset_net() results = model.step() defaults[z, 0] = results['ratio_defaults'] defaults[z, 1] = results['cascade_defaults'] """ the below code saves the results in a .csv file with the date and time added to the name so as to prevent overwriting. The format parameter can be used to change the variable type that is stored in the .csv file, with the result being smaller file sizes when more data efficient variable types are utilized.""" # use fmt='%1c' to save data as characters # use fmt='%1d' to save data as signed decimal integers # use fmt='%1e' to save data in scientific notation # use fmt='%1f' to save data as decimal floating points # use fmt='%1g' to save data as the most efficient of e or f # use fmt='%1o' to save data as signed octals # use fmt='%1s' to save data as string characters # use fmt='%1u' to save data as unsigned decimal integers # use fmt='%1x' to save data as unsigned hexadecimal integer np.savetxt('defaults_' + str(time.strftime("%d_%m_%y_%H%M%S")) + '.csv', defaults, delimiter=',', fmt='%1f') """ Cursory analysis reveals that with the standard settings and no fmt parameter used
import json from tqdm import tqdm import numpy as np import cvxpy as cvx from contagion import binarize_probabilities, distribute_liabilities, make_connections, DeterministicRatioNetwork, TestNetwork for k in range(10): cash_vector = np.random.normal(10000, 10000, 100) cash_vector[cash_vector <= 0] = 1 * 10**-10 # cash_vector[cash_vector > 5000] = 6500 cash_to_connectivity = lambda x: np.log(x).astype(int) connectivity_vector = cash_to_connectivity(cash_vector) # Make the adjacency matrix mat = make_connections(connectivity_vector) mat = binarize_probabilities(mat, cash_vector) # Distribute liabilities leverage_ratios = np.random.normal(10, 2, 100) leverage_ratios[leverage_ratios < 5] = 5 liabilities = np.multiply(cash_vector, leverage_ratios) mat = distribute_liabilities(mat, liabilities) for i, cash in enumerate(cash_vector): mat[i, i] = cash defaults_to_freq = {} for z in tqdm(range(100000)): model = TestNetwork(100, mat) model.reset_net()
import json from tqdm import tqdm import numpy as np import cvxpy as cvx from contagion import binarize_probabilities, distribute_liabilities, make_connections, DeterministicRatioNetwork, TestNetwork steps = 1000000 cash_vector = np.random.normal(10000, 10000, 100) cash_vector[cash_vector <= 0] = 1 * 10**-10 # cash_vector[cash_vector > 5000] = 6500 cash_to_connectivity = lambda x: np.log(x).astype(int) connectivity_vector = cash_to_connectivity(cash_vector) # Make the adjacency matrix mat = make_connections(connectivity_vector) mat = binarize_probabilities(mat) # Distribute liabilities leverage_ratios = np.random.normal(10, 2, 100) leverage_ratios[leverage_ratios < 5] = 5 liabilities = np.multiply(cash_vector, leverage_ratios) mat = distribute_liabilities(mat, liabilities) for i, cash in enumerate(cash_vector): mat[i, i] = cash defaults = np.zeros((steps, 2)) for z in tqdm(range(steps)): model = TestNetwork(100, mat) model.reset_net() results = model.step()