ankargren / mfbvar
Showing 3 of 6 files from the diff.

@@ -181,14 +181,34 @@
Loading
181 181
182 182
  ################################################################
183 183
  ### Final calculations
184 -
  lklhd          <- sum(c(loglike(Y = as.matrix(mZ), Lambda = Lambda, Pi_comp = Pi_comp, Q_comp = Q_comp, n_T = n_T_, n_vars = n_vars, n_comp = n_lags * n_vars, z0 = h0, P0 = P0)[-1]))
185 -
  eval_prior_Pi_Sigma <- dnorminvwish(X = t(post_Pi_mean), Sigma = post_Sigma, M = prior_Pi_mean, P = prior_Pi_Omega, S = prior_S, v = n_vars+2)
186 -
  eval_prior_psi      <- dmultn(x = post_psi, m = prior_psi_mean, Sigma = prior_psi_Omega)
187 -
  eval_RB_Pi_Sigma    <- log(mean(eval_Pi_Sigma_RaoBlack(Z_array = Z_red, d = d, post_psi_center = post_psi, post_Pi_center = post_Pi_mean, post_Sigma_center = post_Sigma,
188 -
                                                         post_nu = post_nu, prior_Pi_mean = prior_Pi_mean, prior_Pi_Omega = prior_Pi_Omega, prior_S = prior_S,
189 -
                                                         n_vars = n_vars, n_lags = n_lags, n_reps = n_reps)))
190 -
  eval_marg_psi   <- log(mean(eval_psi_MargPost(Pi_array = Pi, Sigma_array = Sigma, Z_array = Z, post_psi_center = post_psi, prior_psi_mean = prior_psi_mean,
191 -
                                                prior_psi_Omega = prior_psi_Omega, D_mat = D, n_determ = n_determ, n_vars = n_vars, n_lags = n_lags, n_reps = n_reps)))
184 +
  lklhd          <- sum(c(loglike(Y = as.matrix(mZ), Lambda = Lambda,
185 +
                                  Pi_comp = Pi_comp, Q_comp = Q_comp, n_T = n_T_,
186 +
                                  n_vars = n_vars, n_comp = n_lags * n_vars,
187 +
                                  z0 = h0, P0 = P0)[-1]))
188 +
  eval_prior_Pi_Sigma <- dnorminvwish(X = t(post_Pi_mean), Sigma = post_Sigma,
189 +
                                      M = prior_Pi_mean, P = prior_Pi_Omega,
190 +
                                      S = prior_S, v = n_vars+2)
191 +
  eval_prior_psi      <- dmultn(x = post_psi, m = prior_psi_mean,
192 +
                                Sigma = prior_psi_Omega)
193 +
  eval_log_RB <- eval_Pi_Sigma_RaoBlack(Z_array = Z_red, d = d,
194 +
                                        post_psi_center = post_psi,
195 +
                                        post_Pi_center = post_Pi_mean,
196 +
                                        post_Sigma_center = post_Sigma,
197 +
                                        post_nu = post_nu,
198 +
                                        prior_Pi_mean = prior_Pi_mean,
199 +
                                        prior_Pi_Omega = prior_Pi_Omega,
200 +
                                        prior_S = prior_S, n_vars = n_vars,
201 +
                                        n_lags = n_lags, n_reps = n_reps)
202 +
  const <- median(eval_log_RB)
203 +
  eval_RB_Pi_Sigma    <- log(mean(exp(eval_log_RB-const))) + const
204 +
  eval_marg_psi   <- log(mean(eval_psi_MargPost(Pi_array = Pi, Sigma_array = Sigma,
205 +
                                                Z_array = Z,
206 +
                                                post_psi_center = post_psi,
207 +
                                                prior_psi_mean = prior_psi_mean,
208 +
                                                prior_psi_Omega = prior_psi_Omega,
209 +
                                                D_mat = D, n_determ = n_determ,
210 +
                                                n_vars = n_vars, n_lags = n_lags,
211 +
                                                n_reps = n_reps)))
192 212
193 213
  mdd_estimate <- c(lklhd + eval_prior_Pi_Sigma + eval_prior_psi - (eval_RB_Pi_Sigma + eval_marg_psi))
194 214

@@ -28,7 +28,7 @@
Loading
28 28
  /* This function computes the smoothed state vector */
29 29
  /****************************************************/
30 30
  /* Initialize matrices and cubes */
31 -
  arma::mat QQ = Q_comp * Q_comp.t();
31 +
  arma::mat QQ = arma::symmatu(Q_comp * Q_comp.t());
32 32
  arma::mat mv(n_T, n_vars);
33 33
  mv.fill(NA_REAL);
34 34
  arma::mat me(n_T, n_vars);
@@ -53,7 +53,7 @@
Loading
53 53
54 54
  /* Fill some temporary variables */
55 55
  arma::mat h1 = Pi_comp * z0;
56 -
  arma::mat P1 = Pi_comp * P0 * Pi_comp.t() + QQ;
56 +
  arma::mat P1 = arma::symmatu(Pi_comp * P0 * Pi_comp.t() + QQ);
57 57
  arma::mat mH = Lambda.rows(obs_vars);
58 58
  arma::mat vz = mz.cols(obs_vars);
59 59
@@ -61,7 +61,7 @@
Loading
61 61
  vv.cols(obs_vars) = vz - trans(mH * h1);
