Skip to content

Commit

Permalink
Update online() post processing and class vignette
Browse files Browse the repository at this point in the history
  • Loading branch information
BerriJ committed Dec 14, 2023
1 parent e865574 commit 92a42cb
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 10 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export(make_knots)
export(online)
export(oracle)
export(penalty)
export(post_process_model)
export(splines2_basis)
export(tidy)
import(Rcpp)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ profoc 1.3.0
* A new article on the usage of the `conline` C++ class was added.
* Various functions are now exported to R to allow easier interaction with the `conline` C++ class. These functions are: `init_experts_list()`, `make_basis_mats` and `make_hat_mats`
* The code of `online()` was simplified a bit by utilizing the new `init_experts_list()` function.
* Function `post_process_model()` was improved and is now exposed to be used in conjunction with the `conline` C++ class.
* Move aggregation of timings from cppclock.R to clock.h. This make it faster, easier to maintain and simplifies the code (which will be used in python in the future as well).

profoc 1.2.1
Expand Down
11 changes: 11 additions & 0 deletions R/misc.R
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,17 @@ val_or_def <- function(val, def) {
}
}


#' Post Process Data from conline Class
#'
#' This function works in conjunction with the conline class.
#' After the main learning task, it takes the output of the
#' conline class and returns an object suitable for, visualization,
#' further, and deployment.
#' analysis.
#' @param model_instance An instance of conline.
#' @param names A named list with dimnames of `y` and `experts`.
#' @export
post_process_model <- function(model_instance, names) {
# Generate output
model <- list(
Expand Down
2 changes: 1 addition & 1 deletion R/online.R
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ online <- function(y, experts, tau,

model <- post_process_model(model_instance, names)

model_instance$teardown()
model_instance$get_times()
rm(model_instance)

return(model)
Expand Down
2 changes: 1 addition & 1 deletion R/online_update.R
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ update.online <- function(object,

object <- post_process_model(model_instance, names = names)

model_instance$teardown()
model_instance$get_times()
rm(model_instance)

return(object)
Expand Down
16 changes: 10 additions & 6 deletions inst/include/conline.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,15 @@ class conline
std::string method = "bewa";

std::map<std::string, arma::colvec> params;
std::map<std::string, arma::colvec> params_basis_pr;
std::map<std::string, arma::colvec> params_basis_mv;
std::map<std::string, arma::colvec> params_hat_pr;
std::map<std::string, arma::colvec> params_hat_mv;
std::map<std::string, arma::colvec> params_basis_pr = {{"",
arma::ones(0)}};
std::map<std::string, arma::colvec> params_basis_mv = {{"",
arma::ones(0)}};
std::map<std::string, arma::colvec> params_hat_pr = {{"",
arma::ones(0)}};
std::map<std::string, arma::colvec> params_hat_mv = {{"",
arma::ones(0)}};

double forget_past_performance = 0.0;
bool allow_quantile_crossing = false;
bool trace = true;
Expand Down Expand Up @@ -104,8 +109,7 @@ class conline
Rcpp::List &object,
arma::mat &new_y,
arma::field<arma::cube> &new_experts);
Rcpp::List output();
void teardown()
void get_times()
{
clock.stop();
};
Expand Down
20 changes: 20 additions & 0 deletions man/post_process_model.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/conline_exports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ RCPP_MODULE(conlineEx)
.method("getP", &conline::getP)
.method("getK", &conline::getK)
.method("getX", &conline::getX)
.method("teardown", &conline::teardown);
.method("get_times", &conline::get_times);
}
28 changes: 27 additions & 1 deletion vignettes/class.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,31 @@ The learning process fills the class objects. So we can inspect them using the `
head(model$weights[[T]][, , 1])
```

However, we can also use the post processing functions from `online()` to access the results. This will create output that is identical to the output of `online()`.
However, we can also use the post processing function of `online()` to access the results. This will create output that is identical to the output of `online()`:

```{r}
names <- list(y = dimnames(y))
names$experts <- list(
1:T,
paste("Marginal", 1:D),
tau,
paste("Expert", 1:N)
)
output <- post_process_model(model, names)
```

We can now use `output` in `update()`, `plot()` and others.

At this point, we do not need to keep the model in memory anymore. So we can delete it:

```{r}
rm(model)
```

## Summary

The C++ class `conline` allows you to gain fine grained control over the learning process. However, it is not as convenient as the `online()` wrapper. So you should only use it if you need to alter the default behavior. However, mixing up helper
functions from `online()` and the C++ class is possible. So you can compute your
combinations using the class interface while still being able to use `update()`,
`plot()` and others afterward.

0 comments on commit 92a42cb

Please sign in to comment.