for i in range(100): # ## Generate j_ = 100 arbitrary parameters m_x = rand(n_, 1) m_z = rand(k_, 1) a = rand(n_ + k_, n_ + k_) s2_xz = a @ a.T # ## Compute the coefficients of the classical formulation and the linear formulation # Classical formulation beta = s2_xz[:n_, n_:].dot(pinv(s2_xz[n_:, n_:])) alpha = m_x - beta @ m_z # Linear formulation parameters e_xz_tilde = r_['-1', m_x, s2_xz[:n_, n_:] + m_x @ m_z.T] e_z2_tilde = r_[r_['-1', array([[1]]), m_z.T], r_['-1', m_z, s2_xz[n_:, n_:] + m_z @ m_z.T]] beta_tilde = e_xz_tilde.dot(pinv(e_z2_tilde)) # Frobenius distance dist[i] = linalgnorm(r_['-1', alpha, beta] - beta_tilde, ord='fro') # - # ## Plot the histogram of the Frobenius norms # + f, ax = subplots(1, 1) hist(dist) # save_plot(ax=plt.gca(), extension='png', scriptname=os.path.basename('.')[:-3], count=plt.get_fignums()[-1])
ff[i, :] = cumsum(f) for j in range(j_): # Randomize time series I m, _ = NormalScenarios(zeros((i_, 1)), c2_DGP[k], t_, 'Riccati') U1 = norm.cdf(m) if npsum(U1==0) >= 1: print(k) I = CopMargComb(y, ff, U1) # Evaluate the correlation estimators C2_hat[:,:, j] = corrcoef(I) # sample correlation C2_bar[:,:, j] = real(FactorAnalysis(C2_hat[:,:, j], d, rank)[0]) # shrinkage correlation # Compute the losses L_hat[j] = linalgnorm(C2_hat[:,:, j]-c2_DGP[k], ord='fro')**2 # sample loss L_bar[j] = linalgnorm(C2_bar[:,:, j]-c2_DGP[k], ord='fro')**2 # shrinkage loss # Compute errors er_hat[k] = mean(L_hat) # sample error er_bar[k] = mean(L_bar) # shrinkage error # store loss's distribution and bias for the selected h-th DGP if k == h: # histograms nbins = int(round(10*log(j_))) hgram_hat, x_hat = histogram(L_hat, nbins) hgram_hat = hgram_hat / (nbins*(x_hat[1] - x_hat[0])) hgram_bar, x_bar = histogram(L_bar, nbins) hgram_bar = hgram_bar / (nbins*(x_bar[1] - x_bar[0]))
def GHCalibration(epsi, FP, Tolerance, Da0, Db0, Dg0, Dh0, MaxItex): # ## input # a,b,g,h :[vectors] (n_ x 1) parameters of GH distribution # SqDist :[vector] (n_ x 1) square root of distance beetwen model and data # iter :[vector] (n_ x 1) number of itarations # # # output # epsi :[matrix] (n_ x t_end) time series of invariants # FP :[vector] (1 x t_end) flexible probabilities # Tolerance :[scalar] # Da0,Db0,Dg0,Dh0 :[scalars] step of local search of a,b,g,h respectively # maxitex :[scalar] maximun number of iterations n_ = epsi.shape[0] yy = zeros((epsi.shape)) uu = zeros((epsi.shape)) for nn in range(n_): yy[nn, :], idx = sort(epsi[nn, :]), argsort(epsi[nn, :]) fp = FP[0, idx] uu[nn, :] = cumsum(fp) yy = yy[:, 1:-1] uu = uu[:, 1:-1] n_ = yy.shape[0] aGH = zeros((n_, 1)) bGH = zeros((n_, 1)) gGH = zeros((n_, 1)) hGH = zeros((n_, 1)) SSqDist = zeros((n_, 1)) iiter = zeros((n_, 1)) for k in range(n_): # parameter a0 = mean(yy[k, :]) b0 = std(yy[k, :]) g0 = 1.0e-03 h0 = 1.0e-03 input = norm.ppf(uu[k, :], 0, 1) ## Q_y = a0 + b0 * ((1 / g0) * (exp(g0 * input) - 1) * exp(0.5 * h0 * input**2)) SqDist = linalgnorm(Q_y - yy[k, :]) SqDistfit = 0 iter = 0 ####========================================================================= while (abs((SqDist - SqDistfit)) > Tolerance) and iter < MaxItex: iter = iter + 1 SqDistfit = SqDist ####========================================================================= aa = r_[(a0 - Da0), a0, (a0 + Da0)] bb = r_[(b0 - Db0), b0, (b0 + Db0)] gg = r_[(g0 - Dg0), g0, (g0 + Dg0)] hh = r_[(h0 - Dh0), h0, (h0 + Dh0)] ## DistQ_y = zeros((len(aa), len(bb), len(gg), len(hh))) for ka in range(len(aa)): for kb in range(len(bb)): for kg in range(len(gg)): for kh in range(len(hh)): Q_y = aa[ka] + bb[kb] * (1 / gg[kg]) * ( exp(gg[kg] * input) - 1) * exp( 0.5 * hh[kh] * input**2) DistQ_y[ka, kb, kg, kh] = linalgnorm(Q_y - yy[k, :]) for ka in range(len(aa)): for kb in range(len(bb)): for kg in range(len(gg)): for kh in range(len(hh)): if DistQ_y[ka, kb, kg, kh] == npmin(DistQ_y): SqDist = npmin(DistQ_y) aka = ka bkb = kb gkg = kg hkh = kh break a0 = aa[aka] b0 = bb[bkb] g0 = gg[gkg] h0 = hh[hkh] if h0 < 0: h0 = 0 Dh0 = Dh0 / 100 if g0 < 0: g0 = 0 Dg0 = Dg0 / 100 if b0 < 0: b0 = b0 + Db0 Db0 = Db0 / 100 aGH[k] = a0 bGH[k] = b0 gGH[k] = g0 hGH[k] = h0 SSqDist[k] = SqDist iiter[k] = iter return aGH, bGH, gGH, hGH, SSqDist, iiter
sigma2_hist = cov(epsi) # - # ## Compute the jackknife estimators # + epsi_jack = {} mu_jack = {} sigma2_jack = {} norm_cov = zeros(t_) for t in range(t_): epsi_jack[t] = np.delete(epsi, t, axis=1) mu_jack[t] = mean(epsi_jack[t], 1, keepdims=True) # jackknife mean sigma2_jack[t] = cov(epsi_jack[t]) # jackknife covariance norm_cov[t] = linalgnorm( sigma2_hist - sigma2_jack[t], ord='fro' ) # computation of the distance between the historical and the jackknife covariance estimators # sort the covariance matrices so that the algorithm can select those # which differ the most from the historical one normsort, i_normsort = sort(norm_cov)[::-1], argsort(norm_cov)[::-1] # - # ## Generate figures comparing the historical ellipsoid defined by original data with the jackknife ellipsoid defined by perturbed data. # + k_ = 3 # number of figures for k in range(k_): figure() e_jack = epsi_jack[i_normsort[k_ + 1 - k]]
sigma2 = 5 * (rho * ones((i_)) + (1 - rho) * eye(i_)) # true covariance t_ = 20 # len of time series j_ = 10**4 # number of simulations M = zeros((i_, j_)) L_M = zeros((1, j_)) Sigma2 = zeros((i_, i_, j_)) L_Sigma2 = zeros((1, j_)) for j in range(j_): I = mvnrnd(mu.flatten(), sigma2, t_).T # i_ x t_end # compute the loss of sample mean M[:, j] = mean(I, 1) L_M[0, j] = npsum((mu - M[:, [j]])**2) # compute the loss of sample covariance Sigma2[:, :, j] = cov(I, ddof=1) L_Sigma2[0, j] = linalgnorm(sigma2 - Sigma2[:, :, j], ord='fro')**2 # - # ## Compute error, bias and inefficiency of both estimators # sample mean E_M = mean(M, 1) er_M = mean(L_M) ineff2_M = mean(npsum((M - tile(E_M[..., np.newaxis], (1, j_)))**2, axis=0)) bias2_M = er_M - ineff2_M # sample covariance E_Sigma2 = mean(Sigma2, 2) er_Sigma2 = mean(L_Sigma2) ineff2_Sigma2 = mean( npsum((Sigma2 - tile(E_Sigma2[..., np.newaxis], (1, 1, j_)))**2, axis=(0, 1)))
plt.style.use('seaborn') from cov2corr import cov2corr # - # ## Choose an arbitrary n_ x n_ positive definite covariance matrix sigma2 # initial setup n_ = 50 a = randn(n_, n_) sigma2 = a @ a.T # ## Compute the n_ x n_ positive definite correlation matrix c2 sigma_vec, c2 = cov2corr(sigma2) # ## Perform the Cholesky decomposition of c2 l_tilde_c = cholesky(c2) # ## Compute the symmetric and lower triangular matrix l_tilde_s l_tilde_s = np.diagflat(sigma_vec) @ l_tilde_c # ## Check that sigma2 = l_tilde_s@l_tilde_s.T is the Cholesky decomposition of sigma2 sigma2_chol = l_tilde_s @ l_tilde_s.T check1 = linalgnorm(sigma2_chol - sigma2, ord='fro') check2 = linalgnorm(l_tilde_s - cholesky(sigma2), ord='fro')
e = lambda theta: REnormLRD(theta, mu_, invs2_, n_, k_, matrix)[0] e3 = lambda theta: REnormLRD(theta, mu_, invs2_, n_, k_, matrix)[2] # - # ## Main computations err = zeros((j_, 1)) for j in trange(j_, desc='Simulations'): # Set random variables theta_ = randn(n_ + n_ * k_ + n_, 1) # Compute numerical Hessian numhess = numHess(e, theta_)[0] # Compute analytical Hessian anhess = e3(theta_) # Compute relative error in Frobenius norm err[j] = linalgnorm(anhess - numhess, ord='fro') / linalgnorm(anhess, ord='fro') # ## Display the relative error # + nbins = int(round(10 * log(j_))) figure() p = ones((1, len(err))) / len(err) option = namedtuple('option', 'n_bins') option.n_bins = nbins [n, x] = HistogramFP(err.T, p, option) b = bar(x[:-1], n[0], width=x[1] - x[0], facecolor=[.7, .7, .7]) plt.grid(True)
mu_HBFP[:, 0], sigma2_HBFP[:, :, 0], p_HBFP[0], v_HBFP[0], _ = HighBreakdownFP( epsi, p.copy(), 1) # HBFP mean and covariance from original data # - # ## Detect points outside the HBFP ellipsoid # + lev = 1.2 Diag_lambda2, e = eig(sigma2_HBFP[:, :, 0]) y = zeros((n_, t_)) ynorm = zeros((1, t_)) for t in range(t_): y[:, t] = solve(e @ sqrt(diagflat(Diag_lambda2)), epsi[:, t] - mu_HBFP[:, 0]) ynorm[0, t] = linalgnorm(y[:, t], 2) selection = where(ynorm > lev) # - # ## Shift points outside the HBFP-ellipsoid and compute HFP-mean/cov and HBFP-mean/cov from perturbed data # + print('Computing HFP - mean / cov and HBFP - mean / cov from perturbed data') alpha = 2.9 gamma = 0.27 omega = 0.7 epsi_HBFP = zeros((4, epsi.shape[1])) epsi_HBFP[0:2] = epsi.copy()