R/RcppExports.R

Defines functions gen_phi rand_thompson set_seed sim_params expand thompson update_post

Documented in expand gen_phi rand_thompson set_seed sim_params thompson update_post

# Generated by using Rcpp::compileAttributes() -> do not edit by hand
# Generator token: 10BE3573-1514-4C36-9D1C-5A225CD40393

#' Compute basis expansion.
#' 
#' @param basis A list of basis functions.
#' @param x The scalar to be expanded.
#' @param J_mod The number of basis functions to be used in the model (INCLUDING THE
#' INTERCEPT!).
#' @param J_true the number of basis functions to be used in the true basis expansion (
#' INCLUDING THE INTERCEPT)!
#' 
#' @return A list containing vectors phi_true and phi_mod. 
gen_phi <- function(basis, x, J_mod, J_true) {
    .Call(`_npbanditC_gen_phi`, basis, x, J_mod, J_true)
}

#' Simulate Bandit Problem (Thompson Sampling)
#' 
#' This function is not intended to be used on its own. Instead, use the higher level
#' function `sim_rand_bandit()`.
#' 
#' @param nsteps The number of steps in the trajectory.
#' @param posteriors Posterior distributions must be passed
#' as a single list, containing elements which are lists associated with
#' each posterior distribution i.e. a list of lists. Each posterior distribution
#' must contain "beta","covar","a","b". NOTE: EVEN IF THERE IS JUST ONE ELEMENT,
#' THE INPUT MUST BE A LIST OF LISTS!
#' @param basis A list of basis functions.
#' @param sd A vector containing the standard deviation for each lever.
#' @param J_mod The number of basis functions to use in the model (INCLUDING THE
#' INTERCEPT).
#' @param J_true The number of basis functions to use in thE TRUE expected reward
#' functions (INCLUDING THE INTERCEPT).
#' @param beta_true A matrix, where each row is a set of coefficients used to compute
#' the true expected reward of each lever.
#' 
#' @return An object of the S4 class "bandit".
#' 
rand_thompson <- function(nsteps, posteriors, basis, sd, J_mod, J_true, beta_true, calc_mse) {
    .Call(`_npbanditC_rand_thompson`, nsteps, posteriors, basis, sd, J_mod, J_true, beta_true, calc_mse)
}

#' Set seed
#' 
#' Call to R's set seed function.
#' 
#' @param seed The seed to generate random outputs.
set_seed <- function(seed) {
    invisible(.Call(`_npbanditC_set_seed`, seed))
}

#' Simulate Parameters
#'
#' Randomly generate a list of parameters from each posterior distribution
#' that is supplied as an input.
#'
#' @param posteriors Posterior distributions must be passed
#' as a single list, containing elements which are lists associated with
#' each posterior distribution i.e. a list of lists. Each posterior distribution
#' must contain "beta","covar","a","b". NOTE: EVEN IF THERE IS JUST ONE ELEMENT,
#' THE INPUT MUST BE A LIST OF LISTS!
#'
#' @return A matrix with each row representing a set of beta coefficients.
sim_params <- function(posteriors) {
    .Call(`_npbanditC_sim_params`, posteriors)
}

#' Basis expansion of x
#' @param x A double to be expanded.
#' @param basis A list of basis functions.
expand <- function(x, basis) {
    .Call(`_npbanditC_expand`, x, basis)
}

#' Generic Multi-Armed-Bandit Simulation
#' 
#' Thompson sampling algorithm is used in order to make decisions. With this
#' function you can choose the mean-reward functions, as well as the basis used
#' to model them.
#' 
#' @param nsteps The number of steps in simulation.
#' @param mean A list, each element should be a function representing the mean
#' for one of the levers available to be interacted with.
#' @param sd A vector which contains the standard deviation of the reward from 
#' each lever.
#' @param posteriors A list of posterior distributions. Each posterior
#' distribution is itself a list of model parameters.
#' @param bas_type Either "fourier" or "poly".
#' 
thompson <- function(nsteps, means, sd, basis, posteriors, bas_type) {
    .Call(`_npbanditC_thompson`, nsteps, means, sd, basis, posteriors, bas_type)
}

#' Update a Posterior Distribution
#' 
#' Takes the current posterior parameters as a prior, and updates them using
#' a single observation: phi and reward.
#' 
#' @param phi A vector containing the basis expansion of context vector x.
#' @param reward The observed reward for the context vector x.
#' @param posterior A list of the current posterior parameters.
#' 
#' @return A list containing the updated posterior parameters.
update_post <- function(phi, reward, posterior) {
    .Call(`_npbanditC_update_post`, phi, reward, posterior)
}
dfcorbin/npbanditC documentation built on March 23, 2020, 5:25 a.m.