Helper Functions
Here, we document helper functions in which a typical user may be interested as well as key lower-level helper functions. Less "essential" lower-level helper functions also have docstrings, but we do not report their docstrings because only a developer would need to know them. However, these functions do actually have docstrings, so a developer will find it helpful to use ?
in Julia's interactive mode.
Cloud Helpers
SMC.Cloud
— MethodCloud(n_params::Int, n_parts::Int)
Easier constructor for Cloud, which initializes the weights to be equal, and everything else in the particle object to be empty.
SMC.get_weights
— Methodget_weights(c::Cloud)
Returns Vector{Float64}(n_parts) of weights of particles in cloud.
SMC.get_vals
— Methodget_vals(c::Matrix{Float64})
Returns Matrix{Float64}(nparams, nparts) of parameter values in particle cloud.
Missing docstring for SMC.get_loglh(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.get_old_loglh(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.get_logpost(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.get_logprior(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.get_accept(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.get_likeliest_particle_value(c::Cloud)
. Check Documenter's build log for details.
SMC.split_cloud
— Functionsplit_cloud(filename::String, n_pieces::Int)
splits the cloud saved in filename
to multiple files (n_pieces
of them). For large clouds, the memory usage by one file may be too large. For example, the file may exceed GitHub's 100MB memory limit.
SMC.join_cloud
— Functionjoin_cloud(filename::String, n_pieces::Int; save_cloud::Bool = true)
joins a cloud saved in filename
that had previously been split into multiple files (n_pieces
of them) and saves the newly joined cloud when the kwarg save_cloud
is true
. For large clouds, the memory usage by one file may be too large. For example, the file may exceed GitHub's 100MB memory limit. For this reason, it is useful to split the cloud file into multiple files and then rejoin later.
SMC.add_parameters_to_cloud
— Functionadd_parameters_to_cloud(old_cloud_file::String, para::ParameterVector,
old_para_inds::BitVector; regime_switching::Bool = false)
add_parameters_to_cloud(old_cloud::Cloud, para::ParameterVector, old_para_inds::BitVector;
regime_switching::Bool = false) where {T <: Real}
extends a Cloud
from a previous estimation to include new parameters. This function helps construct a bridge distribution when you want to estimate a model that extends a previous model by adding additional parameters.
To be concrete, suppose we have two models $\mathcal{M}_1$ and $\mathcal{M}_2$ such that the parameters of the first model are a subset of the parameters of the second model. For example, suppose $\theta_1$ are the parameters for the first model, and $\theta_2 = [\theta_1, \tilde{\theta}]^T$, where $\tilde{\theta}$ are the new parameters for $\mathcal{M}_2$. Assume that
(1) the likelihood function for $\mathcal{M}_1` does not depend on$ ilde{\theta}$, (2) the priors for$ heta_1$and$ ilde{ heta}`` are independent.
Then the posterior for $\theta_2$ given $\mathcal{M}_1$ is just $math \begin{aligned} \pi(\theta_2 \vert Y, \mathcal{M}_1) = \pi(\theta_1 \vert Y, \mathcal{M}_1) p(\tilde{\theta}). \end{aligned}$ Therefore, we can construct the posterior $\pi(\theta_2 \vert Y, \mathcal{M}_1)$ by concatenating draws from the prior for $\tilde{\theta}$ to the previous estimation of $\mathcal{M}_1$. This function performs this concatenation and allows for regime-switching.
Inputs
old_cloud
orold_cloud_file
: this input should specify theCloud
from a previous estimation of the old model. If a different package was used to estimate the model, then the user can construct a newCloud
object. The only fields which must be set correctly are theparticles
andESS
field. The others can be set to default values (see?Cloud
and the source code for the construction of theCloud
object).para
: the parameter vector of the new model (i.e. $\theta_2$). The parameters inpara
that belonged to the old model should have the same settings as the ones used in previous estimation, e.g. the same prior.old_para_inds
: indicates which parameters were used in the old estimation. Ifregime_switching = true
, then this vector should specify which values correspond to old parameter values based on the matrix returned bySMC.get_values(para)
. For example, ifold_para
is theParameterVector
used by the old estimation, thenSMC.get_values(old_para) == SMC.get_values(para)[old_para_inds]
Keyword Arguments
regime_switching
: this kwarg is needed to be able to draw from the prior correctly for the new parameters.
Summary Statistics of Posterior
Missing docstring for SMC.weighted_mean(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.weighted_quantile(c::Cloud, i::Int64)
. Check Documenter's build log for details.
Missing docstring for SMC.weighted_std(c::Cloud)
. Check Documenter's build log for details.
Missing docstring for SMC.weighted_cov(c::Cloud)
. Check Documenter's build log for details.
SMC Algorithm Helpers
SMC.solve_adaptive_ϕ
— Method`function solve_adaptive_ϕ(cloud::Cloud, proposed_fixed_schedule::Vector{Float64},
i::Int64, j::Int64, ϕ_prop::Float64, ϕ_n1::Float64,
tempering_target::Float64, resampled_last_period::Bool)`
Solves for next Φ. Returns ϕn, resampledlastperiod, j, ϕprop.
SMC.mvnormal_mixture_draw
— Method`mvnormal_mixture_draw(θ_old::Vector{T}, d_prop::Distribution;
c::T = 1.0, α::T = 1.0) where T<:AbstractFloat`
Create a DegenerateMvNormal
distribution object, d
, from a parameter vector, p
, and a standard deviation matrix (obtained from SVD), σ
.
Generate a draw from the mixture distribution of:
- A
DegenerateMvNormal
centered at θ_old with the standard deviation matrixσ
, scaled bycc^2
and with mixture proportionα
. - A
DegenerateMvNormal
centered at the same mean, but with a standard deviation matrix of the diagonal entries ofσ
scaled bycc^2
with mixture proportion(1 - α)/2
. - A
DegenerateMvNormal
with the same standard deviation matrixσ
but centered at the new proposed mean,θ_prop
, scaled bycc^2
, and with mixture proportion(1 - α)/2
.
If no θ_prop
is given, but an α
is specified, then the mixture will consist of α
of the standard distribution and (1 - α)
of the diagonalized distribution.
Arguments
θ_old::Vector{T}
: The mean of the desired distributionσ::Matrix{T}
: The standard deviation matrix of the desired distribution
Keyword Arguments
cc::T
: The standard deviation matrix scaling factorα::T
: The mixing proportionθ_prop::Vector{T}
: The proposed parameter vector to be used as part of the mixture distribution, set by default to be the weighted mean of the particles, prior to mutation.
Outputs
θ_new::Vector{T}
: The draw from the mixture distribution to be used as the MH proposed step
SMC.compute_ESS
— Methodfunction `compute_ESS(loglh::Vector{T}, current_weights::Vector{T}, ϕ_n::T, ϕ_n1::T;
old_loglh::Vector{T} = zeros(length(loglh))) where {T<:AbstractFloat}`
Compute ESS given log likelihood, current weights, ϕn, ϕ{n-1}, and old log likelihood.
SMC.generate_free_blocks
— Method`generate_free_blocks(n_free_para::Int64, n_blocks::Int64)`
Return a Vector{Vector{Int64}} where each internal Vector{Int64} contains a subset of the range 1:nfreepara of randomly permuted indices. This is used to index out random blocks of free parameters from the covariance matrix for the mutation step.
SMC.generate_all_blocks
— Method`generate_all_blocks(blocks_free::Vector{Vector{Int64}}, free_para_inds::Vector{Int64})`
Return a Vector{Vector{Int64}} where each internal Vector{Int64} contains indices corresponding to those in blocks_free
but mapping to 1:n_para
(as opposed to 1:n_free_para
). These blocks are used to reconstruct the particle vector by inserting the mutated free parameters into the size n_para,
particle vector, which also contains fixed parameters.
Missing docstring for mutation(loglikelihood::Function, parameters::ParameterVector{U}, data::Matrix{S}, p::Vector{S}, d_μ::Vector{S}, d_Σ::Matrix{S}, blocks_free::Vector{Vector{Int}}, blocks_all::Vector{Vector{Int}}, ϕ_n::S, ϕ_n1::S; c::S = 1., α::S = 1., n_mh_steps::Int = 1, old_data::T = T(undef, size(data, 1), 0)) where {S<:AbstractFloat, T<:AbstractMatrix, U<:Number}
. Check Documenter's build log for details.
Missing docstring for one_draw(loglikelihood::Function, parameters::ParameterVector{U}, data::Matrix{Float64}) where {U<:Number}
. Check Documenter's build log for details.
SMC.initial_draw!
— Methodfunction initial_draw!(loglikelihood::Function, parameters::ParameterVector{U},
data::Matrix{Float64}, c::Cloud; parallel::Bool = false,
regime_switching::Bool = false, toggle::Bool = true) where {U<:Number}
Draw from a general starting distribution (set by default to be from the prior) to initialize the SMC algorithm. Returns a tuple (loglh, logprior) and modifies the particle objects in the particle cloud in place.
Set regime_switching
to true if there are regime-switching parameters. Otherwise, not all the values of the regimes will be used or saved.
Set toggle
to false if, after calculating the loglikelihood, the values in the fields of every parameter in parameters
are set to their regime 1 values. The regime-switching version of rand
requires that the fields of all parameters take their regime 1 values, or else sampling may be wrong. The default is true
as a safety, but if speed is a paramount concern, setting toggle = true
will avoid unnecessary computations.
Missing docstring for draw_likelihood(loglikelihood::Function, parameters::ParameterVector{U}, data::Matrix{Float64}, draw::Vector{Float64}) where {U<:Number}
. Check Documenter's build log for details.
Missing docstring for initialize_likelihoods!(loglikelihood::Function, parameters::ParameterVector{U}, data::Matrix{Float64}, c::Cloud; parallel::Bool = false) where {U<:Number}
. Check Documenter's build log for details.
Missing docstring for initialize_cloud_settings!(cloud::Cloud; tempered_update::Bool = false, n_parts::Int = 5_000, n_Φ::Int = 300, c::S = 0.5, accept::S = 0.25) where {S<:AbstractFloat}
. Check Documenter's build log for details.