//' Generate a Quantisation Noise (QN) sequence //' //' Generate an QN sequence given \eqn{Q^2} //' @param N An \code{integer} for signal length. //' @param q2 A \code{double} that contains autocorrection. //' @return A \code{vec} containing the QN process. //' @keywords internal //' @details //' To generate the quantisation noise, we follow this recipe: //' First, we generate using a random uniform distribution: //' \deqn{U_k^*\sim U\left[ {0,1} \right]}{U_k^*~U[0,1]} //' //' Then, we multiple the sequence by \eqn{\sqrt{12}}{sqrt(12)} so: //' \deqn{{U_k} = \sqrt{12} U_k^*}{U_k = sqrt(12)*U_k^*} //' //' Next, we find the derivative of \eqn{{U_k}}{U_k} //' \deqn{{{\dot U}_k} = \frac{{{U_{k + \Delta t}} - {U_k}}}{{\Delta t}}}{U_k^. = (U_(k + (delta)t) - U_k)} //' //' In this case, we modify the derivative such that: //' \eqn{{{\dot U}_k}\Delta t = {U_{k + \Delta t}} - {U_k}}{U_k^. * (delta)t = U_{k + (delta)*t} - U_k} //' //' Thus, we end up with: //' \deqn{{x_k} = \sqrt Q {{\dot U}_k}\Delta t}{x_k = sqrt(Q)*U_k^.*(delta)t} //' \deqn{{x_k} = \sqrt Q \left( {{U_{k + 1}} - {U_k}} \right)}{x_k = sqrt(Q)* (U_(k+1) - U_(k))} //' //' @backref src/gen_process.cpp //' @backref src/gen_process.h //' @examples //' gen_qn(10, 5) // [[Rcpp::export]] arma::vec gen_qn(const unsigned int N, double q2 = .1) { double sqrt12 = sqrt(12); arma::vec gu(N+1); for(unsigned int i=0; i <= N; i++ ) { gu(i) = sqrt12*R::runif(0.0,1.0); } return sqrt(q2)*diff_cpp(gu); }
//' @title Master Wrapper for the GMWM Estimator //' @description This function generates WV, GMWM Estimator, and an initial test estimate. //' @param data A \code{vec} containing the data. //' @param theta A \code{vec} with dimensions N x 1 that contains user-supplied initial values for parameters //' @param desc A \code{vector<string>} indicating the models that should be considered. //' @param objdesc A \code{field<vec>} containing a list of parameters (e.g. AR(1) = c(1,1), ARMA(p,q) = c(p,q,1)) //' @param model_type A \code{string} that represents the model transformation //' @param starting A \code{bool} that indicates whether the supplied values are guessed (T) or are user-based (F). //' @param alpha A \code{double} that handles the alpha level of the confidence interval (1-alpha)*100 //' @param compute_v A \code{string} that describes what kind of covariance matrix should be computed. //' @param K An \code{int} that controls how many times theta is updated. //' @param H An \code{int} that controls how many bootstrap replications are done. //' @param G An \code{int} that controls how many guesses at different parameters are made. //' @param robust A \code{bool} that indicates whether the estimation should be robust or not. //' @param eff A \code{double} that specifies the amount of efficiency required by the robust estimator. //' @return A \code{field<mat>} that contains a list of ever-changing estimates... //' @author JJB //' @references Wavelet variance based estimation for composite stochastic processes, S. Guerrier and Robust Inference for Time Series Models: a Wavelet-Based Framework, S. Guerrier //' @keywords internal //' @export //' @backref src/gmwm_logic.cpp //' @backref src/gmwm_logic.h // [[Rcpp::export]] arma::field<arma::mat> gmwm_master_cpp(arma::vec& data, arma::vec theta, const std::vector<std::string>& desc, const arma::field<arma::vec>& objdesc, std::string model_type, bool starting, double alpha, std::string compute_v, unsigned int K, unsigned int H, unsigned int G, bool robust, double eff){ // Obtain counts of the different models we need to work with std::map<std::string, int> models = count_models(desc); // HACK METHOD (todo: formalize it) // Determine if we need to difference if(models["SARIMA"] > 0){ // Note: s, i, si are 6,7,8 => 5,6,7 for(unsigned int i = 0; i < desc.size(); i ++){ if(objdesc(i).n_elem > 3){ arma::vec sarima_desc = objdesc(i); // Do we need to difference? if(sarima_desc(6) > 0 || sarima_desc(7) > 0){ // Perform differencing in specific order... // First non-seasonal and then seasonal. if(sarima_desc(6) > 0){ // Lag is always 1, number of differences is (i) data = diff_cpp(data, 1, sarima_desc(6)); } if(sarima_desc(7) > 0){ // Lag is always seasonality (s) and differences is (si). data = diff_cpp(data, sarima_desc(5), sarima_desc(7)); } // Kill loop. We only handle the tsmodel object with the first difference. break; } } } } // ------ Variable Declarations // Length of the Time Series unsigned int N = data.n_elem; // Number of Scales (J) unsigned int nlevels = floor(log2(N)); // Number of parameters unsigned int np = theta.n_elem; // Take the mean of the first difference double expect_diff = mean_diff(data); // Guessed values of Theta (user supplied or generated) arma::vec guessed_theta = theta; // MODWT decomp arma::field<arma::vec> modwt_decomp = modwt_cpp(data, "haar", nlevels, "periodic", true); // Obtain WV and confidence intervals arma::mat wvar = wvar_cpp(modwt_decomp, robust, eff, alpha, "eta3"); // Extract arma::vec wv_empir = wvar.col(0); arma::vec ci_lo = wvar.col(1); arma::vec ci_hi = wvar.col(2); //------------------------- // Obtain Covariance Matrix //------------------------- arma::mat V; // compute_cov_cpp is the hard core function. It can only be improved by using parallelization. if(compute_v == "diag" || compute_v == "full"){ arma::field<arma::mat> Vout = compute_cov_cpp(modwt_decomp, nlevels, compute_v, robust, eff); if(robust){ V = Vout(1); }else{ V = Vout(0); } }else{ V = fast_cov_cpp(wvar.col(2), wvar.col(1)); } // Obtain the Omega matrix arma::mat omega = arma::inv(diagmat(V)); // Store the original V matrix (in case of bootstrapping) for use in the update function arma::mat orgV = V; // Calculate the values of the Scales arma::vec scales = scales_cpp(nlevels); // Min-Max / N double ranged = dr_slope(data); // Guess starting values for the theta parameters if(starting){ // Always run guessing algorithm theta = guess_initial(desc, objdesc, model_type, np, expect_diff, N, wvar, scales, ranged, G); // If under ARMA case and only ARMA is in the model, // then see how well these values are. if(desc.size() == 1 && (desc[0] == "SARIMA" || desc[0] == "ARMA11") && N <= 1000){ // Use R's ARIMA function to estimate parameter space arma::vec theta2 = Rcpp_ARIMA(data, objdesc(0)); // Only 1 objdesc in the available. // Obtain the obj function under omega with these initial guesses // DO >>NOT<< USE Yannick's to optimize starting values!!!! double mle_css_obj = getObjFun(theta2, desc, objdesc, model_type, omega, wv_empir, scales); // Obtain the objective function under Yannick's starting algorithm double init_guess_obj = getObjFunStarting(theta, desc, objdesc, model_type, wv_empir, scales); // What performs better? if(mle_css_obj < init_guess_obj){ // Disable starting value optimization if using MLE. theta = theta2; starting = false; } } guessed_theta = theta; } // Obtain the GMWM estimator's estimates. theta = gmwm_engine(theta, desc, objdesc, model_type, wv_empir, omega, scales, starting); // Optim may return a very small value. In this case, instead of saying its zero (yielding a transform issue), make it EPSILON. theta = code_zero(theta); // Enable bootstrapping if(compute_v == "bootstrap"){ for(unsigned int k = 0; k < K; k++){ // Create the full V matrix V = cov_bootstrapper(theta, desc, objdesc, N, robust, eff, H, false); // Update the omega matrix omega = arma::inv(diagmat(V)); // The theta update in this case MUST not use Yannick's starting algorithm. Hence, the false value. theta = gmwm_engine(theta, desc, objdesc, model_type, wv_empir, omega, scales, false); // Optim may return a very small value. In this case, instead of saying its zero (yielding a transform issue), make it EPSILON. theta = code_zero(theta); } } if(desc[0] == "SARIMA" && desc.size() == 1){ arma::vec temp = objdesc(0); unsigned int p = temp(0); if(p != 0 && invert_check(arma::join_cols(arma::ones<arma::vec>(1), -theta.rows(0, p - 1))) == false){ Rcpp::Rcout << "WARNING: This ARMA model contains AR coefficients that are NON-STATIONARY!" << std::endl; } } // Order AR1s / GM so largest phi is first! if(models["AR1"] > 1 || models["GM"] > 1){ theta = order_AR1s(theta, desc, objdesc); } // Obtain the objective value function arma::vec obj_value(1); obj_value(0) = getObjFun(theta, desc, objdesc, model_type, omega, wv_empir, scales); arma::vec dr_s(1); dr_s(0) = ranged; // Decomposition of the WV. arma::mat decomp_theo = decomp_theoretical_wv(theta, desc, objdesc, scales); arma::vec theo = decomp_to_theo_wv(decomp_theo); // Export information back arma::field<arma::mat> out(13); out(0) = theta; out(1) = guessed_theta; out(2) = wv_empir; out(3) = ci_lo; out(4) = ci_hi; out(5) = V; out(6) = orgV; out(7) = expect_diff; out(8) = theo; out(9) = decomp_theo; out(10) = obj_value; out(11) = omega; out(12) = dr_s; return out; }