Skip to content

Commit f7bb60f

Browse files
committed
Merge branch 'feature/mv-header' into develop
2 parents 6791899 + 17eea48 commit f7bb60f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

77 files changed

+5422
-6505
lines changed

R/RcppExports.R

+976-1,008
Large diffs are not rendered by default.

R/forecast.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ predict.bvarmn <- function(object, n_ahead, n_iter = 100L, level = .05, num_thre
182182
# include_mean = object$type == "const",
183183
# nthreads = num_thread
184184
# )
185-
pred_res <- forecast_bvar(object, n_ahead, n_iter)
185+
pred_res <- forecast_bvar(object, n_ahead, n_iter, sample.int(.Machine$integer.max, size = 1))
186186
# Point forecasting (Posterior mean)--------------
187187
pred_mean <- pred_res$posterior_mean
188188
var_names <- colnames(object$y0)
@@ -243,7 +243,7 @@ predict.bvarmn <- function(object, n_ahead, n_iter = 100L, level = .05, num_thre
243243
#' @order 1
244244
#' @export
245245
predict.bvharmn <- function(object, n_ahead, n_iter = 100L, level = .05, num_thread = 1, ...) {
246-
pred_res <- forecast_bvharmn(object, n_ahead, n_iter)
246+
pred_res <- forecast_bvharmn(object, n_ahead, n_iter, sample.int(.Machine$integer.max, size = 1))
247247
# dim_data <- object$m
248248
# num_chains <- object$chain
249249
# if (num_thread > get_maxomp()) {
@@ -335,7 +335,7 @@ predict.bvarflat <- function(object, n_ahead, n_iter = 100L, level = .05, num_th
335335
# include_mean = object$type == "const",
336336
# nthreads = num_thread
337337
# )
338-
pred_res <- forecast_bvar(object, n_ahead, n_iter)
338+
pred_res <- forecast_bvar(object, n_ahead, n_iter, sample.int(.Machine$integer.max, size = 1))
339339
# Point forecasting (Posterior mean)--------------
340340
pred_mean <- pred_res$posterior_mean
341341
var_names <- colnames(object$y0)

R/generate-process.R

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ sim_mnormal <- function(num_sim, mu = rep(0, 5), sig = diag(5), method = c("eige
2525
if (method == "eigen") {
2626
return( sim_mgaussian(num_sim, mu, sig) )
2727
}
28-
sim_mgaussian_chol(num_sim, mu, sig)
28+
sim_mgaussian_chol_export(num_sim, mu, sig)
2929
}
3030

3131
#' Generate Multivariate t Random Vector

R/summary-forecast.R

+6-6
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ forecast_roll.normaliw <- function(object, n_ahead, y_test, num_thread = 1, use_
169169
# }
170170
res_mat <- switch(model_type,
171171
"bvarmn" = {
172-
roll_bvar(y, object$p, object$spec, include_mean, n_ahead, y_test, num_thread)
172+
roll_bvar(y, object$p, object$spec, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
173173
# roll_bvar(
174174
# y, object$p, num_chains, object$iter, object$burn, object$thin,
175175
# fit_ls,
@@ -179,7 +179,7 @@ forecast_roll.normaliw <- function(object, n_ahead, y_test, num_thread = 1, use_
179179
# )
180180
},
181181
"bvarflat" = {
182-
roll_bvarflat(y, object$p, object$spec$U, include_mean, n_ahead, y_test, num_thread)
182+
roll_bvarflat(y, object$p, object$spec$U, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
183183
# roll_bvarflat(
184184
# y, object$p, num_chains, object$iter, object$burn, object$thin,
185185
# fit_ls,
@@ -189,7 +189,7 @@ forecast_roll.normaliw <- function(object, n_ahead, y_test, num_thread = 1, use_
189189
# )
190190
},
191191
"bvharmn" = {
192-
roll_bvhar(y, object$week, object$month, object$spec, include_mean, n_ahead, y_test, num_thread)
192+
roll_bvhar(y, object$week, object$month, object$spec, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
193193
# roll_bvhar(
194194
# y, object$week, object$month, num_chains, object$iter, object$burn, object$thin,
195195
# fit_ls,
@@ -699,7 +699,7 @@ forecast_expand.normaliw <- function(object, n_ahead, y_test, num_thread = 1, us
699699
res_mat <- switch(
700700
model_type,
701701
"bvarmn" = {
702-
expand_bvar(y, object$p, object$spec, include_mean, n_ahead, y_test, num_thread)
702+
expand_bvar(y, object$p, object$spec, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
703703
# expand_bvar(
704704
# y, object$p, num_chains, object$iter, object$burn, object$thin,
705705
# fit_ls,
@@ -709,7 +709,7 @@ forecast_expand.normaliw <- function(object, n_ahead, y_test, num_thread = 1, us
709709
# )
710710
},
711711
"bvarflat" = {
712-
expand_bvarflat(y, object$p, object$spec$U, include_mean, n_ahead, y_test, num_thread)
712+
expand_bvarflat(y, object$p, object$spec$U, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
713713
# expand_bvarflat(
714714
# y, object$p, num_chains, object$iter, object$burn, object$thin,
715715
# fit_ls,
@@ -719,7 +719,7 @@ forecast_expand.normaliw <- function(object, n_ahead, y_test, num_thread = 1, us
719719
# )
720720
},
721721
"bvharmn" = {
722-
expand_bvhar(y, object$week, object$month, object$spec, include_mean, n_ahead, y_test, num_thread)
722+
expand_bvhar(y, object$week, object$month, object$spec, include_mean, n_ahead, y_test, sample.int(.Machine$integer.max, size = num_horizon), num_thread)
723723
# expand_bvhar(
724724
# y, object$week, object$month, num_chains, object$iter, object$burn, object$thin,
725725
# fit_ls,

inst/include/bvhar/bayesfit

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef BVHAR_BAYESFIT_H
2+
#define BVHAR_BAYESFIT_H
3+
4+
// MNIW
5+
#include "src/bayes/mniw/minnesota.h"
6+
7+
// CTA
8+
#include "src/bayes/triangular/triangular.h"
9+
10+
#endif // BVHAR_BAYESFIT_H

inst/include/bvhar/forecast

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef BVHAR_FORECAST_H
2+
#define BVHAR_FORECAST_H
3+
4+
// OLS
5+
#include "src/ols/forecaster.h"
6+
7+
// MNIW
8+
#include "src/bayes/mniw/forecaster.h"
9+
10+
// CTA
11+
#include "src/bayes/triangular/forecaster.h"
12+
13+
#endif // BVHAR_FORECAST_H

inst/include/bvhar/ols

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#ifndef BVHAR_OLS_H
2+
#define BVHAR_OLS_H
3+
4+
#include "src/ols/ols.h"
5+
#include "src/ols/forecaster.h"
6+
#include "src/ols/spillover.h"
7+
8+
#endif // BVHAR_OLS_H

inst/include/bvhar/spillover

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#ifndef BVHAR_SPILLOVER_H
2+
#define BVHAR_SPILLOVER_H
3+
4+
// OLS
5+
#include "src/ols/spillover.h"
6+
7+
// MNIW
8+
#include "src/bayes/mniw/spillover.h"
9+
10+
// CTA
11+
#include "src/bayes/triangular/spillover.h"
12+
13+
#endif // BVHAR_SPILLOVER_H

inst/include/bvhardraw.h renamed to inst/include/bvhar/src/bayes/misc/draw.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#ifndef BVHARDRAW_H
2-
#define BVHARDRAW_H
1+
#ifndef BVHAR_BAYES_MISC_DRAW_H
2+
#define BVHAR_BAYES_MISC_DRAW_H
33

4-
#include "bvharsim.h"
4+
#include "../../math/random.h"
55
#include <set>
66

77
namespace bvhar {
@@ -1310,4 +1310,4 @@ inline Eigen::Matrix<typename Derived::Scalar, Derived::RowsAtCompileTime, Deriv
13101310

13111311
} // namespace bvhar
13121312

1313-
#endif // BVHARDRAW_H
1313+
#endif // BVHAR_BAYES_MISC_DRAW_H

inst/include/minnforecaster.h renamed to inst/include/bvhar/src/bayes/mniw/forecaster.h

+21-18
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#ifndef MINNFORECASTER_H
2-
#define MINNFORECASTER_H
1+
#ifndef BVHAR_BAYES_MNIW_FORECASTER_H
2+
#define BVHAR_BAYES_MNIW_FORECASTER_H
33

4-
#include "minnesota.h"
5-
#include "bvharsim.h"
4+
#include "./minnesota.h"
5+
#include "../../math/random.h"
66

77
namespace bvhar {
88

@@ -12,8 +12,8 @@ class BvharForecaster;
1212

1313
class MinnForecaster {
1414
public:
15-
MinnForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, int ord, int num_sim, bool include_mean)
16-
: response(response_mat),
15+
MinnForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, int ord, int num_sim, bool include_mean, unsigned int seed)
16+
: rng(seed), response(response_mat),
1717
posterior_mean(fit._coef), posterior_sig(fit._prec.inverse()),
1818
posterior_iw_scale(fit._iw_scale), posterior_iw_shape(fit._iw_shape),
1919
// MinnForecaster(const MinnRecords& records, int step, const Eigen::MatrixXd& response_mat, int ord, bool include_mean)
@@ -61,7 +61,7 @@ class MinnForecaster {
6161
void forecastDensity() {
6262
// Eigen::MatrixXd forecastDensity() {
6363
for (int i = 0; i < num_sim; ++i) {
64-
coef_and_sig[i] = sim_mn_iw(posterior_mean, posterior_sig, posterior_iw_scale, posterior_iw_shape, false);
64+
coef_and_sig[i] = sim_mn_iw(posterior_mean, posterior_sig, posterior_iw_scale, posterior_iw_shape, false, rng);
6565
}
6666
// std::lock_guard<std::mutex> lock(mtx);
6767
for (int h = 0; h < step; ++h) {
@@ -81,10 +81,10 @@ class MinnForecaster {
8181
}
8282
// return predictive_distn;
8383
}
84-
Rcpp::List returnForecast() const {
85-
return Rcpp::List::create(
86-
Rcpp::Named("posterior_mean") = pred_save,
87-
Rcpp::Named("predictive") = predictive_distn
84+
LIST returnForecast() const {
85+
return CREATE_LIST(
86+
NAMED("posterior_mean") = pred_save,
87+
NAMED("predictive") = predictive_distn
8888
);
8989
}
9090
Eigen::MatrixXd returnPoint() {
@@ -94,6 +94,7 @@ class MinnForecaster {
9494
protected:
9595
// MinnRecords mn_record;
9696
// std::mutex mtx;
97+
boost::random::mt19937 rng;
9798
Eigen::MatrixXd response;
9899
Eigen::MatrixXd posterior_mean;
99100
Eigen::MatrixXd posterior_sig;
@@ -120,8 +121,8 @@ class MinnForecaster {
120121

121122
class BvarForecaster : public MinnForecaster {
122123
public:
123-
BvarForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, int lag, int num_sim, bool include_mean)
124-
: MinnForecaster(fit, step, response_mat, lag, num_sim, include_mean) {}
124+
BvarForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, int lag, int num_sim, bool include_mean, unsigned int seed)
125+
: MinnForecaster(fit, step, response_mat, lag, num_sim, include_mean, seed) {}
125126
// BvarForecaster(const MinnRecords& records, int step, const Eigen::MatrixXd& response_mat, int lag, bool include_mean)
126127
// : MinnForecaster(records, step, response_mat, lag, include_mean) {}
127128
virtual ~BvarForecaster() = default;
@@ -142,16 +143,17 @@ class BvarForecaster : public MinnForecaster {
142143
// Eigen::Map<Eigen::MatrixXd>(sig_update.block(h, 0, 1, 1).data(), 1, 1), // -> Matrix but too complex
143144
mn_scl,
144145
coef_and_sig[i][1],
145-
false
146+
false,
147+
rng
146148
);
147149
}
148150
}
149151
};
150152

151153
class BvharForecaster : public MinnForecaster {
152154
public:
153-
BvharForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, const Eigen::MatrixXd& har_trans, int month, int num_sim, bool include_mean)
154-
: MinnForecaster(fit, step, response_mat, month, num_sim, include_mean), har_trans(har_trans),
155+
BvharForecaster(const MinnFit& fit, int step, const Eigen::MatrixXd& response_mat, const Eigen::MatrixXd& har_trans, int month, int num_sim, bool include_mean, unsigned int seed)
156+
: MinnForecaster(fit, step, response_mat, month, num_sim, include_mean, seed), har_trans(har_trans),
155157
transformed_sig(har_trans.transpose() * posterior_sig * har_trans) {}
156158
// BvharForecaster(const MinnRecords& records, int step, const Eigen::MatrixXd& response_mat, const Eigen::MatrixXd& har_trans, int month, bool include_mean)
157159
// : MinnForecaster(records, step, response_mat, month, include_mean), har_trans(har_trans) {}
@@ -174,7 +176,8 @@ class BvharForecaster : public MinnForecaster {
174176
// Eigen::MatrixXd::Constant(1, 1, sig_update[h]),
175177
mn_scl,
176178
coef_and_sig[i][1],
177-
false
179+
false,
180+
rng
178181
);
179182
}
180183
}
@@ -185,4 +188,4 @@ class BvharForecaster : public MinnForecaster {
185188

186189
} // namespace bvhar
187190

188-
#endif // MINNFORECASTER_H
191+
#endif // BVHAR_BAYES_MNIW_FORECASTER_H

0 commit comments

Comments
 (0)