-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathsimulate_shocks.py
60 lines (40 loc) · 1.72 KB
/
simulate_shocks.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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 16 15:48:34 2020
@author: Ariah
"""
import numpy as np
import scipy.linalg as la
from scipy import stats
import time
import GJ_cascades_dense as cascades
###############################################################################
def SampleShocks(p, rho, sigma, a, samples):
n = len(p)
u = a*np.ones(n)
cov = sigma**2*( rho*np.ones((n,n)) + (1-rho)*np.eye(n) )
mvnorm = stats.multivariate_normal(mean=u, cov=cov)
rvs = mvnorm.rvs(samples)
return rvs
def setup_simulate(C, Dp, theta, beta, rho, sigma, a, samples):
lu, piv = la.lu_factor(np.eye(len(C))-C)
C_hat = cascades.calc_C_hat(C)
fv = cascades.precompute_fv(cascades.f_GJ, lu, piv, C_hat, beta)
rvs = SampleShocks(Dp, rho, sigma, a, samples)
return lu, piv, C_hat, fv, rvs
def run_simulate(lu, piv, C_hat, fv, rvs, C, Dp, theta, beta, samples, b_frac, b_num):
b_array = np.linspace(0, np.sum(Dp)/b_frac, num=b_num)
S_data = np.zeros((samples, b_num))
T_data = np.zeros(samples)
i=0
for ran in rvs:
Dp_prime = np.multiply(Dp, 1+ran)
tilde_theta, Ind_T = cascades.TransformThresh(lu, piv, C_hat, Dp_prime, theta, beta)
start_time = time.time()
x_dict, S_size_dict = cascades.DiscountFrac_batch(fv, cascades.f_GJ, b_array, C, C_hat, beta, lu, piv, tilde_theta, Ind_T, Dp_prime, theta)
y = np.array([S_size_dict[b] for b in b_array])
S_data[i,:] = y
T_data[i] = np.sum(Ind_T)
print("{} simulation complete, {} initial failures, {} seconds, |T|={}, |S|={}".format(i,np.sum(Ind_T), time.time()-start_time, np.sum(Ind_T), np.sum(S_data[i,-1])))
i += 1
return S_data, T_data, b_array