Connect Deep Parameters with a Model
fill_template.Rd
fill_template
fills a given template
,
i.e. in essence an affine mapping from the free parameters to the linear parameters of a model class specified in the template,
with the given free (deep) parameters th
and returns a model (e.g. an armamod()
or a stspmod()
).
The procedure can be used to generate random models, see e.g. r_model()
, or for M-estimates,
i.e. for estimation procedures where the estimate is obtained by minimizing (maximizing) a criterion (e.g. ML or GMM estimation).
The "inverse function" extract_theta
extracts the free/deep parameters from the linear parameters of a model,
by using the information provided in the template.
To this end the procedure first constructs the vector \(\pi\) of the stacked (linear) model parameters and then determines the deep parameters \(\theta\) as the least squares solution of the equation system
$$(\pi - h) = H\theta$$
If the residuals are non zero, then the model does not (exactly) fit to the model structure.
The threshold tol
is used to decide whether the model fits to the template or not.
The parameter on_error
decides what to do in the case of a "significant" misfit.
For "ignore"
the procedure ignores the misfit, for "warn"
the procedure issues a warning,
and for "stop"
the procedure stops with an appropriate error message.
In many cases the noise covariance is not explicitly parametrized, since \(\Sigma\) is estimated by another route.
This may be accomplished by fixing sigma_L to the identity matrix, with the option sigma_L = "identity"
for the tmpl_***
functions.
In order to extract the system parameters (e.g. the AR and MA parameters for an ARMA model) from a model where sigma_L
is not equal to the identity, one may use the option ignore_sigma_L = TRUE
.
This ignores a possible mismatch for sigma_L
but still checks whether the system parameters are in accordance to the model structure.
Arguments
- th
Vector containing free (deep) parameters.
- template
A template like listed in
model structures()
, or a template explicitly specified by the user withmodel2template()
. Essentially, a template is an affine mapping parametrised as a vectorh
and matrixH
which connect the free (deep) parameters to the linear parameters of the model.- model
A model object, i.e. an
armamod()
,stspmod()
, orrmfdmod()
object, from which deep parameters should be extracted.- tol
In
extract_theta
, small double specifying the tolerance for an acceptable distance between the linear parameters andH
times the deep parameters. Default is st tosqrt(.Machine$double.eps)
.- on_error
In
extract_theta
, character string with possible choicesignore
,warn
,stop
. Specifies what should happen when the distance between the linear parmameters andH
times the deep parameters, is larger than the specifiedtol
. Default isignore
- ignore_sigma_L
Boolean, default set to
FALSE
. If TRUE, the linear and free parameters pertaining the left square rootsigma_L
of the error covariance matrix are ignored. See alsotmpl_sigma_L()
andmodel structures()
for more detail.
Value
fill_template
returns a model object, i.e. an armamod()
, stspmod()
, or
rmfdmod()
object, according to the class of the template (and the given parameters th
).
The function extract_theta
returns the vector of free parameters for a given model and template.
Connection to Likelihood Estimation
These functions are important for likelihood estimation where the following instances of functionality are necessary.
When an initial estimate is given through a model (together with a template), one may use
extract_theta
to extract the deep parameters. This vector of initial free/deep parameter values needs to be supplied to an optimizer.The optimized deep parameter values need to be filled into the model by using the structure provided by a template. This is done with
fill_template
See also
model structures()
, ll()
, ll_theta()
and ll_FUN()
.
Examples
# Extract deep parameter from ARMA object with ARMA(p,q) template ##########
(armamod_obj = test_armamod(dim = c(2,2), degree = c(3,1)))
#> ARMA model [2,2] with orders p = 3 and q = 1
#> AR polynomial a(z):
#> z^0 [,1] [,2] z^1 [,1] [,2] z^2 [,1] [,2] z^3 [,1]
#> [1,] 1 0 0.1232590 -2.257056 -0.7991496 -0.1638405 1.0304314
#> [2,] 0 1 -0.1343075 -0.894365 -0.5522943 2.9012918 -0.5308441
#> [,2]
#> [1,] -0.278207
#> [2,] -1.383985
#> MA polynomial b(z):
#> z^0 [,1] [,2] z^1 [,1] [,2]
#> [1,] 1 0 0.6264770 1.8164786
#> [2,] 0 1 -0.3890147 -0.8933515
#> Left square root of noise covariance Sigma:
#> u[1] u[2]
#> u[1] 2.3172426 0.000000
#> u[2] 0.4490429 1.570209
(tmpl_obj = tmpl_arma_pq(2, 2, 3, 1))
#> $h
#> [1] 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
#>
#> $H
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [4,] 0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0 0 0 0 0
#> [11,] 0 0 0 0 0 0
#> [12,] 0 0 0 0 0 0
#> [13,] 0 0 0 0 0 0
#> [14,] 0 0 0 0 0 0
#> [15,] 0 0 0 0 0 0
#> [16,] 0 0 0 0 0 0
#> [17,] 0 0 0 0 0 0
#> [18,] 0 0 0 0 0 0
#> [19,] 0 0 0 0 0 0
#> [20,] 0 0 0 0 0 0
#> [21,] 0 0 0 0 0 0
#> [22,] 1 0 0 0 0 0
#> [23,] 0 1 0 0 0 0
#> [24,] 0 0 1 0 0 0
#> [25,] 0 0 0 1 0 0
#> [26,] 0 0 0 0 1 0
#> [27,] 0 0 0 0 0 0
#> [28,] 0 0 0 0 0 1
#>
#> $class
#> [1] "armamod"
#>
#> $order
#> [1] 2 2 3 1
#>
#> $n.par
#> [1] 19
#>
extract_theta(armamod_obj, tmpl_obj)
#> [1] 0.1232590 -0.1343075 -2.2570556 -0.8943650 -0.7991496 -0.5522943
#> [7] -0.1638405 2.9012918 1.0304314 -0.5308441 -0.2782070 -1.3839851
#> [13] 0.6264770 -0.3890147 1.8164786 -0.8933515 2.3172426 0.4490429
#> [19] 1.5702092
# Fill template with deep parameters #################
(tmpl_obj = tmpl_arma_echelon(nu = c(3,2)))
#> $h
#> [1] 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#>
#> $H
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13]
#> [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [2,] 1 0 0 0 0 0 0 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [4,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [5,] 0 1 0 0 0 0 0 0 0 0 0 0 0
#> [6,] 0 0 1 0 0 0 0 0 0 0 0 0 0
#> [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0
#> [8,] 0 0 0 1 0 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 0 0 0 0 0 0
#> [3,] 0 0 0 0 0 0 0 0 0 0
#> [4,] 0 0 0 0 0 0 0 0 0 0
#> [5,] 0 0 0 0 0 0 0 0 0 0
#> [6,] 0 0 0 0 0 0 0 0 0 0
#> [7,] 0 0 0 0 0 0 0 0 0 0
#> [8,] 0 0 0 0 0 0 0 0 0 0
#> [9,] 0 0 0 0 0 0 0 0 0 0
#> [10,] 0 0 0 0 0 0 0 0 0 0
#> [11,] 0 0 0 0 0 0 0 0 0 0
#> [12,] 0 0 0 0 0 0 0 0 0 0
#> [13,] 0 0 0 0 0 0 0 0 0 0
#> [14,] 0 0 0 0 0 0 0 0 0 0
#> [15,] 0 0 0 0 0 0 0 0 0 0
#> [16,] 0 0 0 0 0 0 0 0 0 0
#> [17,] 0 0 0 0 0 0 0 0 0 0
#> [18,] 0 0 0 0 0 0 0 0 0 0
#> [19,] 0 0 0 0 0 0 0 0 0 0
#> [20,] 0 0 0 0 0 0 0 0 0 0
#> [21,] 0 0 0 0 0 0 0 0 0 0
#> [22,] 0 0 0 0 0 0 0 0 0 0
#> [23,] 0 0 0 0 0 0 0 0 0 0
#> [24,] 1 0 0 0 0 0 0 0 0 0
#> [25,] 0 1 0 0 0 0 0 0 0 0
#> [26,] 0 0 1 0 0 0 0 0 0 0
#> [27,] 0 0 0 1 0 0 0 0 0 0
#> [28,] 0 0 0 0 1 0 0 0 0 0
#> [29,] 0 0 0 0 0 1 0 0 0 0
#> [30,] 0 0 0 0 0 0 0 0 0 0
#> [31,] 0 0 0 0 0 0 1 0 0 0
#> [32,] 0 0 0 0 0 0 0 0 0 0
#> [33,] 0 0 0 0 0 0 0 1 0 0
#> [34,] 0 0 0 0 0 0 0 0 1 0
#> [35,] 0 0 0 0 0 0 0 0 0 0
#> [36,] 0 0 0 0 0 0 0 0 0 1
#>
#> $class
#> [1] "armamod"
#>
#> $order
#> [1] 2 2 3 3
#>
#> $n.par
#> [1] 23
#>
#> $nu
#> [1] 3 2
#>
# Number of columns of matrix H in affine mapping = number of free parameters
(n_par_deep = dim(tmpl_obj$H)[2])
#> [1] 23
fill_template(rnorm(n_par_deep), tmpl_obj)
#> ARMA model [2,2] with orders p = 3 and q = 3
#> AR polynomial a(z):
#> z^0 [,1] [,2] z^1 [,1] [,2] z^2 [,1] [,2] z^3 [,1] [,2]
#> [1,] 1.000000 0 0.6565915 0.000000 0.7321156 -1.564163 0.2462219 0.6771047
#> [2,] 1.334974 1 0.3286492 1.264941 -0.1946414 1.522243 0.0000000 0.0000000
#> MA polynomial b(z):
#> z^0 [,1] [,2] z^1 [,1] [,2] z^2 [,1] [,2] z^3 [,1]
#> [1,] 1.000000 0 1.0885241 -0.5348286 1.8706426 0.5511869 -0.340422
#> [2,] 1.334974 1 0.1010458 2.3874028 0.2430289 0.7489366 0.000000
#> [,2]
#> [1,] 0.6907129
#> [2,] 0.0000000
#> Left square root of noise covariance Sigma:
#> u[1] u[2]
#> u[1] 0.7245142 0.000000
#> u[2] -1.2959169 1.317316