Construct nested parameters for use with pmcmc(). This creates a utility object that is used internally to work with parameters that may be fixed and the same for all given populations, or varied and possibly-different between populations. Most users only need to construct this object, but see the examples for how it can be used.

Methods


Method new()

Create the pmcmc_parameters object

Usage

pmcmc_parameters_nested$new(
  parameters,
  proposal_varied = NULL,
  proposal_fixed = NULL,
  populations = NULL,
  transform = NULL
)

Arguments

parameters

A list of pmcmc_parameter or pmcmc_varied_parameter objects, each of which describe a single (possibly-varying) parameter in your model. If parameters is named, then these names must match the $name element of each parameter that is used (this is verified).

proposal_varied, proposal_fixed

Square proposal matrices corresponding to the variance-covariance matrix of a multivariate gaussian distribution used to generate new varied and fixed parameters respectively.'. They must have the same number of rows and columns as there are varied and fixed parameters respectively. The names must correspond exactly to the names in parameters. Because it corresponds to a variance-covariance matrix it must be symmetric and positive definite.

populations

Specifies the names of the different populations that the varying parameters change according to. Only required if no pmcmc_varied_parameter objects are included in parameters. Otherwise population names are taken from those objects.

transform

An optional transformation function to apply to your parameter vector immediately before passing it to the model function. If not given, then as.list is used, as dust models require this. However, if you need to generate derived parameters from those being actively sampled you can do arbitrary transformations here.


Method names()

Return the names of the parameters

Usage

pmcmc_parameters_nested$names(type = "both")

Arguments

type

One of "both" (the default, all parameters), "fixed" (parameters that are shared across populations) or "varied" (parameters that vary over populations).


Method populations()

Return the names of the populations

Usage

pmcmc_parameters_nested$populations()


Method validate()

Validate a parameter matrix. This method checks that your matrix has the expected size (rows according to parameters, columns to populations) and if named that the names are exactly what is expected. It also verifies that the fixed parameters are same across all populations.

Usage

pmcmc_parameters_nested$validate(theta)

Arguments

theta

a parameter matrix


Method summary()

Return a data.frame with information about parameters (name, min, max, integer, type (fixed or varied) and population)

Usage

pmcmc_parameters_nested$summary()


Method initial()

Return the initial parameter values as a named matrix with rows corresponding to parameters and columns to populations.

Usage

pmcmc_parameters_nested$initial()


Method mean()

Return the estimate of the mean of the parameters, as set when created (this is not updated by any fitting!)

Usage

pmcmc_parameters_nested$mean(type)


Method vcv()

Return the variance-covariance matrix used for the proposal.

Usage

pmcmc_parameters_nested$vcv(type)


Method prior()

Compute the prior(s) for a parameter matrix. Returns a named vector with names corresponding to populations.

Usage

pmcmc_parameters_nested$prior(theta)

Arguments

theta

a parameter matrix with columns in the same order as $names() and rows in the same order as $populations().


Method propose()

This proposes a new parameter matrix given your current matrix and the variance-covariance matrices of the proposal kernels, rounds any integer values, and reflects bounded parameters until they lie within min:max. Returns matrix with rows corresponding to parameters and columns to populations (i.e., the same orientation as theta).

Usage

pmcmc_parameters_nested$propose(theta, type, scale = 1, vcv = NULL)

Arguments

theta

a parameter matrix with rows in the same order as $names() and columns in the same order as $populations().

type

specifies which type of parameters should be proposed, either fixed parameters only ("fixed"), varied only ("varied"), or both ("both") types. For 'fixed' and 'varied', parameters of the other type are left unchanged.

scale

an optional scaling factor to apply to the proposal distribution. This may be useful in sampling starting points. The parameter is equivalent to a multiplicative factor applied to the variance covariance matrix.


Method model()

Apply the model transformation function to a parameter matrix.

Usage

pmcmc_parameters_nested$model(theta)

Arguments

theta

a parameter matrix with rows in the same order as $names() and columns in the same order as $populations().


Method fix()

Set some parameters to fixed values. Use this to reduce the dimensionality of your system. Note that this function has an unfortunate name collision - we use "fixed" and "varied" parameters generally to refer to ones that are fixed across populations or which vary among populations. However, in the context of this method "fixed" refers to parameters which will be set to a single value and no longer used in inference.

Usage

pmcmc_parameters_nested$fix(fixed)

Arguments

fixed

a named vector of parameters to fix

Examples

# Construct an object with two varied parameters ('a' and 'b'),
# two fixed parameters ('c' and 'd') and two populations ('p1' and 'p2')
parameters <- list(mcstate::pmcmc_varied_parameter("a", c("p1", "p2"), 2),
                   mcstate::pmcmc_varied_parameter("b", c("p1", "p2"), 2),
                   mcstate::pmcmc_parameter("c", 3),
                   mcstate::pmcmc_parameter("d", 4))
proposal_fixed <- diag(2)
proposal_varied <- diag(2) + 1
pars <- mcstate::pmcmc_parameters_nested$new(parameters, proposal_varied,
                                             proposal_fixed)

# Initial parameters
p <- pars$initial()
p
#>   p1 p2
#> a  2  2
#> b  2  2
#> c  3  3
#> d  4  4

# Propose a new parameter point
pars$propose(p, type = "both")
#>         p1         p2
#> a 4.149574 0.06419981
#> b 3.407393 0.31816646
#> c 2.524651 2.52465097
#> d 1.026509 1.02650910
pars$propose(p, type = "fixed")
#>         p1       p2
#> a 2.000000 2.000000
#> b 2.000000 2.000000
#> c 2.307868 2.307868
#> d 3.722765 3.722765
pars$propose(p, type = "varied")
#>         p1       p2
#> a 1.306708 1.107105
#> b 2.516529 1.026720
#> c 3.000000 3.000000
#> d 4.000000 4.000000

# Information about parameters:
pars$names()
#> [1] "a" "b" "c" "d"
pars$names("fixed")
#> [1] "c" "d"
pars$names("varied")
#> [1] "a" "b"
pars$summary()
#>   name  min max discrete integer   type population
#> 1    a -Inf Inf    FALSE   FALSE varied         p1
#> 2    b -Inf Inf    FALSE   FALSE varied         p1
#> 3    c -Inf Inf    FALSE   FALSE  fixed         p1
#> 4    d -Inf Inf    FALSE   FALSE  fixed         p1
#> 5    a -Inf Inf    FALSE   FALSE varied         p2
#> 6    b -Inf Inf    FALSE   FALSE varied         p2
#> 7    c -Inf Inf    FALSE   FALSE  fixed         p2
#> 8    d -Inf Inf    FALSE   FALSE  fixed         p2

# Compute log prior probability, per population
pars$prior(p)
#> p1 p2 
#>  0  0 

# Transform data for your model
pars$model(p)
#> [[1]]
#> [[1]]$a
#> [1] 2
#> 
#> [[1]]$b
#> [1] 2
#> 
#> [[1]]$c
#> [1] 3
#> 
#> [[1]]$d
#> [1] 4
#> 
#> 
#> [[2]]
#> [[2]]$a
#> [1] 2
#> 
#> [[2]]$b
#> [1] 2
#> 
#> [[2]]$c
#> [1] 3
#> 
#> [[2]]$d
#> [1] 4
#> 
#>