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.
new()
Create the pmcmc_parameters object
pmcmc_parameters_nested$new(
parameters,
proposal_varied = NULL,
proposal_fixed = NULL,
populations = NULL,
transform = NULL
)
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.
names()
Return the names of the parameters
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.
summary()
Return a data.frame
with information about
parameters (name, min, max, integer, type (fixed or varied)
and population)
initial()
Return the initial parameter values as a named matrix with rows corresponding to parameters and columns to populations.
mean()
Return the estimate of the mean of the parameters, as set when created (this is not updated by any fitting!)
prior()
Compute the prior(s) for a parameter matrix. Returns a named vector with names corresponding to populations.
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
).
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.
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.
# 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
#>
#>