Title: | Parameter Space Partitioning MCMC for Global Model Evaluation |
---|---|
Description: | Implements an n-dimensional parameter space partitioning algorithm for evaluating the global behaviour of formal computational models as described by Pitt, Kim, Navarro and Myung (2006) <doi:10.1037/0033-295X.113.1.57>. |
Authors: | Lenard Dome [aut, cre], Andy Wills [aut] |
Maintainer: | Lenard Dome <[email protected]> |
License: | GPL (>= 3) |
Version: | 1.0.2 |
Built: | 2024-11-21 04:50:00 UTC |
Source: | https://github.com/lenarddome/psp |
Implements an n-dimensional parameter space partitioning algorithm for evaluating the global behaviour of formal computational models as described by Pitt, Kim, Navarro and Myung (2006) <doi:10.1037/0033-295X.113.1.57>.
Please cite the package in publications. Use citation("psp")
.
Lenard Dome
Maintainer: Lenard Dome [email protected]
Pitt, M. A., Kim, W., Navarro, D. J., & Myung, J. I. (2006). Global model analysis by parameter space partitioning. Psychological Review, 113(1), 57.
psp_control
allows users to define characteristics of the
parameter space partitioning MCMC algorithm as implemented in
psp_global
.
psp_control(radius = 0.1, init, lower, upper, pop = 400, cl = NULL, param_names = NULL, parallel = FALSE, cluster_names = NULL, export_objects = NULL, export_libs = NULL, iterations = 1000)
psp_control(radius = 0.1, init, lower, upper, pop = 400, cl = NULL, param_names = NULL, parallel = FALSE, cluster_names = NULL, export_objects = NULL, export_libs = NULL, iterations = 1000)
radius |
The radius of the hypershere with n-dimensions to sample from. Must be a double or a numeric vector, where elements correspond to parameters in 'init, lower, upper'. Default is 0.1. |
init |
A vector of parameters to use as the first jumping distribution. |
lower , upper
|
Vectors specifiying the lower and upper boundaries of the parameter space for each parameter. The i-th element of lower and upper bounds applies to the i-th parameter. |
pop |
The minimum population psp_global aims to find for each ordinal
pattern discovered. This can stop the parameter search early in case
the population of all ordinal pattern are equal to or larger than
|
parallel |
If TRUE, uses the parallel package to run evaluations of
jumping distributions for each chain parallel. Default value is
|
cl |
If parallel is TRUE, the number of cores to use for
|
param_names |
A character vector that includes the names of each
parameter. If |
cluster_names |
Maintained for backwards-compatibility. See
|
export_objects |
A character vector that includes all of the objects
to be loaded into each cluster. It is handled by
|
export_libs |
A character vector that includes all the packages to
be loaded into each cluster. It is handled by
|
iterations |
The number of global iterations for psp_global. Default is 1000. |
Returns a control list suitable for psp_global
with the above
elements.
# two parameter model psp_control(lower = rep(0, 2), upper = rep(1, 2), init = rep(0.5, 2), radius = rep(0.25, 2), cluster_names = NULL, parallel = FALSE, iterations = 500)
# two parameter model psp_control(lower = rep(0, 2), upper = rep(1, 2), init = rep(0.5, 2), radius = rep(0.25, 2), cluster_names = NULL, parallel = FALSE, iterations = 500)
An all-purpose implementation of the Parameter Space Partitioning MCMC Algorithm described by Pitt, Kim, Navarro, Myung (2006).
psp_global(fn, control = psp_control(), ..., quiet = FALSE)
psp_global(fn, control = psp_control(), ..., quiet = FALSE)
fn |
The ordinal function. It should take a numeric vector (parameter set) as its argument, and return an ordinal response pattern as character (e.g. "A > B"). NA values are not currently allowed. |
control |
a list of control parameters, see |
... |
Additional arguments passed to |
quiet |
If |
This function implements the Parameter Space Partitioning algorithm desribed by Pitt et al. (2006). The algorithm is as follows:
0. Initialize parameter space.
0. Select first set of parameters, and evaluate the model on this set. Its ordinal output will become the first ordinal pattern and the first region in the parameter space.
1. Pick a random jumping distribution from for each ordinal pattern from the sampling region defined by a hypershere with a center of the last recorded parameter set for a given pattern.
2. Evaluate model on all new parameter sets.
3. Record new patterns and their corresponding parameter sets. If the parameter sets returns an already discovered pattern, add parameter set to their records. Return to Step 1.
This process runs can run in parallel for each discovered pattern.
The output of function psp
is a member of the S3
class
of PSP
. A PSP
object is a list with the following items:
ps_partitions |
A |
ps_patterns |
A table with the ordinal patterns discovered and the population of their corresponding region - the number of parameter sets discovered to produce the ordinal pattern. |
ps_ordinal |
A list (if ordinal patterns are multidimensional objects) or character vector (if ordinal patterns are strings or other single values) with the ordinal patterns found. The place of the ordinal pattern corresponds to the names in ps_patterns. |
Pitt, M. A., Kim, W., Navarro, D. J., & Myung, J. I. (2006). Global model analysis by parameter space partitioning. Psychological Review, 113(1), 57.
Weisstein, Eric W. "Hypersphere Point Picking." From MathWorld–A Wolfram Web Resource. https://mathworld.wolfram.com/HyperspherePointPicking.html
library(psp) #' euclidean distance #' #' @param a vector coordinate 1 #' @param b vector coordinate 2 #' @return euclidean distance between coordinates euclidean <- function(a, b) sqrt(sum((a - b)^2)) # define center points for the 10 regions in a two-dimensional space positions <- NULL for (i in seq_len(2)) positions <- cbind(positions, sample(500, 10)) #' dummy hypercube model to test the PSP function #' The model takes in a set of coordinates, calculates its distance from all #' all of available coordinates, then return closest region number. #' This model generalizes to n-dimensions #' #' @param x a vector of coordinates #' @return The number of the region as character #' @examples #' model(runif(5)) model <- function(par) { areas <- NULL for (i in seq_along(par)) { range <- c(1, 0) if (i %% 2 == 0) { range <- c(0, 1) } areas <- cbind(areas, seq(range[1], range[2], length.out = 500)[positions[,i]]) } dist <- apply(areas, 1, function(x) euclidean(par, x)) return(as.character(which.min(dist))) } # run Parameter Space Partitioning with some default settings # Here we run the MCMC for 400 iterations, but the partitioning # will stop if the population of all regions reach 200. # Note that we have to load our utility function into # the clusters, because PSPglobal is currently parallelized. out <- psp_global(model, psp_control(lower = rep(0, 2), upper = rep(1, 2), init = rep(0.5, 2), radius = rep(0.25, 2), pop = 100, parallel = FALSE, iterations = 100)) print(out)
library(psp) #' euclidean distance #' #' @param a vector coordinate 1 #' @param b vector coordinate 2 #' @return euclidean distance between coordinates euclidean <- function(a, b) sqrt(sum((a - b)^2)) # define center points for the 10 regions in a two-dimensional space positions <- NULL for (i in seq_len(2)) positions <- cbind(positions, sample(500, 10)) #' dummy hypercube model to test the PSP function #' The model takes in a set of coordinates, calculates its distance from all #' all of available coordinates, then return closest region number. #' This model generalizes to n-dimensions #' #' @param x a vector of coordinates #' @return The number of the region as character #' @examples #' model(runif(5)) model <- function(par) { areas <- NULL for (i in seq_along(par)) { range <- c(1, 0) if (i %% 2 == 0) { range <- c(0, 1) } areas <- cbind(areas, seq(range[1], range[2], length.out = 500)[positions[,i]]) } dist <- apply(areas, 1, function(x) euclidean(par, x)) return(as.character(which.min(dist))) } # run Parameter Space Partitioning with some default settings # Here we run the MCMC for 400 iterations, but the partitioning # will stop if the population of all regions reach 200. # Note that we have to load our utility function into # the clusters, because PSPglobal is currently parallelized. out <- psp_global(model, psp_control(lower = rep(0, 2), upper = rep(1, 2), init = rep(0.5, 2), radius = rep(0.25, 2), pop = 100, parallel = FALSE, iterations = 100)) print(out)
An all-purpose C++ implementation of the Parameter Space Partitioning MCMC Algorithm described by Pitt, Kim, Navarro, Myung (2006).
pspGlobal(model, discretize, control, save = FALSE, path = ".", extension = ".csv", quiet = FALSE)
pspGlobal(model, discretize, control, save = FALSE, path = ".", extension = ".csv", quiet = FALSE)
model |
It should take a numeric vector (parameter set) as its argument, and return a numeric vector of continuous variables. |
discretize |
The inequality matrix constructor. It should take a numeric
vector of probabilities. It must return a matrix in a |
control |
A |
save |
if |
path |
If 'save = TRUE', the path to the file that will store all evaluated parameters and continuous model outputs. The default path is the current working directory. Evaluated parameters and continuous model outputs are save separately, see Details. |
extension |
If 'save = TRUE', the extension of the file will store
all evaluated parameters and continuous model outputs. The default extension
is |
quiet |
If |
Overview:
This function implements the Parameter Space Partitioning algorithm described by Pitt et al. (2006). The brief overview of the algorithm is as follows:
0. Initialize parameter space.
0. Select the first set of parameters, and evaluate the model on this set. Its ordinal output will become the first ordinal pattern and the first region in the parameter space.
1. Pick a random jumping distribution for each ordinal pattern from the sampling region defined by a hypersphere with a center of the last recorded parameter set for a given pattern. Clamp parameter values with their respective lower and upper bounds.
2. Evaluate the model on all new parameter sets.
3. Record new patterns and their corresponding parameter sets. If the parameter sets return an already discovered pattern, add the parameter set to their records. Return to Step 1.
Tuning the behaviour of the algorithm via control
:
This behavior is further tuned by 'control', which needs to contain a list of the following values:
population. The number of parameter sets in each ordinal region, which serves as a threshold above which pspGlobal will not generate a new jumping distribution for a given ordinal pattern. It has to be an integer.
iterations. The number of global iterations. It has to be an integer. If population is not set or the regions have a population less than the upper bound on their size, the function will stop after the set number of iterations. If population is set, the function will stop after the set number of iterations or when all regions have a population greater than or equal to population, whichever comes first.
lower, upper. Vectors specifying the lower and upper boundaries of the parameter space for each parameter. The i-th element of lower and upper bounds applies to the i-th parameter. If the parameter is not bounded, set the lower and upper bound to -Inf and Inf respectively.
init. A matrix of parameters to use as the first jumping distribution. Each row contains the parameter set, whereas columns correspond to freely varying parameters of the model. The number of columns must be equal to the number of parameters in the model. The number of rows is arbitrary.
radius. The radius of the hypersphere with n-dimensions to sample from. Must be of type double. If you are unsure what to set here, set it to 1.
parameter_names. A character vector that includes the names of each parameter. The order of elements should correspond to the order of parameter columns in init.
stimuli_names. A character vector that includes the names of each continuous model output. The order of elements should correspond to the order of continuous model output in the mode function.
dimensionality. A single integer that specifies the number of dimensions for the inequality matrix. The inequality matrix is a strict upper triangular matrix. The number of rows and columns is equal to each other.
responses. It is an integer that specifies the number of continuous variables in the model output before the ordinal function is applied. See Note 2.
Saving files to disk:
The evaluated parameter sets and their corresponding continuous model outputs
are saved to disk if save = TRUE
. The evaluated parameter sets are saved in
a file with the name path_parameters
and the extension specified,
whereas continuous model outputs are saved in a file with the name path_continuous
and the extension specified.
The output is a list with the following items:
ordinal_patterns |
A 3D array with the ordinal patterns found. The place of the ordinal pattern corresponds to ordinal_counts. |
ordinal_counts |
A table with the ordinal patterns discovered and the population of their corresponding region - the number of parameter sets discovered to produce the ordinal pattern. |
iterations |
Number of iterations completed before reaching a set threshold. |
1. NA values are usually a result of some parameter combination falling outside of what the model implementation can handle. It is best handled outside of the PSP routine, e.g. during the inequality matrix construction. For example, if NA is detected in the matrix, change all values to 99 before returning the output. 2. Ideally, responses and dimensionality should be the same, but we can imagine a scenario where the dimensionality of the inequality matrix will be smaller than the number of responses. For example, when continuous variables are compressed into a more compact format via clustering.
Dome, L., Wills, A. J. (2023) g-distance: On the comparison of model and human heterogeneity. PsyArxiv. doi:10.31234/osf.io/ygmcj.
Pitt, M. A., Kim, W., Navarro, D. J., & Myung, J. I. (2006). Global model analysis by parameter space partitioning. Psychological Review, 113(1), 57. doi:10.1037/0033-295X.113.1.57.
Weisstein, Eric W. "Hypersphere Point Picking." From MathWorld–A Wolfram Web Resource. https://mathworld.wolfram.com/HyperspherePointPicking.html. Accessed 2021-09-30.