62 62
  mv.row(0) = vv;
63 63
64 -
  arma::mat aS = mH * P1 * mH.t();
64 +
  arma::mat aS = arma::symmatu(mH * P1 * mH.t());
65 65
  arma::mat mIS = IS.slice(0);
66 66
  mIS(obs_vars, obs_vars) = inv_sympd(aS);
67 67
  IS.slice(0) = mIS;
@@ -71,7 +71,7 @@
Loading
71 71
  aK.slice(0) = mK;
72 72
73 73
  arma::mat h2 = h1 + mK.cols(obs_vars) * trans(vv.cols(obs_vars));
74 -
  arma::mat P2 = (identity_mat - mK.cols(obs_vars) * mH) * P1;
74 +
  arma::mat P2 = arma::symmatu((identity_mat - mK.cols(obs_vars) * mH) * P1);
75 75
76 76
  double log_det_val;
77 77
  double log_det_sign;
@@ -81,7 +81,7 @@
Loading
81 81
    obs_vars = find_finite(mz);
82 82
83 83
    h1 = Pi_comp * h2;
84 -
    P1 = Pi_comp * P2 * Pi_comp.t() + QQ;
84 +
    P1 = arma::symmatu(Pi_comp * P2 * Pi_comp.t() + QQ);
85 85
86 86
    mH = Lambda.rows(obs_vars);
87 87
    vz = mz.cols(obs_vars);
@@ -90,7 +90,7 @@
Loading
90 90
    vv.cols(obs_vars) = vz - trans(mH * h1);
91 91
    mv.row(i) = vv;
92 92
93 -
    aS = mH * P1 * mH.t();
93 +
    aS = arma::symmatu(mH * P1 * mH.t());
94 94
    mIS = IS.slice(i);
95 95
    mIS(obs_vars, obs_vars) = inv_sympd(aS);
96 96
    IS.slice(i) = mIS;
@@ -100,9 +100,9 @@
Loading
100 100
    aK.slice(i) = mK;
101 101
102 102
    h2 = h1 + mK.cols(obs_vars) * trans(vv.cols(obs_vars));
103 -
    P2 = (identity_mat - mK.cols(obs_vars) * mH) * P1;
103 +
    P2 = arma::symmatu((identity_mat - mK.cols(obs_vars) * mH) * P1);
104 104
    log_det(log_det_val, log_det_sign, aS);
105 -
    logl.row(i) = -0.5* obs_vars.n_elem * log(2*M_PI) - (log_det_val + vv.cols(obs_vars) * mIS(obs_vars, obs_vars) * trans(vv.cols(obs_vars)))*0.5;
105 +
    logl.row(i) = -0.5* obs_vars.n_elem * std::log(2*M_PI) - (log_det_val + vv.cols(obs_vars) * mIS(obs_vars, obs_vars) * trans(vv.cols(obs_vars)))*0.5;
106 106
  }
107 107
108 108
  /* The return is the smoothed state vector */

@@ -29,7 +29,7 @@
Loading
29 29
  for (i in 1:length(evals)) {
30 30
    # Demean z, create Z_array (companion form version)
31 31
    demeaned_z <- Z_array[,,i+1] - d %*% post_psi_center
32 -
    demeaned_Z <- build_Z(z = demeaned_z, n_lags = n_lags)
32 +
    demeaned_Z <- mfbvar:::build_Z(z = demeaned_z, n_lags = n_lags)
33 33
    XX <- demeaned_Z[-nrow(demeaned_Z), ]
34 34
    YY <- demeaned_Z[-1, 1:n_vars]
35 35
    XXt.XX <- crossprod(XX)
@@ -48,7 +48,7 @@
Loading
48 48
    post_s_i <- prior_S + s_sample + t(Pi_diff) %*% chol2inv(chol(prior_Pi_Omega + XXt.XX.inv)) %*% Pi_diff
49 49
50 50
    # Evaluate
51 -
    evals[i] <- exp(dnorminvwish(X = t(post_Pi_center), Sigma = post_Sigma_center, M = post_Pi_i, P = post_Pi_Omega_i, S = post_s_i, v = post_nu))
51 +
    evals[i] <- dnorminvwish(X = t(post_Pi_center), Sigma = post_Sigma_center, M = post_Pi_i, P = post_Pi_Omega_i, S = post_s_i, v = post_nu)
52 52
  }
53 53
54 54
  return(evals)
Files Coverage
R 70.85%
inst/include 92.00%
src 87.37%
Project Totals (50 files) 79.04%
1
comment: false
2

3
coverage:
4
  status:
5
    project:
6
      default:
7
        target: auto
8
        threshold: 1%
9
    patch:
10
      default:
11
        target: auto
12
        threshold: 1%
13

14
language: R
15
sudo: false
16
cache: packages
17
after_success:
18
- Rscript -e 'covr::codecov()'
Sunburst
The inner-most circle is the entire project, moving away from the center are folders then, finally, a single file. The size and color of each slice is representing the number of statements and the coverage, respectively.
Icicle
The top section represents the entire project. Proceeding with folders and finally individual files. The size and color of each slice is representing the number of statements and the coverage, respectively.
Grid
Each block represents a single file in the project. The size and color of each block is represented by the number of statements and the coverage, respectively.
Loading