34. Extending RISE
RISE is pluggable at every step of the standard workflow –
solve, filter, optimize, sample. You can replace any of those
steps with a custom routine without touching the rest of the
toolbox. This chapter lists the hooks that the rise_model
object exposes for that purpose. For users who need to plug in a
whole alternative computational strategy (piecewise
linearization, recursive local linearization, k-th order Taylor
projection, …), the broader interface is described
in Extending RISE through paradigms; the hooks below are
the lighter, per-routine extension points.
34.1. Extracting the structural form
The matrices of the parsed and solved model are reachable via
get on the model object: get(m, 'definitions'),
get(m, 'parameters'), get(m, 'endogenous'),
get(m, 'exo_list'), get(m, 'obs_list'),
get(m, 'sstate') (steady state by regime),
get(m, 'par_list'), get(m, 'chain_list') (Markov chain
names), and so on. The full list of accessor strings is
documented on the get method of the model object.
34.2. Extracting the solution
After solve succeeds, the solution lives in m.state_space,
one cell per parameterization. In m.state_space{1}, Tz holds
the first-order coefficients, a cell over regimes (Tzz, Tzzz,
… the higher orders), with rows in the order of
get(m, 'endo_list'), and transition_matrices the regime
transition matrices. This is the solver’s working layout, not a
stable interface. print_solution(m) displays the solution with
names, and get(m, 'sstate') gives the steady state by regime.
map_solution does something else. [Sol2, adjudicator] =
map_solution(m1, m2) maps the solution of a solved reference model
m1 (one parameterization, one solution) onto the variables and
states of another model m2, as a start value for solving m2;
an optional third argument says how to fill the regimes m2 has
beyond those of m1, and adjudicator changes single entries of
Sol2 by name.
34.3. Your own solver
Set the solver option to a function handle to replace the
built-in fixed-point iteration with a user routine:
m = set(m, solver = @my_solver);
The signature is documented in Solving. The bundled
solvers (rise_1, mfi, mafia, mn, mnk, mfi_full,
loose_commitment) all conform to that signature; the simplest
way to write your own is to copy one of them and modify the inner
loop. mafia applies safeguarded Anderson acceleration to the same
fixed-point map as mfi, without forming a Jacobian.
34.4. Your own filtering algorithm
The Kalman / smoother family is replaceable through the
kf_user_algo option:
m = set(m, kf_user_algo = @my_filter);
m = set(m, kf_user_algo = {@my_filter, opt1, opt2, ...});
The expected signature is
[LogLik, Incr, retcode, Filters] = my_filter(init, y, U, z, options, vargs{:})
where init is the initialization struct the filter dispatcher
builds (the solution matrices T by regime, the steady state, the
initial state a, its covariance P and the initial regime
probabilities PAI00, …), y and U are the observable
and exogenous data, z is a function of the date t that
returns the number and the locations of the observables available
at t, and options carries the filter options.
When the filter needs more than init carries, put a * in
front of its name, given as a character string ('*my_filter';
a handle cannot carry the star). RISE then calls the same function
twice, first with two arguments to let it complete init, then
as usual:
init=my_filter(model_data,init);
[LogLik,Incr,retcode,Filters]=my_filter(init,y,U,z,options);
model_data is the engine’s internal model data (the struct inside
the model object), not the model object itself. The first call must
return init, with whatever it added; the second receives that
init. A star filter therefore branches on the number of inputs:
function varargout=my_filter(varargin)
if nargin==2
varargout{1}=my_init(varargin{1},varargin{2});
return
end
[varargout{1:4}]=my_filtering(varargin{:});
end
where my_init and my_filtering stand for your own code. The
bundled switching_unscented_kalman_filter,
switching_divided_difference_filter and
switching_cubature_kalman_filter are reference implementations
of the main signature.
34.5. Your own optimizer
Posterior maximization is routed through the estim_optimizer
option, which accepts a function name, a function handle, or a cell
holding the optimizer followed by its options (one struct or
name/value pairs):
m=set(m,estim_optimizer=@my_opt);
m=set(m,estim_optimizer={@my_opt,struct(MaxIter=500)});
The expected signature is
[x,fval,exitflag,H]=my_opt(objfun,x0,lb,ub,options): the fourth
output is the Hessian of objfun at x (or []), not an
output struct, and an optimizer with three outputs is accepted
too. wcsminwel, wnewrat, wgmhmaxlik and bee_gate (in
m/optimizers/) and the +globalopt shortcuts rise_de,
rise_cma_es, rise_abc, rise_lshade, rise_jso, … (in
m/shortcuts/) return their first four outputs in that order and
serve as templates; see Posterior maximization.
34.6. Your own likelihood function
The estimation engine evaluates the likelihood through the
routine stored in model_data.routines.likelihood, inside the
model object. There is no public setter for it: the model object
has no routines field, and add_to_routines is an internal
static method that works on the model_data struct, not on the
model. estimate installs the default when none is set: the
closed-form density of a fully observed model with complete data,
otherwise the filter (the Kalman filter, or the configured nonlinear
filter at higher order). The objects that need another likelihood
install it themselves: dsge_var_model installs
likelihood_dsge_var when it is built, and indirect_inference
and optimal_simple_rule install their criteria.
Code working inside the engine, on a model_data struct,
installs a routine with:
model_data=rise.engine.models.rise_model.add_to_routines(model_data,'likelihood',@my_likelihood);
and the routine has the signature
[LogLik, Incr, retcode, model_data, filtration] = my_likelihood(params, model_data).
To estimate a model object with a criterion of your own, use
indirect_inference, below.
34.6.1. Relation to Indirect Inference
The indirect_inference entry point is a thin wrapper around
the same optimization machinery, but with a criterion function
in place of the likelihood. The user supplies the criterion via
indirect_inference(m, myobjective, estim_priors = priors), where
[critmin, retcode] = myobjective(m) returns the (weighted)
distance between the model’s auxiliary statistics and their data
counterparts. Replace myobjective to switch between GMM
(theoretical moments), SMM (sample moments from simulation) and
IRF matching (simulated vs empirical IRFs) without touching
anything else in the pipeline.
34.7. Your own posterior sampler
The MCMC samplers (rwmh, imh, slice and apt of
rise.engine.estimation.rsamplers) are independent of the
model object – they take a target-log-density function handle,
an initial point, bounds, and an options struct. To use a custom
sampler, build a small wrapper that calls pull_objective(m)
to obtain the target handle and then drives the sampler. The
classes in +rise/+engine/+estimation/+rsamplers/ are the
reference implementations and demonstrate the contract that their
base class rise.engine.estimation.rsamplers.rsampler (chains,
progress monitor, checkpointing) expects; its subclass usrsmplr
runs a sampling function of your own under that machinery (see
the reference of usrsmplr).
34.8. Where this is not enough
The hooks above replace one routine at a time. If the goal is to plug in a whole solution paradigm – piecewise-linear OccBin, recursive local linearization, k-th order Taylor projection – the per-routine extension surface is too small, because such paradigms need to own the solution representation as well. That broader surface is the subject of Extending RISE through paradigms.