Esempio n. 1
0
    u = t.cdf(xi[:, n],
              df=10**6,
              loc=mu_marg[n],
              scale=np.sqrt(sigma2_marg[n]))
    u[u <= 10**(-7)] = 10**(-7)
    u[u >= 1 - 10**(-7)] = 1 - 10**(-7)
    xi_tilde[:, n] = t.ppf(u, df=10**6)

# ## [Step 6](https://www.arpm.co/lab/redirect.php?permalink=s_dcc_fit-implementation-step06): Estimate the unconditional correlation matrix via MLFP

# +
_, sigma2_xi_tilde = fit_locdisp_mlfp(xi_tilde, p=p, nu=10**6)
rho2_xi_tilde, _ = cov_2_corr(sigma2_xi_tilde)
rho2 = rho2_xi_tilde

beta, delta2 = factor_analysis_paf(rho2_xi_tilde, k_)
rho2 = beta @ beta.T + np.diag(delta2)
# -

# ## [Step 7](https://www.arpm.co/lab/redirect.php?permalink=s_dcc_fit-implementation-step07): Compute the time series of true invariants via DCC fit

params, r2_t, epsi, q2_t_ = fit_dcc_t(xi_tilde, p, rho2=rho2)
c, a, b = params
q2_t_nextstep = c*rho2 +\
                b*q2_t_ +\
                a*(np.array([epsi[-1, :]])[email protected]([epsi[-1, :]]))
r2_t_nextstep, _ = cov_2_corr(q2_t_nextstep)

# ## Save the data to temporary databases

path = '../../../databases/temporary-databases/'
Esempio n. 2
0
for l in range(l_):
    # calculate standardized invariants
    for i in range(i_):
        epsi_tilde[:, i, l] = tstu.ppf(u[:, i], nu_vec_cop[l])

    # estimate copula parameters with maximum likelihood
    _, sig2 = \
        fit_locdisp_mlfp_difflength(epsi_tilde[:, :, l],
                                    p=p_copula,
                                    nu=nu_vec_cop[l],
                                    threshold=10 ** -3,
                                    maxiter=1000)

    # shrinkage: factor analysis
    beta, delta2 = factor_analysis_paf(sig2, k_)
    sig2_fa = beta @ beta.T + np.diag(delta2)

    # compute correlation matrix
    rho2_copula_vec[:, :, l], _ = cov_2_corr(sig2_fa)

    # compute log-likelihood at times with no missing values
    llike_nu[l] = np.sum(p_copula_bonds * np.log(
        mvt_pdf(epsi_bonds, np.zeros(i_), rho2_copula_vec[:, :, l],
                nu_vec_cop[l])))

# choose nu that gives the highest log-likelihood
l_nu = np.argsort(llike_nu)[-1]
db_estimation_copula = {
    'nu': np.int(nu_vec_cop[l_nu]),
    'rho2': rho2_copula_vec[:, :, l_nu]
Esempio n. 3
0
# +
import numpy as np

from arpym.estimation import factor_analysis_mlf, factor_analysis_paf
from arpym.views import rel_entropy_normal
# -

# ## [Input parameters](https://www.arpm.co/lab/redirect.php?permalink=s_factor_analysis_algos-parameters)

# positive definite matrix
sigma2 = np.array([[1.20, 0.46, 0.77], [0.46, 2.31, 0.08], [0.77, 0.08, 0.98]])
k_ = 1  # number of columns

# ## [Step 1](https://www.arpm.co/lab/redirect.php?permalink=s_factor_analysis_algos-implementation-step01): Compute PAF decomposition of sigma2

beta_paf, delta2_paf = factor_analysis_paf(sigma2, k_=k_)
sigma2_paf = beta_paf @ beta_paf.T + np.diagflat(delta2_paf)

# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_factor_analysis_algos-implementation-step02): Compute MLF decomposition of sigma2

beta_mlf, delta2_mlf = factor_analysis_mlf(sigma2,
                                           k_=k_,
                                           b=beta_paf,
                                           v=delta2_paf)
sigma2_mlf = beta_mlf @ beta_mlf.T + np.diagflat(delta2_mlf)

# ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_factor_analysis_algos-implementation-step03): Compute Frobenius and relative entropy error

err_paf_frobenius = np.linalg.norm(sigma2 - sigma2_paf, ord='fro')
err_mlf_frobenius = np.linalg.norm(sigma2 - sigma2_mlf, ord='fro')
mean = np.array(np.zeros(sigma2.shape[0]))
# ## [Step 2](https://www.arpm.co/lab/redirect.php?permalink=s_systematic_idiosyncratic_lfm-implementation-step02): Compute the covariance of X

# target covariance
if len(beta.shape) == 1:
    k_ = 1
else:
    k_ = beta.shape[1]  # number of factors
if k_ == 1:
    sigma2_x = beta.reshape(-1, 1) * sigma2_h @ beta.reshape(1, -1) + sigma2_u
else:
    sigma2_x = beta @ sigma2_h @ beta.T + sigma2_u

# ## [Step 3](https://www.arpm.co/lab/redirect.php?permalink=s_systematic_idiosyncratic_lfm-implementation-step03): Compute factor loadings

# PAF loadings and residual std. deviations
beta_, delta2 = factor_analysis_paf(sigma2_x, k_=k_, eps=1e-5)
if k_ == 1:
    beta_ = beta_.reshape(-1, 1)

# ## [Step 4](https://www.arpm.co/lab/redirect.php?permalink=s_systematic_idiosyncratic_lfm-implementation-step04): Compute the inverse of the covariance of X

omega2 = np.diagflat(1./delta2)
omega2_beta = omega2 @ beta_
# if k_ == 1:
#     omega2_beta = omega2_beta.reshape(-1, 1)
sigma2_x_inv = omega2 - omega2_beta @ \
    np.linalg.solve(beta_.T @ omega2_beta + np.eye(k_), omega2_beta.T)

# ## [Step 5](https://www.arpm.co/lab/redirect.php?permalink=s_systematic_idiosyncratic_lfm-implementation-step05): Compute the r-squared

r2 = np.trace(beta_.T @ sigma2_x_inv @ beta_)/k_